]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/load/babel/AddFilterDialog.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / load / babel / AddFilterDialog.java
1 package tim.prune.load.babel;
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.JFrame;
12 import javax.swing.JPanel;
13
14 import tim.prune.I18nManager;
15 import tim.prune.gui.WizardLayout;
16
17
18 /**
19  * Class to manage a dialog for adding a single GPSBabel filter
20  */
21 public class AddFilterDialog
22 {
23         /** Parent panel to pass the filter back to */
24         private BabelFilterPanel _parentPanel = null;
25         /** Reference to parent frame */
26         private JFrame _parentFrame = null;
27         /** Main dialog */
28         private JDialog _dialog = null;
29         /** layout for dealing with cards */
30         private WizardLayout _wizard = null;
31         /** Array of filter definitions */
32         private FilterDefinition[] _filters = new FilterDefinition[4];
33         /** Finish button */
34         private JButton _finishButton = null;
35         /** back button */
36         private JButton _backButton = null;
37
38         // Selector class for one of the filter types
39         class FilterTypeListener implements ActionListener
40         {
41                 private int _index = 0;
42                 public FilterTypeListener(int inIndex) {_index = inIndex;}
43                 public void actionPerformed(ActionEvent e) {
44                         _wizard.showCard(_index);
45                         _backButton.setEnabled(true);
46                         filterParamsChanged(); // to check parameters and enable/disable Finish button
47                 }
48         }
49
50         /**
51          * Constructor
52          * @param inParent parent panel to inform of selected filter
53          * @param inParentFrame parent frame to reference for dialogs
54          */
55         public AddFilterDialog(BabelFilterPanel inParent, JFrame inParentFrame)
56         {
57                 _parentPanel = inParent;
58                 _parentFrame = inParentFrame;
59         }
60
61         /**
62          * Show the dialog to add a new filter
63          */
64         public void showDialog()
65         {
66                 if (_dialog == null)
67                 {
68                         _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.addfilter.title"), true);
69                         _dialog.setLocationRelativeTo(_parentFrame);
70                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
71                         _dialog.getContentPane().add(makeDialogComponents());
72                         _dialog.pack();
73                 }
74                 // TODO: Initialise cards, clear entries?
75                 _wizard.showFirstCard();
76                 _backButton.setEnabled(false);
77                 _finishButton.setEnabled(false);
78                 _dialog.setVisible(true);
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
90                 // card panel in the middle
91                 JPanel cardPanel = new JPanel();
92                 _wizard = new WizardLayout(cardPanel);
93                 JPanel typesCard = new JPanel();
94                 JButton discardButton = new JButton(I18nManager.getText("dialog.gpsbabel.filter.discard"));
95                 discardButton.addActionListener(new FilterTypeListener(1));
96                 typesCard.add(discardButton);
97                 JButton simplifyButton = new JButton(I18nManager.getText("dialog.gpsbabel.filter.simplify"));
98                 simplifyButton.addActionListener(new FilterTypeListener(2));
99                 typesCard.add(simplifyButton);
100                 JButton distanceButton = new JButton(I18nManager.getText("dialog.gpsbabel.filter.distance"));
101                 distanceButton.addActionListener(new FilterTypeListener(3));
102                 typesCard.add(distanceButton);
103                 JButton interpButton = new JButton(I18nManager.getText("dialog.gpsbabel.filter.interpolate"));
104                 interpButton.addActionListener(new FilterTypeListener(4));
105                 typesCard.add(interpButton);
106
107                 // discard panel
108                 _filters[0] = new DiscardFilter(this);
109                 // simplify panel
110                 _filters[1] = new SimplifyFilter(this);
111                 // distance panel
112                 _filters[2] = new DistanceFilter(this);
113                 // interpolate panel
114                 _filters[3] = new InterpolateFilter(this);
115
116                 // Add cards to the wizard
117                 _wizard.addCard(typesCard);
118                 _wizard.addCard(_filters[0]);
119                 _wizard.addCard(_filters[1]);
120                 _wizard.addCard(_filters[2]);
121                 _wizard.addCard(_filters[3]);
122                 dialogPanel.add(cardPanel, BorderLayout.CENTER);
123
124                 // button panel at bottom
125                 JPanel buttonPanel = new JPanel();
126                 buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
127                 _backButton = new JButton(I18nManager.getText("button.back"));
128                 _backButton.addActionListener(new ActionListener() {
129                         public void actionPerformed(ActionEvent e)
130                         {
131                                 _wizard.showCard(0);
132                                 _backButton.setEnabled(!_wizard.isFirstCard());
133                                 _finishButton.setEnabled(false);
134                         }
135                 });
136                 _backButton.setEnabled(false);
137                 buttonPanel.add(_backButton);
138                 _finishButton = new JButton(I18nManager.getText("button.finish"));
139                 ActionListener okListener = new ActionListener() {
140                         public void actionPerformed(ActionEvent e)
141                         {
142                                 finish();
143                         }
144                 };
145                 _finishButton.addActionListener(okListener);
146                 _finishButton.setEnabled(false);
147                 buttonPanel.add(_finishButton);
148                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
149                 cancelButton.addActionListener(new ActionListener() {
150                         public void actionPerformed(ActionEvent e)
151                         {
152                                 _dialog.dispose();
153                         }
154                 });
155                 buttonPanel.add(cancelButton);
156                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
157                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
158
159                 return dialogPanel;
160         }
161
162         /**
163          * React to changes in the filter parameters (such as enabling/disabling the ok button)
164          */
165         public void filterParamsChanged()
166         {
167                 final int currCard = _wizard.getCurrentCardIndex();
168                 if (currCard > 0 && currCard < 5) {
169                         _finishButton.setEnabled(_filters[currCard-1].isFilterValid());
170                 }
171         }
172
173         /**
174          * Finish the dialog when OK pressed
175          */
176         private void finish()
177         {
178                 // finish dialog and pass results back to the parent panel
179                 final int currCard = _wizard.getCurrentCardIndex();
180                 if (currCard > 0 && currCard < 5) {
181                         _parentPanel.addFilter(_filters[currCard-1].getString());
182                 }
183                 _dialog.dispose();
184         }
185 }