]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/RearrangeFunction.java
01cd1d106e2936686492375146ca43b62d7eb910
[GpsPrune.git] / tim / prune / function / RearrangeFunction.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.BoxLayout;
10 import javax.swing.ButtonGroup;
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JRadioButton;
16
17 import tim.prune.App;
18 import tim.prune.GenericFunction;
19 import tim.prune.I18nManager;
20 import tim.prune.data.sort.SortMode;
21
22 /**
23  * Abstract superclass for the functions which rearrange points,
24  * such as waypoints or photo points
25  */
26 public abstract class RearrangeFunction extends GenericFunction
27 {
28         /** Function dialog */
29         private JDialog _dialog = null;
30         /** Radio buttons for start/end/nearest */
31         private JRadioButton[] _positionRadios = null;
32         /** Radio buttons for sorting */
33         private JRadioButton[] _sortRadios = null;
34         /** Is the "nearest" option available? */
35         private boolean _nearestAvailable = false;
36
37
38         /** Enumeration for rearrange commands */
39         protected enum Rearrange
40         {
41                 /** Rearrange all waypoints to start */
42                 TO_START,
43                 /** Rearrange all waypoints to end */
44                 TO_END,
45                 /** Rearrange each waypoint to nearest track point */
46                 TO_NEAREST
47         }
48
49
50         /**
51          * Constructor
52          * @param inApp app object
53          * @param isNearestAvailable true if nearest option is visible
54          */
55         public RearrangeFunction(App inApp, boolean isNearestAvailable)
56         {
57                 super(inApp);
58                 _nearestAvailable = isNearestAvailable;
59         }
60
61         /**
62          * Begin the function by showing the dialog
63          */
64         public void begin()
65         {
66                 // Make dialog window
67                 if (_dialog == null)
68                 {
69                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
70                         _dialog.setLocationRelativeTo(_parentFrame);
71                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
72                         _dialog.getContentPane().add(makeDialogComponents());
73                         _dialog.pack();
74                 }
75                 // If sorting by time isn't available, then disable radio button
76                 _sortRadios[2].setEnabled(isSortByTimeAllowed());
77                 // Show dialog
78                 _dialog.setVisible(true);
79         }
80
81
82         /**
83          * Create dialog components
84          * @return Panel containing all gui elements in dialog
85          */
86         private JPanel makeDialogComponents()
87         {
88                 JPanel dialogPanel = new JPanel();
89                 dialogPanel.setLayout(new BorderLayout());
90                 JLabel descLabel = new JLabel(I18nManager.getText(getDescriptionKey()));
91                 descLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
92                 dialogPanel.add(descLabel, BorderLayout.NORTH);
93                 // Radios for position (start / end / nearest)
94                 _positionRadios = new JRadioButton[3];
95                 final String[] posNames = {"tostart", "toend", "tonearest"};
96                 ButtonGroup posGroup = new ButtonGroup();
97                 JPanel posPanel = new JPanel();
98                 posPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
99                 for (int i=0; i<posNames.length; i++)
100                 {
101                         _positionRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + posNames[i]));
102                         posGroup.add(_positionRadios[i]);
103                         posPanel.add(_positionRadios[i]);
104                 }
105                 _positionRadios[0].setSelected(true);
106                 _positionRadios[2].setVisible(_nearestAvailable);
107
108                 // Radios for sort (none / filename / time)
109                 _sortRadios = new JRadioButton[3];
110                 final String[] sortNames = {"nosort", getSortNameKey(), "sortbytime"};
111                 ButtonGroup sortGroup = new ButtonGroup();
112                 JPanel sortPanel = new JPanel();
113                 sortPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
114                 for (int i=0; i<3; i++)
115                 {
116                         _sortRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + sortNames[i]));
117                         sortGroup.add(_sortRadios[i]);
118                         sortPanel.add(_sortRadios[i]);
119                 }
120                 _sortRadios[0].setSelected(true);
121                 // Use listener to disable all sort options if nearest type chosen, re-enable otherwise
122                 ActionListener rearrListener = new ActionListener() {
123                         public void actionPerformed(ActionEvent arg0) {
124                                 final boolean sortAvailable = !_positionRadios[2].isSelected();
125                                 for (int i=0; i<_sortRadios.length; i++) {
126                                         _sortRadios[i].setEnabled(sortAvailable);
127                                 }
128                         }
129                 };
130                 for (int i=0; i<_positionRadios.length; i++) {
131                         _positionRadios[i].addActionListener(rearrListener);
132                 }
133                 // add to middle of dialog
134                 JPanel centrePanel = new JPanel();
135                 centrePanel.setLayout(new BoxLayout(centrePanel, BoxLayout.Y_AXIS));
136                 centrePanel.add(posPanel);
137                 centrePanel.add(sortPanel);
138                 dialogPanel.add(centrePanel, BorderLayout.CENTER);
139                 // button panel at bottom
140                 JPanel buttonPanel = new JPanel();
141                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
142                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
143                 okButton.addActionListener(new ActionListener() {
144                         public void actionPerformed(ActionEvent e) {
145                                 finish();
146                                 _dialog.dispose();
147                         }
148                 });
149                 buttonPanel.add(okButton);
150                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
151                 cancelButton.addActionListener(new ActionListener() {
152                         public void actionPerformed(ActionEvent e) {
153                                 _dialog.dispose();
154                         }
155                 });
156                 buttonPanel.add(cancelButton);
157                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
158                 return dialogPanel;
159         }
160
161         /**
162          * @return true if sorting by time is allowed, false otherwise
163          */
164         protected boolean isSortByTimeAllowed()
165         {
166                 return true;
167         }
168
169         /**
170          * @return the selected rearrange option
171          */
172         protected Rearrange getRearrangeOption()
173         {
174                 if (_positionRadios[0].isSelected()) {
175                         return Rearrange.TO_START;
176                 }
177                 if (_positionRadios[1].isSelected()) {
178                         return Rearrange.TO_END;
179                 }
180                 return Rearrange.TO_NEAREST;
181         }
182
183         /**
184          * @return the selected sort mode
185          */
186         protected SortMode getSortMode()
187         {
188                 if (_sortRadios[0].isSelected()) {
189                         return SortMode.DONT_SORT;
190                 }
191                 if (_sortRadios[1].isSelected()) {
192                         return SortMode.SORTBY_NAME;
193                 }
194                 return SortMode.SORTBY_TIME;
195         }
196
197         /** @return key for description */
198         protected abstract String getDescriptionKey();
199
200         /** @return partial key for the sort by name radio */
201         protected abstract String getSortNameKey();
202
203         /**
204          * Perform the rearrange
205          */
206         protected abstract void finish();
207 }