KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > ConsoleDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.internal.console;
12
13
14 import java.util.List JavaDoc;
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 /**
31  * Drop down action in the console to select the console to display.
32  */

33 class ConsoleDropDownAction extends Action implements IMenuCreator, IConsoleListener, IUpdate {
34
35     private IConsoleView fView;
36     private Menu fMenu;
37     
38     /* (non-Javadoc)
39      * @see org.eclipse.ui.texteditor.IUpdate#update()
40      */

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     /* (non-Javadoc)
58      * @see org.eclipse.jface.action.IMenuCreator#dispose()
59      */

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     /* (non-Javadoc)
70      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
71      */

72     public Menu getMenu(Menu parent) {
73         return null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
78      */

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 JavaDoc label= new StringBuffer JavaDoc();
99             //add the numerical accelerator
100
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     /* (non-Javadoc)
111      * @see org.eclipse.jface.action.IAction#run()
112      */

113     public void run() {
114         ConsoleView consoleView = (ConsoleView) fView;
115         boolean pinned = consoleView.isPinned();
116         if (pinned) {
117             consoleView.setPinned(false);
118         }
119         List JavaDoc 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     /* (non-Javadoc)
130      * @see org.eclipse.ui.console.IConsoleListener#consolesAdded(org.eclipse.ui.console.IConsole[])
131      */

132     public void consolesAdded(IConsole[] consoles) {
133         Display display = ConsolePlugin.getStandardDisplay();
134         display.asyncExec(new Runnable JavaDoc() {
135             public void run() {
136                 update();
137             }
138         });
139     }
140
141     /* (non-Javadoc)
142      *
143      * Dispose the menu when a launch is removed, such that the actions in this
144      * menu do not hang on to associated resources.
145      *
146      * @see org.eclipse.ui.console.IConsoleListener#consolesRemoved(org.eclipse.ui.console.IConsole[])
147      */

148     public void consolesRemoved(IConsole[] consoles) {
149         Display display = ConsolePlugin.getStandardDisplay();
150         display.asyncExec(new Runnable JavaDoc() {
151             public void run() {
152                 if (fMenu != null) {
153                     fMenu.dispose();
154                 }
155                 update();
156             }
157         });
158     }
159 }
160
Popular Tags