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.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JTextField;
20 import tim.prune.I18nManager;
21 import tim.prune.data.DataPoint;
22 import tim.prune.data.Field;
25 * Class to manage the display and editing of waypoint names
27 public class PointNameEditor
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;
39 * @param inApp application object to inform of success
40 * @param inParentFrame parent frame
42 public PointNameEditor(App inApp, JFrame inParentFrame)
45 _parentFrame = inParentFrame;
50 * Show the edit point name dialog
51 * @param inPoint point to edit
53 public void showDialog(DataPoint inPoint)
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());
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.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)
86 // update App with edit
91 _nameField = new JTextField("", 12);
92 _nameField.addKeyListener(new KeyAdapter() {
93 public void keyReleased(KeyEvent e)
95 // close dialog if escape pressed
96 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
100 // Enable ok button if name changed
101 _okButton.setEnabled(hasNameChanged());
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)
113 _nameField.setText(_nameField.getText().toUpperCase());
114 _okButton.setEnabled(true);
115 _nameField.requestFocus();
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)
123 _nameField.setText(_nameField.getText().toLowerCase());
124 _okButton.setEnabled(true);
125 _nameField.requestFocus();
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)
133 _nameField.setText(sentenceCase(_nameField.getText()));
134 _okButton.setEnabled(true);
135 _nameField.requestFocus();
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)
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);
161 * Reset the dialog with the given name
162 * @param inName waypoint name
164 private void resetDialog(String inName)
166 _nameField.setText(inName);
167 _okButton.setEnabled(false);
171 * Turn a String into sentence case by capitalizing each word
172 * @param inString String to convert
173 * @return capitalized String
175 private static String sentenceCase(String inString)
177 // Check first for empty strings
178 if (inString == null || inString.equals(""))
182 StringBuffer buffer = new StringBuffer();
183 // loop through characters
184 char lastChar = ' ', currChar = ' ';
185 for (int i=0; i<inString.length(); i++)
187 currChar = inString.charAt(i);
188 buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
191 return buffer.toString();
196 * Confirm the edit and inform the app
198 private void confirmEdit()
200 // Check whether name has really changed
201 if (hasNameChanged())
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()));
209 // Pass back to App to perform edit
210 _app.completePointEdit(editList, undoList);
215 * Check whether the name has been changed or not
216 * @return true if the new name is different
218 private boolean hasNameChanged()
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));