KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
27  * @since 3.1
28  */

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

46     public void dispose() {
47         fFactoryExtensions = null;
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
52      */

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 JavaDoc 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 JavaDoc label= new StringBuffer JavaDoc();
75             //add the numerical accelerator
76
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     /* (non-Javadoc)
88      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
89      */

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 JavaDoc label, ImageDescriptor image, ConsoleFactoryExtension extension) {
100             setText(label);
101             if (image != null) {
102                 setImageDescriptor(image);
103             }
104             fConfig = extension;
105         }
106         
107         
108         /* (non-Javadoc)
109          * @see org.eclipse.jface.action.IAction#run()
110          */

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         /* (non-Javadoc)
124          * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event)
125          */

126         public void runWithEvent(Event event) {
127             run();
128         }
129     }
130 }
131
Popular Tags