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