KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > actions > AntOpenWithMenu


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 package org.eclipse.ant.internal.ui.views.actions;
12
13
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.ant.internal.ui.AntUIPlugin;
24 import org.eclipse.ant.internal.ui.AntUtil;
25 import org.eclipse.ant.internal.ui.IAntUIConstants;
26 import org.eclipse.ant.internal.ui.model.AntElementNode;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.jface.action.ContributionItem;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.program.Program;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.MenuItem;
37 import org.eclipse.ui.IEditorDescriptor;
38 import org.eclipse.ui.IEditorRegistry;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.ide.IDE;
43
44 /**
45  *
46  * Code mostly a copy of the OpenWithMenu which cannot be effectively subclassed
47  */

48 public class AntOpenWithMenu extends ContributionItem {
49
50     private IWorkbenchPage fPage;
51     private IEditorRegistry fRegistry = PlatformUI.getWorkbench().getEditorRegistry();
52     private static final String JavaDoc SYSTEM_EDITOR_ID= PlatformUI.PLUGIN_ID + ".SystemEditor"; //$NON-NLS-1$
53

54     private static Map JavaDoc imageCache = new Hashtable JavaDoc(11);
55
56     private AntElementNode fNode;
57     
58     /**
59      * The id of this action.
60      */

61     public static final String JavaDoc ID = IAntUIConstants.PLUGIN_ID + ".AntOpenWithMenu"; //$NON-NLS-1$
62

63     public AntOpenWithMenu(IWorkbenchPage page) {
64         super(ID);
65         this.fPage= page;
66     }
67     
68     /* (non-Javadoc)
69      * @see org.eclipse.jface.action.IContributionItem#dispose()
70      */

71     public void dispose() {
72         super.dispose();
73         Iterator JavaDoc iter= imageCache.values().iterator();
74         while (iter.hasNext()) {
75             Image image = (Image) iter.next();
76             image.dispose();
77         }
78         imageCache.clear();
79     }
80
81     /**
82      * Returns an image to show for the corresponding editor descriptor.
83      *
84      * @param editorDesc the editor descriptor, or <code>null</code> for the system editor
85      * @return the image or <code>null</code>
86      */

87     private Image getImage(IEditorDescriptor editorDesc) {
88         ImageDescriptor imageDesc = getImageDescriptor(editorDesc);
89         if (imageDesc == null) {
90             return null;
91         }
92         Image image = (Image) imageCache.get(imageDesc);
93         if (image == null) {
94             image = imageDesc.createImage();
95             imageCache.put(imageDesc, image);
96         }
97         return image;
98     }
99
100     /**
101      * Returns the image descriptor for the given editor descriptor,
102      * or <code>null</code> if it has no image.
103      */

104     private ImageDescriptor getImageDescriptor(IEditorDescriptor editorDesc) {
105         ImageDescriptor imageDesc = null;
106         if (editorDesc == null) {
107             imageDesc = fRegistry.getImageDescriptor(fNode.getIFile().getName());
108         } else {
109             imageDesc = editorDesc.getImageDescriptor();
110         }
111         if (imageDesc == null && editorDesc != null) {
112             if (editorDesc.getId().equals(SYSTEM_EDITOR_ID)) {
113                 imageDesc = getSystemEditorImageDescriptor(fNode.getIFile().getFileExtension());
114             }
115         }
116         return imageDesc;
117     }
118
119     /**
120      * Return the image descriptor of the system editor
121      * that is registered with the OS to edit files of
122      * this type. <code>null</code> if none can be found.
123      */

124     private ImageDescriptor getSystemEditorImageDescriptor(String JavaDoc extension) {
125         Program externalProgram = null;
126         if (extension != null) {
127             externalProgram = Program.findProgram(extension);
128         }
129         if (externalProgram == null) {
130             return null;
131         }
132         return new EditorImageDescriptor(externalProgram);
133     }
134     /**
135      * Creates the menu item for the editor descriptor.
136      *
137      * @param menu the menu to add the item to
138      * @param descriptor the editor descriptor, or null for the system editor
139      * @param preferredEditor the descriptor of the preferred editor, or <code>null</code>
140      */

141     private void createMenuItem(Menu menu, final IEditorDescriptor descriptor, final IEditorDescriptor preferredEditor) {
142         // XXX: Would be better to use bold here, but SWT does not support it.
143
final MenuItem menuItem = new MenuItem(menu, SWT.RADIO);
144         boolean isPreferred = preferredEditor != null && descriptor.getId().equals(preferredEditor.getId());
145         menuItem.setSelection(isPreferred);
146         menuItem.setText(descriptor.getLabel());
147         Image image = getImage(descriptor);
148         if (image != null) {
149             menuItem.setImage(image);
150         }
151         Listener listener = new Listener() {
152             /* (non-Javadoc)
153              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
154              */

155             public void handleEvent(Event event) {
156                 switch (event.type) {
157                     case SWT.Selection :
158                         if (menuItem.getSelection()) {
159                             openEditor(descriptor);
160                         }
161                         break;
162                 }
163             }
164         };
165         menuItem.addListener(SWT.Selection, listener);
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
170      */

171     public void fill(Menu menu, int index) {
172         IFile fileResource = fNode.getIFile();
173         if (fileResource == null) {
174             return;
175         }
176
177         IEditorDescriptor defaultEditor = fRegistry.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); // should not be null
178
IEditorDescriptor preferredEditor = IDE.getDefaultEditor(fileResource); // may be null
179

180         Object JavaDoc[] editors= fRegistry.getEditors(fileResource.getName());
181         Arrays.sort(editors, new Comparator JavaDoc() {
182             /* (non-Javadoc)
183              * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
184              */

185             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
186                 String JavaDoc s1 = ((IEditorDescriptor) o1).getLabel();
187                 String JavaDoc s2 = ((IEditorDescriptor) o2).getLabel();
188                 //Return true if elementTwo is 'greater than' elementOne
189
return s1.compareToIgnoreCase(s2);
190             }
191         });
192         IEditorDescriptor antEditor= fRegistry.findEditor("org.eclipse.ant.internal.ui.editor.AntEditor"); //$NON-NLS-1$
193

194         boolean defaultFound = false;
195         boolean antFound= false;
196         List JavaDoc alreadyAddedEditors= new ArrayList JavaDoc(editors.length);
197         for (int i = 0; i < editors.length; i++) {
198             IEditorDescriptor editor = (IEditorDescriptor) editors[i];
199             if (alreadyAddedEditors.contains(editor.getId())) {
200                 continue;
201             }
202             createMenuItem(menu, editor, preferredEditor);
203             if (defaultEditor != null && editor.getId().equals(defaultEditor.getId())) {
204                 defaultFound = true;
205             }
206             if (antEditor != null && editor.getId().equals(antEditor.getId())) {
207                 antFound= true;
208             }
209             alreadyAddedEditors.add(editor.getId());
210             
211         }
212
213         // Only add a separator if there is something to separate
214
if (editors.length > 0) {
215             new MenuItem(menu, SWT.SEPARATOR);
216         }
217
218         // Add ant editor.
219
if (!antFound && antEditor != null) {
220              createMenuItem(menu, antEditor, preferredEditor);
221          }
222              
223         // Add default editor.
224
if (!defaultFound && defaultEditor != null) {
225             createMenuItem(menu, defaultEditor, preferredEditor);
226         }
227
228         // Add system editor.
229
IEditorDescriptor descriptor = fRegistry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
230         createMenuItem(menu, descriptor, preferredEditor);
231         createDefaultMenuItem(menu, fileResource);
232     }
233
234     /* (non-Javadoc)
235      * @see org.eclipse.jface.action.IContributionItem#isDynamic()
236      */

