KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.internal.dialogs;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.content.IContentType;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferencePage;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
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.Event;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Listener;
39 import org.eclipse.swt.widgets.Table;
40 import org.eclipse.swt.widgets.TableItem;
41 import org.eclipse.ui.IEditorDescriptor;
42 import org.eclipse.ui.IFileEditorMapping;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchPreferencePage;
45 import org.eclipse.ui.dialogs.EditorSelectionDialog;
46 import org.eclipse.ui.dialogs.PreferenceLinkArea;
47 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
48 import org.eclipse.ui.internal.WorkbenchMessages;
49 import org.eclipse.ui.internal.WorkbenchPlugin;
50 import org.eclipse.ui.internal.registry.EditorDescriptor;
51 import org.eclipse.ui.internal.registry.EditorRegistry;
52 import org.eclipse.ui.internal.registry.FileEditorMapping;
53 import org.eclipse.ui.internal.util.PrefUtil;
54 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
55
56 /**
57  * The file editors page presents the collection of file names and extensions
58  * for which the user has registered editors. It also lets the user add new
59  * internal or external (program) editors for a given file name and extension.
60  *
61  * The user can add an editor for either a specific file name and extension
62  * (e.g. report.doc), or for all file names of a given extension (e.g. *.doc)
63  *
64  * The set of registered editors is tracked by the EditorRegistery
65  * available from the workbench plugin.
66  */

