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