1 11 package org.eclipse.ui.internal.console; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.action.ActionContributionItem; 16 import org.eclipse.jface.action.IMenuCreator; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Event; 20 import org.eclipse.swt.widgets.Menu; 21 import org.eclipse.ui.PlatformUI; 22 import org.eclipse.ui.activities.WorkbenchActivityHelper; 23 import org.eclipse.ui.console.ConsolePlugin; 24 import org.eclipse.ui.console.IConsoleFactory; 25 26 29 public class OpenConsoleAction extends Action implements IMenuCreator { 30 31 private ConsoleFactoryExtension[] fFactoryExtensions; 32 private Menu fMenu; 33 34 public OpenConsoleAction() { 35 fFactoryExtensions = ((ConsoleManager)ConsolePlugin.getDefault().getConsoleManager()).getConsoleFactoryExtensions(); 36 setText(ConsoleMessages.OpenConsoleAction_0); 37 setToolTipText(ConsoleMessages.OpenConsoleAction_1); 38 setImageDescriptor(ConsolePluginImages.getImageDescriptor(IInternalConsoleConstants.IMG_ELCL_NEW_CON)); 39 setMenuCreator(this); 40 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IConsoleHelpContextIds.CONSOLE_OPEN_CONSOLE_ACTION); 41 } 42 43 46 public void dispose() { 47 fFactoryExtensions = null; 48 } 49 50 53 public Menu getMenu(Control parent) { 54 if (fMenu != null) { 55 fMenu.dispose(); 56 } 57 58 fMenu= new Menu(parent); 59 int accel = 1; 60 for (int i = 0; i < fFactoryExtensions.length; i++) { 61 ConsoleFactoryExtension extension = fFactoryExtensions[i]; 62 if (!WorkbenchActivityHelper.filterItem(extension) && extension.isEnabled()) { 63 String label = extension.getLabel(); 64 ImageDescriptor image = extension.getImageDescriptor(); 65 addActionToMenu(fMenu, new ConsoleFactoryAction(label, image, extension), accel); 66 accel++; 67 } 68 } 69 return fMenu; 70 } 71 72 private void addActionToMenu(Menu parent, Action action, int accelerator) { 73 if (accelerator < 10) { 74 StringBuffer label= new StringBuffer (); 75 label.append('&'); 77 label.append(accelerator); 78 label.append(' '); 79 label.append(action.getText()); 80 action.setText(label.toString()); 81 } 82 83 ActionContributionItem item= new ActionContributionItem(action); 84 item.fill(parent, -1); 85 } 86 87 90 public Menu getMenu(Menu parent) { 91 return null; 92 } 93 94 private class ConsoleFactoryAction extends Action { 95 96 private ConsoleFactoryExtension fConfig; 97 private IConsoleFactory fFactory; 98 99 public ConsoleFactoryAction(String label, ImageDescriptor image, ConsoleFactoryExtension extension) { 100 setText(label); 101 if (image != null) { 102 setImageDescriptor(image); 103 } 104 fConfig = extension; 105 } 106 107 108 111 public void run() { 112 try { 113 if (fFactory == null) { 114 fFactory = fConfig.createFactory(); 115 } 116 117 fFactory.openConsole(); 118 } catch (CoreException e) { 119 ConsolePlugin.log(e); 120 } 121 } 122 123 126 public void runWithEvent(Event event) { 127 run(); 128 } 129 } 130 } 131 | Popular Tags |