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.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;
20 import tim.prune.GenericFunction;
21 import tim.prune.I18nManager;
22 import tim.prune.data.DataPoint;
23 import tim.prune.data.Field;
26 * Function to manage the display and editing of waypoint names
28 public class PointNameEditor extends GenericFunction
30 private JDialog _dialog = null;
31 private DataPoint _point = null;
32 private JTextField _nameField = null;
33 private JButton _okButton = null;
38 * @param inApp application object to inform of success
40 public PointNameEditor(App inApp)
45 /** Get the name key */
46 public String getNameKey() {
47 return "function.editwaypointname";
51 * Begin the function by showing the edit point name dialog
55 _point = _app.getTrackInfo().getCurrentPoint();
58 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
59 _dialog.setLocationRelativeTo(_parentFrame);
60 // Create Gui and show it
61 _dialog.getContentPane().add(makeDialogComponents());
64 // Check current waypoint name, if any
65 String name = _point.getWaypointName();
67 _dialog.setVisible(true);
72 * Make the dialog components
73 * @return the GUI components for the dialog
75 private Component makeDialogComponents()
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)
87 // update App with edit
92 _nameField = new JTextField("", 12);
93 _nameField.addKeyListener(new KeyAdapter() {
94 public void keyReleased(KeyEvent e)
96 // close dialog if escape pressed
97 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
101 // Enable ok button if name changed
102 _okButton.setEnabled(hasNameChanged());
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)
119 _nameField.setText(_nameField.getText().toUpperCase());
120 _okButton.setEnabled(true);
121 _nameField.requestFocus();
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)
129 _nameField.setText(_nameField.getText().toLowerCase());
130 _okButton.setEnabled(true);
131 _nameField.requestFocus();
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)
139 _nameField.setText(titleCase(_nameField.getText()));
140 _okButton.setEnabled(true);
141 _nameField.requestFocus();
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)
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);
167 * Reset the dialog with the given name
168 * @param inName waypoint name
170 private void resetDialog(String inName)
172 _nameField.setText(inName);
173 _okButton.setEnabled(false);
177 * Turn a String into title case by capitalizing each word
178 * @param inString String to convert
179 * @return capitalized String
181 private static String titleCase(String inString)
183 // Check first for empty strings
184 if (inString == null || inString.equals(""))
188 StringBuffer buffer = new StringBuffer();
189 // loop through characters
190 char lastChar = ' ', currChar = ' ';
191 for (int i=0; i<inString.length(); i++)
193 currChar = inString.charAt(i);
194 buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
197 return buffer.toString();
202 * Confirm the edit and inform the app
204 private void confirmEdit()
206 // Check whether name has really changed
207 if (hasNameChanged())
209 // If a new name has been added, changing the point
210 // from trackpoint to waypoint, duplicate it
213 _app.createPoint(_point.clonePoint());
216 // make lists for edit and undo, and add the changed field
217 FieldEditList editList = new FieldEditList();
218 FieldEditList undoList = new FieldEditList();
219 editList.addEdit(new FieldEdit(Field.WAYPT_NAME, _nameField.getText().trim()));
220 undoList.addEdit(new FieldEdit(Field.WAYPT_NAME, _point.getWaypointName()));
222 // Pass back to App to perform edit
223 _app.completePointEdit(editList, undoList);
228 * Check whether the name has been changed or not
229 * @return true if the new name is different
231 private boolean hasNameChanged()
233 String prevName = _point.getWaypointName();
234 String newName = _nameField.getText().trim();
235 boolean prevNull = (prevName == null || prevName.equals(""));
236 boolean newNull = (newName == null || newName.equals(""));
237 return (prevNull && !newNull)
238 || (!prevNull && newNull)
239 || (!prevNull && !newNull && !prevName.equals(newName));
243 * Check whether a new name has been added
244 * @return true if it has indeed
246 private boolean wasNameAdded()
248 String prevName = _point.getWaypointName();
249 String newName = _nameField.getText().trim();
250 boolean prevNull = (prevName == null || prevName.equals(""));
251 boolean newNull = (newName == null || newName.equals(""));
252 return (prevNull && !newNull);