]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/load/babel/BabelFilterPanel.java
b5b564f0d8b21f25872d13386a234975924b531e
[GpsPrune.git] / src / tim / prune / load / babel / BabelFilterPanel.java
1 package tim.prune.load.babel;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.KeyAdapter;
7 import java.awt.event.KeyEvent;
8 import java.util.regex.Pattern;
9
10 import javax.swing.BorderFactory;
11 import javax.swing.Box;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JPanel;
16 import javax.swing.JTextField;
17 import javax.swing.SwingUtilities;
18
19 import tim.prune.I18nManager;
20 import tim.prune.gui.StatusIcon;
21
22 /**
23  * Gui element to allow the specification of filters for GPSBabel.
24  * Used for loading from GPS and loading from file
25  */
26 public class BabelFilterPanel extends JPanel
27 {
28         /** Text field for entering filters manually */
29         private JTextField _filterField = null;
30         /** Icon for showing whether the value is valid for GPSBabel or not */
31         private StatusIcon _validIcon = null;
32         /** Dialog for adding a new filter */
33         private AddFilterDialog _addDialog = null;
34
35         /** Regular expression for detecting valid filter strings */
36         private static final Pattern FILTER_PATTERN
37                 = Pattern.compile("(-x [a-z,\\.0-9=]+ *)+");
38
39         /**
40          * Constructor
41          * @param inParentFrame parent frame for launching popup dialog
42          */
43         public BabelFilterPanel(JFrame inParentFrame)
44         {
45                 setBorder(BorderFactory.createCompoundBorder(
46                         BorderFactory.createTitledBorder(I18nManager.getText("dialog.gpsbabel.filters")),
47                         BorderFactory.createEmptyBorder(2, 2, 2, 2)));
48                 initPanel();
49                 _addDialog = new AddFilterDialog(this, inParentFrame);
50         }
51
52         /**
53          * Set up the panel with all the components inside
54          */
55         private void initPanel()
56         {
57                 setLayout(new BorderLayout(4, 4));
58                 // text field for the filter text
59                 _filterField = new JTextField(20);
60                 _filterField.addKeyListener(new KeyAdapter() {
61                         public void keyTyped(KeyEvent arg0) {
62                                 SwingUtilities.invokeLater(new Runnable() {
63                                         public void run() {
64                                                 checkFilter();
65                                         }
66                                 });
67                         }
68                 });
69                 JPanel filterFieldPanel = new JPanel();
70                 filterFieldPanel.setLayout(new BorderLayout(3, 3));
71                 JPanel filterIconPanel = new JPanel();
72                 filterIconPanel.setLayout(new BorderLayout(3, 3));
73                 filterIconPanel.add(_filterField, BorderLayout.CENTER);
74                 _validIcon = new StatusIcon();
75                 filterIconPanel.add(_validIcon, BorderLayout.EAST);
76                 filterFieldPanel.add(filterIconPanel, BorderLayout.NORTH);
77                 add(filterFieldPanel, BorderLayout.CENTER);
78                 // Add and clear buttons
79                 JPanel buttonPanel = new JPanel();
80                 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
81                 JButton addButton = new JButton(I18nManager.getText("button.addnew"));
82                 addButton.addActionListener(new ActionListener() {
83                         public void actionPerformed(ActionEvent arg0) {
84                                 // System.out.println("Filter exists: " + hasFilter() + ", valid: " + isFilterValid());
85                                 _addDialog.showDialog();
86                         }
87                 });
88                 buttonPanel.add(addButton);
89                 buttonPanel.add(Box.createVerticalStrut(2));
90                 JButton clearButton = new JButton(I18nManager.getText("button.delete"));
91                 clearButton.addActionListener(new ActionListener() {
92                         public void actionPerformed(ActionEvent arg0) {
93                                 _filterField.setText("");
94                                 checkFilter();
95                         }
96                 });
97                 buttonPanel.add(clearButton);
98                 add(buttonPanel, BorderLayout.EAST);
99         }
100
101         /**
102          * @param inFilter filter string to set (normally from config)
103          */
104         public void setFilterString(String inFilter)
105         {
106                 if (inFilter != null && _filterField != null) {
107                         _filterField.setText(inFilter.trim());
108                 }
109                 checkFilter();
110         }
111
112         /**
113          * @return trimmed filter string, or null
114          */
115         public String getFilterString()
116         {
117                 String filter = _filterField.getText();
118                 if (filter != null) filter = filter.trim();
119                 return filter;
120         }
121
122         /**
123          * @return true if a filter has been given (which may or may not be valid)
124          */
125         public boolean hasFilter()
126         {
127                 String str = getFilterString();
128                 return str != null && str.length() > 0;
129         }
130
131         /**
132          * @return true if the given filter string is valid
133          */
134         public boolean isFilterValid()
135         {
136                 String str = getFilterString();
137                 if (str == null) return false;
138                 return FILTER_PATTERN.matcher(str).matches();
139         }
140
141         /**
142          * Called from the add filter dialog to indicate completion
143          * @param inFilter filter to add
144          */
145         public void addFilter(String inFilter)
146         {
147                 if (inFilter != null)
148                 {
149                         String newFilter = inFilter.trim();
150                         String currFilter = getFilterString();
151                         if (!newFilter.equals(""))
152                         {
153                                 if (currFilter == null || currFilter.equals("")) {
154                                         currFilter = newFilter;
155                                 }
156                                 else { // append
157                                         currFilter = currFilter + " " + newFilter;
158                                 }
159                         }
160                         _filterField.setText(currFilter);
161                 }
162                 checkFilter();
163         }
164
165         /**
166          * See if the current filter is valid or not, and update the icon accordingly
167          */
168         private void checkFilter()
169         {
170                 if (hasFilter())
171                 {
172                         if (isFilterValid()) {
173                                 _validIcon.setStatusValid();
174                         }
175                         else {
176                                 _validIcon.setStatusInvalid();
177                         }
178                 }
179                 else
180                 {
181                         _validIcon.setStatusBlank();
182                 }
183         }
184 }