KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > NewTypeDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.ui.wizards;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionPoint;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Platform;
20
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Menu;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.ActionContributionItem;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.action.IMenuCreator;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.viewers.ISelection;
31
32 import org.eclipse.ui.INewWizard;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.jdt.ui.actions.AbstractOpenWizardAction;
38 import org.eclipse.jdt.ui.actions.OpenNewClassWizardAction;
39
40 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42 import org.eclipse.jdt.internal.ui.util.CoreUtility;
43
44 import org.osgi.framework.Bundle;
45
46
47 /**
48  * A type wizard is added to the type drop down if it has a paramater 'javatype':
49  * <wizard
50  * name="My Type Wizard"
51  * icon="icons/wiz.gif"
52  * category="mycategory"
53  * id="xx.MyWizard">
54  * <class class="org.xx.MyWizard">
55  * <parameter name="javatype" value="true"/>
56  * </class>
57  * <description>
58  * My Type Wizard
59  * </description>
60  * </wizard>
61  */

62 public class NewTypeDropDownAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 {
63
64     public static class OpenTypeWizardAction extends AbstractOpenWizardAction {
65
66         private final static String JavaDoc ATT_NAME = "name";//$NON-NLS-1$
67
private final static String JavaDoc ATT_CLASS = "class";//$NON-NLS-1$
68
private final static String JavaDoc ATT_ICON = "icon";//$NON-NLS-1$
69
private static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
70

71         private IConfigurationElement fConfigurationElement;
72
73         public OpenTypeWizardAction(IConfigurationElement element) {
74             fConfigurationElement= element;
75             setText(element.getAttribute(ATT_NAME));
76             
77             String JavaDoc description= getDescriptionFromConfig(fConfigurationElement);
78             setDescription(description);
79             setToolTipText(description);
80             setImageDescriptor(getIconFromConfig(fConfigurationElement));
81         }
82         
83         private String JavaDoc getDescriptionFromConfig(IConfigurationElement config) {
84             IConfigurationElement [] children = config.getChildren(TAG_DESCRIPTION);
85             if (children.length>=1) {
86                 return children[0].getValue();
87             }
88             return ""; //$NON-NLS-1$
89
}
90
91         private ImageDescriptor getIconFromConfig(IConfigurationElement config) {
92             String JavaDoc iconName = config.getAttribute(ATT_ICON);
93             if (iconName != null) {
94                 Bundle bundle= Platform.getBundle(config.getContributor().getName());
95                 return JavaPluginImages.createImageDescriptor(bundle, new Path(iconName), true);
96             }
97             return null;
98         }
99         
100         /* (non-Javadoc)
101          * @see org.eclipse.jdt.internal.ui.wizards.AbstractOpenWizardAction#createWizard()
102          */

103         protected INewWizard createWizard() throws CoreException {
104             return (INewWizard) CoreUtility.createExtension(fConfigurationElement, ATT_CLASS);
105         }
106     }
107     
108     
109     
110     private final static String JavaDoc TAG_WIZARD = "wizard";//$NON-NLS-1$
111
private final static String JavaDoc ATT_JAVATYPE = "javatype";//$NON-NLS-1$
112

113     private final static String JavaDoc TAG_PARAMETER = "parameter";//$NON-NLS-1$
114
private final static String JavaDoc TAG_NAME = "name";//$NON-NLS-1$
115
private final static String JavaDoc TAG_VALUE = "value";//$NON-NLS-1$
116

117     private static final String JavaDoc PL_NEW = "newWizards"; //$NON-NLS-1$
118
private static final String JavaDoc TAG_CLASS = "class"; //$NON-NLS-1$
119

120     private Menu fMenu;
121     
122     private Shell fWizardShell;
123     
124     public NewTypeDropDownAction() {
125         fMenu= null;
126         setMenuCreator(this);
127         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_CLASS_WIZARD_ACTION);
128     }
129
130     public void dispose() {
131         if (fMenu != null) {
132             fMenu.dispose();
133             fMenu= null;
134         }
135     }
136
137     public Menu getMenu(Menu parent) {
138         return null;
139     }
140
141     public Menu getMenu(Control parent) {
142         if (fMenu == null) {
143             fMenu= new Menu(parent);
144             OpenTypeWizardAction[] actions= getActionFromDescriptors();
145             for (int i= 0; i < actions.length; i++) {
146                 OpenTypeWizardAction curr= actions[i];
147                 curr.setShell(fWizardShell);
148                 ActionContributionItem item= new ActionContributionItem(curr);
149                 item.fill(fMenu, -1);
150             }
151         
152         }
153         return fMenu;
154     }
155     
156     public void run() {
157         new OpenNewClassWizardAction().run();
158     }
159     
160     public static OpenTypeWizardAction[] getActionFromDescriptors() {
161         ArrayList JavaDoc containers= new ArrayList JavaDoc();
162         
163         IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(PlatformUI.PLUGIN_ID, PL_NEW);
164         if (extensionPoint != null) {
165             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
166             for (int i = 0; i < elements.length; i++) {
167                 IConfigurationElement element= elements[i];
168                 if (element.getName().equals(TAG_WIZARD) && isJavaTypeWizard(element)) {
169                     containers.add(new OpenTypeWizardAction(element));
170                 }
171             }
172         }
173         return (OpenTypeWizardAction[]) containers.toArray(new OpenTypeWizardAction[containers.size()]);
174     }
175         
176     private static boolean isJavaTypeWizard(IConfigurationElement element) {
177         IConfigurationElement[] classElements= element.getChildren(TAG_CLASS);
178         if (classElements.length > 0) {
179             for (int i= 0; i < classElements.length; i++) {
180                 IConfigurationElement[] paramElements= classElements[i].getChildren(TAG_PARAMETER);
181                 for (int k = 0; k < paramElements.length; k++) {
182                     IConfigurationElement curr= paramElements[k];
183                     if (ATT_JAVATYPE.equals(curr.getAttribute(TAG_NAME))) {
184                         return Boolean.valueOf(curr.getAttribute(TAG_VALUE)).booleanValue();
185                     }
186                 }
187             }
188         }
189         // old way, deprecated
190
if (Boolean.valueOf(element.getAttribute(ATT_JAVATYPE)).booleanValue()) {
191             return true;
192         }
193         return false;
194     }
195         
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
199      */

200     public void init(IWorkbenchWindow window) {
201         fWizardShell= window.getShell();
202     }
203     
204     /* (non-Javadoc)
205      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
206      */

207     public void run(IAction action) {
208         run();
209     }
210     
211     /* (non-Javadoc)
212      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
213      */

214     public void selectionChanged(IAction action, ISelection selection) {
215     }
216
217 }
218
Popular Tags