1 11 package org.eclipse.ant.internal.ui.views.actions; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Comparator ; 18 import java.util.Hashtable ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 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 48 public class AntOpenWithMenu extends ContributionItem { 49 50 private IWorkbenchPage fPage; 51 private IEditorRegistry fRegistry = PlatformUI.getWorkbench().getEditorRegistry(); 52 private static final String SYSTEM_EDITOR_ID= PlatformUI.PLUGIN_ID + ".SystemEditor"; 54 private static Map imageCache = new Hashtable (11); 55 56 private AntElementNode fNode; 57 58 61 public static final String ID = IAntUIConstants.PLUGIN_ID + ".AntOpenWithMenu"; 63 public AntOpenWithMenu(IWorkbenchPage page) { 64 super(ID); 65 this.fPage= page; 66 } 67 68 71 public void dispose() { 72 super.dispose(); 73 Iterator iter= imageCache.values().iterator(); 74 while (iter.hasNext()) { 75 Image image = (Image) iter.next(); 76 image.dispose(); 77 } 78 imageCache.clear(); 79 } 80 81 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 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 124 private ImageDescriptor getSystemEditorImageDescriptor(String 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 141 private void createMenuItem(Menu menu, final IEditorDescriptor descriptor, final IEditorDescriptor preferredEditor) { 142 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 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 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); IEditorDescriptor preferredEditor = IDE.getDefaultEditor(fileResource); 180 Object [] editors= fRegistry.getEditors(fileResource.getName()); 181 Arrays.sort(editors, new Comparator () { 182 185 public int compare(Object o1, Object o2) { 186 String s1 = ((IEditorDescriptor) o1).getLabel(); 187 String s2 = ((IEditorDescriptor) o2).getLabel(); 188 return s1.compareToIgnoreCase(s2); 190 } 191 }); 192 IEditorDescriptor antEditor= fRegistry.findEditor("org.eclipse.ant.internal.ui.editor.AntEditor"); 194 boolean defaultFound = false; 195 boolean antFound= false; 196 List alreadyAddedEditors= new ArrayList (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 if (editors.length > 0) { 215 new MenuItem(menu, SWT.SEPARATOR); 216 } 217 218 if (!antFound && antEditor != null) { 220 createMenuItem(menu, antEditor, preferredEditor); 221 } 222 223 if (!defaultFound && defaultEditor != null) { 225 createMenuItem(menu, defaultEditor, preferredEditor); 226 } 227 228 IEditorDescriptor descriptor = fRegistry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 230 createMenuItem(menu, descriptor, preferredEditor); 231 createDefaultMenuItem(menu, fileResource); 232 } 233 234 237 public boolean isDynamic() { 238 return true; 239 } 240 245 private void openEditor(IEditorDescriptor editorDescriptor) { 246 AntUtil.openInEditor(fPage, editorDescriptor, fNode); 247 } 248 249 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 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 []{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 |