1 package tim.prune.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)
56 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.pointnameedit.title"), true);
57 _dialog.setLocationRelativeTo(_parentFrame);
58 // Check current waypoint name, if any
59 String name = _point.getWaypointName();
60 // Create Gui and show it
61 _dialog.getContentPane().add(makeDialogComponents(name));
68 * Make the dialog components
69 * @param inName initial name of point
70 * @return the GUI components for the dialog
72 private Component makeDialogComponents(String inName)
74 JPanel panel = new JPanel();
75 panel.setLayout(new BorderLayout());
76 // Create GUI layout for point name editor
77 JPanel centrePanel = new JPanel();
78 centrePanel.add(new JLabel(I18nManager.getText("dialog.pointnameedit.name") + ":"));
79 // Make listener to react to ok being pressed
80 ActionListener okActionListener = new ActionListener() {
81 public void actionPerformed(ActionEvent e)
83 // Check for empty name
84 if (_nameField.getText().length() > 0)
86 // update App with edit
92 _nameField = new JTextField(inName, 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 not empty
102 _okButton.setEnabled(_nameField.getText().length() > 0);
105 _nameField.addActionListener(okActionListener);
106 centrePanel.add(_nameField);
107 panel.add(centrePanel);
108 JPanel rightPanel = new JPanel();
109 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
110 JButton upperButton = new JButton(I18nManager.getText("dialog.pointnameedit.uppercase"));
111 upperButton.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e)
114 _nameField.setText(_nameField.getText().toUpperCase());
115 _okButton.setEnabled(true);
116 _nameField.requestFocus();
119 rightPanel.add(upperButton);
120 JButton lowerButton = new JButton(I18nManager.getText("dialog.pointnameedit.lowercase"));
121 lowerButton.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent e)
124 _nameField.setText(_nameField.getText().toLowerCase());
125 _okButton.setEnabled(true);
126 _nameField.requestFocus();
129 rightPanel.add(lowerButton);
130 JButton sentenceButton = new JButton(I18nManager.getText("dialog.pointnameedit.sentencecase"));
131 sentenceButton.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e)
134 _nameField.setText(sentenceCase(_nameField.getText()));
135 _okButton.setEnabled(true);
136 _nameField.requestFocus();
139 rightPanel.add(sentenceButton);
140 panel.add(rightPanel, BorderLayout.EAST);
141 // Bottom panel for OK, cancel buttons
142 JPanel lowerPanel = new JPanel();
143 lowerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
144 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
145 cancelButton.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e)
151 lowerPanel.add(cancelButton);
152 _okButton = new JButton(I18nManager.getText("button.ok"));
153 _okButton.setEnabled(false);
154 _okButton.addActionListener(okActionListener);
155 lowerPanel.add(_okButton);
156 panel.add(lowerPanel, BorderLayout.SOUTH);
162 * Turn a String into sentence case by capitalizing each word
163 * @param inString String to convert
164 * @return capitalized String
166 private static String sentenceCase(String inString)
168 // Check first for empty strings
169 if (inString == null || inString.equals(""))
173 StringBuffer buffer = new StringBuffer();
174 // loop through characters
175 char lastChar = ' ', currChar = ' ';
176 for (int i=0; i<inString.length(); i++)
178 currChar = inString.charAt(i);
179 buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
182 return buffer.toString();
187 * Confirm the edit and inform the app
189 private void confirmEdit()
191 // Check whether name has really changed
192 String prevName = _point.getWaypointName();
193 String newName = _nameField.getText().trim();
194 boolean prevNull = (prevName == null || prevName.equals(""));
195 boolean newNull = (newName == null || newName.equals(""));
196 if ( (prevNull && !newNull)
197 || (!prevNull && newNull)
198 || (!prevNull && !newNull && !prevName.equals(newName)) )
200 // Make lists for edit and undo, and add the changed field
201 FieldEditList editList = new FieldEditList();
202 FieldEditList undoList = new FieldEditList();
203 editList.addEdit(new FieldEdit(Field.WAYPT_NAME, newName));
204 undoList.addEdit(new FieldEdit(Field.WAYPT_NAME, prevName));
206 // Pass back to App to perform edit
207 _app.completePointEdit(editList, undoList);