1 package tim.prune.load.babel;
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;
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;
19 import tim.prune.I18nManager;
20 import tim.prune.gui.StatusIcon;
23 * Gui element to allow the specification of filters for GPSBabel.
24 * Used for loading from GPS and loading from file
26 public class BabelFilterPanel extends JPanel
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;
35 /** Regular expression for detecting valid filter strings */
36 private static final Pattern FILTER_PATTERN
37 = Pattern.compile("(-x [a-z,\\.0-9=]+ *)+");
41 * @param inParentFrame parent frame for launching popup dialog
43 public BabelFilterPanel(JFrame inParentFrame)
45 setBorder(BorderFactory.createCompoundBorder(
46 BorderFactory.createTitledBorder(I18nManager.getText("dialog.gpsbabel.filters")),
47 BorderFactory.createEmptyBorder(2, 2, 2, 2)));
49 _addDialog = new AddFilterDialog(this, inParentFrame);
53 * Set up the panel with all the components inside
55 private void initPanel()
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() {
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();
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("");
97 buttonPanel.add(clearButton);
98 add(buttonPanel, BorderLayout.EAST);
102 * @param inFilter filter string to set (normally from config)
104 public void setFilterString(String inFilter)
106 if (inFilter != null && _filterField != null) {
107 _filterField.setText(inFilter.trim());
113 * @return trimmed filter string, or null
115 public String getFilterString()
117 String filter = _filterField.getText();
118 if (filter != null) filter = filter.trim();
123 * @return true if a filter has been given (which may or may not be valid)
125 public boolean hasFilter()
127 String str = getFilterString();
128 return str != null && str.length() > 0;
132 * @return true if the given filter string is valid
134 public boolean isFilterValid()
136 String str = getFilterString();
137 if (str == null) return false;
138 return FILTER_PATTERN.matcher(str).matches();
142 * Called from the add filter dialog to indicate completion
143 * @param inFilter filter to add
145 public void addFilter(String inFilter)
147 if (inFilter != null)
149 String newFilter = inFilter.trim();
150 String currFilter = getFilterString();
151 if (!newFilter.equals(""))
153 if (currFilter == null || currFilter.equals("")) {
154 currFilter = newFilter;
157 currFilter = currFilter + " " + newFilter;
160 _filterField.setText(currFilter);
166 * See if the current filter is valid or not, and update the icon accordingly
168 private void checkFilter()
172 if (isFilterValid()) {
173 _validIcon.setStatusValid();
176 _validIcon.setStatusInvalid();
181 _validIcon.setStatusBlank();