]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/edit/PointNameEditor.java
Version 2, March 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 import tim.prune.data.Track;
24
25 /**
26  * Class to manage the display and editing of waypoint names
27  */
28 public class PointNameEditor
29 {
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;
37
38
39         /**
40          * Constructor
41          * @param inApp application object to inform of success
42          * @param inParentFrame parent frame
43          */
44         public PointNameEditor(App inApp, JFrame inParentFrame)
45         {
46                 _app = inApp;
47                 _parentFrame = inParentFrame;
48         }
49
50
51         /**
52          * Show the edit point name dialog
53          * @param inTrack track object
54          * @param inPoint point to edit
55          */
56         public void showDialog(Track inTrack, DataPoint inPoint)
57         {
58                 _track = inTrack;
59                 _point = 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));
66                 _dialog.pack();
67                 _dialog.show();
68         }
69
70
71         /**
72          * Make the dialog components
73          * @param inName initial name of point
74          * @return the GUI components for the dialog
75          */
76         private Component makeDialogComponents(String inName)
77         {
78                 JPanel panel = new JPanel();
79                 panel.setLayout(new BorderLayout());
80                 // Create GUI layout for point name editor
81                 JPanel centrePanel = new JPanel();
82                 // centrePanel.set
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)
87                         {
88                                 _okButton.setEnabled(true);
89                         }
90                 });
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)
98                         {
99                                 _nameField.setText(_nameField.getText().toUpperCase());
100                                 _okButton.setEnabled(true);
101                         }
102                 });
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)
107                         {
108                                 _nameField.setText(_nameField.getText().toLowerCase());
109                                 _okButton.setEnabled(true);
110                         }
111                 });
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)
116                         {
117                                 _nameField.setText(sentenceCase(_nameField.getText()));
118                                 _okButton.setEnabled(true);
119                         }
120                 });
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)
129                         {
130                                 _dialog.dispose();
131                         }
132                 });
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)
138                         {
139                                 // update App with edit
140                                 confirmEdit();
141                                 _dialog.dispose();
142                         }
143                 });
144                 lowerPanel.add(_okButton);
145                 panel.add(lowerPanel, BorderLayout.SOUTH);
146                 return panel;
147         }
148
149
150         /**
151          * Turn a String into sentence case by capitalizing each word
152          * @param inString String to convert
153          * @return capitalized String
154          */
155         private static String sentenceCase(String inString)
156         {
157                 // Check first for empty strings
158                 if (inString == null || inString.equals(""))
159                 {
160                         return "";
161                 }
162                 StringBuffer buffer = new StringBuffer();
163                 // loop through characters
164                 char lastChar = ' ', currChar = ' ';
165                 for (int i=0; i<inString.length(); i++)
166                 {
167                         currChar = inString.charAt(i);
168                         buffer.append(lastChar == ' '?Character.toUpperCase(currChar):Character.toLowerCase(currChar));
169                         lastChar = currChar;
170                 }
171                 return buffer.toString();
172         }
173
174
175         /**
176          * Confirm the edit and inform the app
177          */
178         private void confirmEdit()
179         {
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)) )
188                 {
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));
194
195                         // Pass back to App to perform edit
196                         _app.completePointEdit(editList, undoList);
197                 }
198         }
199 }