File: BabelFilterPanel.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (184 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tim.prune.load.babel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import tim.prune.I18nManager;
import tim.prune.gui.StatusIcon;

/**
 * Gui element to allow the specification of filters for GPSBabel.
 * Used for loading from GPS and loading from file
 */
public class BabelFilterPanel extends JPanel
{
	/** Text field for entering filters manually */
	private JTextField _filterField = null;
	/** Icon for showing whether the value is valid for GPSBabel or not */
	private StatusIcon _validIcon = null;
	/** Dialog for adding a new filter */
	private AddFilterDialog _addDialog = null;

	/** Regular expression for detecting valid filter strings */
	private static final Pattern FILTER_PATTERN
		= Pattern.compile("(-x [a-z,\\.0-9=]+ *)+");

	/**
	 * Constructor
	 * @param inParentFrame parent frame for launching popup dialog
	 */
	public BabelFilterPanel(JFrame inParentFrame)
	{
		setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(I18nManager.getText("dialog.gpsbabel.filters")),
			BorderFactory.createEmptyBorder(2, 2, 2, 2)));
		initPanel();
		_addDialog = new AddFilterDialog(this, inParentFrame);
	}

	/**
	 * Set up the panel with all the components inside
	 */
	private void initPanel()
	{
		setLayout(new BorderLayout(4, 4));
		// text field for the filter text
		_filterField = new JTextField(20);
		_filterField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent arg0) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						checkFilter();
					}
				});
			}
		});
		JPanel filterFieldPanel = new JPanel();
		filterFieldPanel.setLayout(new BorderLayout(3, 3));
		JPanel filterIconPanel = new JPanel();
		filterIconPanel.setLayout(new BorderLayout(3, 3));
		filterIconPanel.add(_filterField, BorderLayout.CENTER);
		_validIcon = new StatusIcon();
		filterIconPanel.add(_validIcon, BorderLayout.EAST);
		filterFieldPanel.add(filterIconPanel, BorderLayout.NORTH);
		add(filterFieldPanel, BorderLayout.CENTER);
		// Add and clear buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		JButton addButton = new JButton(I18nManager.getText("button.addnew"));
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// System.out.println("Filter exists: " + hasFilter() + ", valid: " + isFilterValid());
				_addDialog.showDialog();
			}
		});
		buttonPanel.add(addButton);
		buttonPanel.add(Box.createVerticalStrut(2));
		JButton clearButton = new JButton(I18nManager.getText("button.delete"));
		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				_filterField.setText("");
				checkFilter();
			}
		});
		buttonPanel.add(clearButton);
		add(buttonPanel, BorderLayout.EAST);
	}

	/**
	 * @param inFilter filter string to set (normally from config)
	 */
	public void setFilterString(String inFilter)
	{
		if (inFilter != null && _filterField != null) {
			_filterField.setText(inFilter.trim());
		}
		checkFilter();
	}

	/**
	 * @return trimmed filter string, or null
	 */
	public String getFilterString()
	{
		String filter = _filterField.getText();
		if (filter != null) filter = filter.trim();
		return filter;
	}

	/**
	 * @return true if a filter has been given (which may or may not be valid)
	 */
	public boolean hasFilter()
	{
		String str = getFilterString();
		return str != null && str.length() > 0;
	}

	/**
	 * @return true if the given filter string is valid
	 */
	public boolean isFilterValid()
	{
		String str = getFilterString();
		if (str == null) return false;
		return FILTER_PATTERN.matcher(str).matches();
	}

	/**
	 * Called from the add filter dialog to indicate completion
	 * @param inFilter filter to add
	 */
	public void addFilter(String inFilter)
	{
		if (inFilter != null)
		{
			String newFilter = inFilter.trim();
			String currFilter = getFilterString();
			if (!newFilter.equals(""))
			{
				if (currFilter == null || currFilter.equals("")) {
					currFilter = newFilter;
				}
				else { // append
					currFilter = currFilter + " " + newFilter;
				}
			}
			_filterField.setText(currFilter);
		}
		checkFilter();
	}

	/**
	 * See if the current filter is valid or not, and update the icon accordingly
	 */
	private void checkFilter()
	{
		if (hasFilter())
		{
			if (isFilterValid()) {
				_validIcon.setStatusValid();
			}
			else {
				_validIcon.setStatusInvalid();
			}
		}
		else
		{
			_validIcon.setStatusBlank();
		}
	}
}