KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Benjamin Muskalla - Bug 29633 [EditorMgmt] "Open" menu should
11  * have Open With-->Other
12  *******************************************************************************/

13
14 package org.eclipse.ui.dialogs;
15
16 import java.util.ArrayList JavaDoc;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Cursor;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Listener;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.ui.IEditorDescriptor;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
40 import org.eclipse.ui.internal.WorkbenchMessages;
41 import org.eclipse.ui.internal.WorkbenchPlugin;
42 import org.eclipse.ui.internal.registry.EditorDescriptor;
43 import org.eclipse.ui.internal.registry.EditorRegistry;
44
45 /**
46  * This class is used to allow the user to select a dialog from the set of
47  * internal and external editors.
48  * @since 3.3
49  *
50  */

51
52 public final class EditorSelectionDialog extends Dialog {
53     private EditorDescriptor selectedEditor;
54
55     private Button externalButton;
56
57     private Table editorTable;
58
59     private Button browseExternalEditorsButton;
60
61     private Button internalButton;
62
63     private Button okButton;
64
65     private static final String JavaDoc STORE_ID_INTERNAL_EXTERNAL = "EditorSelectionDialog.STORE_ID_INTERNAL_EXTERNAL";//$NON-NLS-1$
66

67     private String JavaDoc message = WorkbenchMessages.EditorSelection_chooseAnEditor;
68
69     // collection of IEditorDescriptor
70
private IEditorDescriptor[] externalEditors;
71
72     private IEditorDescriptor[] internalEditors;
73
74     private Image[] externalEditorImages;
75
76     private Image[] internalEditorImages;
77
78     private IEditorDescriptor[] editorsToFilter;
79
80     private DialogListener listener = new DialogListener();
81
82     private static final String JavaDoc[] Executable_Filters;
83
84     private static final int TABLE_WIDTH = 200;
85     static {
86         if (SWT.getPlatform().equals("win32")) {//$NON-NLS-1$
87
Executable_Filters = new String JavaDoc[] { "*.exe", "*.bat", "*.*" };//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
88
} else {
89             Executable_Filters = new String JavaDoc[] { "*" }; //$NON-NLS-1$
90
}
91     }
92
93     /**
94      * Create an instance of this class.
95      *
96      * @param parentShell
97      * the parent shell
98      */

99     public EditorSelectionDialog(Shell parentShell) {
100         super(parentShell);
101     }
102
103     /**
104      * This method is called if a button has been pressed.
105      */

106     protected void buttonPressed(int buttonId) {
107         if (buttonId == IDialogConstants.OK_ID) {
108             saveWidgetValues();
109         }
110         super.buttonPressed(buttonId);
111     }
112
113     /**
114      * Close the window.
115      */

116     public boolean close() {
117         if (internalEditorImages != null) {
118             for (int i = 0; i < internalEditorImages.length; i++) {
119                 internalEditorImages[i].dispose();
120             }
121             internalEditorImages = null;
122         }
123         if (externalEditorImages != null) {
124             for (int i = 0; i < externalEditorImages.length; i++) {
125                 externalEditorImages[i].dispose();
126             }
127             externalEditorImages = null;
128         }
129         return super.close();
130     }
131
132     /*
133      * (non-Javadoc) Method declared in Window.
134      */

135     protected void configureShell(Shell shell) {
136         super.configureShell(shell);
137         shell.setText(WorkbenchMessages.EditorSelection_title);
138         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
139                 IWorkbenchHelpContextIds.EDITOR_SELECTION_DIALOG);
140     }
141
142     /**
143      * Creates and returns the contents of the upper part of the dialog (above
144      * the button bar).
145      *
146      * Subclasses should overide.
147      *
148      * @param parent
149      * the parent composite to contain the dialog area
150      * @return the dialog area control
151      */

