KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > MatchFilterSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.search2.internal.ui;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.Text;
30
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.dialogs.StatusDialog;
33 import org.eclipse.jface.viewers.ArrayContentProvider;
34 import org.eclipse.jface.viewers.CheckStateChangedEvent;
35 import org.eclipse.jface.viewers.CheckboxTableViewer;
36 import org.eclipse.jface.viewers.ICheckStateListener;
37 import org.eclipse.jface.viewers.ISelectionChangedListener;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.LabelProvider;
40 import org.eclipse.jface.viewers.SelectionChangedEvent;
41
42 import org.eclipse.search.ui.NewSearchUI;
43 import org.eclipse.search.ui.text.MatchFilter;
44
45 import org.eclipse.search.internal.ui.SearchPlugin;
46
47
48 /**
49  * A dialog that lets users configure the active {@link MatchFilter match filters} and (optionally) the
50  * maximal number of top level elements.
51  *
52  * @since 3.3
53  */

54 public class MatchFilterSelectionDialog extends StatusDialog {
55
56     private final boolean fShowLimitConfigurationControls;
57     private final MatchFilter[] fAllFilters;
58     
59     private MatchFilter[] fEnabledFilters;
60     
61     private CheckboxTableViewer fListViewer;
62     private Button fLimitElementsCheckbox;
63     private Text fLimitElementsField;
64     private Text fDescription;
65     
66     private int fLimitElementCount;
67     private int fLastLimit;
68     private final boolean fEnableMatchFilterConfiguration;
69
70
71     /**
72      * Creates a {@link MatchFilterSelectionDialog}.
73      *
74      * @param shell the parent shell
75      * @param enableMatchFilterConfiguration
76      * @param allFilters all filters available for selection
77      * @param selectedFilters the initially selected filters
78      * @param enableLimitConfiguration if set, the dialog will also contain controls to limit
79      * the number of top level elements
80      * @param limit the initial limit or -1 if no limit should be used.
81      */

82     public MatchFilterSelectionDialog(Shell shell, boolean enableMatchFilterConfiguration, MatchFilter[] allFilters, MatchFilter[] selectedFilters, boolean enableLimitConfiguration, int limit) {
83         super(shell);
84
85         setTitle(SearchMessages.MatchFilterSelectionDialog_label);
86         setStatusLineAboveButtons(true);
87         setShellStyle(getShellStyle() | SWT.RESIZE);
88         
89         fShowLimitConfigurationControls= enableLimitConfiguration;
90         fEnableMatchFilterConfiguration= enableMatchFilterConfiguration;
91
92         fAllFilters= allFilters;
93         fEnabledFilters= selectedFilters;
94         
95         fLimitElementCount= limit;
96         fLastLimit= limit != -1 ? limit : 1000;
97     }
98     
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
101      */

102     protected IDialogSettings getDialogBoundsSettings() {
103         String JavaDoc name= "MatchFilterSelectionDialog_" + String.valueOf(fShowLimitConfigurationControls) + '.' + String.valueOf(fEnableMatchFilterConfiguration); //$NON-NLS-1$
104
return SearchPlugin.getDefault().getDialogSettingsSection(name);
105     }
106         
107     /**
108      * Returns the currently selected match filters.
109      *
110      * @return the currently selected match filters.
111      */

112     public MatchFilter[] getMatchFilters() {
113         return fEnabledFilters;
114     }
115
116     /**
117      * Returns the currently configured limit for top level elements. <code>-1</code> is returned if
118      * no limit should be applied.
119      *
120      * @return the currently configured limit for top level elements or -1 if no limit should be used.
121      */

122     public int getLimit() {
123         return fLimitElementCount;
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
128      */

129     protected Control createDialogArea(Composite composite) {
130         Composite parent = (Composite) super.createDialogArea(composite);
131         initializeDialogUnits(composite);
132
133         if (fShowLimitConfigurationControls) {
134             createTableLimit(parent);
135         }
136         if (fEnableMatchFilterConfiguration) {
137             createMatchFilterControls(parent);
138         }
139         return parent;
140     }
141
142     private void createMatchFilterControls(Composite parent) {
143         // Create list viewer
144
Label l= new Label(parent, SWT.NONE);
145         l.setFont(parent.getFont());
146         l.setText(SearchMessages.MatchFilterSelectionDialog_filter_description);
147         
148         Table table = new Table(parent, SWT.CHECK | SWT.BORDER);
149         table.setFont(parent.getFont());
150         fListViewer = new CheckboxTableViewer(table);
151         
152         GridData data = new GridData(GridData.FILL_BOTH);
153         data.minimumHeight= convertHeightInCharsToPixels(8);
154         table.setLayoutData(data);
155
156         class ListenerAndLabelProvider extends LabelProvider implements ISelectionChangedListener, ICheckStateListener {
157             public void selectionChanged(SelectionChangedEvent event) {
158                 performFilterListSelectionChanged();
159             }
160
161             public void checkStateChanged(CheckStateChangedEvent event) {
162                 performFilterListCheckStateChanged();
163             }
164             
165             public String JavaDoc getText(Object JavaDoc element) {
166                 return ((MatchFilter) element).getName();
167             }
168         }
169         ListenerAndLabelProvider listenerAndLP= new ListenerAndLabelProvider();
170         
171         fListViewer.setLabelProvider(listenerAndLP);
172         fListViewer.setContentProvider(new ArrayContentProvider());
173         fListViewer.addSelectionChangedListener(listenerAndLP);
174         fListViewer.addCheckStateListener(listenerAndLP);
175         fListViewer.setInput(fAllFilters);
176         fListViewer.setCheckedElements(fEnabledFilters);
177
178         l= new Label(parent, SWT.NONE);
179         l.setFont(parent.getFont());
180         l.setText(SearchMessages.MatchFilterSelectionDialog_description_label);
181         fDescription = new Text(parent, SWT.LEFT | SWT.WRAP | SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL);
182         fDescription.setFont(parent.getFont());
183         data = new GridData(GridData.FILL_HORIZONTAL);
184         data.heightHint = convertHeightInCharsToPixels(3);
185         fDescription.setLayoutData(data);
186     }
187
188     private void createTableLimit(Composite ancestor) {
189         Composite parent = new Composite(ancestor, SWT.NONE);
190         parent.setFont(ancestor.getFont());
191         GridLayout gl = new GridLayout();
192         gl.numColumns = 2;
193         gl.marginWidth = 0;
194         gl.marginHeight = 0;
195         parent.setLayout(gl);
196         GridData gd = new GridData();
197         gd.horizontalSpan = 2;
198         parent.setLayoutData(gd);
199
200         fLimitElementsCheckbox = new Button(parent, SWT.CHECK);
201         fLimitElementsCheckbox.setText(SearchMessages.MatchFilterSelectionDialog_limit_description);
202         fLimitElementsCheckbox.setLayoutData(new GridData());
203         fLimitElementsCheckbox.setFont(parent.getFont());
204         
205         fLimitElementsField = new Text(parent, SWT.BORDER);
206         fLimitElementsField.setFont(parent.getFont());
207         gd = new GridData();
208         gd.widthHint = convertWidthInCharsToPixels(6);
209         fLimitElementsField.setLayoutData(gd);
210
211         fLimitElementsCheckbox.addSelectionListener(new SelectionAdapter() {
212             public void widgetSelected(SelectionEvent e) {
213                 performLimitCheckboxChanged();
214             }
215         });
216
217         fLimitElementsField.addModifyListener(new ModifyListener() {
218             public void modifyText(ModifyEvent e) {
219                 performLimitTextModified();
220             }
221         });
222         fLimitElementsCheckbox.setSelection(fLimitElementCount != -1);
223         fLimitElementsField.setText(String.valueOf(fLastLimit));
224         fLimitElementsField.setEnabled(fLimitElementsCheckbox.getSelection());
225     }
226
227     private void performFilterListSelectionChanged() {
228         Object JavaDoc selectedElement = ((IStructuredSelection) fListViewer.getSelection()).getFirstElement();
229         if (selectedElement != null)
230             fDescription.setText(((MatchFilter) selectedElement).getDescription());
231         else
232             fDescription.setText(new String JavaDoc());
233     }
234     
235     private void performFilterListCheckStateChanged() {
236         Object JavaDoc[] checked= fListViewer.getCheckedElements();
237         fEnabledFilters= new MatchFilter[checked.length];
238         System.arraycopy(checked, 0, fEnabledFilters, 0, checked.length);
239     }
240     
241     private void performLimitCheckboxChanged() {
242         fLimitElementsField.setEnabled(fLimitElementsCheckbox.getSelection());
243         fLimitElementCount= fLimitElementsCheckbox.getSelection() ? fLastLimit : -1;
244         performLimitTextModified();
245     }
246
247     private void performLimitTextModified() {
248         String JavaDoc text = fLimitElementsField.getText();
249         int value = -1;
250         try {
251             value = Integer.valueOf(text).intValue();
252         } catch (NumberFormatException JavaDoc e) {
253         }
254         fLimitElementCount= value;
255         if (fLimitElementsCheckbox.getSelection() && value <= 0)
256             updateStatus(createStatus(IStatus.ERROR, SearchMessages.MatchFilterSelectionDialog_error_invalid_limit));
257         else
258             updateStatus(createStatus(IStatus.OK, "")); //$NON-NLS-1$
259
}
260     
261     private IStatus createStatus(int severity, String JavaDoc message) {
262         return new Status(severity, NewSearchUI.PLUGIN_ID, severity, message, null);
263     }
264 }
265
Popular Tags