1 11 package org.eclipse.jdt.internal.ui.wizards; 12 13 import java.util.ArrayList ; 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 62 public class NewTypeDropDownAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 { 63 64 public static class OpenTypeWizardAction extends AbstractOpenWizardAction { 65 66 private final static String ATT_NAME = "name"; private final static String ATT_CLASS = "class"; private final static String ATT_ICON = "icon"; private static final String TAG_DESCRIPTION = "description"; 71 private IConfigurationElement fConfigurationElement; 72 73 public OpenTypeWizardAction(IConfigurationElement element) { 74 fConfigurationElement= element; 75 setText(element.getAttribute(ATT_NAME)); 76 77 String description= getDescriptionFromConfig(fConfigurationElement); 78 setDescription(description); 79 setToolTipText(description); 80 setImageDescriptor(getIconFromConfig(fConfigurationElement)); 81 } 82 83 private String getDescriptionFromConfig(IConfigurationElement config) { 84 IConfigurationElement [] children = config.getChildren(TAG_DESCRIPTION); 85 if (children.length>=1) { 86 return children[0].getValue(); 87 } 88 return ""; } 90 91 private ImageDescriptor getIconFromConfig(IConfigurationElement config) { 92 String 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 103 protected INewWizard createWizard() throws CoreException { 104 return (INewWizard) CoreUtility.createExtension(fConfigurationElement, ATT_CLASS); 105 } 106 } 107 108 109 110 private final static String TAG_WIZARD = "wizard"; private final static String ATT_JAVATYPE = "javatype"; 113 private final static String TAG_PARAMETER = "parameter"; private final static String TAG_NAME = "name"; private final static String TAG_VALUE = "value"; 117 private static final String PL_NEW = "newWizards"; private static final String TAG_CLASS = "class"; 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 containers= new ArrayList (); 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 if (Boolean.valueOf(element.getAttribute(ATT_JAVATYPE)).booleanValue()) { 191 return true; 192 } 193 return false; 194 } 195 196 197 200 public void init(IWorkbenchWindow window) { 201 fWizardShell= window.getShell(); 202 } 203 204 207 public void run(IAction action) { 208 run(); 209 } 210 211 214 public void selectionChanged(IAction action, ISelection selection) { 215 } 216 217 } 218 | Popular Tags |