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.BorderFactory;
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JOptionPane;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.ListSelectionModel;
20 import javax.swing.event.ListSelectionEvent;
21 import javax.swing.event.ListSelectionListener;
24 import tim.prune.I18nManager;
25 import tim.prune.data.DataPoint;
26 import tim.prune.data.Field;
27 import tim.prune.data.FieldList;
28 import tim.prune.data.Track;
31 * Class to manage the display and editing of point data
33 public class PointEditor
35 private App _app = null;
36 private JFrame _parentFrame = null;
37 private JDialog _dialog = null;
38 private JTable _table = null;
39 private Track _track = null;
40 private DataPoint _point = null;
41 private EditFieldsTableModel _model = null;
42 private JButton _editButton = null;
43 private JButton _okButton = null;
48 * @param inApp application object to inform of success
49 * @param inParentFrame parent frame
51 public PointEditor(App inApp, JFrame inParentFrame)
54 _parentFrame = inParentFrame;
59 * Show the edit point dialog
60 * @param inTrack track object
61 * @param inPoint point to edit
63 public void showDialog(Track inTrack, DataPoint inPoint)
67 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.pointedit.title"), true);
68 _dialog.setLocationRelativeTo(_parentFrame);
70 FieldList fieldList = _track.getFieldList();
71 int numFields = fieldList.getNumFields();
72 // Create table model for point editor
73 _model = new EditFieldsTableModel(numFields);
74 for (int i=0; i<numFields; i++)
76 Field field = fieldList.getField(i);
77 _model.addFieldInfo(field.getName(), _point.getFieldValue(field), i);
79 // Create Gui and show it
80 _dialog.getContentPane().add(makeDialogComponents());
82 _dialog.setVisible(true);
87 * Make the dialog components
88 * @return the GUI components for the dialog
90 private Component makeDialogComponents()
92 JPanel panel = new JPanel();
93 panel.setLayout(new BorderLayout(1, 10));
94 // Create GUI layout for point editor
95 _table = new JTable(_model);
96 _table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
97 _table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
98 public void valueChanged(ListSelectionEvent e)
100 // enable edit button when row selected
101 _editButton.setEnabled(true);
104 _table.setPreferredScrollableViewportSize(new Dimension(_table.getWidth(), _table.getRowHeight() * 6));
105 panel.add(new JScrollPane(_table), BorderLayout.CENTER);
107 JLabel topLabel = new JLabel(I18nManager.getText("dialog.pointedit.text"));
108 topLabel.setBorder(BorderFactory.createEmptyBorder(8, 6, 3, 6));
109 panel.add(topLabel, BorderLayout.NORTH);
110 _editButton = new JButton(I18nManager.getText("button.edit"));
111 _editButton.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e)
114 // Update field value and enable ok button
115 String currValue = _model.getValue(_table.getSelectedRow());
116 Object newValue = JOptionPane.showInputDialog(_dialog,
117 I18nManager.getText("dialog.pointedit.changevalue.text"),
118 I18nManager.getText("dialog.pointedit.changevalue.title"),
119 JOptionPane.QUESTION_MESSAGE, null, null, currValue);
121 && _model.updateValue(_table.getSelectedRow(), newValue.toString()))
123 _okButton.setEnabled(true);
127 _editButton.setEnabled(false);
128 JPanel rightPanel = new JPanel();
129 rightPanel.add(_editButton);
130 panel.add(rightPanel, BorderLayout.EAST);
131 // Bottom panel for OK, cancel buttons
132 JPanel lowerPanel = new JPanel();
133 lowerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
134 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
135 cancelButton.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e)
141 lowerPanel.add(cancelButton);
142 _okButton = new JButton(I18nManager.getText("button.ok"));
143 _okButton.setEnabled(false);
144 _okButton.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent e)
147 // update App with edit
152 lowerPanel.add(_okButton);
153 panel.add(lowerPanel, BorderLayout.SOUTH);
159 * Confirm the edit and inform the app
161 private void confirmEdit()
163 // Package the modified fields into an object
164 FieldList fieldList = _track.getFieldList();
165 int numFields = fieldList.getNumFields();
166 // Make lists for edit and undo, and add each changed field in turn
167 FieldEditList editList = new FieldEditList();
168 FieldEditList undoList = new FieldEditList();
169 for (int i=0; i<numFields; i++)
171 if (_model.getChanged(i))
173 Field field = fieldList.getField(i);
174 editList.addEdit(new FieldEdit(field, _model.getValue(i)));
175 undoList.addEdit(new FieldEdit(field, _point.getFieldValue(field)));
178 _app.completePointEdit(editList, undoList);