KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > TypeFilteringDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11  * activated and used by other components.
12  * Markus Schorn <markus.schorn@windriver.com> - Fix for bug 136591 -
13  * [Dialogs] TypeFilteringDialog appends unnecessary comma
14  *******************************************************************************/

15 package org.eclipse.ui.dialogs;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.viewers.CheckboxTableViewer;
25 import org.eclipse.jface.viewers.ViewerComparator;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.graphics.Font;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.IFileEditorMapping;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
42 import org.eclipse.ui.internal.WorkbenchMessages;
43 import org.eclipse.ui.internal.registry.EditorRegistry;
44
45 /**
46  * The TypeFilteringDialog is a SelectionDialog that allows the user to select a
47  * file editor.
48  */

49 public class TypeFilteringDialog extends SelectionDialog {
50     Button addTypesButton;
51
52     Collection JavaDoc initialSelections;
53
54     // the visual selection widget group
55
CheckboxTableViewer listViewer;
56
57     // sizing constants
58
private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
59
60     private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
61
62     private final static String JavaDoc TYPE_DELIMITER = WorkbenchMessages.TypesFiltering_typeDelimiter;
63
64     //Define a title for the filter entry field.
65
private String JavaDoc filterTitle = WorkbenchMessages.TypesFiltering_otherExtensions;
66
67     Text userDefinedText;
68
69     IFileEditorMapping[] currentInput;
70
71     /**
72      * Creates a type filtering dialog using the supplied entries. Set the
73      * initial selections to those whose extensions match the preselections.
74      * @param parentShell The shell to parent the dialog from.
75      * @param preselections
76      * of String - a Collection of String to define the preselected
77      * types
78      */

79     public TypeFilteringDialog(Shell parentShell, Collection JavaDoc preselections) {
80         super(parentShell);
81         setTitle(WorkbenchMessages.TypesFiltering_title);
82         this.initialSelections = preselections;
83         setMessage(WorkbenchMessages.TypesFiltering_message);
84     }
85
86     /**
87      * Creates a type filtering dialog using the supplied entries. Set the
88      * initial selections to those whose extensions match the preselections.
89      *
90      * @param parentShell The shell to parent the dialog from.
91      * @param preselections
92      * of String - a Collection of String to define the preselected
93      * types
94      * @param filterText -
95      * the title of the text entry field for other extensions.
96      */

97     public TypeFilteringDialog(Shell parentShell, Collection JavaDoc preselections,
98             String JavaDoc filterText) {
99         this(parentShell, preselections);
100         this.filterTitle = filterText;
101     }
102
103     /**
104      * Add the selection and deselection buttons to the dialog.
105      *
106      * @param composite
107      * org.eclipse.swt.widgets.Composite
108      */

109     private void addSelectionButtons(Composite composite) {
110         Composite buttonComposite = new Composite(composite, SWT.RIGHT);
111         GridLayout layout = new GridLayout();
112         layout.numColumns = 2;
113         buttonComposite.setLayout(layout);
114         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
115                 | GridData.GRAB_HORIZONTAL);
116         data.grabExcessHorizontalSpace = true;
117         composite.setData(data);
118         Button selectButton = createButton(buttonComposite,
119                 IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_selectAll, false);
120         SelectionListener listener = new SelectionAdapter() {
121             public void widgetSelected(SelectionEvent e) {
122                 listViewer.setAllChecked(true);
123             }
124         };
125         selectButton.addSelectionListener(listener);
126         Button deselectButton = createButton(buttonComposite,
127                 IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_deselectAll, false);
128         listener = new SelectionAdapter() {
129             public void widgetSelected(SelectionEvent e) {
130                 listViewer.setAllChecked(false);
131             }
132         };
133         deselectButton.addSelectionListener(listener);
134     }
135
136     /**
137      * Add the currently-specified extensions to result.
138      * @param result
139      */

140     private void addUserDefinedEntries(List JavaDoc result) {
141         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(userDefinedText
142                 .getText(), TYPE_DELIMITER);
143         //Allow the *. and . prefix and strip out the extension
144
while (tokenizer.hasMoreTokens()) {
145             String JavaDoc currentExtension = tokenizer.nextToken().trim();
146             if (!currentExtension.equals("")) { //$NON-NLS-1$
147
if (currentExtension.startsWith("*.")) { //$NON-NLS-1$
148
result.add(currentExtension.substring(2));
149                 } else {
150                     if (currentExtension.startsWith(".")) { //$NON-NLS-1$
151
result.add(currentExtension.substring(1));
152                     } else {
153                         result.add(currentExtension);
154                     }
155                 }
156             }
157         }
158     }
159
160     /**
161      * Visually checks the previously-specified elements in this dialog's list
162      * viewer.
163      */

