]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/edit/PointNameEditor.java
Version 3, August 2007
[GpsPrune.git] / tim / prune / edit / PointNameEditor.java
1 package tim.prune.edit;
2
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;
10
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;
18
19 import tim.prune.App;
20 import tim.prune.I18nManager;
21 import tim.prune.data.DataPoint;
22 import tim.prune.data.Field;
23
24 /**
25  * Class to manage the display and editing of waypoint names
26  */
27 public class PointNameEditor
28 {
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;
35
36
37         /**
38          * Constructor
39          * @param inApp application object to inform of success
40          * @param inParentFrame parent frame
41          */
42         public PointNameEditor(App inApp, JFrame inParentFrame)
43         {
44                 _app = inApp;
45                 _parentFrame = inParentFrame;
46         }
47
48
49         /**
50          * Show the edit point name dialog
51          * @param inPoint point to edit
52          */
53         public void showDialog(DataPoint inPoint)
54         {
55                 _point = 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));
62                 _dialog.pack();
63                 _dialog.show();
64         }
65
66
67         /**
68          * Make the dialog components
69          * @param inName initial name of point
70          * @return the GUI components for the dialog
71          */
72         private Component makeDialogComponents(String inName)
73         {
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)
82                         {
83                                 // Check for empty name
84                                 if (_nameField.getText().length() > 0)
85                                 {
86                                         // update App with edit
87                                         confirmEdit();
88                                         _dialog.dispose();
89                                 }
90                         }
91                 };
92                 _nameField = new JTextField(inName, 12);
93                 _nameField.addKeyListener(new KeyAdapter() {
94                         public void keyReleased(KeyEvent e)
95                         {
96                                 // close dialog if escape pressed
97                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
98                                 {
99                                         _dialog.dispose();
100                                 }
101                                 // Enable ok button if name not empty
102                                 _okButton.setEnabled(_nameField.getText().length() > 0);
103                         }
104                 });
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)
113                         {
114                                 _nameField.setText(_nameField.getText().toUpperCase());
115                                 _okButton.setEnabled(true);
116                                 _nameField.requestFocus();
117                         }
118                 });
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)
123                         {
124                                 _nameField.setText(_nameField.getText().toLowerCase());
125                                 _okButton.setEnabled(true);
126                                 _nameField.requestFocus();
127                         }
128                 });
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)
133                         {
134                                 _nameField.setText(sentenceCase(_nameField.getText()));
135                                 _okButton.setEnabled(true);
136                                 _nameField.requestFocus();
137                         }
138                 });
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)
147                         {
148                                 _dialog.dispose();
149                         }
150                 });
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);
157                 return panel;
158         }
159
160
161         /**
162          * Turn a String into sentence case by capitalizing each word
163          * @param inString String to convert
164          * @return capitalized String
165          */
166         private static String sentenceCase(String inString)
167         {
168                 // Check first for empty strings
169                 if (inString == null || inString.equals(""))
170                 {
171                         return "";
172                 }
173                 StringBuffer buffer = new StringBuffer();
174                 // loop through characters
175                 char lastChar = ' ', currChar = ' ';
176                 for (int i=0; i<inString.length(); i++)
177                 {
178                         currChar = inString.charAt(i);
179                         buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
180                         lastChar = currChar;
181                 }
182                 return buffer.toString();
183         }
184
185
186         /**
187          * Confirm the edit and inform the app
188          */
189         private void confirmEdit()
190         {
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)) )
199                 {
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));
205
206                         // Pass back to App to perform edit
207                         _app.completePointEdit(editList, undoList);
208                 }
209         }
210 }