1 package tim.prune.function.edit;
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;
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;
19 import tim.prune.GenericFunction;
20 import tim.prune.I18nManager;
21 import tim.prune.data.DataPoint;
22 import tim.prune.data.Field;
25 * Function to manage the display and editing of waypoint names
27 public class PointNameEditor extends GenericFunction
29 private JDialog _dialog = null;
30 private DataPoint _point = null;
31 private JTextField _nameField = null;
32 private JButton _okButton = null;
37 * @param inApp application object to inform of success
39 public PointNameEditor(App inApp)
44 /** Get the name key */
45 public String getNameKey() {
46 return "function.editwaypointname";
50 * Begin the function by showing the edit point name dialog
54 _point = _app.getTrackInfo().getCurrentPoint();
57 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
58 _dialog.setLocationRelativeTo(_parentFrame);
59 // Create Gui and show it
60 _dialog.getContentPane().add(makeDialogComponents());
63 // Check current waypoint name, if any
64 String name = _point.getWaypointName();
66 _dialog.setVisible(true);
71 * Make the dialog components
72 * @return the GUI components for the dialog
74 private Component makeDialogComponents()
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)
85 // update App with edit
90 _nameField = new JTextField("", 12);
91 _nameField.addKeyListener(new KeyAdapter() {
92 public void keyReleased(KeyEvent e)
94 // close dialog if escape pressed
95 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
99 // Enable ok button if name changed
100 _okButton.setEnabled(hasNameChanged());
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)
112 _nameField.setText(_nameField.getText().toUpperCase());
113 _okButton.setEnabled(true);
114 _nameField.requestFocus();
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)
122 _nameField.setText(_nameField.getText().toLowerCase());
123 _okButton.setEnabled(true);
124 _nameField.requestFocus();
127 rightPanel.add(lowerButton);
128 JButton sentenceButton = new JButton(I18nManager.getText("dialog.pointnameedit.sentencecase"));
129 sentenceButton.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e)
132 _nameField.setText(sentenceCase(_nameField.getText()));
133 _okButton.setEnabled(true);
134 _nameField.requestFocus();
137 rightPanel.add(sentenceButton);
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)
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);
160 * Reset the dialog with the given name
161 * @param inName waypoint name
163 private void resetDialog(String inName)
165 _nameField.setText(inName);
166 _okButton.setEnabled(false);
170 * Turn a String into sentence case by capitalizing each word
171 * @param inString String to convert
172 * @return capitalized String
174 private static String sentenceCase(String inString)
176 // Check first for empty strings
177 if (inString == null || inString.equals(""))
181 StringBuffer buffer = new StringBuffer();
182 // loop through characters
183 char lastChar = ' ', currChar = ' ';
184 for (int i=0; i<inString.length(); i++)
186 currChar = inString.charAt(i);
187 buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
190 return buffer.toString();
195 * Confirm the edit and inform the app
197 private void confirmEdit()
199 // Check whether name has really changed
200 if (hasNameChanged())
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()));
208 // Pass back to App to perform edit
209 _app.completePointEdit(editList, undoList);
214 * Check whether the name has been changed or not
215 * @return true if the new name is different
217 private boolean hasNameChanged()
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));