152     protected Control createDialogArea(Composite parent) {
153         Font font = parent.getFont();
154         // create main group
155
Composite contents = (Composite) super.createDialogArea(parent);
156         ((GridLayout) contents.getLayout()).numColumns = 2;
157
158         // begin the layout
159
Label textLabel = new Label(contents, SWT.NONE);
160         textLabel.setText(message);
161         GridData data = new GridData();
162         data.horizontalSpan = 2;
163         textLabel.setLayoutData(data);
164         textLabel.setFont(font);
165
166         internalButton = new Button(contents, SWT.RADIO | SWT.LEFT);
167         internalButton.setText(WorkbenchMessages.EditorSelection_internal);
168         internalButton.addListener(SWT.Selection, listener);
169         data = new GridData();
170         data.horizontalSpan = 1;
171         internalButton.setLayoutData(data);
172         internalButton.setFont(font);
173
174         externalButton = new Button(contents, SWT.RADIO | SWT.LEFT);
175         externalButton.setText(WorkbenchMessages.EditorSelection_external);
176         externalButton.addListener(SWT.Selection, listener);
177         data = new GridData();
178         data.horizontalSpan = 1;
179         externalButton.setLayoutData(data);
180         externalButton.setFont(font);
181
182         editorTable = new Table(contents, SWT.SINGLE | SWT.BORDER);
183         editorTable.addListener(SWT.Selection, listener);
184         editorTable.addListener(SWT.DefaultSelection, listener);
185         editorTable.addListener(SWT.MouseDoubleClick, listener);
186         data = new GridData();
187         data.widthHint = convertHorizontalDLUsToPixels(TABLE_WIDTH);
188         data.horizontalAlignment = GridData.FILL;
189         data.grabExcessHorizontalSpace = true;
190         data.verticalAlignment = GridData.FILL;
191         data.grabExcessVerticalSpace = true;
192         data.horizontalSpan = 2;
193         editorTable.setLayoutData(data);
194         editorTable.setFont(font);
195         data.heightHint = editorTable.getItemHeight() * 12;
196
197         browseExternalEditorsButton = new Button(contents, SWT.PUSH);
198         browseExternalEditorsButton
199                 .setText(WorkbenchMessages.EditorSelection_browse);
200         browseExternalEditorsButton.addListener(SWT.Selection, listener);
201         data = new GridData();
202         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
203         data.widthHint = Math.max(widthHint, browseExternalEditorsButton
204                 .computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
205         browseExternalEditorsButton.setLayoutData(data);
206         browseExternalEditorsButton.setFont(font);
207
208         restoreWidgetValues(); // Place buttons to the appropriate state
209

210         fillEditorTable();
211
212         updateEnableState();
213
214         return contents;
215     }
216
217     protected void fillEditorTable() {
218         editorTable.removeAll();
219         editorTable.update();
220         IEditorDescriptor[] editors;
221         Image[] images;
222         if (internalButton.getSelection()) {
223             editors = getInternalEditors();
224             images = internalEditorImages;
225         } else {
226             editors = getExternalEditors();
227             images = externalEditorImages;
228         }
229
230         // 1FWHIEX: ITPUI:WINNT - Need to call setRedraw
231
editorTable.setRedraw(false);
232         for (int i = 0; i < editors.length; i++) {
233             TableItem item = new TableItem(editorTable, SWT.NULL);
234             item.setData(editors[i]);
235             item.setText(editors[i].getLabel());
236             item.setImage(images[i]);
237         }
238         editorTable.setRedraw(true);
239     }
240
241     /**
242      * Return the dialog store to cache values into
243      */

244
245     protected IDialogSettings getDialogSettings() {
246         IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault()
247                 .getDialogSettings();
248         IDialogSettings section = workbenchSettings
249                 .getSection("EditorSelectionDialog");//$NON-NLS-1$
250
if (section == null) {
251             section = workbenchSettings.addNewSection("EditorSelectionDialog");//$NON-NLS-1$
252
}
253         return section;
254     }
255
256     /**
257      * Get a list of registered programs from the OS
258      */

259     protected IEditorDescriptor[] getExternalEditors() {
260         if (externalEditors == null) {
261             // Since this can take a while, show the busy
262
// cursor. If the dialog is not yet visible,
263
// then use the parent shell.
264
Control shell = getShell();
265             if (!shell.isVisible()) {
266                 Control topShell = shell.getParent();
267                 if (topShell != null) {
268                     shell = topShell;
269                 }
270             }
271             Cursor busy = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT);
272             shell.setCursor(busy);
273             // Get the external editors available
274
EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault()
275                     .getEditorRegistry();
276             externalEditors = reg.getSortedEditorsFromOS();
277             externalEditors = filterEditors(externalEditors);
278             externalEditorImages = getImages(externalEditors);
279             // Clean up
280
shell.setCursor(null);
281             busy.dispose();
282         }
283         return externalEditors;
284     }
285
286     /**
287      * Returns an array of editors which have been filtered according to the
288      * array of editors in the editorsToFilter instance variable.
289      *
290      * @param editors
291      * an array of editors to filter
292      * @return a filtered array of editors
293      */

