001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
011: * activated and used by other components.
012: * Markus Schorn <markus.schorn@windriver.com> - Fix for bug 136591 -
013: * [Dialogs] TypeFilteringDialog appends unnecessary comma
014: *******************************************************************************/package org.eclipse.ui.dialogs;
015:
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.StringTokenizer;
021:
022: import org.eclipse.jface.dialogs.IDialogConstants;
023: import org.eclipse.jface.viewers.CheckboxTableViewer;
024: import org.eclipse.jface.viewers.ViewerComparator;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.events.SelectionListener;
029: import org.eclipse.swt.graphics.Font;
030: import org.eclipse.swt.layout.GridData;
031: import org.eclipse.swt.layout.GridLayout;
032: import org.eclipse.swt.widgets.Button;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Label;
036: import org.eclipse.swt.widgets.Shell;
037: import org.eclipse.swt.widgets.Text;
038: import org.eclipse.ui.IFileEditorMapping;
039: import org.eclipse.ui.PlatformUI;
040: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
041: import org.eclipse.ui.internal.WorkbenchMessages;
042: import org.eclipse.ui.internal.registry.EditorRegistry;
043:
044: /**
045: * The TypeFilteringDialog is a SelectionDialog that allows the user to select a
046: * file editor.
047: */
048: public class TypeFilteringDialog extends SelectionDialog {
049: Button addTypesButton;
050:
051: Collection initialSelections;
052:
053: // the visual selection widget group
054: CheckboxTableViewer listViewer;
055:
056: // sizing constants
057: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
058:
059: private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
060:
061: private final static String TYPE_DELIMITER = WorkbenchMessages.TypesFiltering_typeDelimiter;
062:
063: //Define a title for the filter entry field.
064: private String filterTitle = WorkbenchMessages.TypesFiltering_otherExtensions;
065:
066: Text userDefinedText;
067:
068: IFileEditorMapping[] currentInput;
069:
070: /**
071: * Creates a type filtering dialog using the supplied entries. Set the
072: * initial selections to those whose extensions match the preselections.
073: * @param parentShell The shell to parent the dialog from.
074: * @param preselections
075: * of String - a Collection of String to define the preselected
076: * types
077: */
078: public TypeFilteringDialog(Shell parentShell,
079: Collection preselections) {
080: super (parentShell);
081: setTitle(WorkbenchMessages.TypesFiltering_title);
082: this .initialSelections = preselections;
083: setMessage(WorkbenchMessages.TypesFiltering_message);
084: }
085:
086: /**
087: * Creates a type filtering dialog using the supplied entries. Set the
088: * initial selections to those whose extensions match the preselections.
089: *
090: * @param parentShell The shell to parent the dialog from.
091: * @param preselections
092: * of String - a Collection of String to define the preselected
093: * types
094: * @param filterText -
095: * the title of the text entry field for other extensions.
096: */
097: public TypeFilteringDialog(Shell parentShell,
098: Collection preselections, String filterText) {
099: 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,
120: WorkbenchMessages.WizardTransferPage_selectAll, false);
121: SelectionListener listener = new SelectionAdapter() {
122: public void widgetSelected(SelectionEvent e) {
123: listViewer.setAllChecked(true);
124: }
125: };
126: selectButton.addSelectionListener(listener);
127: Button deselectButton = createButton(buttonComposite,
128: IDialogConstants.DESELECT_ALL_ID,
129: WorkbenchMessages.WizardTransferPage_deselectAll, false);
130: listener = new SelectionAdapter() {
131: public void widgetSelected(SelectionEvent e) {
132: listViewer.setAllChecked(false);
133: }
134: };
135: deselectButton.addSelectionListener(listener);
136: }
137:
138: /**
139: * Add the currently-specified extensions to result.
140: * @param result
141: */
142: private void addUserDefinedEntries(List result) {
143: StringTokenizer tokenizer = new StringTokenizer(userDefinedText
144: .getText(), TYPE_DELIMITER);
145: //Allow the *. and . prefix and strip out the extension
146: while (tokenizer.hasMoreTokens()) {
147: String currentExtension = tokenizer.nextToken().trim();
148: if (!currentExtension.equals("")) { //$NON-NLS-1$
149: if (currentExtension.startsWith("*.")) { //$NON-NLS-1$
150: result.add(currentExtension.substring(2));
151: } else {
152: if (currentExtension.startsWith(".")) { //$NON-NLS-1$
153: result.add(currentExtension.substring(1));
154: } else {
155: result.add(currentExtension);
156: }
157: }
158: }
159: }
160: }
161:
162: /**
163: * Visually checks the previously-specified elements in this dialog's list
164: * viewer.
165: */
166: private void checkInitialSelections() {
167: IFileEditorMapping editorMappings[] = ((EditorRegistry) PlatformUI
168: .getWorkbench().getEditorRegistry())
169: .getUnifiedMappings();
170: ArrayList selectedMappings = new ArrayList();
171: for (int i = 0; i < editorMappings.length; i++) {
172: IFileEditorMapping mapping = editorMappings[i];
173: //Check for both extension and label matches
174: if (this .initialSelections.contains(mapping.getExtension())) {
175: listViewer.setChecked(mapping, true);
176: selectedMappings.add(mapping.getExtension());
177: } else {
178: if (this .initialSelections.contains(mapping.getLabel())) {
179: listViewer.setChecked(mapping, true);
180: selectedMappings.add(mapping.getLabel());
181: }
182: }
183: }
184: //Now add in the ones not selected to the user defined list
185: Iterator initialIterator = this .initialSelections.iterator();
186: StringBuffer entries = new StringBuffer();
187: while (initialIterator.hasNext()) {
188: String nextExtension = (String) initialIterator.next();
189: if (!selectedMappings.contains(nextExtension)) {
190: if (entries.length() != 0) {
191: entries.append(',');
192: }
193: entries.append(nextExtension);
194: }
195: }
196: this .userDefinedText.setText(entries.toString());
197: }
198:
199: /*
200: * (non-Javadoc)
201: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
202: */
203: protected void configureShell(Shell shell) {
204: super .configureShell(shell);
205: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
206: IWorkbenchHelpContextIds.TYPE_FILTERING_DIALOG);
207: }
208:
209: /*
210: * (non-Javadoc)
211: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
212: */
213: protected Control createDialogArea(Composite parent) {
214: // page group
215: Composite composite = (Composite) super
216: .createDialogArea(parent);
217: createMessageArea(composite);
218: listViewer = CheckboxTableViewer.newCheckList(composite,
219: SWT.BORDER);
220: GridData data = new GridData(GridData.FILL_BOTH);
221: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
222: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
223: listViewer.getTable().setLayoutData(data);
224: listViewer.getTable().setFont(parent.getFont());
225: listViewer
226: .setLabelProvider(FileEditorMappingLabelProvider.INSTANCE);
227: listViewer
228: .setContentProvider(FileEditorMappingContentProvider.INSTANCE);
229: listViewer.setComparator(new ViewerComparator());
230: addSelectionButtons(composite);
231: createUserEntryGroup(composite);
232: initializeViewer();
233: // initialize page
234: if (this .initialSelections != null
235: && !this .initialSelections.isEmpty()) {
236: checkInitialSelections();
237: }
238: return composite;
239: }
240:
241: /**
242: * Create the group that shows the user defined entries for the dialog.
243: *
244: * @param parent
245: * the parent this is being created in.
246: */
247: private void createUserEntryGroup(Composite parent) {
248: Font font = parent.getFont();
249: // destination specification group
250: Composite userDefinedGroup = new Composite(parent, SWT.NONE);
251: GridLayout layout = new GridLayout();
252: layout.numColumns = 2;
253: userDefinedGroup.setLayout(layout);
254: userDefinedGroup.setLayoutData(new GridData(
255: GridData.HORIZONTAL_ALIGN_FILL
256: | GridData.VERTICAL_ALIGN_FILL));
257: Label fTitle = new Label(userDefinedGroup, SWT.NONE);
258: fTitle.setFont(font);
259: fTitle.setText(filterTitle);
260: // user defined entry field
261: userDefinedText = new Text(userDefinedGroup, SWT.SINGLE
262: | SWT.BORDER);
263: userDefinedText.setFont(font);
264: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
265: | GridData.GRAB_HORIZONTAL);
266: userDefinedText.setLayoutData(data);
267: }
268:
269: /**
270: * Return the input to the dialog.
271: * @return IFileEditorMapping[]
272: */
273: private IFileEditorMapping[] getInput() {
274: //Filter the mappings to be just those with a wildcard extension
275: if (currentInput == null) {
276: List wildcardEditors = new ArrayList();
277: IFileEditorMapping[] allMappings = ((EditorRegistry) PlatformUI
278: .getWorkbench().getEditorRegistry())
279: .getUnifiedMappings();
280: for (int i = 0; i < allMappings.length; i++) {
281: if (allMappings[i].getName().equals("*")) { //$NON-NLS-1$
282: wildcardEditors.add(allMappings[i]);
283: }
284: }
285: currentInput = new IFileEditorMapping[wildcardEditors
286: .size()];
287: wildcardEditors.toArray(currentInput);
288: }
289: return currentInput;
290: }
291:
292: /**
293: * Initializes this dialog's viewer after it has been laid out.
294: */
295: private void initializeViewer() {
296: listViewer.setInput(getInput());
297: }
298:
299: /**
300: * The <code>TypeFilteringDialog</code> implementation of this
301: * <code>Dialog</code> method builds a list of the selected elements for
302: * later retrieval by the client and closes this dialog.
303: */
304: protected void okPressed() {
305: // Get the input children.
306: IFileEditorMapping[] children = getInput();
307: List list = new ArrayList();
308: // Build a list of selected children.
309: for (int i = 0; i < children.length; ++i) {
310: IFileEditorMapping element = children[i];
311: if (listViewer.getChecked(element)) {
312: list.add(element.getExtension());
313: }
314: }
315: addUserDefinedEntries(list);
316: setResult(list);
317: super.okPressed();
318: }
319: }
|