]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/DeleteFieldValues.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / function / DeleteFieldValues.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 import javax.swing.BorderFactory;
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JLabel;
12 import javax.swing.JList;
13 import javax.swing.JPanel;
14 import javax.swing.JScrollPane;
15 import javax.swing.ListSelectionModel;
16 import javax.swing.event.ListSelectionEvent;
17 import javax.swing.event.ListSelectionListener;
18
19 import tim.prune.App;
20 import tim.prune.DataSubscriber;
21 import tim.prune.GenericFunction;
22 import tim.prune.I18nManager;
23 import tim.prune.UpdateMessageBroker;
24 import tim.prune.data.Field;
25 import tim.prune.data.FieldList;
26 import tim.prune.data.Track;
27 import tim.prune.undo.UndoDeleteFieldValues;
28
29 /**
30  * Class to provide the function to delete the values of a single field
31  * for all points in the current range
32  */
33 public class DeleteFieldValues extends GenericFunction
34 {
35         private JDialog _dialog = null;
36         private JList _fieldList = null;
37         private FieldListModel _listModel = null;
38         private JButton _okButton = null;
39
40
41         /**
42          * Constructor
43          * @param inApp application object for callback
44          */
45         public DeleteFieldValues(App inApp)
46         {
47                 super(inApp);
48         }
49
50         /** Get the name key */
51         public String getNameKey() {
52                 return "function.deletefieldvalues";
53         }
54
55         /**
56          * Begin the function
57          */
58         public void begin()
59         {
60                 // Make dialog window
61                 if (_dialog == null)
62                 {
63                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
64                         _dialog.setLocationRelativeTo(_parentFrame);
65                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
66                         _dialog.getContentPane().add(makeDialogComponents());
67                         _dialog.pack();
68                 }
69                 // refresh the dialog
70                 initDialog();
71                 // Check whether any fields left
72                 if (_listModel.getSize() < 1) {
73                         _app.showErrorMessage(getNameKey(), "dialog.deletefieldvalues.nofields");
74                 }
75                 else {
76                         _dialog.setVisible(true);
77                 }
78         }
79
80
81         /**
82          * Create dialog components
83          * @return Panel containing all gui elements in dialog
84          */
85         private JPanel makeDialogComponents()
86         {
87                 JPanel dialogPanel = new JPanel();
88                 dialogPanel.setLayout(new BorderLayout());
89                 dialogPanel.add(new JLabel(I18nManager.getText("dialog.deletefieldvalues.intro")), BorderLayout.NORTH);
90                 // List in centre
91                 _fieldList = new JList(new String[] {"First field", "Second field"});
92                 // These entries will be replaced by the initDialog method
93                 _fieldList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
94                 _fieldList.addListSelectionListener(new ListSelectionListener() {
95                         public void valueChanged(ListSelectionEvent e) {
96                                 _okButton.setEnabled(_fieldList.getSelectedIndex() >= 0);
97                         }
98                 });
99                 dialogPanel.add(new JScrollPane(_fieldList), BorderLayout.CENTER);
100                 // button panel at bottom
101                 JPanel buttonPanel = new JPanel();
102                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
103                 _okButton = new JButton(I18nManager.getText("button.ok"));
104                 ActionListener okListener = new ActionListener() {
105                         public void actionPerformed(ActionEvent e)
106                         {
107                                 finish();
108                         }
109                 };
110                 _okButton.addActionListener(okListener);
111                 _okButton.setEnabled(false);
112                 buttonPanel.add(_okButton);
113                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
114                 cancelButton.addActionListener(new ActionListener() {
115                         public void actionPerformed(ActionEvent e)
116                         {
117                                 _dialog.dispose();
118                         }
119                 });
120                 buttonPanel.add(cancelButton);
121                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
122                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
123                 return dialogPanel;
124         }
125
126         /**
127          * Initialise the dialog with the field list
128          */
129         private void initDialog()
130         {
131                 _listModel = new FieldListModel();
132                 int selStart = _app.getTrackInfo().getSelection().getStart();
133                 int selEnd = _app.getTrackInfo().getSelection().getEnd();
134                 // Loop over fields in track
135                 final Track track = _app.getTrackInfo().getTrack();
136                 FieldList fieldsInTrack = track.getFieldList();
137                 for (int i=0; i<fieldsInTrack.getNumFields(); i++) {
138                         Field field = fieldsInTrack.getField(i);
139                         if (field != Field.LATITUDE && field != Field.LONGITUDE
140                                 && track.hasData(field, selStart, selEnd))
141                         {
142                                 _listModel.addField(field);
143                         }
144                 }
145                 _fieldList.setModel(_listModel);
146                 _fieldList.clearSelection();
147                 _okButton.setEnabled(false);
148         }
149
150         /**
151          * Finish the dialog when OK pressed
152          */
153         private void finish()
154         {
155                 // Complete function
156                 Field field = _listModel.getField(_fieldList.getSelectedIndex());
157                 if (field != null)
158                 {
159                         UndoDeleteFieldValues undo = new UndoDeleteFieldValues(_app.getTrackInfo(), field);
160                         final int selStart = _app.getTrackInfo().getSelection().getStart();
161                         final int selEnd = _app.getTrackInfo().getSelection().getEnd();
162                         final Track track = _app.getTrackInfo().getTrack();
163                         for (int i=selStart; i<= selEnd; i++) {
164                                 track.getPoint(i).setFieldValue(field, null, false);
165                         }
166                         _dialog.dispose();
167                         _app.getTrackInfo().getSelection().markInvalid();
168                         UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_EDITED);
169                         _app.completeFunction(undo, I18nManager.getText("confirm.deletefieldvalues"));
170                 }
171         }
172 }