]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/edit/PointNameEditor.java
53e1f834b962fa8a098a9fc9345934dbdac3003f
[GpsPrune.git] / tim / prune / function / edit / PointNameEditor.java
1 package tim.prune.function.edit;
2
3 import java.awt.Component;
4 import java.awt.BorderLayout;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JTextField;
18
19 import tim.prune.App;
20 import tim.prune.GenericFunction;
21 import tim.prune.I18nManager;
22 import tim.prune.data.DataPoint;
23 import tim.prune.data.Field;
24
25 /**
26  * Function to manage the display and editing of waypoint names
27  */
28 public class PointNameEditor extends GenericFunction
29 {
30         private JDialog _dialog = null;
31         private DataPoint _point = null;
32         private JTextField _nameField = null;
33         private JButton _okButton = null;
34
35
36         /**
37          * Constructor
38          * @param inApp application object to inform of success
39          */
40         public PointNameEditor(App inApp)
41         {
42                 super(inApp);
43         }
44
45         /** Get the name key */
46         public String getNameKey() {
47                 return "function.editwaypointname";
48         }
49
50         /**
51          * Begin the function by showing the edit point name dialog
52          */
53         public void begin()
54         {
55                 _point = _app.getTrackInfo().getCurrentPoint();
56                 if (_dialog == null)
57                 {
58                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
59                         _dialog.setLocationRelativeTo(_parentFrame);
60                         // Create Gui and show it
61                         _dialog.getContentPane().add(makeDialogComponents());
62                         _dialog.pack();
63                 }
64                 // Check current waypoint name, if any
65                 String name = _point.getWaypointName();
66                 resetDialog(name);
67                 _dialog.setVisible(true);
68         }
69
70
71         /**
72          * Make the dialog components
73          * @return the GUI components for the dialog
74          */
75         private Component makeDialogComponents()
76         {
77                 JPanel panel = new JPanel();
78                 panel.setLayout(new BorderLayout());
79                 // Create GUI layout for point name editor
80                 JPanel centrePanel = new JPanel();
81                 centrePanel.setLayout(new BorderLayout(8, 8));
82                 centrePanel.add(new JLabel(I18nManager.getText("dialog.pointnameedit.name") + ": "), BorderLayout.WEST);
83                 // Make listener to react to ok being pressed
84                 ActionListener okActionListener = new ActionListener() {
85                         public void actionPerformed(ActionEvent e)
86                         {
87                                 // update App with edit
88                                 confirmEdit();
89                                 _dialog.dispose();
90                         }
91                 };
92                 _nameField = new JTextField("", 12);
93                 _nameField.addKeyListener(new KeyAdapter() {
94                         public void keyReleased(KeyEvent e)
95                         {
96                                 // close dialog if escape pressed
97                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
98                                 {
99                                         _dialog.dispose();
100                                 }
101                                 // Enable ok button if name changed
102                                 _okButton.setEnabled(hasNameChanged());
103                         }
104                 });
105                 _nameField.addActionListener(okActionListener);
106                 centrePanel.add(_nameField, BorderLayout.CENTER);
107                 // holder panel to stop the text box from being stretched
108                 JPanel holderPanel = new JPanel();
109                 holderPanel.setLayout(new BorderLayout());
110                 holderPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
111                 holderPanel.add(centrePanel, BorderLayout.NORTH);
112                 panel.add(holderPanel, BorderLayout.CENTER);
113                 JPanel rightPanel = new JPanel();
114                 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
115                 JButton upperButton = new JButton(I18nManager.getText("dialog.pointnameedit.uppercase"));
116                 upperButton.addActionListener(new ActionListener() {
117                         public void actionPerformed(ActionEvent e)
118                         {
119                                 _nameField.setText(_nameField.getText().toUpperCase());
120                                 _okButton.setEnabled(true);
121                                 _nameField.requestFocus();
122                         }
123                 });
124                 rightPanel.add(upperButton);
125                 JButton lowerButton = new JButton(I18nManager.getText("dialog.pointnameedit.lowercase"));
126                 lowerButton.addActionListener(new ActionListener() {
127                         public void actionPerformed(ActionEvent e)
128                         {
129                                 _nameField.setText(_nameField.getText().toLowerCase());
130                                 _okButton.setEnabled(true);
131                                 _nameField.requestFocus();
132                         }
133                 });
134                 rightPanel.add(lowerButton);
135                 JButton titleButton = new JButton(I18nManager.getText("dialog.pointnameedit.titlecase"));
136                 titleButton.addActionListener(new ActionListener() {
137                         public void actionPerformed(ActionEvent e)
138                         {
139                                 _nameField.setText(titleCase(_nameField.getText()));
140                                 _okButton.setEnabled(true);
141                                 _nameField.requestFocus();
142                         }
143                 });
144                 rightPanel.add(titleButton);
145                 panel.add(rightPanel, BorderLayout.EAST);
146                 // Bottom panel for OK, cancel buttons
147                 JPanel lowerPanel = new JPanel();
148                 lowerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
149                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
150                 cancelButton.addActionListener(new ActionListener() {
151                         public void actionPerformed(ActionEvent e)
152                         {
153                                 _dialog.dispose();
154                         }
155                 });
156                 lowerPanel.add(cancelButton);
157                 _okButton = new JButton(I18nManager.getText("button.ok"));
158                 _okButton.setEnabled(false);
159                 _okButton.addActionListener(okActionListener);
160                 lowerPanel.add(_okButton);
161                 panel.add(lowerPanel, BorderLayout.SOUTH);
162                 return panel;
163         }
164
165
166         /**
167          * Reset the dialog with the given name
168          * @param inName waypoint name
169          */
170         private void resetDialog(String inName)
171         {
172                 _nameField.setText(inName);
173                 _okButton.setEnabled(false);
174         }
175
176         /**
177          * Turn a String into title case by capitalizing each word
178          * @param inString String to convert
179          * @return capitalized String
180          */
181         private static String titleCase(String inString)
182         {
183                 // Check first for empty strings
184                 if (inString == null || inString.equals(""))
185                 {
186                         return "";
187                 }
188                 StringBuffer buffer = new StringBuffer();
189                 // loop through characters
190                 char lastChar = ' ', currChar = ' ';
191                 for (int i=0; i<inString.length(); i++)
192                 {
193                         currChar = inString.charAt(i);
194                         buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
195                         lastChar = currChar;
196                 }
197                 return buffer.toString();
198         }
199
200
201         /**
202          * Confirm the edit and inform the app
203          */
204         private void confirmEdit()
205         {
206                 // Check whether name has really changed
207                 if (hasNameChanged())
208                 {
209                         // Make lists for edit and undo, and add the changed field
210                         FieldEditList editList = new FieldEditList();
211                         FieldEditList undoList = new FieldEditList();
212                         editList.addEdit(new FieldEdit(Field.WAYPT_NAME, _nameField.getText().trim()));
213                         undoList.addEdit(new FieldEdit(Field.WAYPT_NAME, _point.getWaypointName()));
214
215                         // Pass back to App to perform edit
216                         _app.completePointEdit(editList, undoList);
217                 }
218         }
219
220         /**
221          * Check whether the name has been changed or not
222          * @return true if the new name is different
223          */
224         private boolean hasNameChanged()
225         {
226                 String prevName = _point.getWaypointName();
227                 String newName = _nameField.getText().trim();
228                 boolean prevNull = (prevName == null || prevName.equals(""));
229                 boolean newNull = (newName == null || newName.equals(""));
230                 return (prevNull && !newNull)
231                         || (!prevNull && newNull)
232                         || (!prevNull && !newNull && !prevName.equals(newName));
233         }
234 }