]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/edit/PointEditor.java
Version 2, March 2007
[GpsPrune.git] / tim / prune / edit / PointEditor.java
1 package tim.prune.edit;
2
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;
9
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;
21
22 import tim.prune.App;
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;
28
29 /**
30  * Class to manage the display and editing of point data
31  */
32 public class PointEditor
33 {
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;
43
44
45         /**
46          * Constructor
47          * @param inApp application object to inform of success
48          * @param inParentFrame parent frame
49          */
50         public PointEditor(App inApp, JFrame inParentFrame)
51         {
52                 _app = inApp;
53                 _parentFrame = inParentFrame;
54         }
55
56
57         /**
58          * Show the edit point dialog
59          * @param inTrack track object
60          * @param inPoint point to edit
61          */
62         public void showDialog(Track inTrack, DataPoint inPoint)
63         {
64                 _track = inTrack;
65                 _point = inPoint;
66                 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.pointedit.title"), true);
67                 _dialog.setLocationRelativeTo(_parentFrame);
68                 // Check field list
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++)
74                 {
75                         Field field = fieldList.getField(i);
76                         _model.addFieldInfo(field.getName(), _point.getFieldValue(field), i);
77                 }
78                 // Create Gui and show it
79                 _dialog.getContentPane().add(makeDialogComponents());
80                 _dialog.pack();
81                 _dialog.show();
82         }
83
84
85         /**
86          * Make the dialog components
87          * @return the GUI components for the dialog
88          */
89         private Component makeDialogComponents()
90         {
91                 JPanel panel = new JPanel();
92                 panel.setLayout(new BorderLayout());
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)
98                         {
99                                 // enable edit button when row selected
100                                 _editButton.setEnabled(true);
101                         }
102                 });
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)
109                         {
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);
116                                 if (newValue != null
117                                         && _model.updateValue(_table.getSelectedRow(), newValue.toString()))
118                                 {
119                                         _okButton.setEnabled(true);
120                                 }
121                         }
122                 });
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)
133                         {
134                                 _dialog.dispose();
135                         }
136                 });
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)
142                         {
143                                 // update App with edit
144                                 confirmEdit();
145                                 _dialog.dispose();
146                         }
147                 });
148                 lowerPanel.add(_okButton);
149                 panel.add(lowerPanel, BorderLayout.SOUTH);
150                 return panel;
151         }
152
153
154         /**
155          * Confirm the edit and inform the app
156          */
157         private void confirmEdit()
158         {
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++)
166                 {
167                         if (_model.getChanged(i))
168                         {
169                                 Field field = fieldList.getField(i);
170                                 editList.addEdit(new FieldEdit(field, _model.getValue(i)));
171                                 undoList.addEdit(new FieldEdit(field, _point.getFieldValue(field)));
172                         }
173                 }
174                 _app.completePointEdit(editList, undoList);
175         }
176 }