KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal.dialogs;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Cursor;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.Image;
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.Event;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableItem;
35 import org.eclipse.ui.IEditorDescriptor;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
38 import org.eclipse.ui.internal.WorkbenchMessages;
39 import org.eclipse.ui.internal.WorkbenchPlugin;
40 import org.eclipse.ui.internal.registry.EditorDescriptor;
41 import org.eclipse.ui.internal.registry.EditorRegistry;
42
43 /**
44  * This class is used to allow the user to select a dialog from the set of
45  * internal and external editors.
46  *
47  * @private
48  * This class is internal to the workbench and must not be called outside the workbench
49  */

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

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

95     public EditorSelectionDialog(Shell parentShell) {
96         super(parentShell);
97     }
98
99     /**
100      * This method is called if a button has been pressed.
101      */

102     protected void buttonPressed(int buttonId) {
103         if (buttonId == IDialogConstants.OK_ID) {
104             saveWidgetValues();
105         }
106         super.buttonPressed(buttonId);
107     }
108
109     /**
110      * Close the window.
111      */

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

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

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

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

238
239     protected IDialogSettings getDialogSettings() {
240         IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault()
241                 .getDialogSettings();
242         IDialogSettings section = workbenchSettings
243                 .getSection("EditorSelectionDialog");//$NON-NLS-1$
244
if (section == null) {
245             section = workbenchSettings.addNewSection("EditorSelectionDialog");//$NON-NLS-1$
246
}
247         return section;
248     }
249
250     /**
251      * Get a list of registered programs from the OS
252      */

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

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

316     protected Image[] getImages(IEditorDescriptor[] editors) {
317         Image[] images = new Image[editors.length];
318         for (int i = 0; i < editors.length; i++) {
319             images[i] = editors[i].getImageDescriptor().createImage();
320         }
321         return images;
322     }
323
324     /**
325      * Returns the internal editors
326      */

327     protected IEditorDescriptor[] getInternalEditors() {
328         if (internalEditors == null) {
329             EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault()
330                     .getEditorRegistry();
331             internalEditors = reg.getSortedEditorsFromPlugins();
332             internalEditors = filterEditors(internalEditors);
333             internalEditorImages = getImages(internalEditors);
334         }
335         return internalEditors;
336     }
337
338     /**
339      * Return the editor the user selected
340      *
341      * @return the selected editor
342      */

343     public IEditorDescriptor getSelectedEditor() {
344         return selectedEditor;
345     }
346
347     public void handleEvent(Event event) {
348         if (event.type == SWT.MouseDoubleClick) {
349             handleDoubleClickEvent();
350             return;
351         }
352         if (event.widget == externalButton) {
353             fillEditorTable();
354         } else if (event.widget == browseExternalEditorsButton) {
355             promptForExternalEditor();
356         } else if (event.widget == editorTable) {
357             if (editorTable.getSelectionIndex() != -1) {
358                 selectedEditor = (EditorDescriptor) editorTable.getSelection()[0]
359                         .getData();
360             } else {
361                 selectedEditor = null;
362                 okButton.setEnabled(false);
363             }
364         }
365         updateEnableState();
366     }
367
368     protected void promptForExternalEditor() {
369         FileDialog dialog = new FileDialog(getShell(), SWT.OPEN
370                 | SWT.PRIMARY_MODAL);
371         dialog.setFilterExtensions(Executable_Filters);
372         String JavaDoc result = dialog.open();
373         if (result != null) {
374             EditorDescriptor editor = EditorDescriptor.createForProgram(result);
375             // pretend we had obtained it from the list of os registered editors
376
TableItem ti = new TableItem(editorTable, SWT.NULL);
377             ti.setData(editor);
378             ti.setText(editor.getLabel());
379             Image image = editor.getImageDescriptor().createImage();
380             ti.setImage(image);
381
382             // need to pass an array to setSelection -- 1FSKYVO: SWT:ALL - inconsistent setSelection api on Table
383
editorTable.setSelection(new TableItem[] { ti });
384             editorTable.showSelection();
385             editorTable.setFocus();
386             selectedEditor = editor;
387
388             /* add to our collection of cached external editors in case the user
389              flips back and forth between internal/external */

390             IEditorDescriptor[] newEditors = new IEditorDescriptor[externalEditors.length + 1];
391             System.arraycopy(externalEditors, 0, newEditors, 0,
392                     externalEditors.length);
393             newEditors[newEditors.length - 1] = editor;
394             externalEditors = newEditors;
395
396             Image[] newImages = new Image[externalEditorImages.length + 1];
397             System.arraycopy(externalEditorImages, 0, newImages, 0,
398                     externalEditorImages.length);
399             newImages[newImages.length - 1] = image;
400             externalEditorImages = newImages;
401         }
402     }
403
404     /**
405      * Handle a double click event on the list
406      */

407     protected void handleDoubleClickEvent() {
408         buttonPressed(IDialogConstants.OK_ID);
409     }
410
411     /**
412      * Use the dialog store to restore widget values to the values that they held
413      * last time this wizard was used to completion
414      */

415     protected void restoreWidgetValues() {
416         IDialogSettings settings = getDialogSettings();
417         boolean wasExternal = settings.getBoolean(STORE_ID_INTERNAL_EXTERNAL);
418         internalButton.setSelection(!wasExternal);
419         externalButton.setSelection(wasExternal);
420     }
421
422     /**
423      * Since Finish was pressed, write widget values to the dialog store so that they
424      * will persist into the next invocation of this wizard page
425      */

426     protected void saveWidgetValues() {
427         IDialogSettings settings = getDialogSettings();
428         // record whether use was viewing internal or external editors
429
settings
430                 .put(STORE_ID_INTERNAL_EXTERNAL, !internalButton.getSelection());
431     }
432
433     /**
434      * Set the message displayed by this message dialog
435      *
436      * @param aMessage the message
437      */

438     public void setMessage(String JavaDoc aMessage) {
439         message = aMessage;
440     }
441
442     /**
443      * Set the editors which will not appear in the dialog.
444      *
445      * @param editors an array of editors
446      */

447     public void setEditorsToFilter(IEditorDescriptor[] editors) {
448         editorsToFilter = editors;
449     }
450
451     /**
452      * Update enabled state.
453      */

454     public void updateEnableState() {
455         boolean enableExternal = externalButton.getSelection();
456         browseExternalEditorsButton.setEnabled(enableExternal);
457         updateOkButton();
458     }
459
460     protected void createButtonsForButtonBar(Composite parent) {
461         okButton = createButton(parent, IDialogConstants.OK_ID,
462                 IDialogConstants.OK_LABEL, true);
463         createButton(parent, IDialogConstants.CANCEL_ID,
464                 IDialogConstants.CANCEL_LABEL, false);
465         //initially there is no selection so OK button should not be enabled
466
okButton.setEnabled(false);
467
468     }
469
470     /**
471      * Update the button enablement state.
472      */

473     protected void updateOkButton() {
474         // Buttons are null during dialog creation
475
if (okButton == null) {
476             return;
477         }
478         // If there is no selection, do not enable OK button
479
if (editorTable.getSelectionCount() == 0) {
480             okButton.setEnabled(false);
481             return;
482         }
483         // At this point, there is a selection
484
okButton.setEnabled(selectedEditor != null);
485     }
486 }
487
Popular Tags