294     protected IEditorDescriptor[] filterEditors(IEditorDescriptor[] editors) {
295         if ((editors == null) || (editors.length < 1)) {
296             return editors;
297         }
298
299         if ((editorsToFilter == null) || (editorsToFilter.length < 1)) {
300             return editors;
301         }
302
303         ArrayList JavaDoc filteredList = new ArrayList JavaDoc();
304         for (int i = 0; i < editors.length; i++) {
305             boolean add = true;
306             for (int j = 0; j < editorsToFilter.length; j++) {
307                 if (editors[i].getId().equals(editorsToFilter[j].getId())) {
308                     add = false;
309                 }
310             }
311             if (add) {
312                 filteredList.add(editors[i]);
313             }
314         }
315
316         return (IEditorDescriptor[]) filteredList
317                 .toArray(new IEditorDescriptor[filteredList.size()]);
318     }
319
320     /**
321      * Returns an array of images for the given array of editors
322      */

323     protected Image[] getImages(IEditorDescriptor[] editors) {
324         Image[] images = new Image[editors.length];
325         for (int i = 0; i < editors.length; i++) {
326             images[i] = editors[i].getImageDescriptor().createImage();
327         }
328         return images;
329     }
330
331     /**
332      * Returns the internal editors
333      */

334     protected IEditorDescriptor[] getInternalEditors() {
335         if (internalEditors == null) {
336             EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault()
337                     .getEditorRegistry();
338             internalEditors = reg.getSortedEditorsFromPlugins();
339             internalEditors = filterEditors(internalEditors);
340             internalEditorImages = getImages(internalEditors);
341         }
342         return internalEditors;
343     }
344
345     /**
346      * Return the editor the user selected
347      *
348      * @return the selected editor
349      */

350     public IEditorDescriptor getSelectedEditor() {
351         return selectedEditor;
352     }
353
354     protected void promptForExternalEditor() {
355         FileDialog dialog = new FileDialog(getShell(), SWT.OPEN
356                 | SWT.PRIMARY_MODAL);
357         dialog.setFilterExtensions(Executable_Filters);
358         String JavaDoc result = dialog.open();
359         if (result != null) {
360             EditorDescriptor editor = EditorDescriptor.createForProgram(result);
361             // pretend we had obtained it from the list of os registered editors
362
TableItem ti = new TableItem(editorTable, SWT.NULL);
363             ti.setData(editor);
364             ti.setText(editor.getLabel());
365             Image image = editor.getImageDescriptor().createImage();
366             ti.setImage(image);
367
368             // need to pass an array to setSelection -- 1FSKYVO: SWT:ALL -
369
// inconsistent setSelection api on Table
370
editorTable.setSelection(new TableItem[] { ti });
371             editorTable.showSelection();
372             editorTable.setFocus();
373             selectedEditor = editor;
374
375             /*
376              * add to our collection of cached external editors in case the user
377              * flips back and forth between internal/external
378              */

379             IEditorDescriptor[] newEditors = new IEditorDescriptor[externalEditors.length + 1];
380             System.arraycopy(externalEditors, 0, newEditors, 0,
381                     externalEditors.length);
382             newEditors[newEditors.length - 1] = editor;
383             externalEditors = newEditors;
384
385             Image[] newImages = new Image[externalEditorImages.length + 1];
386             System.arraycopy(externalEditorImages, 0, newImages, 0,
387                     externalEditorImages.length);
388             newImages[newImages.length - 1] = image;
389             externalEditorImages = newImages;
390         }
391     }
392
393     /**
394      * Handle a double click event on the list
395      */

