1 11 package org.eclipse.ui.internal.console; 12 13 14 import java.util.List ; 15 16 import org.eclipse.jface.action.Action; 17 import org.eclipse.jface.action.ActionContributionItem; 18 import org.eclipse.jface.action.IMenuCreator; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Menu; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.console.ConsolePlugin; 24 import org.eclipse.ui.console.IConsole; 25 import org.eclipse.ui.console.IConsoleConstants; 26 import org.eclipse.ui.console.IConsoleListener; 27 import org.eclipse.ui.console.IConsoleView; 28 import org.eclipse.ui.texteditor.IUpdate; 29 30 33 class ConsoleDropDownAction extends Action implements IMenuCreator, IConsoleListener, IUpdate { 34 35 private IConsoleView fView; 36 private Menu fMenu; 37 38 41 public void update() { 42 IConsole[] consoles = ConsolePlugin.getDefault().getConsoleManager().getConsoles(); 43 setEnabled(consoles.length > 1); 44 } 45 46 public ConsoleDropDownAction(IConsoleView view) { 47 fView= view; 48 setText(ConsoleMessages.ConsoleDropDownAction_0); 49 setToolTipText(ConsoleMessages.ConsoleDropDownAction_1); 50 setImageDescriptor(ConsolePluginImages.getImageDescriptor(IConsoleConstants.IMG_VIEW_CONSOLE)); 51 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IConsoleHelpContextIds.CONSOLE_DISPLAY_CONSOLE_ACTION); 52 setMenuCreator(this); 53 ConsolePlugin.getDefault().getConsoleManager().addConsoleListener(this); 54 update(); 55 } 56 57 60 public void dispose() { 61 if (fMenu != null) { 62 fMenu.dispose(); 63 } 64 65 fView= null; 66 ConsolePlugin.getDefault().getConsoleManager().removeConsoleListener(this); 67 } 68 69 72 public Menu getMenu(Menu parent) { 73 return null; 74 } 75 76 79 public Menu getMenu(Control parent) { 80 if (fMenu != null) { 81 fMenu.dispose(); 82 } 83 84 fMenu= new Menu(parent); 85 IConsole[] consoles= ConsolePlugin.getDefault().getConsoleManager().getConsoles(); 86 IConsole current = fView.getConsole(); 87 for (int i = 0; i < consoles.length; i++) { 88 IConsole console = consoles[i]; 89 Action action = new ShowConsoleAction(fView, console); 90 action.setChecked(console.equals(current)); 91 addActionToMenu(fMenu, action, i + 1); 92 } 93 return fMenu; 94 } 95 96 private void addActionToMenu(Menu parent, Action action, int accelerator) { 97 if (accelerator < 10) { 98 StringBuffer label= new StringBuffer (); 99 label.append('&'); 101 label.append(accelerator); 102 label.append(' '); 103 label.append(action.getText()); 104 action.setText(label.toString()); 105 } 106 ActionContributionItem item= new ActionContributionItem(action); 107 item.fill(parent, -1); 108 } 109 110 113 public void run() { 114 ConsoleView consoleView = (ConsoleView) fView; 115 boolean pinned = consoleView.isPinned(); 116 if (pinned) { 117 consoleView.setPinned(false); 118 } 119 List stack = consoleView.getConsoleStack(); 120 if (stack.size() > 1) { 121 IConsole console = (IConsole) stack.get(1); 122 fView.display(console); 123 } 124 if (pinned) { 125 consoleView.setPinned(true); 126 } 127 } 128 129 132 public void consolesAdded(IConsole[] consoles) { 133 Display display = ConsolePlugin.getStandardDisplay(); 134 display.asyncExec(new Runnable () { 135 public void run() { 136 update(); 137 } 138 }); 139 } 140 141 148 public void consolesRemoved(IConsole[] consoles) { 149 Display display = ConsolePlugin.getStandardDisplay(); 150 display.asyncExec(new Runnable () { 151 public void run() { 152 if (fMenu != null) { 153 fMenu.dispose(); 154 } 155 update(); 156 } 157 }); 158 } 159 } 160 | Popular Tags |