1 |
d7389118
|
l.suhrbier
|
/*******************************************************************************
|
2 |
|
|
* Copyright (c) 2006, 2009 IBM Corporation and others.
|
3 |
|
|
* All rights reserved. This program and the accompanying materials
|
4 |
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
5 |
|
|
* which accompanies this distribution, and is available at
|
6 |
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
7 |
|
|
*
|
8 |
|
|
* Contributors:
|
9 |
|
|
* IBM Corporation - initial API and implementation
|
10 |
|
|
*******************************************************************************/
|
11 |
|
|
|
12 |
|
|
package org.eclipse.ui.internal.navigator.filters;
|
13 |
|
|
|
14 |
|
|
import java.util.Arrays;
|
15 |
|
|
import java.util.Comparator;
|
16 |
|
|
import java.util.Iterator;
|
17 |
|
|
import java.util.Set;
|
18 |
|
|
|
19 |
|
|
import org.eclipse.jface.viewers.ILabelProvider;
|
20 |
|
|
import org.eclipse.jface.viewers.ISelection;
|
21 |
|
|
import org.eclipse.jface.viewers.Viewer;
|
22 |
|
|
import org.eclipse.jface.viewers.ViewerFilter;
|
23 |
|
|
import org.eclipse.jface.viewers.ViewerSorter;
|
24 |
|
|
import org.eclipse.swt.SWT;
|
25 |
|
|
// RAP [bmichalik]: accessibility
|
26 |
|
|
//import org.eclipse.swt.accessibility.AccessibleAdapter;
|
27 |
|
|
//import org.eclipse.swt.accessibility.AccessibleEvent;
|
28 |
|
|
import org.eclipse.swt.events.FocusAdapter;
|
29 |
|
|
import org.eclipse.swt.events.FocusEvent;
|
30 |
|
|
import org.eclipse.swt.events.KeyAdapter;
|
31 |
|
|
import org.eclipse.swt.events.KeyEvent;
|
32 |
|
|
import org.eclipse.swt.events.ModifyEvent;
|
33 |
|
|
import org.eclipse.swt.events.ModifyListener;
|
34 |
|
|
import org.eclipse.swt.events.MouseAdapter;
|
35 |
|
|
import org.eclipse.swt.events.MouseEvent;
|
36 |
|
|
import org.eclipse.swt.events.TraverseEvent;
|
37 |
|
|
import org.eclipse.swt.events.TraverseListener;
|
38 |
|
|
import org.eclipse.swt.layout.GridData;
|
39 |
|
|
import org.eclipse.swt.widgets.Composite;
|
40 |
|
|
import org.eclipse.swt.widgets.Display;
|
41 |
|
|
import org.eclipse.swt.widgets.TableItem;
|
42 |
|
|
import org.eclipse.swt.widgets.Text;
|
43 |
|
|
import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
|
44 |
|
|
import org.eclipse.ui.internal.navigator.StringMatcher;
|
45 |
|
|
import org.eclipse.ui.navigator.ICommonFilterDescriptor;
|
46 |
|
|
import org.eclipse.ui.navigator.INavigatorContentService;
|
47 |
|
|
import org.eclipse.ui.navigator.INavigatorFilterService;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* @since 3.2
|
51 |
|
|
*
|
52 |
|
|
*/
|
53 |
|
|
public class CommonFiltersTab extends CustomizationTab {
|
54 |
|
|
|
55 |
|
|
private static final String ALL = "*"; //$NON-NLS-1$
|
56 |
|
|
|
57 |
|
|
private String initialFilterTextValue = CommonNavigatorMessages.CommonFilterSelectionDialog_enter_name_of_filte_;
|
58 |
|
|
|
59 |
|
|
private Text filterText;
|
60 |
|
|
|
61 |
|
|
private ILabelProvider filterLabelProvider = new CommonFilterLabelProvider();
|
62 |
|
|
|
63 |
|
|
private CommonFilterContentProvider filterContentProvider = new CommonFilterContentProvider();
|
64 |
|
|
|
65 |
|
|
private TablePatternFilter patternFilter = new TablePatternFilter();
|
66 |
|
|
|
67 |
|
|
protected CommonFiltersTab(Composite parent,
|
68 |
|
|
INavigatorContentService aContentService) {
|
69 |
|
|
super(parent, aContentService);
|
70 |
|
|
createControl();
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
private void createControl() {
|
74 |
|
|
|
75 |
|
|
createInstructionsLabel(CommonNavigatorMessages.CommonFilterSelectionDialog_Select_the_filters_to_apply);
|
76 |
|
|
|
77 |
|
|
createPatternFilterText(this);
|
78 |
|
|
|
79 |
|
|
createTable();
|
80 |
|
|
|
81 |
|
|
getTableViewer().setContentProvider(filterContentProvider);
|
82 |
|
|
getTableViewer().setLabelProvider(filterLabelProvider);
|
83 |
|
|
getTableViewer().setSorter(new CommonFilterSorter());
|
84 |
|
|
getTableViewer().setInput(getContentService());
|
85 |
|
|
|
86 |
|
|
getTableViewer().addFilter(patternFilter);
|
87 |
|
|
|
88 |
|
|
updateFiltersCheckState();
|
89 |
|
|
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
private void createPatternFilterText(Composite composite) {
|
93 |
|
|
filterText = new Text(composite, SWT.SINGLE | SWT.BORDER);
|
94 |
|
|
GridData filterTextGridData = new GridData(GridData.FILL_HORIZONTAL);
|
95 |
|
|
filterText.setLayoutData(filterTextGridData);
|
96 |
|
|
filterText.setText(initialFilterTextValue);
|
97 |
|
|
filterText.setFont(composite.getFont());
|
98 |
|
|
|
99 |
|
|
// RAP [bmichalik]: accessability
|
100 |
|
|
// filterText.getAccessible().addAccessibleListener(
|
101 |
|
|
// new AccessibleAdapter() {
|
102 |
|
|
// /*
|
103 |
|
|
// * (non-Javadoc)
|
104 |
|
|
// *
|
105 |
|
|
// * @see org.eclipse.swt.accessibility.AccessibleListener#getName(org.eclipse.swt.accessibility.AccessibleEvent)
|
106 |
|
|
// */
|
107 |
|
|
// public void getName(AccessibleEvent e) {
|
108 |
|
|
// String filterTextString = filterText.getText();
|
109 |
|
|
// if (filterTextString.length() == 0) {
|
110 |
|
|
// e.result = initialFilterTextValue;
|
111 |
|
|
// } else {
|
112 |
|
|
// e.result = filterTextString;
|
113 |
|
|
// }
|
114 |
|
|
// }
|
115 |
|
|
// });
|
116 |
|
|
|
117 |
|
|
filterText.addFocusListener(new FocusAdapter() {
|
118 |
|
|
/*
|
119 |
|
|
* (non-Javadoc)
|
120 |
|
|
*
|
121 |
|
|
* @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
|
122 |
|
|
*/
|
123 |
|
|
public void focusGained(FocusEvent e) {
|
124 |
|
|
if (initialFilterTextValue.equals(filterText.getText().trim())) {
|
125 |
|
|
filterText.selectAll();
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
});
|
129 |
|
|
|
130 |
|
|
filterText.addMouseListener(new MouseAdapter() {
|
131 |
|
|
/*
|
132 |
|
|
* (non-Javadoc)
|
133 |
|
|
*
|
134 |
|
|
* @see org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events.MouseEvent)
|
135 |
|
|
*/
|
136 |
|
|
public void mouseUp(MouseEvent e) {
|
137 |
|
|
super.mouseUp(e);
|
138 |
|
|
if (initialFilterTextValue.equals(filterText.getText().trim())) {
|
139 |
|
|
filterText.selectAll();
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
});
|
143 |
|
|
|
144 |
|
|
filterText.addKeyListener(new KeyAdapter() {
|
145 |
|
|
/*
|
146 |
|
|
* (non-Javadoc)
|
147 |
|
|
*
|
148 |
|
|
* @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
|
149 |
|
|
*/
|
150 |
|
|
public void keyPressed(KeyEvent e) {
|
151 |
|
|
// on a CR we want to transfer focus to the list
|
152 |
|
|
boolean hasItems = getTable().getItemCount() > 0;
|
153 |
|
|
if (hasItems && e.keyCode == SWT.ARROW_DOWN) {
|
154 |
|
|
getTable().setFocus();
|
155 |
|
|
} else if (e.character == SWT.CR) {
|
156 |
|
|
return;
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
});
|
160 |
|
|
|
161 |
|
|
// enter key set focus to tree
|
162 |
|
|
filterText.addTraverseListener(new TraverseListener() {
|
163 |
|
|
public void keyTraversed(TraverseEvent e) {
|
164 |
|
|
if (e.detail == SWT.TRAVERSE_RETURN) {
|
165 |
|
|
e.doit = false;
|
166 |
|
|
if (getTableViewer().getTable().getItemCount() == 0) {
|
167 |
|
|
Display.getCurrent().beep();
|
168 |
|
|
} else {
|
169 |
|
|
// if the initial filter text hasn't changed, do not try
|
170 |
|
|
// to match
|
171 |
|
|
boolean hasFocus = getTable().setFocus();
|
172 |
|
|
boolean textChanged = !initialFilterTextValue
|
173 |
|
|
.equals(filterText.getText().trim());
|
174 |
|
|
if (hasFocus && textChanged
|
175 |
|
|
&& filterText.getText().trim().length() > 0) {
|
176 |
|
|
TableItem item = getFirstHighlightedItem(getTable()
|
177 |
|
|
.getItems());
|
178 |
|
|
if (item != null) {
|
179 |
|
|
getTable().setSelection(
|
180 |
|
|
new TableItem[] { item });
|
181 |
|
|
ISelection sel = getTableViewer()
|
182 |
|
|
.getSelection();
|
183 |
|
|
getTableViewer().setSelection(sel, true);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
private TableItem getFirstHighlightedItem(TableItem[] items) {
|
191 |
|
|
for (int i = 0; i < items.length; i++) {
|
192 |
|
|
if (patternFilter.match(items[i].getText())) {
|
193 |
|
|
return items[i];
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
return null;
|
197 |
|
|
}
|
198 |
|
|
});
|
199 |
|
|
|
200 |
|
|
filterText.addModifyListener(new ModifyListener() {
|
201 |
|
|
/*
|
202 |
|
|
* (non-Javadoc)
|
203 |
|
|
*
|
204 |
|
|
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
|
205 |
|
|
*/
|
206 |
|
|
public void modifyText(ModifyEvent e) {
|
207 |
|
|
textChanged();
|
208 |
|
|
}
|
209 |
|
|
});
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
void setInitialFocus() {
|
213 |
|
|
filterText.forceFocus();
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
private void textChanged() {
|
217 |
|
|
patternFilter.setPattern(filterText.getText());
|
218 |
|
|
getTableViewer().refresh();
|
219 |
|
|
|
220 |
|
|
Set checkedItems = getCheckedItems();
|
221 |
|
|
for (Iterator iterator = checkedItems.iterator(); iterator.hasNext();) {
|
222 |
|
|
getTableViewer().setChecked(iterator.next(), true);
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
private void updateFiltersCheckState() {
|
227 |
|
|
Object[] children = filterContentProvider
|
228 |
|
|
.getElements(getContentService());
|
229 |
|
|
ICommonFilterDescriptor filterDescriptor;
|
230 |
|
|
INavigatorFilterService filterService = getContentService()
|
231 |
|
|
.getFilterService();
|
232 |
|
|
for (int i = 0; i < children.length; i++) {
|
233 |
|
|
filterDescriptor = (ICommonFilterDescriptor) children[i];
|
234 |
|
|
if(filterService.isActive(filterDescriptor.getId())) {
|
235 |
|
|
getTableViewer().setChecked(children[i], true);
|
236 |
|
|
getCheckedItems().add(children[i]);
|
237 |
|
|
} else {
|
238 |
|
|
getTableViewer().setChecked(children[i], false);
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
private class TablePatternFilter extends ViewerFilter {
|
244 |
|
|
|
245 |
|
|
private StringMatcher matcher = null;
|
246 |
|
|
|
247 |
|
|
/*
|
248 |
|
|
* (non-Javadoc)
|
249 |
|
|
*
|
250 |
|
|
* @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
|
251 |
|
|
* java.lang.Object, java.lang.Object)
|
252 |
|
|
*/
|
253 |
|
|
public boolean select(Viewer viewer, Object parentElement,
|
254 |
|
|
Object element) {
|
255 |
|
|
return match(filterLabelProvider.getText(element));
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
protected void setPattern(String newPattern) {
|
259 |
|
|
if (newPattern == null || newPattern.trim().length() == 0) {
|
260 |
|
|
matcher = new StringMatcher(ALL, true, false);
|
261 |
|
|
} else {
|
262 |
|
|
String patternString = ALL + newPattern + ALL;
|
263 |
|
|
matcher = new StringMatcher(patternString, true, false);
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
* Answers whether the given String matches the pattern.
|
270 |
|
|
*
|
271 |
|
|
* @param input
|
272 |
|
|
* the String to test
|
273 |
|
|
*
|
274 |
|
|
* @return whether the string matches the pattern
|
275 |
|
|
*/
|
276 |
|
|
protected boolean match(String input) {
|
277 |
|
|
if (input == null) {
|
278 |
|
|
return false;
|
279 |
|
|
}
|
280 |
|
|
return matcher == null || matcher.match(input);
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
private class CommonFilterSorter extends ViewerSorter {
|
285 |
|
|
|
286 |
|
|
/* (non-Javadoc)
|
287 |
|
|
* @see org.eclipse.jface.viewers.ViewerSorter#sort(org.eclipse.jface.viewers.Viewer, java.lang.Object[])
|
288 |
|
|
*/
|
289 |
|
|
public void sort(Viewer viewer, Object[] elements) {
|
290 |
|
|
Arrays.sort(elements, new Comparator() {
|
291 |
|
|
/* (non-Javadoc)
|
292 |
|
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
293 |
|
|
*/
|
294 |
|
|
public int compare(Object o1, Object o2) {
|
295 |
|
|
ICommonFilterDescriptor lvalue = (ICommonFilterDescriptor) o1;
|
296 |
|
|
ICommonFilterDescriptor rvalue = (ICommonFilterDescriptor) o2;
|
297 |
|
|
|
298 |
|
|
return lvalue.getName().compareTo(rvalue.getName());
|
299 |
|
|
}
|
300 |
|
|
});
|
301 |
|
|
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
}
|
305 |
|
|
|
306 |
|
|
}
|