396     protected void handleDoubleClickEvent() {
397         buttonPressed(IDialogConstants.OK_ID);
398     }
399
400     /**
401      * Use the dialog store to restore widget values to the values that they
402      * held last time this wizard was used to completion
403      */

404     protected void restoreWidgetValues() {
405         IDialogSettings settings = getDialogSettings();
406         boolean wasExternal = settings.getBoolean(STORE_ID_INTERNAL_EXTERNAL);
407         internalButton.setSelection(!wasExternal);
408         externalButton.setSelection(wasExternal);
409     }
410
411     /**
412      * Since Finish was pressed, write widget values to the dialog store so that
413      * they will persist into the next invocation of this wizard page
414      */

415     protected void saveWidgetValues() {
416         IDialogSettings settings = getDialogSettings();
417         // record whether use was viewing internal or external editors
418
settings
419                 .put(STORE_ID_INTERNAL_EXTERNAL, !internalButton.getSelection());
420     }
421
422     /**
423      * Set the message displayed by this message dialog
424      *
425      * @param aMessage
426      * the message
427      */

428     public void setMessage(String JavaDoc aMessage) {
429         message = aMessage;
430     }
431
432     /**
433      * Set the editors which will not appear in the dialog.
434      *
435      * @param editors
436      * an array of editors
437      */

438     public void setEditorsToFilter(IEditorDescriptor[] editors) {
439         editorsToFilter = editors;
440     }
441
442     /**
443      * Update enabled state.
444      */

445     protected void updateEnableState() {
446         boolean enableExternal = externalButton.getSelection();
447         browseExternalEditorsButton.setEnabled(enableExternal);
448         updateOkButton();
449     }
450
451     protected void createButtonsForButtonBar(Composite parent) {
452         okButton = createButton(parent, IDialogConstants.OK_ID,
453                 IDialogConstants.OK_LABEL, true);
454         createButton(parent, IDialogConstants.CANCEL_ID,
455                 IDialogConstants.CANCEL_LABEL, false);
456         // initially there is no selection so OK button should not be enabled
457
okButton.setEnabled(false);
458
459     }
460
461     /**
462      * Update the button enablement state.
463      */

464     protected void updateOkButton() {
465         // Buttons are null during dialog creation
466
if (okButton == null) {
467             return;
468         }
469         // If there is no selection, do not enable OK button
470
if (editorTable.getSelectionCount() == 0) {
471             okButton.setEnabled(false);
472             return;
473         }
474         // At this point, there is a selection
475
okButton.setEnabled(selectedEditor != null);
476     }
477
478     private class DialogListener implements Listener {
479
480         /*
481          * (non-Javadoc)
482          *
483          * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
484          */

485         public void handleEvent(Event event) {
486             if (event.type == SWT.MouseDoubleClick) {
487                 handleDoubleClickEvent();
488                 return;
489             }
490             if (event.widget == externalButton) {
491                 fillEditorTable();
492             } else if (event.widget == browseExternalEditorsButton) {
493                 promptForExternalEditor();
494             } else if (event.widget == editorTable) {
495                 if (editorTable.getSelectionIndex() != -1) {
496                     selectedEditor = (EditorDescriptor) editorTable
497                             .getSelection()[0].getData();
498                 } else {
499                     selectedEditor = null;
500                     okButton.setEnabled(false);
501                 }
502             }
503             updateEnableState();
504         }
505
506     }
507 }
508
Popular Tags