Source Code Cross Referenced for FileEditorsPreferencePage.java in  » IDE-Eclipse » ui-workbench » org » eclipse » ui » internal » dialogs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui workbench » org.eclipse.ui.internal.dialogs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         *     Benjamin Muskalla -	Bug 29633 [EditorMgmt] "Open" menu should
011:         *     						have Open With-->Other
012:         *******************************************************************************/package org.eclipse.ui.internal.dialogs;
013:
014:        import java.util.ArrayList;
015:        import java.util.HashMap;
016:        import java.util.Iterator;
017:        import java.util.List;
018:        import java.util.Map;
019:
020:        import org.eclipse.core.runtime.Assert;
021:        import org.eclipse.core.runtime.Platform;
022:        import org.eclipse.core.runtime.content.IContentType;
023:        import org.eclipse.jface.dialogs.MessageDialog;
024:        import org.eclipse.jface.preference.IPreferenceStore;
025:        import org.eclipse.jface.preference.PreferencePage;
026:        import org.eclipse.jface.window.Window;
027:        import org.eclipse.osgi.util.NLS;
028:        import org.eclipse.swt.SWT;
029:        import org.eclipse.swt.graphics.Image;
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.Event;
036:        import org.eclipse.swt.widgets.Label;
037:        import org.eclipse.swt.widgets.Listener;
038:        import org.eclipse.swt.widgets.Table;
039:        import org.eclipse.swt.widgets.TableItem;
040:        import org.eclipse.ui.IEditorDescriptor;
041:        import org.eclipse.ui.IFileEditorMapping;
042:        import org.eclipse.ui.IWorkbench;
043:        import org.eclipse.ui.IWorkbenchPreferencePage;
044:        import org.eclipse.ui.dialogs.EditorSelectionDialog;
045:        import org.eclipse.ui.dialogs.PreferenceLinkArea;
046:        import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
047:        import org.eclipse.ui.internal.WorkbenchMessages;
048:        import org.eclipse.ui.internal.WorkbenchPlugin;
049:        import org.eclipse.ui.internal.registry.EditorDescriptor;
050:        import org.eclipse.ui.internal.registry.EditorRegistry;
051:        import org.eclipse.ui.internal.registry.FileEditorMapping;
052:        import org.eclipse.ui.internal.util.PrefUtil;
053:        import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
054:
055:        /**
056:         * The file editors page presents the collection of file names and extensions
057:         * for which the user has registered editors. It also lets the user add new
058:         * internal or external (program) editors for a given file name and extension.
059:         *
060:         * The user can add an editor for either a specific file name and extension
061:         * (e.g. report.doc), or for all file names of a given extension (e.g. *.doc)
062:         *
063:         * The set of registered editors is tracked by the EditorRegistery
064:         * available from the workbench plugin.
065:         */
066:        public class FileEditorsPreferencePage extends PreferencePage implements 
067:                IWorkbenchPreferencePage, Listener {
068:
069:            private static final String DATA_EDITOR = "editor"; //$NON-NLS-1$
070:
071:            private static final String DATA_FROM_CONTENT_TYPE = "type"; //$NON-NLS-1$
072:
073:            protected Table resourceTypeTable;
074:
075:            protected Button addResourceTypeButton;
076:
077:            protected Button removeResourceTypeButton;
078:
079:            protected Table editorTable;
080:
081:            protected Button addEditorButton;
082:
083:            protected Button removeEditorButton;
084:
085:            protected Button defaultEditorButton;
086:
087:            protected Label editorLabel;
088:
089:            protected IWorkbench workbench;
090:
091:            protected List imagesToDispose;
092:
093:            protected Map editorsToImages;
094:
095:            /**
096:             * Add a new resource type to the collection shown in the top of the page.
097:             * This is typically called after the extension dialog is shown to the user.
098:             * 
099:             * @param newName the new name
100:             * @param newExtension the new extension
101:             */
102:            public void addResourceType(String newName, String newExtension) {
103:                // Either a file name or extension must be provided
104:                Assert
105:                        .isTrue((newName != null && newName.length() != 0)
106:                                || (newExtension != null && newExtension
107:                                        .length() != 0));
108:
109:                // Wild card only valid by itself (i.e. rep* is not valid)
110:                // And must have an extension
111:                int index = newName.indexOf('*');
112:                if (index > -1) {
113:                    Assert.isTrue(index == 0 && newName.length() == 1);
114:                    Assert.isTrue(newExtension != null
115:                            && newExtension.length() != 0);
116:                }
117:
118:                // Find the index at which to insert the new entry.
119:                String newFilename = (newName + (newExtension == null
120:                        || newExtension.length() == 0 ? "" : "." + newExtension)).toUpperCase();//$NON-NLS-1$ //$NON-NLS-2$
121:                IFileEditorMapping resourceType;
122:                TableItem[] items = resourceTypeTable.getItems();
123:                boolean found = false;
124:                int i = 0;
125:
126:                while (i < items.length && !found) {
127:                    resourceType = (IFileEditorMapping) items[i].getData();
128:                    int result = newFilename.compareToIgnoreCase(resourceType
129:                            .getLabel());
130:                    if (result == 0) {
131:                        // Same resource type not allowed!
132:                        MessageDialog
133:                                .openInformation(
134:                                        getControl().getShell(),
135:                                        WorkbenchMessages.FileEditorPreference_existsTitle,
136:                                        WorkbenchMessages.FileEditorPreference_existsMessage);
137:                        return;
138:                    }
139:
140:                    if (result < 0) {
141:                        found = true;
142:                    } else {
143:                        i++;
144:                    }
145:                }
146:
147:                // Create the new type and insert it
148:                resourceType = new FileEditorMapping(newName, newExtension);
149:                TableItem item = newResourceTableItem(resourceType, i, true);
150:                resourceTypeTable.setFocus();
151:                resourceTypeTable.showItem(item);
152:                fillEditorTable();
153:            }
154:
155:            /**
156:             * Creates the page's UI content.
157:             */
158:            protected Control createContents(Composite parent) {
159:                imagesToDispose = new ArrayList();
160:                editorsToImages = new HashMap(50);
161:
162:                // define container & its gridding
163:                Composite pageComponent = new Composite(parent, SWT.NULL);
164:                GridLayout layout = new GridLayout();
165:                layout.numColumns = 2;
166:                layout.marginWidth = 0;
167:                layout.marginHeight = 0;
168:                pageComponent.setLayout(layout);
169:                GridData data = new GridData();
170:                data.verticalAlignment = GridData.FILL;
171:                data.horizontalAlignment = GridData.FILL;
172:                pageComponent.setLayoutData(data);
173:
174:                //layout the contents
175:
176:                PreferenceLinkArea contentTypeArea = new PreferenceLinkArea(
177:                        pageComponent,
178:                        SWT.NONE,
179:                        "org.eclipse.ui.preferencePages.ContentTypes", WorkbenchMessages.FileEditorPreference_contentTypesRelatedLink,//$NON-NLS-1$
180:                        (IWorkbenchPreferenceContainer) getContainer(), null);
181:
182:                data = new GridData(GridData.FILL_HORIZONTAL
183:                        | GridData.GRAB_HORIZONTAL);
184:                contentTypeArea.getControl().setLayoutData(data);
185:
186:                //layout the top table & its buttons
187:                Label label = new Label(pageComponent, SWT.LEFT);
188:                label.setText(WorkbenchMessages.FileEditorPreference_fileTypes);
189:                data = new GridData();
190:                data.horizontalAlignment = GridData.FILL;
191:                data.horizontalSpan = 2;
192:                label.setLayoutData(data);
193:
194:                resourceTypeTable = new Table(pageComponent, SWT.SINGLE
195:                        | SWT.BORDER | SWT.FULL_SELECTION);
196:                resourceTypeTable.addListener(SWT.Selection, this );
197:                resourceTypeTable.addListener(SWT.DefaultSelection, this );
198:                data = new GridData(GridData.FILL_HORIZONTAL);
199:
200:                int availableRows = DialogUtil.availableRows(pageComponent);
201:
202:                data.heightHint = resourceTypeTable.getItemHeight()
203:                        * (availableRows / 8);
204:                resourceTypeTable.setLayoutData(data);
205:
206:                Composite groupComponent = new Composite(pageComponent,
207:                        SWT.NULL);
208:                GridLayout groupLayout = new GridLayout();
209:                groupLayout.marginWidth = 0;
210:                groupLayout.marginHeight = 0;
211:                groupComponent.setLayout(groupLayout);
212:                data = new GridData();
213:                data.verticalAlignment = GridData.FILL;
214:                data.horizontalAlignment = GridData.FILL;
215:                groupComponent.setLayoutData(data);
216:
217:                addResourceTypeButton = new Button(groupComponent, SWT.PUSH);
218:                addResourceTypeButton
219:                        .setText(WorkbenchMessages.FileEditorPreference_add);
220:                addResourceTypeButton.addListener(SWT.Selection, this );
221:                addResourceTypeButton.setLayoutData(data);
222:                setButtonLayoutData(addResourceTypeButton);
223:
224:                removeResourceTypeButton = new Button(groupComponent, SWT.PUSH);
225:                removeResourceTypeButton
226:                        .setText(WorkbenchMessages.FileEditorPreference_remove);
227:                removeResourceTypeButton.addListener(SWT.Selection, this );
228:                setButtonLayoutData(removeResourceTypeButton);
229:
230:                //Spacer
231:                label = new Label(pageComponent, SWT.LEFT);
232:                data = new GridData();
233:                data.horizontalAlignment = GridData.FILL;
234:                data.horizontalSpan = 2;
235:                label.setLayoutData(data);
236:
237:                // layout the bottom table & its buttons
238:                editorLabel = new Label(pageComponent, SWT.LEFT);
239:                editorLabel
240:                        .setText(WorkbenchMessages.FileEditorPreference_associatedEditors);
241:                data = new GridData();
242:                data.horizontalAlignment = GridData.FILL;
243:                data.horizontalSpan = 2;
244:                editorLabel.setLayoutData(data);
245:
246:                editorTable = new Table(pageComponent, SWT.SINGLE | SWT.BORDER);
247:                editorTable.addListener(SWT.Selection, this );
248:                editorTable.addListener(SWT.DefaultSelection, this );
249:                data = new GridData(GridData.FILL_BOTH);
250:                data.heightHint = editorTable.getItemHeight() * 7;
251:                editorTable.setLayoutData(data);
252:
253:                groupComponent = new Composite(pageComponent, SWT.NULL);
254:                groupLayout = new GridLayout();
255:                groupLayout.marginWidth = 0;
256:                groupLayout.marginHeight = 0;
257:                groupComponent.setLayout(groupLayout);
258:                data = new GridData();
259:                data.verticalAlignment = GridData.FILL;
260:                data.horizontalAlignment = GridData.FILL;
261:                groupComponent.setLayoutData(data);
262:
263:                addEditorButton = new Button(groupComponent, SWT.PUSH);
264:                addEditorButton
265:                        .setText(WorkbenchMessages.FileEditorPreference_addEditor);
266:                addEditorButton.addListener(SWT.Selection, this );
267:                addEditorButton.setLayoutData(data);
268:                setButtonLayoutData(addEditorButton);
269:
270:                removeEditorButton = new Button(groupComponent, SWT.PUSH);
271:                removeEditorButton
272:                        .setText(WorkbenchMessages.FileEditorPreference_removeEditor);
273:                removeEditorButton.addListener(SWT.Selection, this );
274:                setButtonLayoutData(removeEditorButton);
275:
276:                defaultEditorButton = new Button(groupComponent, SWT.PUSH);
277:                defaultEditorButton
278:                        .setText(WorkbenchMessages.FileEditorPreference_default);
279:                defaultEditorButton.addListener(SWT.Selection, this );
280:                setButtonLayoutData(defaultEditorButton);
281:
282:                fillResourceTypeTable();
283:                if (resourceTypeTable.getItemCount() > 0) {
284:                    resourceTypeTable.setSelection(0);
285:                }
286:                fillEditorTable();
287:                updateEnabledState();
288:
289:                workbench.getHelpSystem().setHelp(parent,
290:                        IWorkbenchHelpContextIds.FILE_EDITORS_PREFERENCE_PAGE);
291:                applyDialogFont(pageComponent);
292:
293:                return pageComponent;
294:            }
295:
296:            /**
297:             * The preference page is going to be disposed. So deallocate all allocated
298:             * SWT resources that aren't disposed automatically by disposing the page
299:             * (i.e fonts, cursors, etc). Subclasses should reimplement this method to 
300:             * release their own allocated SWT resources.
301:             */
302:            public void dispose() {
303:                super .dispose();
304:                if (imagesToDispose != null) {
305:                    for (Iterator e = imagesToDispose.iterator(); e.hasNext();) {
306:                        ((Image) e.next()).dispose();
307:                    }
308:                    imagesToDispose = null;
309:                }
310:                if (editorsToImages != null) {
311:                    for (Iterator e = editorsToImages.values().iterator(); e
312:                            .hasNext();) {
313:                        ((Image) e.next()).dispose();
314:                    }
315:                    editorsToImages = null;
316:                }
317:            }
318:
319:            /**
320:             * Hook method to get a page specific preference store. Reimplement this
321:             * method if a page don't want to use its parent's preference store.
322:             */
323:            protected IPreferenceStore doGetPreferenceStore() {
324:                return WorkbenchPlugin.getDefault().getPreferenceStore();
325:            }
326:
327:            protected void fillEditorTable() {
328:                editorTable.removeAll();
329:                FileEditorMapping resourceType = getSelectedResourceType();
330:                if (resourceType != null) {
331:                    IEditorDescriptor[] array = resourceType.getEditors();
332:                    for (int i = 0; i < array.length; i++) {
333:                        IEditorDescriptor editor = array[i];
334:                        TableItem item = new TableItem(editorTable, SWT.NULL);
335:                        item.setData(DATA_EDITOR, editor);
336:                        // Check if it is the default editor
337:                        String defaultString = null;
338:                        if (resourceType != null) {
339:                            if (resourceType.getDefaultEditor() == editor
340:                                    && resourceType
341:                                            .isDeclaredDefaultEditor(editor)) {
342:                                defaultString = WorkbenchMessages.FileEditorPreference_defaultLabel;
343:                            }
344:                        }
345:
346:                        if (defaultString != null) {
347:                            item.setText(editor.getLabel()
348:                                    + " " + defaultString); //$NON-NLS-1$
349:                        } else {
350:                            item.setText(editor.getLabel());
351:                        }
352:                        item.setImage(getImage(editor));
353:                    }
354:
355:                    // now add any content type editors
356:                    EditorRegistry registry = (EditorRegistry) WorkbenchPlugin
357:                            .getDefault().getEditorRegistry();
358:                    IContentType[] contentTypes = Platform
359:                            .getContentTypeManager().findContentTypesFor(
360:                                    resourceType.getLabel());
361:                    for (int i = 0; i < contentTypes.length; i++) {
362:                        array = registry
363:                                .getEditorsForContentType(contentTypes[i]);
364:                        for (int j = 0; j < array.length; j++) {
365:                            IEditorDescriptor editor = array[j];
366:                            // don't add duplicates
367:                            TableItem[] items = editorTable.getItems();
368:                            TableItem foundItem = null;
369:                            for (int k = 0; k < items.length; k++) {
370:                                if (items[k].getData(DATA_EDITOR)
371:                                        .equals(editor)) {
372:                                    foundItem = items[k];
373:                                    break;
374:                                }
375:                            }
376:                            if (foundItem == null) {
377:                                TableItem item = new TableItem(editorTable,
378:                                        SWT.NULL);
379:                                item.setData(DATA_EDITOR, editor);
380:                                item.setData(DATA_FROM_CONTENT_TYPE,
381:                                        contentTypes[i]);
382:                                setLockedItemText(item, editor.getLabel());
383:                                item.setImage(getImage(editor));
384:                            } else { // update the item to reflect its origin
385:                                foundItem.setData(DATA_FROM_CONTENT_TYPE,
386:                                        contentTypes[i]);
387:                                setLockedItemText(foundItem, foundItem
388:                                        .getText());
389:                            }
390:                        }
391:                    }
392:
393:                }
394:            }
395:
396:            /**
397:             * Set the locked message on the item. Assumes the item has an instance of
398:             * IContentType in the data map.
399:             * 
400:             * @param item the item to set
401:             * @param baseLabel the base label
402:             */
403:            private void setLockedItemText(TableItem item, String baseLabel) {
404:                item.setText(NLS.bind(
405:                        WorkbenchMessages.FileEditorPreference_isLocked,
406:                        baseLabel, ((IContentType) item
407:                                .getData(DATA_FROM_CONTENT_TYPE)).getName()));
408:            }
409:
410:            /**
411:             * Place the existing resource types in the table
412:             */
413:            protected void fillResourceTypeTable() {
414:                // Populate the table with the items
415:                IFileEditorMapping[] array = WorkbenchPlugin.getDefault()
416:                        .getEditorRegistry().getFileEditorMappings();
417:                for (int i = 0; i < array.length; i++) {
418:                    FileEditorMapping mapping = (FileEditorMapping) array[i];
419:                    mapping = (FileEditorMapping) mapping.clone(); // want a copy
420:                    newResourceTableItem(mapping, i, false);
421:                }
422:            }
423:
424:            /**
425:             * Returns the image associated with the given editor.
426:             */
427:            protected Image getImage(IEditorDescriptor editor) {
428:                Image image = (Image) editorsToImages.get(editor);
429:                if (image == null) {
430:                    image = editor.getImageDescriptor().createImage();
431:                    editorsToImages.put(editor, image);
432:                }
433:                return image;
434:            }
435:
436:            protected FileEditorMapping getSelectedResourceType() {
437:                TableItem[] items = resourceTypeTable.getSelection();
438:                if (items.length > 0) {
439:                    return (FileEditorMapping) items[0].getData(); //Table is single select
440:                }
441:                return null;
442:            }
443:
444:            protected IEditorDescriptor[] getAssociatedEditors() {
445:                if (getSelectedResourceType() == null) {
446:                    return null;
447:                }
448:                if (editorTable.getItemCount() > 0) {
449:                    ArrayList editorList = new ArrayList();
450:                    for (int i = 0; i < editorTable.getItemCount(); i++) {
451:                        editorList.add(editorTable.getItem(i).getData(
452:                                DATA_EDITOR));
453:                    }
454:
455:                    return (IEditorDescriptor[]) editorList
456:                            .toArray(new IEditorDescriptor[editorList.size()]);
457:                }
458:                return null;
459:            }
460:
461:            public void handleEvent(Event event) {
462:                if (event.widget == addResourceTypeButton) {
463:                    promptForResourceType();
464:                } else if (event.widget == removeResourceTypeButton) {
465:                    removeSelectedResourceType();
466:                } else if (event.widget == addEditorButton) {
467:                    promptForEditor();
468:                } else if (event.widget == removeEditorButton) {
469:                    removeSelectedEditor();
470:                } else if (event.widget == defaultEditorButton) {
471:                    setSelectedEditorAsDefault();
472:                } else if (event.widget == resourceTypeTable) {
473:                    fillEditorTable();
474:                }
475:
476:                updateEnabledState();
477:
478:            }
479:
480:            /**
481:             * @see IWorkbenchPreferencePage
482:             */
483:            public void init(IWorkbench aWorkbench) {
484:                this .workbench = aWorkbench;
485:                noDefaultAndApplyButton();
486:            }
487:
488:            /*
489:             * Create a new <code>TableItem</code> to represent the resource
490:             * type editor description supplied.
491:             */
492:            protected TableItem newResourceTableItem(
493:                    IFileEditorMapping mapping, int index, boolean selected) {
494:                Image image = mapping.getImageDescriptor().createImage(false);
495:                if (image != null) {
496:                    imagesToDispose.add(image);
497:                }
498:
499:                TableItem item = new TableItem(resourceTypeTable, SWT.NULL,
500:                        index);
501:                if (image != null) {
502:                    item.setImage(image);
503:                }
504:                item.setText(mapping.getLabel());
505:                item.setData(mapping);
506:                if (selected) {
507:                    resourceTypeTable.setSelection(index);
508:                }
509:
510:                return item;
511:            }
512:
513:            /**
514:             * This is a hook for sublcasses to do special things when the ok
515:             * button is pressed.
516:             * For example reimplement this method if you want to save the 
517:             * page's data into the preference bundle.
518:             */
519:            public boolean performOk() {
520:                TableItem[] items = resourceTypeTable.getItems();
521:                FileEditorMapping[] resourceTypes = new FileEditorMapping[items.length];
522:                for (int i = 0; i < items.length; i++) {
523:                    resourceTypes[i] = (FileEditorMapping) (items[i].getData());
524:                }
525:                EditorRegistry registry = (EditorRegistry) WorkbenchPlugin
526:                        .getDefault().getEditorRegistry(); // cast to allow save to be called
527:                registry.setFileEditorMappings(resourceTypes);
528:                registry.saveAssociations();
529:
530:                PrefUtil.savePrefs();
531:                return true;
532:            }
533:
534:            /**
535:             * Prompt for editor.
536:             */
537:            public void promptForEditor() {
538:                EditorSelectionDialog dialog = new EditorSelectionDialog(
539:                        getControl().getShell());
540:                dialog.setEditorsToFilter(getAssociatedEditors());
541:                dialog.setMessage(NLS.bind(
542:                        WorkbenchMessages.Choose_the_editor_for_file,
543:                        getSelectedResourceType().getLabel()));
544:                if (dialog.open() == Window.OK) {
545:                    EditorDescriptor editor = (EditorDescriptor) dialog
546:                            .getSelectedEditor();
547:                    if (editor != null) {
548:                        int i = editorTable.getItemCount();
549:                        boolean isEmpty = i < 1;
550:                        TableItem item = new TableItem(editorTable, SWT.NULL, i);
551:                        item.setData(DATA_EDITOR, editor);
552:                        if (isEmpty) {
553:                            item
554:                                    .setText(editor.getLabel()
555:                                            + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
556:                        } else {
557:                            item.setText(editor.getLabel());
558:                        }
559:                        item.setImage(getImage(editor));
560:                        editorTable.setSelection(i);
561:                        editorTable.setFocus();
562:                        getSelectedResourceType().addEditor(editor);
563:                        if (isEmpty) {
564:                            getSelectedResourceType().setDefaultEditor(editor);
565:                        }
566:                        updateSelectedResourceType(); //in case of new default
567:                    }
568:                }
569:            }
570:
571:            /**
572:             * Prompt for resource type.
573:             */
574:            public void promptForResourceType() {
575:                FileExtensionDialog dialog = new FileExtensionDialog(
576:                        getControl().getShell());
577:                if (dialog.open() == Window.OK) {
578:                    String name = dialog.getName();
579:                    String extension = dialog.getExtension();
580:                    addResourceType(name, extension);
581:                }
582:            }
583:
584:            /**
585:             * Remove the editor from the table
586:             */
587:            public void removeSelectedEditor() {
588:                TableItem[] items = editorTable.getSelection();
589:                boolean defaultEditor = editorTable.getSelectionIndex() == 0;
590:                if (items.length > 0) {
591:                    getSelectedResourceType().removeEditor(
592:                            (EditorDescriptor) items[0].getData(DATA_EDITOR));
593:                    items[0].dispose(); //Table is single selection
594:                }
595:                if (defaultEditor && editorTable.getItemCount() > 0) {
596:                    TableItem item = editorTable.getItem(0);
597:                    // explicitly set the new editor first editor to default
598:                    getSelectedResourceType().setDefaultEditor(
599:                            (EditorDescriptor) item.getData(DATA_EDITOR));
600:                    if (item != null) {
601:                        item
602:                                .setText(((EditorDescriptor) (item
603:                                        .getData(DATA_EDITOR))).getLabel()
604:                                        + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
605:                    }
606:                    if (!isEditorRemovable(item)) {
607:                        setLockedItemText(item, item.getText());
608:                    }
609:                }
610:
611:            }
612:
613:            /**
614:             * Remove the type from the table
615:             */
616:            public void removeSelectedResourceType() {
617:                TableItem[] items = resourceTypeTable.getSelection();
618:                if (items.length > 0) {
619:                    items[0].dispose(); //Table is single selection
620:                }
621:                //Clear out the editors too
622:                editorTable.removeAll();
623:            }
624:
625:            /**
626:             * Add the selected editor to the default list.
627:             */
628:            public void setSelectedEditorAsDefault() {
629:                TableItem[] items = editorTable.getSelection();
630:                if (items.length > 0) {
631:                    // First change the label of the old default
632:                    TableItem oldDefaultItem = editorTable.getItem(0);
633:                    oldDefaultItem.setText(((EditorDescriptor) oldDefaultItem
634:                            .getData(DATA_EDITOR)).getLabel());
635:                    // update the label to reflect the locked state
636:                    if (!isEditorRemovable(oldDefaultItem)) {
637:                        setLockedItemText(oldDefaultItem, oldDefaultItem
638:                                .getText());
639:                    }
640:                    // Now set the new default
641:                    EditorDescriptor editor = (EditorDescriptor) items[0]
642:                            .getData(DATA_EDITOR);
643:                    getSelectedResourceType().setDefaultEditor(editor);
644:                    IContentType fromContentType = (IContentType) items[0]
645:                            .getData(DATA_FROM_CONTENT_TYPE);
646:                    items[0].dispose(); //Table is single selection
647:                    TableItem item = new TableItem(editorTable, SWT.NULL, 0);
648:                    item.setData(DATA_EDITOR, editor);
649:                    if (fromContentType != null) {
650:                        item.setData(DATA_FROM_CONTENT_TYPE, fromContentType);
651:                    }
652:                    item
653:                            .setText(editor.getLabel()
654:                                    + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); //$NON-NLS-1$
655:                    item.setImage(getImage(editor));
656:                    if (!isEditorRemovable(item)) {
657:                        setLockedItemText(item, item.getText());
658:                    }
659:                    editorTable.setSelection(new TableItem[] { item });
660:                }
661:            }
662:
663:            /**
664:             * Update the enabled state.
665:             */
666:            public void updateEnabledState() {
667:                //Update enabled state
668:                boolean resourceTypeSelected = resourceTypeTable
669:                        .getSelectionIndex() != -1;
670:                boolean editorSelected = editorTable.getSelectionIndex() != -1;
671:
672:                removeResourceTypeButton.setEnabled(resourceTypeSelected);
673:                editorLabel.setEnabled(resourceTypeSelected);
674:                addEditorButton.setEnabled(resourceTypeSelected);
675:                removeEditorButton.setEnabled(editorSelected
676:                        && isEditorRemovable());
677:                defaultEditorButton.setEnabled(editorSelected);
678:            }
679:
680:            /**
681:             * Return whether the selected editor is removable. An editor is removable
682:             * if it is not submitted via a content-type binding.
683:             * 
684:             * @return whether the selected editor is removable
685:             * @since 3.1
686:             */
687:            private boolean isEditorRemovable() {
688:                TableItem[] items = editorTable.getSelection();
689:                if (items.length > 0) {
690:                    return isEditorRemovable(items[0]);
691:                }
692:                return false;
693:            }
694:
695:            /**
696:             * Return whether the given editor is removable. An editor is removable
697:             * if it is not submitted via a content-type binding.
698:             * 
699:             * @param item the item to test
700:             * @return whether the selected editor is removable
701:             * @since 3.1
702:             */
703:            private boolean isEditorRemovable(TableItem item) {
704:                IContentType fromContentType = (IContentType) item
705:                        .getData(DATA_FROM_CONTENT_TYPE);
706:                return fromContentType == null;
707:            }
708:
709:            /**
710:             * Update the selected type.
711:             */
712:            public void updateSelectedResourceType() {
713:                // TableItem item = resourceTypeTable.getSelection()[0]; //Single select
714:                //  Image image = ((IFileEditorMapping)item.getData()).getImageDescriptor().getImage();
715:                //  imagesToDispose.addElement(image);
716:                //  item.setImage(image);
717:            }
718:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.