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;
23 import tim.prune.data.Track;
26 * Class to manage the display and editing of waypoint names
28 public class PointNameEditor
30 private App _app = null;
31 private JFrame _parentFrame = null;
32 private JDialog _dialog = null;
33 private Track _track = null;
34 private DataPoint _point = null;
35 private JTextField _nameField = null;
36 private JButton _okButton = null;
41 * @param inApp application object to inform of success
42 * @param inParentFrame parent frame
44 public PointNameEditor(App inApp, JFrame inParentFrame)
47 _parentFrame = inParentFrame;
52 * Show the edit point name dialog
53 * @param inTrack track object
54 * @param inPoint point to edit
56 public void showDialog(Track inTrack, DataPoint inPoint)
60 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.pointnameedit.title"), true);
61 _dialog.setLocationRelativeTo(_parentFrame);
62 // Check current waypoint name, if any
63 String name = _point.getWaypointName();
64 // Create Gui and show it
65 _dialog.getContentPane().add(makeDialogComponents(name));
72 * Make the dialog components
73 * @param inName initial name of point
74 * @return the GUI components for the dialog
76 private Component makeDialogComponents(String inName)
78 JPanel panel = new JPanel();
79 panel.setLayout(new BorderLayout());
80 // Create GUI layout for point name editor
81 JPanel centrePanel = new JPanel();
83 centrePanel.add(new JLabel(I18nManager.getText("dialog.pointnameedit.name") + ":"));
84 _nameField = new JTextField(inName, 12);
85 _nameField.addKeyListener(new KeyAdapter() {
86 public void keyTyped(KeyEvent e)
88 _okButton.setEnabled(true);
91 centrePanel.add(_nameField);
92 panel.add(centrePanel);
93 JPanel rightPanel = new JPanel();
94 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
95 JButton upperButton = new JButton(I18nManager.getText("dialog.pointnameedit.uppercase"));
96 upperButton.addActionListener(new ActionListener() {
97 public void actionPerformed(ActionEvent e)
99 _nameField.setText(_nameField.getText().toUpperCase());
100 _okButton.setEnabled(true);
103 rightPanel.add(upperButton);
104 JButton lowerButton = new JButton(I18nManager.getText("dialog.pointnameedit.lowercase"));
105 lowerButton.addActionListener(new ActionListener() {
106 public void actionPerformed(ActionEvent e)
108 _nameField.setText(_nameField.getText().toLowerCase());
109 _okButton.setEnabled(true);
112 rightPanel.add(lowerButton);
113 JButton sentenceButton = new JButton(I18nManager.getText("dialog.pointnameedit.sentencecase"));
114 sentenceButton.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e)
117 _nameField.setText(sentenceCase(_nameField.getText()));
118 _okButton.setEnabled(true);
121 rightPanel.add(sentenceButton);
122 panel.add(rightPanel, BorderLayout.EAST);
123 // Bottom panel for OK, cancel buttons
124 JPanel lowerPanel = new JPanel();
125 lowerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
126 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
127 cancelButton.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e)
133 lowerPanel.add(cancelButton);
134 _okButton = new JButton(I18nManager.getText("button.ok"));
135 _okButton.setEnabled(false);
136 _okButton.addActionListener(new ActionListener() {
137 public void actionPerformed(ActionEvent e)
139 // update App with edit
144 lowerPanel.add(_okButton);
145 panel.add(lowerPanel, BorderLayout.SOUTH);
151 * Turn a String into sentence case by capitalizing each word
152 * @param inString String to convert
153 * @return capitalized String
155 private static String sentenceCase(String inString)
157 // Check first for empty strings
158 if (inString == null || inString.equals(""))
162 StringBuffer buffer = new StringBuffer();
163 // loop through characters
164 char lastChar = ' ', currChar = ' ';
165 for (int i=0; i<inString.length(); i++)
167 currChar = inString.charAt(i);
168 buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
171 return buffer.toString();
176 * Confirm the edit and inform the app
178 private void confirmEdit()
180 // Check whether name has really changed
181 String prevName = _point.getWaypointName();
182 String newName = _nameField.getText().trim();
183 boolean prevNull = (prevName == null || prevName.equals(""));
184 boolean newNull = (newName == null || newName.equals(""));
185 if ( (prevNull && !newNull)
186 || (!prevNull && newNull)
187 || (!prevNull && !newNull && !prevName.equals(newName)) )
189 // Make lists for edit and undo, and add the changed field
190 FieldEditList editList = new FieldEditList();
191 FieldEditList undoList = new FieldEditList();
192 editList.addEdit(new FieldEdit(Field.WAYPT_NAME, newName));
193 undoList.addEdit(new FieldEdit(Field.WAYPT_NAME, prevName));
195 // Pass back to App to perform edit
196 _app.completePointEdit(editList, undoList);