1 13 package org.eclipse.ui.actions; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Collections ; 18 import java.util.Comparator ; 19 import java.util.Hashtable ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.jface.action.ContributionItem; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.osgi.util.NLS; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.graphics.Image; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.MenuItem; 34 import org.eclipse.ui.IEditorDescriptor; 35 import org.eclipse.ui.IEditorRegistry; 36 import org.eclipse.ui.IWorkbenchPage; 37 import org.eclipse.ui.PartInitException; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.dialogs.EditorSelectionDialog; 40 import org.eclipse.ui.ide.IDE; 41 import org.eclipse.ui.internal.WorkbenchPage; 42 import org.eclipse.ui.internal.ide.DialogUtil; 43 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 44 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 45 import org.eclipse.ui.part.FileEditorInput; 46 47 import com.ibm.icu.text.Collator; 48 49 61 public class OpenWithMenu extends ContributionItem { 62 private IWorkbenchPage page; 63 64 private IAdaptable file; 65 66 private IEditorRegistry registry = PlatformUI.getWorkbench() 67 .getEditorRegistry(); 68 69 private static Hashtable imageCache = new Hashtable (11); 70 71 74 public static final String ID = PlatformUI.PLUGIN_ID + ".OpenWithMenu"; 76 79 private static final int MATCH_BOTH = IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID; 80 81 84 private static final Comparator comparer = new Comparator () { 85 private Collator collator = Collator.getInstance(); 86 87 public int compare(Object arg0, Object arg1) { 88 String s1 = ((IEditorDescriptor) arg0).getLabel(); 89 String s2 = ((IEditorDescriptor) arg1).getLabel(); 90 return collator.compare(s1, s2); 91 } 92 }; 93 94 106 public OpenWithMenu(IWorkbenchPage page) { 107 this(page, null); 108 } 109 110 117 public OpenWithMenu(IWorkbenchPage page, IAdaptable file) { 118 super(ID); 119 this.page = page; 120 this.file = file; 121 } 122 123 129 private Image getImage(IEditorDescriptor editorDesc) { 130 ImageDescriptor imageDesc = getImageDescriptor(editorDesc); 131 if (imageDesc == null) { 132 return null; 133 } 134 Image image = (Image) imageCache.get(imageDesc); 135 if (image == null) { 136 image = imageDesc.createImage(); 137 imageCache.put(imageDesc, image); 138 } 139 return image; 140 } 141 142 146 private ImageDescriptor getImageDescriptor(IEditorDescriptor editorDesc) { 147 ImageDescriptor imageDesc = null; 148 if (editorDesc == null) { 149 imageDesc = registry 150 .getImageDescriptor(getFileResource().getName()); 151 } else { 153 imageDesc = editorDesc.getImageDescriptor(); 154 } 155 if (imageDesc == null) { 156 if (editorDesc.getId().equals( 157 IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { 158 imageDesc = registry 159 .getSystemExternalEditorImageDescriptor(getFileResource() 160 .getName()); 161 } 162 } 163 return imageDesc; 164 } 165 166 173 private void createMenuItem(Menu menu, final IEditorDescriptor descriptor, 174 final IEditorDescriptor preferredEditor) { 175 final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); 177 boolean isPreferred = preferredEditor != null 178 && descriptor.getId().equals(preferredEditor.getId()); 179 menuItem.setSelection(isPreferred); 180 menuItem.setText(descriptor.getLabel()); 181 Image image = getImage(descriptor); 182 if (image != null) { 183 menuItem.setImage(image); 184 } 185 Listener listener = new Listener() { 186 public void handleEvent(Event event) { 187 switch (event.type) { 188 case SWT.Selection: 189 if (menuItem.getSelection()) { 190 openEditor(descriptor, false); 191 } 192 break; 193 } 194 } 195 }; 196 menuItem.addListener(SWT.Selection, listener); 197 } 198 199 204 private void createOtherMenuItem(final Menu menu) { 205 final IFile fileResource = getFileResource(); 206 if (fileResource == null) { 207 return; 208 } 209 new MenuItem(menu, SWT.SEPARATOR); 210 final MenuItem menuItem = new MenuItem(menu, SWT.PUSH); 211 menuItem.setText(IDEWorkbenchMessages.OpenWithMenu_Other); 212 Listener listener = new Listener() { 213 public void handleEvent(Event event) { 214 switch (event.type) { 215 case SWT.Selection: 216 EditorSelectionDialog dialog = new EditorSelectionDialog( 217 menu.getShell()); 218 dialog 219 .setMessage(NLS 220 .bind( 221 IDEWorkbenchMessages.OpenWithMenu_OtherDialogDescription, 222 fileResource.getName())); 223 if (dialog.open() == Window.OK) { 224 IEditorDescriptor editor = dialog.getSelectedEditor(); 225 if (editor != null) { 226 openEditor(editor, editor.isOpenExternal()); 227 } 228 } 229 break; 230 } 231 } 232 }; 233 menuItem.addListener(SWT.Selection, listener); 234 } 235 236 239 public void fill(Menu menu, int index) { 240 IFile file = getFileResource(); 241 if (file == null) { 242 return; 243 } 244 245 IEditorDescriptor defaultEditor = registry 246 .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); IEditorDescriptor preferredEditor = IDE.getDefaultEditor(file); 249 Object [] editors = registry.getEditors(file.getName(), IDE.getContentType(file)); 250 Collections.sort(Arrays.asList(editors), comparer); 251 252 boolean defaultFound = false; 253 254 ArrayList alreadyMapped = new ArrayList (); 257 258 for (int i = 0; i < editors.length; i++) { 259 IEditorDescriptor editor = (IEditorDescriptor) editors[i]; 260 if (!alreadyMapped.contains(editor)) { 261 createMenuItem(menu, editor, preferredEditor); 262 if (defaultEditor != null 263 && editor.getId().equals(defaultEditor.getId())) { 264 defaultFound = true; 265 } 266 alreadyMapped.add(editor); 267 } 268 } 269 270 if (editors.length > 0) { 272 new MenuItem(menu, SWT.SEPARATOR); 273 } 274 275 if (!defaultFound && defaultEditor != null) { 277 createMenuItem(menu, defaultEditor, preferredEditor); 278 } 279 280 IEditorDescriptor descriptor = registry 282 .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 283 createMenuItem(menu, descriptor, preferredEditor); 284 285 descriptor = registry 287 .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); 288 if (descriptor != null) { 289 createMenuItem(menu, descriptor, preferredEditor); 290 } 291 createDefaultMenuItem(menu, file); 292 293 createOtherMenuItem(menu); 295 } 296 297 298 301 private IFile getFileResource() { 302 if (this.file instanceof IFile) { 303 return (IFile) this.file; 304 } 305 IResource resource = (IResource) this.file 306 .getAdapter(IResource.class); 307 if (resource instanceof IFile) { 308 return (IFile) resource; 309 } 310 311 return null; 312 } 313 314 317 public boolean isDynamic() { 318 return true; 319 } 320 321 328 private void openEditor(IEditorDescriptor editor, boolean openUsingDescriptor) { 329 IFile file = getFileResource(); 330 if (file == null) { 331 return; 332 } 333 try { 334 if (openUsingDescriptor) { 335 ((WorkbenchPage) page).openEditorFromDescriptor(new FileEditorInput(file), editor, true, null); 336 } else { 337 String editorId = editor == null ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID 338 : editor.getId(); 339 340 ((WorkbenchPage) page).openEditor(new FileEditorInput(file), editorId, true, MATCH_BOTH); 341 IDE.setDefaultEditor(file, editorId); 343 } 344 } catch (PartInitException e) { 345 DialogUtil.openError(page.getWorkbenchWindow().getShell(), 346 IDEWorkbenchMessages.OpenWithMenu_dialogTitle, 347 e.getMessage(), e); 348 } 349 } 350 351 357 private void createDefaultMenuItem(Menu menu, final IFile file) { 358 final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); 359 menuItem.setSelection(IDE.getDefaultEditor(file) == null); 360 menuItem.setText(IDEWorkbenchMessages.DefaultEditorDescription_name); 361 362 Listener listener = new Listener() { 363 public void handleEvent(Event event) { 364 switch (event.type) { 365 case SWT.Selection: 366 if (menuItem.getSelection()) { 367 IDE.setDefaultEditor(file, null); 368 try { 369 IEditorDescriptor desc = IDE.getEditorDescriptor(file); 370 page.openEditor(new FileEditorInput(file), desc.getId(), true, MATCH_BOTH); 371 } catch (PartInitException e) { 372 DialogUtil.openError(page.getWorkbenchWindow() 373 .getShell(), IDEWorkbenchMessages.OpenWithMenu_dialogTitle, 374 e.getMessage(), e); 375 } 376 } 377 break; 378 } 379 } 380 }; 381 382 menuItem.addListener(SWT.Selection, listener); 383 } 384 } 385 | Popular Tags |