1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.filters;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.List;
19 import java.util.Stack;
20 import java.util.StringTokenizer;
22 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
23 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
25 import org.eclipse.jface.dialogs.IDialogConstants;
27 //import org.eclipse.jface.text.Assert;
28 import org.eclipse.core.runtime.Assert;
29 import org.eclipse.jface.viewers.ArrayContentProvider;
30 import org.eclipse.jface.viewers.CheckStateChangedEvent;
31 import org.eclipse.jface.viewers.CheckboxTableViewer;
32 import org.eclipse.jface.viewers.ICheckStateListener;
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.LabelProvider;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.events.SelectionListener;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Text;
52 import org.eclipse.ui.PlatformUI;
53 import org.eclipse.ui.dialogs.SelectionDialog;
55 public class CustomFiltersDialog extends SelectionDialog {
57 private static final String SEPARATOR = ","; //$NON-NLS-1$
59 private String fViewId;
61 private boolean fEnablePatterns;
63 private String[] fPatterns;
65 private String[] fEnabledFilterIds;
67 private FilterDescriptor[] fBuiltInFilters;
69 private CheckboxTableViewer fCheckBoxList;
71 private Button fEnableUserDefinedPatterns;
73 private Text fUserDefinedPatterns;
75 private Stack fFilterDescriptorChangeHistory;
78 * Creates a dialog to customize Java element filters.
84 * @param enablePatterns
85 * <code>true</code> if pattern filters are enabled
88 * @param enabledFilterIds
89 * the Ids of the enabled filters
91 public CustomFiltersDialog(Shell shell, String viewId,
92 boolean enablePatterns, String[] patterns, String[] enabledFilterIds) {
95 Assert.isNotNull(viewId);
96 Assert.isNotNull(patterns);
97 Assert.isNotNull(enabledFilterIds);
100 fPatterns = patterns;
101 fEnablePatterns = enablePatterns;
102 fEnabledFilterIds = enabledFilterIds;
104 fBuiltInFilters = FilterDescriptor.getFilterDescriptors(fViewId);
105 fFilterDescriptorChangeHistory = new Stack();
106 setShellStyle(getShellStyle() | SWT.RESIZE);
109 protected void configureShell(Shell shell) {
110 setTitle(FilterMessages.getString("CustomFiltersDialog.title")); //$NON-NLS-1$
111 setMessage(FilterMessages
112 .getString("CustomFiltersDialog.filterList.label")); //$NON-NLS-1$
113 super.configureShell(shell);
114 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
115 IJavaHelpContextIds.CUSTOM_FILTERS_DIALOG);
119 * Overrides method in Dialog
121 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
123 protected Control createDialogArea(Composite parent) {
124 initializeDialogUnits(parent);
125 // create a composite with standard margins and spacing
126 Composite composite = new Composite(parent, SWT.NONE);
127 GridLayout layout = new GridLayout();
128 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
129 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
130 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
131 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
132 composite.setLayout(layout);
133 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
134 composite.setFont(parent.getFont());
135 Composite group = composite;
138 fEnableUserDefinedPatterns = new Button(group, SWT.CHECK);
139 fEnableUserDefinedPatterns.setFocus();
140 fEnableUserDefinedPatterns.setText(FilterMessages
141 .getString("CustomFiltersDialog.enableUserDefinedPattern")); //$NON-NLS-1$
144 fUserDefinedPatterns = new Text(group, SWT.SINGLE | SWT.BORDER);
145 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
146 | GridData.GRAB_HORIZONTAL);
147 data.widthHint = convertWidthInCharsToPixels(59);
148 fUserDefinedPatterns.setLayoutData(data);
149 String patterns = convertToString(fPatterns, SEPARATOR);
150 fUserDefinedPatterns.setText(patterns);
153 final Label info = new Label(group, SWT.LEFT);
154 info.setText(FilterMessages
155 .getString("CustomFiltersDialog.patternInfo")); //$NON-NLS-1$
157 // Enabling / disabling of pattern group
158 fEnableUserDefinedPatterns.setSelection(fEnablePatterns);
159 fUserDefinedPatterns.setEnabled(fEnablePatterns);
160 info.setEnabled(fEnablePatterns);
161 fEnableUserDefinedPatterns.addSelectionListener(new SelectionAdapter() {
162 public void widgetSelected(SelectionEvent e) {
163 boolean state = fEnableUserDefinedPatterns.getSelection();
164 fUserDefinedPatterns.setEnabled(state);
165 info.setEnabled(fEnableUserDefinedPatterns.getSelection());
167 fUserDefinedPatterns.setFocus();
171 // Filters provided by extension point
172 if (fBuiltInFilters.length > 0)
173 createCheckBoxList(group);
175 applyDialogFont(parent);
179 private void createCheckBoxList(Composite parent) {
181 new Label(parent, SWT.NONE);
183 Label info = new Label(parent, SWT.LEFT);
184 info.setText(FilterMessages
185 .getString("CustomFiltersDialog.filterList.label")); //$NON-NLS-1$
187 fCheckBoxList = CheckboxTableViewer.newCheckList(parent, SWT.BORDER);
188 GridData data = new GridData(GridData.FILL_BOTH);
189 data.heightHint = fCheckBoxList.getTable().getItemHeight() * 10;
190 fCheckBoxList.getTable().setLayoutData(data);
192 fCheckBoxList.setLabelProvider(createLabelPrivder());
193 fCheckBoxList.setContentProvider(new ArrayContentProvider());
195 fCheckBoxList.setInput(fBuiltInFilters);
196 setInitialSelections(getEnabledFilterDescriptors());
198 List initialSelection = getInitialElementSelections();
199 if (initialSelection != null && !initialSelection.isEmpty())
200 checkInitialSelections();
203 info = new Label(parent, SWT.LEFT);
204 info.setText(FilterMessages
205 .getString("CustomFiltersDialog.description.label")); //$NON-NLS-1$
206 final Text description = new Text(parent, SWT.LEFT | SWT.WRAP
207 | SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL);
208 data = new GridData(GridData.FILL_HORIZONTAL);
209 data.heightHint = convertHeightInCharsToPixels(3);
210 description.setLayoutData(data);
212 .addSelectionChangedListener(new ISelectionChangedListener() {
213 public void selectionChanged(SelectionChangedEvent event) {
214 ISelection selection = event.getSelection();
215 if (selection instanceof IStructuredSelection) {
216 Object selectedElement = ((IStructuredSelection) selection)
218 if (selectedElement instanceof FilterDescriptor)
220 .setText(((FilterDescriptor) selectedElement)
225 fCheckBoxList.addCheckStateListener(new ICheckStateListener() {
227 * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
229 public void checkStateChanged(CheckStateChangedEvent event) {
230 Object element = event.getElement();
231 if (element instanceof FilterDescriptor) {
232 // renew if already touched
233 if (fFilterDescriptorChangeHistory.contains(element))
234 fFilterDescriptorChangeHistory.remove(element);
235 fFilterDescriptorChangeHistory.push(element);
240 addSelectionButtons(parent);
243 private void addSelectionButtons(Composite composite) {
244 Composite buttonComposite = new Composite(composite, SWT.RIGHT);
245 GridLayout layout = new GridLayout();
246 layout.numColumns = 2;
247 buttonComposite.setLayout(layout);
248 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
249 | GridData.GRAB_HORIZONTAL);
250 data.grabExcessHorizontalSpace = true;
251 composite.setData(data);
254 String label = FilterMessages
255 .getString("CustomFiltersDialog.SelectAllButton.label"); //$NON-NLS-1$
256 Button selectButton = createButton(buttonComposite,
257 IDialogConstants.SELECT_ALL_ID, label, false);
258 SWTUtil.setButtonDimensionHint(selectButton);
259 SelectionListener listener = new SelectionAdapter() {
260 public void widgetSelected(SelectionEvent e) {
261 fCheckBoxList.setAllChecked(true);
262 fFilterDescriptorChangeHistory.clear();
263 for (int i = 0; i < fBuiltInFilters.length; i++)
264 fFilterDescriptorChangeHistory.push(fBuiltInFilters[i]);
267 selectButton.addSelectionListener(listener);
269 // De-select All button
270 label = FilterMessages
271 .getString("CustomFiltersDialog.DeselectAllButton.label"); //$NON-NLS-1$
272 Button deselectButton = createButton(buttonComposite,
273 IDialogConstants.DESELECT_ALL_ID, label, false);
274 SWTUtil.setButtonDimensionHint(deselectButton);
275 listener = new SelectionAdapter() {
276 public void widgetSelected(SelectionEvent e) {
277 fCheckBoxList.setAllChecked(false);
278 fFilterDescriptorChangeHistory.clear();
279 for (int i = 0; i < fBuiltInFilters.length; i++)
280 fFilterDescriptorChangeHistory.push(fBuiltInFilters[i]);
283 deselectButton.addSelectionListener(listener);
286 private void checkInitialSelections() {
287 Iterator itemsToCheck = getInitialElementSelections().iterator();
288 while (itemsToCheck.hasNext())
289 fCheckBoxList.setChecked(itemsToCheck.next(), true);
292 protected void okPressed() {
293 if (fBuiltInFilters != null) {
294 ArrayList result = new ArrayList();
295 for (int i = 0; i < fBuiltInFilters.length; ++i) {
296 if (fCheckBoxList.getChecked(fBuiltInFilters[i]))
297 result.add(fBuiltInFilters[i]);
304 private ILabelProvider createLabelPrivder() {
305 return new LabelProvider() {
306 public Image getImage(Object element) {
310 public String getText(Object element) {
311 if (element instanceof FilterDescriptor)
312 return ((FilterDescriptor) element).getName();
319 // ---------- result handling ----------
321 protected void setResult(List newResult) {
322 super.setResult(newResult);
323 if (fUserDefinedPatterns.getText().length() > 0) {
324 fEnablePatterns = fEnableUserDefinedPatterns.getSelection();
325 fPatterns = convertFromString(fUserDefinedPatterns.getText(),
328 fEnablePatterns = false;
329 fPatterns = new String[0];
334 * @return the patterns which have been entered by the user
336 public String[] getUserDefinedPatterns() {
341 * @return the Ids of the enabled built-in filters
343 public String[] getEnabledFilterIds() {
344 Object[] result = getResult();
345 Set enabledIds = new HashSet(result.length);
346 for (int i = 0; i < result.length; i++)
347 enabledIds.add(((FilterDescriptor) result[i]).getId());
348 return (String[]) enabledIds.toArray(new String[enabledIds.size()]);
352 * @return <code>true</code> if the user-defined patterns are disabled
354 public boolean areUserDefinedPatternsEnabled() {
355 return fEnablePatterns;
359 * @return a stack with the filter descriptor check history
362 public Stack getFilterDescriptorChangeHistory() {
363 return fFilterDescriptorChangeHistory;
366 private FilterDescriptor[] getEnabledFilterDescriptors() {
367 FilterDescriptor[] filterDescs = fBuiltInFilters;
368 List result = new ArrayList(filterDescs.length);
369 List enabledFilterIds = Arrays.asList(fEnabledFilterIds);
370 for (int i = 0; i < filterDescs.length; i++) {
371 String id = filterDescs[i].getId();
372 if (enabledFilterIds.contains(id))
373 result.add(filterDescs[i]);
375 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
379 public static String[] convertFromString(String patterns, String separator) {
380 StringTokenizer tokenizer = new StringTokenizer(patterns, separator,
382 int tokenCount = tokenizer.countTokens();
383 List result = new ArrayList(tokenCount);
384 boolean escape = false;
385 boolean append = false;
386 while (tokenizer.hasMoreTokens()) {
387 String token = tokenizer.nextToken().trim();
388 if (separator.equals(token)) {
392 addPattern(result, separator);
399 addPattern(result, token);
404 return (String[]) result.toArray(new String[result.size()]);
407 private static void addPattern(List list, String pattern) {
411 int index = list.size() - 1;
412 list.set(index, ((String) list.get(index)) + pattern);
416 public static String convertToString(String[] patterns, String separator) {
417 int length = patterns.length;
418 StringBuffer strBuf = new StringBuffer();
420 strBuf.append(escapeSeparator(patterns[0], separator));
422 return ""; //$NON-NLS-1$
425 strBuf.append(separator);
426 strBuf.append(" "); //$NON-NLS-1$
427 strBuf.append(escapeSeparator(patterns[i++], separator));
429 return strBuf.toString();
432 private static String escapeSeparator(String pattern, String separator) {
433 int length = pattern.length();
434 StringBuffer buf = new StringBuffer(length);
435 for (int i = 0; i < length; i++) {
436 char ch = pattern.charAt(i); //$NON-NLS-1$
437 if (separator.equals(String.valueOf(ch)))
441 return buf.toString();