237     public boolean isDynamic() {
238         return true;
239     }
240     /**
241      * Opens the given editor on the selected file.
242      *
243      * @param editor the editor descriptor, or <code>null</code> for the system editor
244      */

245     private void openEditor(IEditorDescriptor editorDescriptor) {
246         AntUtil.openInEditor(fPage, editorDescriptor, fNode);
247     }
248
249     /**
250      * Creates the menu item for the default editor
251      *
252      * @param menu the menu to add the item to
253      * @param file the file being edited
254      * @param registry the editor registry
255      */

256     private void createDefaultMenuItem(Menu menu, final IFile fileResource) {
257         final MenuItem menuItem = new MenuItem(menu, SWT.RADIO);
258         menuItem.setSelection(IDE.getDefaultEditor(fileResource) == null);
259         menuItem.setText(AntViewActionMessages.AntViewOpenWithMenu_Default_Editor_4);
260
261         Listener listener = new Listener() {
262             /* (non-Javadoc)
263              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
264              */

265             public void handleEvent(Event event) {
266                 switch (event.type) {
267                     case SWT.Selection :
268                         if (menuItem.getSelection()) {
269                             IDE.setDefaultEditor(fileResource, null);
270                             try {
271                                 IDE.openEditor(fPage, fileResource, true);
272                             } catch (PartInitException e) {
273                                 AntUIPlugin.log(MessageFormat.format(AntViewActionMessages.AntViewOpenWithMenu_Editor_failed, new String JavaDoc[]{fileResource.getLocation().toOSString()}), e);
274                             }
275                         }
276                         break;
277                 }
278             }
279         };
280
281         menuItem.addListener(SWT.Selection, listener);
282     }
283
284     public void setNode(AntElementNode node) {
285         fNode= node;
286     }
287 }
288
Popular Tags