164     private void checkInitialSelections() {
165         IFileEditorMapping editorMappings[] = ((EditorRegistry) PlatformUI
166                 .getWorkbench().getEditorRegistry()).getUnifiedMappings();
167         ArrayList JavaDoc selectedMappings = new ArrayList JavaDoc();
168         for (int i = 0; i < editorMappings.length; i++) {
169             IFileEditorMapping mapping = editorMappings[i];
170             //Check for both extension and label matches
171
if (this.initialSelections.contains(mapping.getExtension())) {
172                 listViewer.setChecked(mapping, true);
173                 selectedMappings.add(mapping.getExtension());
174             } else {
175                 if (this.initialSelections.contains(mapping.getLabel())) {
176                     listViewer.setChecked(mapping, true);
177                     selectedMappings.add(mapping.getLabel());
178                 }
179             }
180         }
181         //Now add in the ones not selected to the user defined list
182
Iterator JavaDoc initialIterator = this.initialSelections.iterator();
183         StringBuffer JavaDoc entries = new StringBuffer JavaDoc();
184         while (initialIterator.hasNext()) {
185             String JavaDoc nextExtension = (String JavaDoc) initialIterator.next();
186             if (!selectedMappings.contains(nextExtension)) {
187                 if (entries.length() != 0) {
188                     entries.append(',');
189                 }
190                 entries.append(nextExtension);
191             }
192         }
193         this.userDefinedText.setText(entries.toString());
194     }
195
196     /*
197      * (non-Javadoc)
198      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
199      */

200     protected void configureShell(Shell shell) {
201         super.configureShell(shell);
202         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
203                 IWorkbenchHelpContextIds.TYPE_FILTERING_DIALOG);
204     }
205
206     /*
207      * (non-Javadoc)
208      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
209      */

210     protected Control createDialogArea(Composite parent) {
211         // page group
212
Composite composite = (Composite) super.createDialogArea(parent);
213         createMessageArea(composite);
214         listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
215         GridData data = new GridData(GridData.FILL_BOTH);
216         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
217         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
218         listViewer.getTable().setLayoutData(data);
219         listViewer.getTable().setFont(parent.getFont());
220         listViewer.setLabelProvider(FileEditorMappingLabelProvider.INSTANCE);
221         listViewer
222                 .setContentProvider(FileEditorMappingContentProvider.INSTANCE);
223         listViewer.setComparator(new ViewerComparator());
224         addSelectionButtons(composite);
225         createUserEntryGroup(composite);
226         initializeViewer();
227         // initialize page
228
if (this.initialSelections != null && !this.initialSelections.isEmpty()) {
229             checkInitialSelections();
230         }
231         return composite;
232     }
233
234     /**
235      * Create the group that shows the user defined entries for the dialog.
236      *
237      * @param parent
238      * the parent this is being created in.
239      */

240     private void createUserEntryGroup(Composite parent) {
241         Font font = parent.getFont();
242         // destination specification group
243
Composite userDefinedGroup = new Composite(parent, SWT.NONE);
244         GridLayout layout = new GridLayout();
245         layout.numColumns = 2;
246         userDefinedGroup.setLayout(layout);
247         userDefinedGroup.setLayoutData(new GridData(
248                 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
249         Label fTitle = new Label(userDefinedGroup, SWT.NONE);
250         fTitle.setFont(font);
251         fTitle.setText(filterTitle);
252         // user defined entry field
253
userDefinedText = new Text(userDefinedGroup, SWT.SINGLE | SWT.BORDER);
254         userDefinedText.setFont(font);
255         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
256                 | GridData.GRAB_HORIZONTAL);
257         userDefinedText.setLayoutData(data);
258     }
259
260     /**
261      * Return the input to the dialog.
262      * @return IFileEditorMapping[]
263      */

264     private IFileEditorMapping[] getInput() {
265         //Filter the mappings to be just those with a wildcard extension
266
if (currentInput == null) {
267             List JavaDoc wildcardEditors = new ArrayList JavaDoc();
268             IFileEditorMapping[] allMappings = ((EditorRegistry)PlatformUI.getWorkbench()
269                     .getEditorRegistry()).getUnifiedMappings();
270             for (int i = 0; i < allMappings.length; i++) {
271                 if (allMappings[i].getName().equals("*")) { //$NON-NLS-1$
272
wildcardEditors.add(allMappings[i]);
273                 }
274             }
275             currentInput = new IFileEditorMapping[wildcardEditors.size()];
276             wildcardEditors.toArray(currentInput);
277         }
278         return currentInput;
279     }
280
281     /**
282      * Initializes this dialog's viewer after it has been laid out.
283      */

284     private void initializeViewer() {
285         listViewer.setInput(getInput());
286     }
287
288     /**
289      * The <code>TypeFilteringDialog</code> implementation of this
290      * <code>Dialog</code> method builds a list of the selected elements for
291      * later retrieval by the client and closes this dialog.
292      */

293     protected void okPressed() {
294         // Get the input children.
295
IFileEditorMapping[] children = getInput();
296         List JavaDoc list = new ArrayList JavaDoc();
297         // Build a list of selected children.
298
for (int i = 0; i < children.length; ++i) {
299             IFileEditorMapping element = children[i];
300             if (listViewer.getChecked(element)) {
301                 list.add(element.getExtension());
302             }
303         }
304         addUserDefinedEntries(list);
305         setResult(list);
306         super.okPressed();
307     }
308 }
309
Popular Tags