1 11 package org.eclipse.pde.internal.ui.wizards.product; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Properties ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.pde.core.plugin.IPluginAttribute; 23 import org.eclipse.pde.core.plugin.IPluginElement; 24 import org.eclipse.pde.core.plugin.IPluginExtension; 25 import org.eclipse.pde.core.plugin.IPluginModelBase; 26 import org.eclipse.pde.core.plugin.IPluginObject; 27 import org.eclipse.pde.core.plugin.PluginRegistry; 28 import org.eclipse.pde.internal.core.iproduct.IAboutInfo; 29 import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo; 30 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo; 31 import org.eclipse.pde.internal.core.iproduct.IProduct; 32 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory; 33 import org.eclipse.pde.internal.core.iproduct.IProductPlugin; 34 import org.eclipse.pde.internal.core.iproduct.ISplashInfo; 35 import org.eclipse.pde.internal.core.iproduct.IWindowImages; 36 import org.eclipse.pde.internal.core.product.SplashInfo; 37 import org.eclipse.pde.internal.core.product.WorkspaceProductModel; 38 import org.eclipse.pde.internal.ui.IPDEUIConstants; 39 import org.eclipse.pde.internal.ui.PDEPlugin; 40 import org.eclipse.pde.internal.ui.PDEUIMessages; 41 import org.eclipse.swt.widgets.Display; 42 import org.eclipse.ui.IWorkbenchPage; 43 import org.eclipse.ui.IWorkbenchPart; 44 import org.eclipse.ui.IWorkbenchWindow; 45 import org.eclipse.ui.PartInitException; 46 import org.eclipse.ui.actions.WorkspaceModifyOperation; 47 import org.eclipse.ui.branding.IProductConstants; 48 import org.eclipse.ui.ide.IDE; 49 import org.eclipse.ui.part.ISetSelectionTarget; 50 51 52 public class BaseProductCreationOperation extends WorkspaceModifyOperation { 53 54 private IFile fFile; 55 56 public BaseProductCreationOperation(IFile file) { 57 fFile = file; 58 } 59 60 63 protected void execute(IProgressMonitor monitor) throws CoreException, 64 InvocationTargetException , InterruptedException { 65 monitor.beginTask(PDEUIMessages.BaseProductCreationOperation_taskName, 2); 66 createContent(); 67 monitor.worked(1); 68 openFile(); 69 monitor.done(); 70 } 71 72 private void createContent() { 73 WorkspaceProductModel model = new WorkspaceProductModel(fFile, false); 74 initializeProduct(model.getProduct()); 75 model.save(); 76 model.dispose(); 77 } 78 79 protected void initializeProduct(IProduct product) { 80 IProductModelFactory factory = product.getModel().getFactory(); 81 IConfigurationFileInfo info = factory.createConfigFileInfo(); 82 info.setUse("default"); product.setConfigurationFileInfo(info); 84 IArgumentsInfo args = factory.createLauncherArguments(); 86 args.setVMArguments("-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts", IArgumentsInfo.L_ARGS_MACOS); product.setLauncherArguments(args); 88 } 89 90 private Properties getProductProperties(IPluginElement element) { 91 Properties prop = new Properties (); 92 IPluginObject[] children = element.getChildren(); 93 for (int i = 0; i < children.length; i++) { 94 IPluginElement child = (IPluginElement)children[i]; 95 if (child.getName().equals("property")) { String name = null; 97 String value = null; 98 IPluginAttribute attr = child.getAttribute("name"); if (attr != null) 100 name = attr.getValue(); 101 attr = child.getAttribute("value"); if (attr != null) 103 value = attr.getValue(); 104 if (name != null && value != null) 105 prop.put(name, value); 106 } 107 } 108 return prop; 109 } 110 111 protected IPluginElement getProductExtension(String productId) { 112 int lastDot = productId.lastIndexOf('.'); 113 if (lastDot == -1) 114 return null; 115 String pluginId = productId.substring(0, lastDot); 116 IPluginModelBase model = PluginRegistry.findModel(pluginId); 117 if (model != null) { 118 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 119 for (int i = 0; i < extensions.length; i++) { 120 if ("org.eclipse.core.runtime.products".equals(extensions[i].getPoint()) && productId.substring(lastDot+1).equals(extensions[i].getId())) { 122 IPluginObject[] children = extensions[i].getChildren(); 123 if (children.length > 0) { 124 IPluginElement object = (IPluginElement)children[0]; 125 if (object.getName().equals("product")) return object; 127 } 128 } 129 } 130 } 131 return null; 132 } 133 134 protected void initializeProductInfo(IProductModelFactory factory, IProduct product, String id) { 135 product.setId(id); 136 IPluginElement element = getProductExtension(id); 137 if (element != null) { 138 IPluginAttribute attr = element.getAttribute("application"); if (attr != null) 140 product.setApplication(attr.getValue()); 141 attr = element.getAttribute("name"); if (attr != null) 143 product.setName(attr.getValue()); 144 Properties prop = getProductProperties(element); 145 String aboutText = prop.getProperty(IProductConstants.ABOUT_TEXT); 146 String aboutImage = prop.getProperty(IProductConstants.ABOUT_IMAGE); 147 if (aboutText != null || aboutImage != null) { 148 IAboutInfo info = factory.createAboutInfo(); 149 info.setText(aboutText); 150 info.setImagePath(aboutImage); 151 product.setAboutInfo(info); 152 } 153 IWindowImages winImages = factory.createWindowImages(); 154 String path = prop.getProperty(IProductConstants.WINDOW_IMAGES); 155 if (path != null) { 156 StringTokenizer tokenizer = new StringTokenizer (path, ",", true); int size = 0; 158 while (tokenizer.hasMoreTokens()) { 159 String token = tokenizer.nextToken(); 160 if (token.equals(",")) size++; 162 else 163 winImages.setImagePath(token, size); 164 } 165 } 166 product.setWindowImages(winImages); 167 168 ISplashInfo splashInfo = factory.createSplashInfo(); 169 splashInfo.setForegroundColor(prop.getProperty(IProductConstants.STARTUP_FOREGROUND_COLOR), true); 170 int[] barGeo = SplashInfo.getGeometryArray(prop.getProperty(IProductConstants.STARTUP_PROGRESS_RECT)); 171 splashInfo.setProgressGeometry(barGeo, true); 172 int[] messageGeo = SplashInfo.getGeometryArray(prop.getProperty(IProductConstants.STARTUP_MESSAGE_RECT)); 173 splashInfo.setMessageGeometry(messageGeo, true); 174 product.setSplashInfo(splashInfo); 175 } 176 } 177 178 protected void addPlugins(IProductModelFactory factory, IProduct product, IPluginModelBase[] plugins) { 179 IProductPlugin[] pplugins = new IProductPlugin[plugins.length]; 180 for (int i = 0; i < plugins.length; i++) { 181 IProductPlugin pplugin = factory.createPlugin(); 182 pplugin.setId(plugins[i].getPluginBase().getId()); 183 pplugins[i] = pplugin; 184 } 185 product.addPlugins(pplugins); 186 } 187 188 protected void addPlugins(IProductModelFactory factory, IProduct product, String [] plugins) { 189 IProductPlugin[] pplugins = new IProductPlugin[plugins.length]; 190 for (int i = 0; i < plugins.length; i++) { 191 IProductPlugin pplugin = factory.createPlugin(); 192 pplugin.setId(plugins[i]); 193 pplugins[i] = pplugin; 194 } 195 product.addPlugins(pplugins); 196 } 197 198 private void openFile() { 199 Display.getCurrent().asyncExec(new Runnable () { 200 public void run() { 201 IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); 202 if (ww == null) { 203 return; 204 } 205 IWorkbenchPage page = ww.getActivePage(); 206 if (page == null || !fFile.exists()) 207 return; 208 IWorkbenchPart focusPart = page.getActivePart(); 209 if (focusPart instanceof ISetSelectionTarget) { 210 ISelection selection = new StructuredSelection(fFile); 211 ((ISetSelectionTarget) focusPart).selectReveal(selection); 212 } 213 try { 214 IDE.openEditor(page, fFile, IPDEUIConstants.PRODUCT_EDITOR_ID); 215 } catch (PartInitException e) { 216 } 217 } 218 }); 219 } 220 221 } 222 | Popular Tags |