1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
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;
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;
30 * Class to provide the function to delete the values of a single field
31 * for all points in the current range
33 public class DeleteFieldValues extends GenericFunction
35 private JDialog _dialog = null;
36 private JList<String> _fieldList = null;
37 private FieldListModel _listModel = null;
38 private JButton _okButton = null;
43 * @param inApp application object for callback
45 public DeleteFieldValues(App inApp)
50 /** Get the name key */
51 public String getNameKey() {
52 return "function.deletefieldvalues";
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());
71 // Check whether any fields left
72 if (_listModel.getSize() < 1) {
73 _app.showErrorMessage(getNameKey(), "dialog.deletefieldvalues.nofields");
76 _dialog.setVisible(true);
82 * Create dialog components
83 * @return Panel containing all gui elements in dialog
85 private JPanel makeDialogComponents()
87 JPanel dialogPanel = new JPanel();
88 dialogPanel.setLayout(new BorderLayout());
89 dialogPanel.add(new JLabel(I18nManager.getText("dialog.deletefieldvalues.intro")), BorderLayout.NORTH);
91 _fieldList = new JList<String>(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);
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)
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)
120 buttonPanel.add(cancelButton);
121 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
122 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
127 * Initialise the dialog with the field list
129 private void initDialog()
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))
142 _listModel.addField(field);
145 _fieldList.setModel(_listModel);
146 _fieldList.clearSelection();
147 _okButton.setEnabled(false);
151 * Finish the dialog when OK pressed
153 private void finish()
156 Field field = _listModel.getField(_fieldList.getSelectedIndex());
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);
167 _app.getTrackInfo().getSelection().markInvalid();
168 UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_EDITED);
169 _app.completeFunction(undo, I18nManager.getText("confirm.deletefieldvalues"));