67 public class FileEditorsPreferencePage extends PreferencePage implements
68         IWorkbenchPreferencePage, Listener {
69     
70     private static final String JavaDoc DATA_EDITOR = "editor"; //$NON-NLS-1$
71

72     private static final String JavaDoc DATA_FROM_CONTENT_TYPE = "type"; //$NON-NLS-1$
73

74     protected Table resourceTypeTable;
75
76     protected Button addResourceTypeButton;
77
78     protected Button removeResourceTypeButton;
79
80     protected Table editorTable;
81
82     protected Button addEditorButton;
83
84     protected Button removeEditorButton;
85
86     protected Button defaultEditorButton;
87
88     protected Label editorLabel;
89
90     protected IWorkbench workbench;
91
92     protected List JavaDoc imagesToDispose;
93
94     protected Map JavaDoc editorsToImages;
95
96     /**
97      * Add a new resource type to the collection shown in the top of the page.
98      * This is typically called after the extension dialog is shown to the user.
99      *
100      * @param newName the new name
101      * @param newExtension the new extension
102      */

103     public void addResourceType(String JavaDoc newName, String JavaDoc newExtension) {
104         // Either a file name or extension must be provided
105
Assert.isTrue((newName != null && newName.length() != 0)
106                 || (newExtension != null && newExtension.length() != 0));
107
108         // Wild card only valid by itself (i.e. rep* is not valid)
109
// And must have an extension
110
int index = newName.indexOf('*');
111         if (index > -1) {
112             Assert.isTrue(index == 0 && newName.length() == 1);
113             Assert.isTrue(newExtension != null && newExtension.length() != 0);
114         }
115
116         // Find the index at which to insert the new entry.
117
String JavaDoc newFilename = (newName + (newExtension == null
118                 || newExtension.length() == 0 ? "" : "." + newExtension)).toUpperCase();//$NON-NLS-1$ //$NON-NLS-2$
119
IFileEditorMapping resourceType;
120         TableItem[] items = resourceTypeTable.getItems();
121         boolean found = false;
122         int i = 0;
123
124         while (i < items.length && !found) {
125             resourceType = (IFileEditorMapping) items[i].getData();
126             int result = newFilename.compareToIgnoreCase(resourceType
127                     .getLabel());
128             if (result == 0) {
129                 // Same resource type not allowed!
130
MessageDialog
131                         .openInformation(
132                                 getControl().getShell(),
133                                 WorkbenchMessages.FileEditorPreference_existsTitle,
134                                 WorkbenchMessages.FileEditorPreference_existsMessage);
135                 return;
136             }
137
138             if (result < 0) {
139                 found = true;
140             } else {
141                 i++;
142             }
143         }
144
145         // Create the new type and insert it
146
resourceType = new FileEditorMapping(newName, newExtension);
147         TableItem item = newResourceTableItem(resourceType, i, true);
148         resourceTypeTable.setFocus();
149         resourceTypeTable.showItem(item);
150         fillEditorTable();
151     }
152
153     /**
154      * Creates the page's UI content.
155      */

156     protected Control createContents(Composite parent) {
157         imagesToDispose = new ArrayList JavaDoc();
158         editorsToImages = new HashMap JavaDoc(50);
159
160         // define container & its gridding
161
Composite pageComponent = new Composite(parent, SWT.NULL);
162         GridLayout layout = new GridLayout();
163         layout.numColumns = 2;
164         layout.marginWidth = 0;
165         layout.marginHeight = 0;
166         pageComponent.setLayout(layout);
167         GridData data = new GridData();
168         data.verticalAlignment = GridData.FILL;
169         data.horizontalAlignment = GridData.FILL;
170         pageComponent.setLayoutData(data);
171
172         //layout the contents
173

174         PreferenceLinkArea contentTypeArea = new PreferenceLinkArea(pageComponent, SWT.NONE,
175                 "org.eclipse.ui.preferencePages.ContentTypes", WorkbenchMessages.FileEditorPreference_contentTypesRelatedLink,//$NON-NLS-1$
176
(IWorkbenchPreferenceContainer) getContainer(),null);
177         
178         data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
179         contentTypeArea.getControl().setLayoutData(data);
180
181         //layout the top table & its buttons
182
Label label = new Label(pageComponent, SWT.LEFT);
183         label.setText(WorkbenchMessages.FileEditorPreference_fileTypes);
184         data = new GridData();
185         data.horizontalAlignment = GridData.FILL;
186         data.horizontalSpan = 2;
187         label.setLayoutData(data);
188
189         resourceTypeTable = new Table(pageComponent, SWT.SINGLE | SWT.BORDER
190                 | SWT.FULL_SELECTION);
191         resourceTypeTable.addListener(SWT.Selection, this);
192         resourceTypeTable.addListener(SWT.DefaultSelection, this);
193         data = new GridData(GridData.FILL_HORIZONTAL);
194
195         int availableRows = DialogUtil.availableRows(pageComponent);
196
197         data.heightHint = resourceTypeTable.getItemHeight()
198                 * (availableRows / 8);
199         resourceTypeTable.setLayoutData(data);
200
201         Composite groupComponent = new Composite(pageComponent, SWT.NULL);
202         GridLayout groupLayout = new GridLayout();
203         groupLayout.marginWidth = 0;
204         groupLayout.marginHeight = 0;
205         groupComponent.setLayout(groupLayout);
206         data = new GridData();
207         data.verticalAlignment = GridData.FILL;
208         data.horizontalAlignment = GridData.FILL;
209         groupComponent.setLayoutData(data);
210
211         addResourceTypeButton = new Button(groupComponent, SWT.PUSH);
212         addResourceTypeButton.setText(WorkbenchMessages.FileEditorPreference_add);
213         addResourceTypeButton.addListener(SWT.Selection, this);
214         addResourceTypeButton.setLayoutData(data);
215         setButtonLayoutData(addResourceTypeButton);
216
217         removeResourceTypeButton = new Button(groupComponent, SWT.PUSH);
218         removeResourceTypeButton.setText(WorkbenchMessages.FileEditorPreference_remove);
219         removeResourceTypeButton.addListener(SWT.Selection, this);
220         setButtonLayoutData(removeResourceTypeButton);
221
222         //Spacer
223
label = new Label(pageComponent, SWT.LEFT);
224         data = new GridData();
225         data.horizontalAlignment = GridData.FILL;
226         data.horizontalSpan = 2;
227         label.setLayoutData(data);
228
229         // layout the bottom table & its buttons
230
editorLabel = new Label(pageComponent, SWT.LEFT);
231         editorLabel.setText(WorkbenchMessages.FileEditorPreference_associatedEditors);
232         data = new GridData();
233         data.horizontalAlignment = GridData.FILL;
234         data.horizontalSpan = 2;
235         editorLabel.setLayoutData(data);
236
237         editorTable = new Table(pageComponent, SWT.SINGLE | SWT.BORDER);
238         editorTable.addListener(SWT.Selection, this);
239         editorTable.addListener(SWT.DefaultSelection, this);
240         data = new GridData(GridData.FILL_BOTH);
241         data.heightHint = editorTable.getItemHeight() * 7;
242         editorTable.setLayoutData(data);
243
244         groupComponent = new Composite(pageComponent, SWT.NULL);
245         groupLayout = new GridLayout();
246         groupLayout.marginWidth = 0;
247         groupLayout.marginHeight = 0;
248         groupComponent.setLayout(groupLayout);
249         data = new GridData();
250         data.verticalAlignment = GridData.FILL;
251         data.horizontalAlignment = GridData.FILL;
252         groupComponent.setLayoutData(data);
253
254         addEditorButton = new Button(groupComponent, SWT.PUSH);
255         addEditorButton.setText(WorkbenchMessages.FileEditorPreference_addEditor);
256         addEditorButton.addListener(SWT.Selection, this);
257         addEditorButton.setLayoutData(data);
258         setButtonLayoutData(addEditorButton);
259
260         removeEditorButton = new Button(groupComponent, SWT.PUSH);
261         removeEditorButton.setText(WorkbenchMessages.FileEditorPreference_removeEditor);
262         removeEditorButton.addListener(SWT.Selection, this);
263         setButtonLayoutData(removeEditorButton);
264
265         defaultEditorButton = new Button(groupComponent, SWT.PUSH);
266         defaultEditorButton.setText(WorkbenchMessages.FileEditorPreference_default);
267         defaultEditorButton.addListener(SWT.Selection, this);
268         setButtonLayoutData(defaultEditorButton);
269
270         fillResourceTypeTable();
271         if (resourceTypeTable.getItemCount() > 0) {
272             resourceTypeTable.setSelection(0);
273         }
274         fillEditorTable();
275         updateEnabledState();
276
277         workbench.getHelpSystem().setHelp(parent,
278                 IWorkbenchHelpContextIds.FILE_EDITORS_PREFERENCE_PAGE);
279         applyDialogFont(pageComponent);
280
281         return pageComponent;
282     }
283
284     /**
285      * The preference page is going to be disposed. So deallocate all allocated
286      * SWT resources that aren't disposed automatically by disposing the page
287      * (i.e fonts, cursors, etc). Subclasses should reimplement this method to
288      * release their own allocated SWT resources.
289      */

290     public void dispose() {
291         super.dispose();
292         if (imagesToDispose != null) {
293             for (Iterator JavaDoc e = imagesToDispose.iterator(); e.hasNext();) {
294                 ((Image) e.next()).dispose();
295             }
296             imagesToDispose = null;
297         }
298         if (editorsToImages != null) {
299             for (Iterator JavaDoc e = editorsToImages.values().iterator(); e.hasNext();) {
300                 ((Image) e.next()).dispose();
301             }
302             editorsToImages = null;
303         }
304     }
305
306     /**
307      * Hook method to get a page specific preference store. Reimplement this
308      * method if a page don't want to use its parent's preference store.
309      */

310     protected IPreferenceStore doGetPreferenceStore() {
311         return WorkbenchPlugin.getDefault().getPreferenceStore();
312     }
313
314     protected void fillEditorTable() {
315         editorTable.removeAll();
316         FileEditorMapping resourceType = getSelectedResourceType();
317         if (resourceType != null) {
318             IEditorDescriptor[] array = resourceType.getEditors();
319             for (int i = 0; i < array.length; i++) {
320                 IEditorDescriptor editor = array[i];
321                 TableItem item = new TableItem(editorTable, SWT.NULL);
322                 item.setData(DATA_EDITOR, editor);
323                 // Check if it is the default editor
324
String JavaDoc defaultString = null;
325                 if (resourceType != null) {
326                     if (resourceType.getDefaultEditor() == editor && resourceType.isDeclaredDefaultEditor(editor)) {
327                         defaultString = WorkbenchMessages.FileEditorPreference_defaultLabel;
328                     }
329                 }
330
331                 if (defaultString != null) {
332                     item.setText(editor.getLabel() + " " + defaultString); //$NON-NLS-1$
333
} else {
334                     item.setText(editor.getLabel());
335                 }
336                 item.setImage(getImage(editor));
337             }
338             
339             // now add any content type editors
340
EditorRegistry registry = (EditorRegistry) WorkbenchPlugin
341                     .getDefault().getEditorRegistry();
342             IContentType[] contentTypes = Platform.getContentTypeManager()
343                     .findContentTypesFor(resourceType.getLabel());
344             for (int i = 0; i < contentTypes.length; i++) {
345                 array = registry.getEditorsForContentType(contentTypes[i]);
346                 for (int j = 0; j < array.length; j++) {
347                     IEditorDescriptor editor = array[j];
348                     // don't add duplicates
349
TableItem[] items = editorTable.getItems();
350                     TableItem foundItem = null;
351                     for (int k = 0; k < items.length; k++) {
352                         if (items[k].getData(DATA_EDITOR).equals(editor)) {
353                             foundItem = items[k];
354                             break;
355                         }
356                     }
357                     if (foundItem == null) {
358                         TableItem item = new TableItem(editorTable, SWT.NULL);
359                         item.setData(DATA_EDITOR, editor);
360                         item.setData(DATA_FROM_CONTENT_TYPE, contentTypes[i]);
361                         setLockedItemText(item, editor.getLabel());
362                         item.setImage(getImage(editor));
363                     } else { // update the item to reflect its origin
364
foundItem.setData(DATA_FROM_CONTENT_TYPE, contentTypes[i]);
365                         setLockedItemText(foundItem, foundItem.getText());
366                     }
367                 }
368             }
369             
370         }
371     }
372
373     /**
374      * Set the locked message on the item. Assumes the item has an instance of
375      * IContentType in the data map.
376      *
377      * @param item the item to set
378      * @param baseLabel the base label
379      */

380     private void setLockedItemText(TableItem item, String JavaDoc baseLabel) {
381         item.setText(NLS
382                 .bind(WorkbenchMessages.FileEditorPreference_isLocked,
383                         baseLabel, ((IContentType) item
384                                 .getData(DATA_FROM_CONTENT_TYPE)).getName()));
385     }
386     
387     /**
388      * Place the existing resource types in the table
389      */

390     protected void fillResourceTypeTable() {
391         // Populate the table with the items
392
IFileEditorMapping[] array = WorkbenchPlugin.getDefault()
393                 .getEditorRegistry().getFileEditorMappings();
394         for (int i = 0; i < array.length; i++) {
395             FileEditorMapping mapping = (FileEditorMapping) array[i];
396             mapping = (FileEditorMapping) mapping.clone(); // want a copy
397
newResourceTableItem(mapping, i, false);
398         }
399     }
400
401     /**
402      * Returns the image associated with the given editor.
403      */

404     protected Image getImage(IEditorDescriptor editor) {
405         Image image = (Image) editorsToImages.get(editor);
406         if (image == null) {
407             image = editor.getImageDescriptor().createImage();
408             editorsToImages.put(editor, image);
409         }
410         return image;
411     }
412
413     protected FileEditorMapping getSelectedResourceType() {
414         TableItem[] items = resourceTypeTable.getSelection();
415         if (items.length > 0) {
416             return (FileEditorMapping) items[0].getData(); //Table is single select
417
}
418         return null;
419     }
420
421     protected IEditorDescriptor[] getAssociatedEditors() {
422         if (getSelectedResourceType() == null) {
423             return null;
424         }
425         if (editorTable.getItemCount() > 0) {
426             ArrayList JavaDoc editorList = new ArrayList JavaDoc();
427             for (int i = 0; i < editorTable.getItemCount(); i++) {
428                 editorList.add(editorTable.getItem(i).getData(DATA_EDITOR));
429             }
430
431             return (IEditorDescriptor[]) editorList
432                     .toArray(new IEditorDescriptor[editorList.size()]);
433         }
434         return null;
435     }
436
437     public void handleEvent(Event event) {
438         if (event.widget == addResourceTypeButton) {
439             promptForResourceType();
440         } else if (event.widget == removeResourceTypeButton) {
441             removeSelectedResourceType();
442         } else if (event.widget == addEditorButton) {
443             promptForEditor();
444         } else if (event.widget == removeEditorButton) {
445             removeSelectedEditor();
446         } else if (event.widget == defaultEditorButton) {
447             setSelectedEditorAsDefault();
448         } else if (event.widget == resourceTypeTable) {
449             fillEditorTable();
450         }
451
452         updateEnabledState();
453
454     }
455
456     /**
457      * @see IWorkbenchPreferencePage
458      */

459     public void init(IWorkbench aWorkbench) {
460         this.workbench = aWorkbench;
461         noDefaultAndApplyButton();
462     }
463
464     /*
465      * Create a new <code>TableItem</code> to represent the resource
466      * type editor description supplied.
467      */

468     protected TableItem newResourceTableItem(IFileEditorMapping mapping,
469             int index, boolean selected) {
470         Image image = mapping.getImageDescriptor().createImage(false);
471         if (image != null) {
472             imagesToDispose.add(image);
473         }
474
475         TableItem item = new TableItem(resourceTypeTable, SWT.NULL, index);
476         if (image != null) {
477             item.setImage(image);
478         }
479         item.setText(mapping.getLabel());
480         item.setData(mapping);
481         if (selected) {
482             resourceTypeTable.setSelection(index);
483         }
484
485         return item;
486     }
487
488     /**
489      * This is a hook for sublcasses to do special things when the ok
490      * button is pressed.
491      * For example reimplement this method if you want to save the
492      * page's data into the preference bundle.
493      */

494     public boolean performOk() {
495         TableItem[] items = resourceTypeTable.getItems();
496         FileEditorMapping[] resourceTypes = new FileEditorMapping[items.length];
497         for (int i = 0; i < items.length; i++) {
498             resourceTypes[i] = (FileEditorMapping) (items[i].getData());
499         }
500         EditorRegistry registry = (EditorRegistry) WorkbenchPlugin.getDefault()
501                 .getEditorRegistry(); // cast to allow save to be called
502
registry.setFileEditorMappings(resourceTypes);
503         registry.saveAssociations();
504         
505         PrefUtil.savePrefs();
506         return true;
507     }
508
509     /**
510      * Prompt for editor.
511      */

512     public void promptForEditor() {
513         EditorSelectionDialog dialog = new EditorSelectionDialog(getControl()
514                 .getShell());
515         dialog.setEditorsToFilter(getAssociatedEditors());
516         dialog
517                 .setMessage(NLS.bind(WorkbenchMessages.Choose_the_editor_for_file,getSelectedResourceType().getLabel() ));
518         if (dialog.open() == Window.OK) {
519             EditorDescriptor editor = (EditorDescriptor) dialog
520                     .getSelectedEditor();
521             if (editor != null) {
522                 int i = editorTable.getItemCount();
523                 boolean isEmpty = i < 1;
524                 TableItem item = new TableItem(editorTable, SWT.NULL, i);
525                 item.setData(DATA_EDITOR, editor);
526                 if (isEmpty) {
527                     item
528                             .setText(editor.getLabel()
529                                     + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
530
} else {
531                     item.setText(editor.getLabel());
532                 }
533                 item.setImage(getImage(editor));
534                 editorTable.setSelection(i);
535                 editorTable.setFocus();
536                 getSelectedResourceType().addEditor(editor);
537                 if (isEmpty) {
538                     getSelectedResourceType().setDefaultEditor(editor);
539                 }
540                 updateSelectedResourceType(); //in case of new default
541
}
542         }
543     }
544
545     /**
546      * Prompt for resource type.
547      */

548     public void promptForResourceType() {
549         FileExtensionDialog dialog = new FileExtensionDialog(getControl()
550                 .getShell());
551         if (dialog.open() == Window.OK) {
552             String JavaDoc name = dialog.getName();
553             String JavaDoc extension = dialog.getExtension();
554             addResourceType(name, extension);
555         }
556     }
557
558     /**
559      * Remove the editor from the table
560      */

561     public void removeSelectedEditor() {
562         TableItem[] items = editorTable.getSelection();
563         boolean defaultEditor = editorTable.getSelectionIndex() == 0;
564         if (items.length > 0) {
565             getSelectedResourceType().removeEditor(
566                     (EditorDescriptor) items[0].getData(DATA_EDITOR));
567             items[0].dispose(); //Table is single selection
568
}
569         if (defaultEditor && editorTable.getItemCount() > 0) {
570             TableItem item = editorTable.getItem(0);
571             // explicitly set the new editor first editor to default
572
getSelectedResourceType().setDefaultEditor(
573                     (EditorDescriptor) item.getData(DATA_EDITOR));
574             if (item != null) {
575                 item
576                         .setText(((EditorDescriptor) (item.getData(DATA_EDITOR)))
577                                 .getLabel()
578                                 + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
579
}
580             if (!isEditorRemovable(item)) {
581                 setLockedItemText(item, item.getText());
582             }
583         }
584
585     }
586
587     /**
588      * Remove the type from the table
589      */

590     public void removeSelectedResourceType() {
591         TableItem[] items = resourceTypeTable.getSelection();
592         if (items.length > 0) {
593             items[0].dispose(); //Table is single selection
594
}
595         //Clear out the editors too
596
editorTable.removeAll();
597     }
598
599     /**
600      * Add the selected editor to the default list.
601      */

602     public void setSelectedEditorAsDefault() {
603         TableItem[] items = editorTable.getSelection();
604         if (items.length > 0) {
605             // First change the label of the old default
606
TableItem oldDefaultItem = editorTable.getItem(0);
607             oldDefaultItem
608                     .setText(((EditorDescriptor) oldDefaultItem.getData(DATA_EDITOR))
609                             .getLabel());
610             // update the label to reflect the locked state
611
if (!isEditorRemovable(oldDefaultItem)) {
612                 setLockedItemText(oldDefaultItem, oldDefaultItem.getText());
613             }
614             // Now set the new default
615
EditorDescriptor editor = (EditorDescriptor) items[0].getData(DATA_EDITOR);
616             getSelectedResourceType().setDefaultEditor(editor);
617             IContentType fromContentType = (IContentType) items[0].getData(DATA_FROM_CONTENT_TYPE);
618             items[0].dispose(); //Table is single selection
619
TableItem item = new TableItem(editorTable, SWT.NULL, 0);
620             item.setData(DATA_EDITOR, editor);
621             if (fromContentType != null) {
622                 item.setData(DATA_FROM_CONTENT_TYPE, fromContentType);
623             }
624             item
625                     .setText(editor.getLabel()
626                             + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
627
item.setImage(getImage(editor));
628             if (!isEditorRemovable(item)) {
629                 setLockedItemText(item, item.getText());
630             }
631             editorTable.setSelection(new TableItem[] { item });
632         }
633     }
634
635     /**
636      * Update the enabled state.
637      */

638     public void updateEnabledState() {
639         //Update enabled state
640
boolean resourceTypeSelected = resourceTypeTable.getSelectionIndex() != -1;
641         boolean editorSelected = editorTable.getSelectionIndex() != -1;
642
643         removeResourceTypeButton.setEnabled(resourceTypeSelected);
644         editorLabel.setEnabled(resourceTypeSelected);
645         addEditorButton.setEnabled(resourceTypeSelected);
646         removeEditorButton.setEnabled(editorSelected && isEditorRemovable());
647         defaultEditorButton.setEnabled(editorSelected);
648     }
649     
650     /**
651      * Return whether the selected editor is removable. An editor is removable
652      * if it is not submitted via a content-type binding.
653      *
654      * @return whether the selected editor is removable
655      * @since 3.1
656      */

657     private boolean isEditorRemovable() {
658         TableItem[] items = editorTable.getSelection();
659         if (items.length > 0) {
660             return isEditorRemovable(items[0]);
661         }
662         return false;
663     }
664     
665     /**
666      * Return whether the given editor is removable. An editor is removable
667      * if it is not submitted via a content-type binding.
668      *
669      * @param item the item to test
670      * @return whether the selected editor is removable
671      * @since 3.1
672      */

673     private boolean isEditorRemovable(TableItem item) {
674         IContentType fromContentType = (IContentType) item.getData(DATA_FROM_CONTENT_TYPE);
675         return fromContentType == null;
676     }
677
678     /**
679      * Update the selected type.
680      */

681     public void updateSelectedResourceType() {
682         // TableItem item = resourceTypeTable.getSelection()[0]; //Single select
683
// Image image = ((IFileEditorMapping)item.getData()).getImageDescriptor().getImage();
684
// imagesToDispose.addElement(image);
685
// item.setImage(image);
686
}
687 }
688
Popular Tags