1 package tim.prune.function.edit;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JOptionPane;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.JTable;
18 import javax.swing.ListSelectionModel;
19 import javax.swing.event.ListSelectionEvent;
20 import javax.swing.event.ListSelectionListener;
23 import tim.prune.I18nManager;
24 import tim.prune.data.DataPoint;
25 import tim.prune.data.Field;
26 import tim.prune.data.FieldList;
27 import tim.prune.data.Track;
30 * Class to manage the display and editing of point data
32 public class PointEditor
34 private App _app = null;
35 private JFrame _parentFrame = null;
36 private JDialog _dialog = null;
37 private JTable _table = null;
38 private Track _track = null;
39 private DataPoint _point = null;
40 private EditFieldsTableModel _model = null;
41 private JButton _editButton = null;
42 private JButton _okButton = null;
47 * @param inApp application object to inform of success
48 * @param inParentFrame parent frame
50 public PointEditor(App inApp, JFrame inParentFrame)
53 _parentFrame = inParentFrame;
58 * Show the edit point dialog
59 * @param inTrack track object
60 * @param inPoint point to edit
62 public void showDialog(Track inTrack, DataPoint inPoint)
66 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.pointedit.title"), true);
67 _dialog.setLocationRelativeTo(_parentFrame);
69 FieldList fieldList = _track.getFieldList();
70 int numFields = fieldList.getNumFields();
71 // Create table model for point editor
72 _model = new EditFieldsTableModel(numFields);
73 for (int i=0; i<numFields; i++)
75 Field field = fieldList.getField(i);
76 _model.addFieldInfo(field.getName(), _point.getFieldValue(field), i);
78 // Create Gui and show it
79 _dialog.getContentPane().add(makeDialogComponents());
81 _dialog.setVisible(true);
86 * Make the dialog components
87 * @return the GUI components for the dialog
89 private Component makeDialogComponents()
91 JPanel panel = new JPanel();
92 panel.setLayout(new BorderLayout(1, 10));
93 // Create GUI layout for point editor
94 _table = new JTable(_model);
95 _table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
96 _table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
97 public void valueChanged(ListSelectionEvent e)
99 // enable edit button when row selected
100 _editButton.setEnabled(true);
103 _table.setPreferredScrollableViewportSize(new Dimension(_table.getWidth(), _table.getRowHeight() * 6));
104 panel.add(new JScrollPane(_table), BorderLayout.CENTER);
105 panel.add(new JLabel(I18nManager.getText("dialog.pointedit.text")), BorderLayout.NORTH);
106 _editButton = new JButton(I18nManager.getText("button.edit"));
107 _editButton.addActionListener(new ActionListener() {
108 public void actionPerformed(ActionEvent e)
110 // Update field value and enable ok button
111 String currValue = _model.getValue(_table.getSelectedRow());
112 Object newValue = JOptionPane.showInputDialog(_dialog,
113 I18nManager.getText("dialog.pointedit.changevalue.text"),
114 I18nManager.getText("dialog.pointedit.changevalue.title"),
115 JOptionPane.QUESTION_MESSAGE, null, null, currValue);
117 && _model.updateValue(_table.getSelectedRow(), newValue.toString()))
119 _okButton.setEnabled(true);
123 _editButton.setEnabled(false);
124 JPanel rightPanel = new JPanel();
125 rightPanel.add(_editButton);
126 panel.add(rightPanel, BorderLayout.EAST);
127 // Bottom panel for OK, cancel buttons
128 JPanel lowerPanel = new JPanel();
129 lowerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
130 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
131 cancelButton.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e)
137 lowerPanel.add(cancelButton);
138 _okButton = new JButton(I18nManager.getText("button.ok"));
139 _okButton.setEnabled(false);
140 _okButton.addActionListener(new ActionListener() {
141 public void actionPerformed(ActionEvent e)
143 // update App with edit
148 lowerPanel.add(_okButton);
149 panel.add(lowerPanel, BorderLayout.SOUTH);
155 * Confirm the edit and inform the app
157 private void confirmEdit()
159 // Package the modified fields into an object
160 FieldList fieldList = _track.getFieldList();
161 int numFields = fieldList.getNumFields();
162 // Make lists for edit and undo, and add each changed field in turn
163 FieldEditList editList = new FieldEditList();
164 FieldEditList undoList = new FieldEditList();
165 for (int i=0; i<numFields; i++)
167 if (_model.getChanged(i))
169 Field field = fieldList.getField(i);
170 editList.addEdit(new FieldEdit(field, _model.getValue(i)));
171 undoList.addEdit(new FieldEdit(field, _point.getFieldValue(field)));
174 _app.completePointEdit(editList, undoList);