KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > util > TypeFilteringDialog


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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31
32 import org.eclipse.jface.dialogs.IDialogConstants;
33 import org.eclipse.jface.viewers.CheckboxTableViewer;
34
35 import org.eclipse.ui.IFileEditorMapping;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.dialogs.FileEditorMappingContentProvider;
38 import org.eclipse.ui.dialogs.FileEditorMappingLabelProvider;
39 import org.eclipse.ui.dialogs.SelectionDialog;
40 import org.eclipse.ui.help.WorkbenchHelp;
41
42 import org.eclipse.search.internal.ui.ISearchHelpContextIds;
43 import org.eclipse.search.internal.ui.SearchMessages;
44
45 /**
46  * The TypeFilteringDialog is a SelectionDialog that allows the user to select a file editor.
47  * XXX: Workbench should offer this dialog (public API), see: bug 2763: TypeFilteringDialog should be public API
48  */

49 public class TypeFilteringDialog extends SelectionDialog {
50
51     private Collection JavaDoc fInitialSelections;
52
53     // the visual selection widget group
54
private CheckboxTableViewer fListViewer;
55
56     // sizing constants
57
private final static int SIZING_SELECTION_WIDGET_HEIGHT= 250;
58     private final static int SIZING_SELECTION_WIDGET_WIDTH= 300;
59
60     private Text fUserDefinedText;
61
62     private IFileEditorMapping[] fCurrentInput;
63     /**
64      * Creates a type selection dialog using the supplied entries. Set the initial selections to those
65      * whose extensions match the preselections.
66      */

67     public TypeFilteringDialog(Shell parentShell, Collection JavaDoc preselections) {
68         super(parentShell);
69         setTitle(SearchMessages.getString("TypesFiltering.title")); //$NON-NLS-1$
70
fInitialSelections= preselections;
71         setMessage(SearchMessages.getString("TypesFiltering.message")); //$NON-NLS-1$
72
}
73
74     /**
75      * Add the selection and deselection buttons to the dialog.
76      * @param composite org.eclipse.swt.widgets.Composite
77      */

78     private void addSelectionButtons(Composite composite) {
79
80         Composite buttonComposite= new Composite(composite, SWT.RIGHT);
81         GridLayout layout= new GridLayout();
82         layout.numColumns= 2;
83         buttonComposite.setLayout(layout);
84         GridData data =
85             new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
86         data.grabExcessHorizontalSpace= true;
87         composite.setData(data);
88
89         Button selectButton =
90             createButton(
91                 buttonComposite,
92                 IDialogConstants.SELECT_ALL_ID,
93                 SearchMessages.getString("TypesFiltering.selectAll"), //$NON-NLS-1$
94
false);
95
96         SelectionListener listener= new SelectionAdapter() {
97             public void widgetSelected(SelectionEvent e) {
98                 getListViewer().setAllChecked(true);
99             }
100         };
101         selectButton.addSelectionListener(listener);
102
103         Button deselectButton =
104             createButton(
105                 buttonComposite,
106                 IDialogConstants.DESELECT_ALL_ID,
107                 SearchMessages.getString("TypesFiltering.deselectAll"), //$NON-NLS-1$
108
false);
109
110         listener= new SelectionAdapter() {
111             public void widgetSelected(SelectionEvent e) {
112                 getListViewer().setAllChecked(false);
113
114             }
115         };
116         deselectButton.addSelectionListener(listener);
117
118     }
119
120     /**
121      * Add the currently-specified extensions.
122      */

123     private void addUserDefinedEntries(List JavaDoc result) {
124
125         StringTokenizer JavaDoc tokenizer =
126             new StringTokenizer JavaDoc(fUserDefinedText.getText(), FileTypeEditor.TYPE_DELIMITER);
127
128         //Allow the *. and . prefix and strip out the extension
129
while (tokenizer.hasMoreTokens()) {
130             String JavaDoc currentExtension= tokenizer.nextToken().trim();
131             if (!currentExtension.equals("")) //$NON-NLS-1$
132
result.add(currentExtension);
133         }
134     }
135
136     /**
137      * Visually checks the previously-specified elements in this dialog's list
138      * viewer.
139      */

140     private void checkInitialSelections() {
141
142         IFileEditorMapping editorMappings[] =
143             PlatformUI.getWorkbench().getEditorRegistry().getFileEditorMappings();
144         ArrayList JavaDoc selectedMappings= new ArrayList JavaDoc();
145
146         for (int i= 0; i < editorMappings.length; i++) {
147             IFileEditorMapping mapping= editorMappings[i];
148             if (fInitialSelections.contains(mapping.getLabel())) {
149                 fListViewer.setChecked(mapping, true);
150                 selectedMappings.add(mapping.getLabel());
151             }
152         }
153
154         //Now add in the ones not selected to the user defined list
155
Iterator JavaDoc initialIterator= fInitialSelections.iterator();
156         StringBuffer JavaDoc entries= new StringBuffer JavaDoc();
157         boolean first= true;
158         while (initialIterator.hasNext()) {
159             String JavaDoc nextExtension= (String JavaDoc)initialIterator.next();
160             if (!selectedMappings.contains(nextExtension)) {
161                 if (!first) {
162                     entries.append(FileTypeEditor.TYPE_DELIMITER);
163                     entries.append(" "); //$NON-NLS-1$
164
}
165                 first= false;
166                 entries.append(nextExtension);
167             }
168         }
169         fUserDefinedText.setText(entries.toString());
170     }
171
172     /* (non-Javadoc)
173      * Method declared in Window.
174      */

175     protected void configureShell(Shell shell) {
176         super.configureShell(shell);
177         WorkbenchHelp.setHelp(shell, ISearchHelpContextIds.TYPE_FILTERING_DIALOG);
178     }
179
180     /* (non-Javadoc)
181      * Method declared on Dialog.
182      */

183     protected Control createDialogArea(Composite parent) {
184         // page group
185
Composite composite= (Composite)super.createDialogArea(parent);
186
187         createMessageArea(composite);
188
189         fListViewer= CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
190         GridData data= new GridData(GridData.FILL_BOTH);
191         data.heightHint= SIZING_SELECTION_WIDGET_HEIGHT;
192         data.widthHint= SIZING_SELECTION_WIDGET_WIDTH;
193         fListViewer.getTable().setLayoutData(data);
194
195         fListViewer.setLabelProvider(FileEditorMappingLabelProvider.INSTANCE);
196         fListViewer.setContentProvider(FileEditorMappingContentProvider.INSTANCE);
197
198         addSelectionButtons(composite);
199
200         createUserEntryGroup(composite);
201
202         initializeViewer();
203
204         // initialize page
205
if (fInitialSelections != null && !fInitialSelections.isEmpty())
206             checkInitialSelections();
207
208         applyDialogFont(composite);
209         return composite;
210     }
211
212     /**
213      * Create the group that shows the user defined entries for the dialog.
214      * @param parent the parent this is being created in.
215      */

216     private void createUserEntryGroup(Composite parent) {
217
218         // destination specification group
219
Composite userDefinedGroup= new Composite(parent, SWT.NONE);
220         GridLayout layout= new GridLayout();
221         layout.numColumns= 2;
222         userDefinedGroup.setLayout(layout);
223         userDefinedGroup.setLayoutData(
224             new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
225
226         new Label(userDefinedGroup, SWT.NONE).setText(
227             SearchMessages.getString("TypesFiltering.otherExtensions")); //$NON-NLS-1$
228

229         // user defined entry field
230
fUserDefinedText= new Text(userDefinedGroup, SWT.SINGLE | SWT.BORDER);
231         GridData data =
232             new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
233         fUserDefinedText.setLayoutData(data);
234     }
235
236     /**
237      * Return the input to the dialog.
238      */

239     private IFileEditorMapping[] getInput() {
240
241         //Filter the mappings to be just those with a wildcard extension
242
if (fCurrentInput == null) {
243             List JavaDoc wildcardEditors= new ArrayList JavaDoc();
244             IFileEditorMapping[] allMappings =
245                 PlatformUI.getWorkbench().getEditorRegistry().getFileEditorMappings();
246             for (int i= 0; i < allMappings.length; i++) {
247                 if (allMappings[i].getName().equals("*")) //$NON-NLS-1$
248
wildcardEditors.add(allMappings[i]);
249             }
250             fCurrentInput= new IFileEditorMapping[wildcardEditors.size()];
251             wildcardEditors.toArray(fCurrentInput);
252         }
253
254         return fCurrentInput;
255     }
256
257     /**
258      * Initializes this dialog's viewer after it has been laid out.
259      */

260     private void initializeViewer() {
261         fListViewer.setInput(getInput());
262     }
263
264     /**
265      * The <code>ListSelectionDialog</code> implementation of this
266      * <code>Dialog</code> method builds a list of the selected elements for later
267      * retrieval by the client and closes this dialog.
268      */

269     protected void okPressed() {
270
271         // Get the input children.
272
IFileEditorMapping[] children= getInput();
273
274         List JavaDoc list= new ArrayList JavaDoc();
275
276         // Build a list of selected children.
277
for (int i= 0; i < children.length; ++i) {
278             IFileEditorMapping element= children[i];
279             if (fListViewer.getChecked(element))
280                 list.add(element.getLabel());
281         }
282
283         addUserDefinedEntries(list);
284         setResult(list);
285         super.okPressed();
286     }
287
288     protected CheckboxTableViewer getListViewer() {
289         return fListViewer;
290     }
291 }
292
Popular Tags