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