KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > product > ProductIntroOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.wizards.product;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.FileLocator;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.osgi.util.NLS;
34 import org.eclipse.pde.core.IBaseModel;
35 import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
36 import org.eclipse.pde.core.plugin.IPluginBase;
37 import org.eclipse.pde.core.plugin.IPluginElement;
38 import org.eclipse.pde.core.plugin.IPluginExtension;
39 import org.eclipse.pde.core.plugin.IPluginModelBase;
40 import org.eclipse.pde.core.plugin.PluginRegistry;
41 import org.eclipse.pde.internal.core.TargetPlatformHelper;
42 import org.eclipse.pde.internal.core.iproduct.IProduct;
43 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
44 import org.eclipse.pde.internal.ui.PDEPlugin;
45 import org.eclipse.pde.internal.ui.PDEUIMessages;
46 import org.eclipse.pde.internal.ui.util.ModelModification;
47 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
48 import org.eclipse.pde.internal.ui.wizards.templates.ControlStack;
49 import org.eclipse.pde.ui.templates.IVariableProvider;
50 import org.eclipse.swt.widgets.Shell;
51
52 public class ProductIntroOperation extends BaseManifestOperation implements IVariableProvider {
53
54     protected String JavaDoc fIntroId;
55     private Shell fShell;
56     private IProduct fProduct;
57     private IProject fProject;
58     private static final String JavaDoc INTRO_POINT = "org.eclipse.ui.intro"; //$NON-NLS-1$
59
private static final String JavaDoc INTRO_CONFIG_POINT = "org.eclipse.ui.intro.config"; //$NON-NLS-1$
60
private static final String JavaDoc INTRO_CLASS = "org.eclipse.ui.intro.config.CustomizableIntroPart"; //$NON-NLS-1$
61
private static final String JavaDoc KEY_PRODUCT_NAME = "productName"; //$NON-NLS-1$
62

63     public ProductIntroOperation(IProduct product, String JavaDoc pluginId, String JavaDoc introId, Shell shell) {
64         super(shell, pluginId);
65         fIntroId = introId;
66         fProduct = product;
67         fProject = PluginRegistry.findModel(pluginId).getUnderlyingResource().getProject();
68     }
69
70     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
71             InterruptedException JavaDoc {
72         try {
73             IFile file = getFile();
74             if (!file.exists()) {
75                 createNewFile(file);
76             } else {
77                 modifyExistingFile(file, monitor);
78             }
79             updateSingleton(monitor);
80             generateFiles(monitor);
81         } catch (CoreException e) {
82             throw new InvocationTargetException JavaDoc(e);
83         }
84     }
85
86     private void createNewFile(IFile file) throws CoreException {
87         WorkspacePluginModelBase model = (WorkspacePluginModelBase) getModel(file);
88         IPluginBase base = model.getPluginBase();
89         base.setSchemaVersion(TargetPlatformHelper.getTargetVersion() < 3.2 ? "3.0" : "3.2"); //$NON-NLS-1$ //$NON-NLS-2$
90
base.add(createIntroExtension(model));
91         base.add(createIntroConfigExtension(model));
92         model.save();
93     }
94
95     private IPluginExtension createIntroExtension(IPluginModelBase model)
96             throws CoreException {
97         IPluginExtension extension = model.getFactory().createExtension();
98         extension.setPoint(INTRO_POINT);
99         extension.add(createIntroExtensionContent(extension));
100         extension.add(createIntroBindingExtensionContent(extension));
101         return extension;
102     }
103
104     private IPluginExtension createIntroConfigExtension(IPluginModelBase model)
105             throws CoreException {
106         IPluginExtension extension = model.getFactory().createExtension();
107         extension.setPoint(INTRO_CONFIG_POINT);
108         extension.add(createIntroConfigExtensionContent(extension));
109         return extension;
110     }
111
112     private IPluginElement createIntroExtensionContent(IPluginExtension extension) throws CoreException {
113         IPluginElement element = extension.getModel().getFactory().createElement(extension);
114         element.setName("intro"); //$NON-NLS-1$
115
element.setAttribute("id", fIntroId); //$NON-NLS-1$
116
element.setAttribute("class", INTRO_CLASS); //$NON-NLS-1$
117
return element;
118     }
119
120     private IPluginElement createIntroBindingExtensionContent(IPluginExtension extension) throws CoreException {
121         IPluginElement element = extension.getModel().getFactory().createElement(extension);
122         element.setName("introProductBinding"); //$NON-NLS-1$
123
element.setAttribute("productId", fProduct.getId()); //$NON-NLS-1$
124
element.setAttribute("introId", fIntroId); //$NON-NLS-1$
125
return element;
126     }
127     
128     private IPluginElement createIntroConfigExtensionContent(
129             IPluginExtension extension) throws CoreException {
130         IPluginElement element = extension.getModel().getFactory()
131                 .createElement(extension);
132         element.setName("config"); //$NON-NLS-1$
133
element.setAttribute("id", fPluginId + ".introConfigId"); //$NON-NLS-1$ //$NON-NLS-2$
134
element.setAttribute("introId", fIntroId); //$NON-NLS-1$
135
element.setAttribute("content", "introContent.xml"); //$NON-NLS-1$ //$NON-NLS-2$
136
element.add(createPresentationElement(element));
137
138         return element;
139     }
140
141     private IPluginElement createPresentationElement(IPluginElement parent)
142             throws CoreException {
143         IPluginElement presentation = null;
144         IPluginElement implementation = null;
145         IExtensionsModelFactory factory = parent.getModel().getFactory();
146         
147         presentation = factory.createElement(parent);
148         presentation.setName("presentation"); //$NON-NLS-1$
149
presentation.setAttribute("home-page-id", "root"); //$NON-NLS-1$ //$NON-NLS-2$
150

151         implementation = factory.createElement(presentation);
152         implementation.setName("implementation"); //$NON-NLS-1$
153
implementation.setAttribute("kind", "html"); //$NON-NLS-1$ //$NON-NLS-2$
154
implementation.setAttribute("style", "content/shared.css"); //$NON-NLS-1$ //$NON-NLS-2$
155
implementation.setAttribute("os", "win32,linux,macosx"); //$NON-NLS-1$ //$NON-NLS-2$
156

157         presentation.add(implementation);
158         
159         return presentation;
160     }
161
162     private void modifyExistingFile(IFile file, IProgressMonitor monitor) throws CoreException {
163         IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] { file }, fShell);
164         if (status.getSeverity() != IStatus.OK)
165             throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, NLS.bind(PDEUIMessages.ProductDefinitionOperation_readOnly, fPluginId), null)); //$NON-NLS-1$
166

167         ModelModification mod = new ModelModification(file) {
168             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
169                 if (!(model instanceof IPluginModelBase))
170                     return;
171                 IPluginModelBase pluginModel = (IPluginModelBase)model;
172                 IPluginExtension extension = getExtension(pluginModel, INTRO_POINT);
173                 if (extension == null) {
174                     extension = createIntroExtension(pluginModel);
175                     pluginModel.getPluginBase().add(extension);
176                 } else {
177                     extension.add(createIntroExtensionContent(extension));
178                     extension.add(createIntroBindingExtensionContent(extension));
179                 }
180                 
181                 extension = getExtension(pluginModel, INTRO_CONFIG_POINT);
182                 if (extension == null) {
183                     extension = createIntroConfigExtension(pluginModel);
184                     pluginModel.getPluginBase().add(extension);
185                 } else {
186                     extension.add(createIntroConfigExtensionContent(extension));
187                 }
188             }
189         };
190         PDEModelUtility.modifyModel(mod, monitor);
191     }
192
193     private IPluginExtension getExtension(IPluginModelBase model,
194             String JavaDoc tPoint) throws CoreException {
195         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
196         for (int i = 0; i < extensions.length; i++) {
197             String JavaDoc point = extensions[i].getPoint();
198             if (tPoint.equals(point)) {
199                 return extensions[i];
200             }
201         }
202         return null;
203     }
204     
205     protected void generateFiles(IProgressMonitor monitor) throws CoreException {
206         monitor.setTaskName(PDEUIMessages.AbstractTemplateSection_generating);
207         
208         URL JavaDoc locationUrl = null;
209         try {
210             locationUrl = new URL JavaDoc(PDEPlugin.getDefault().getInstallURL(), "templates_3.1/intro/"); //$NON-NLS-1$
211
} catch (MalformedURLException JavaDoc e1) { return; }
212         if (locationUrl == null) {
213             return;
214         }
215         try {
216             locationUrl = FileLocator.resolve(locationUrl);
217             locationUrl = FileLocator.toFileURL(locationUrl);
218         } catch (IOException JavaDoc e) {
219             return;
220         }
221         if ("file".equals(locationUrl.getProtocol())) { //$NON-NLS-1$
222
File JavaDoc templateDirectory = new File JavaDoc(locationUrl.getFile());
223             if (!templateDirectory.exists())
224                 return;
225             generateFiles(templateDirectory, fProject, true, false, monitor);
226         }
227         monitor.subTask(""); //$NON-NLS-1$
228
monitor.worked(1);
229     }
230     
231     
232     private void generateFiles(File JavaDoc src, IContainer dst, boolean firstLevel,
233             boolean binary, IProgressMonitor monitor) throws CoreException {
234         File JavaDoc[] members = src.listFiles();
235
236         for (int i = 0; i < members.length; i++) {
237             File JavaDoc member = members[i];
238             if (member.getName().equals("ext.xml") || //$NON-NLS-1$
239
member.getName().equals("java") || //$NON-NLS-1$
240
member.getName().equals("concept3.xhtml") || //$NON-NLS-1$
241
member.getName().equals("extContent.xhtml")) //$NON-NLS-1$
242
continue;
243             else if (member.isDirectory()) {
244                 IContainer dstContainer = null;
245                 if (firstLevel) {
246                     binary = false;
247                     if (member.getName().equals("bin")) { //$NON-NLS-1$
248
binary = true;
249                         dstContainer = dst;
250                     }
251                 }
252                 if (dstContainer == null) {
253                     dstContainer = dst.getFolder(new Path(member.getName()));
254                 }
255                 if (dstContainer instanceof IFolder && !dstContainer.exists())
256                     ((IFolder) dstContainer).create(true, true, monitor);
257                 generateFiles(member, dstContainer, false, binary, monitor);
258             } else {
259                 if (firstLevel)
260                     binary = false;
261                 InputStream JavaDoc in = null;
262                 try {
263                     in = new FileInputStream JavaDoc(member);
264                     copyFile(member.getName(), in, dst, binary, monitor);
265                 } catch (IOException JavaDoc ioe) {
266                 } finally {
267                     if (in != null)
268                         try {
269                             in.close();
270                         } catch (IOException JavaDoc ioe2) {
271                         }
272                 }
273             }
274         }
275     }
276
277     private void copyFile(String JavaDoc fileName, InputStream JavaDoc input, IContainer dst, boolean binary,
278             IProgressMonitor monitor) throws CoreException {
279
280         monitor.subTask(fileName);
281         IFile dstFile = dst.getFile(new Path(fileName));
282
283         try {
284             InputStream JavaDoc stream = getProcessedStream(fileName, input, binary);
285             if (dstFile.exists()) {
286                 dstFile.setContents(stream, true, true, monitor);
287             } else {
288                 dstFile.create(stream, true, monitor);
289             }
290             stream.close();
291
292         } catch (IOException JavaDoc e) {
293         }
294     }
295     
296
297     private InputStream JavaDoc getProcessedStream(String JavaDoc fileName, InputStream JavaDoc stream,
298             boolean binary) throws IOException JavaDoc, CoreException {
299         if (binary)
300             return stream;
301
302         InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(stream);
303         int bufsize = 1024;
304         char[] cbuffer = new char[bufsize];
305         int read = 0;
306         StringBuffer JavaDoc keyBuffer = new StringBuffer JavaDoc();
307         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc();
308         ControlStack preStack = new ControlStack();
309         preStack.setValueProvider(this);
310
311         boolean replacementMode = false;
312         while (read != -1) {
313             read = reader.read(cbuffer);
314             for (int i = 0; i < read; i++) {
315                 char c = cbuffer[i];
316
317                 if (preStack.getCurrentState() == false) {
318                     continue;
319                 }
320
321                 if (c == '$') {
322                     if (replacementMode) {
323                         replacementMode = false;
324                         String JavaDoc key = keyBuffer.toString();
325                         String JavaDoc value = key.length() == 0 ? "$" //$NON-NLS-1$
326
: getReplacementString(fileName, key);
327                         outBuffer.append(value);
328                         keyBuffer.delete(0, keyBuffer.length());
329                     } else {
330                         replacementMode = true;
331                     }
332                 } else {
333                     if (replacementMode)
334                         keyBuffer.append(c);
335                     else {
336                         outBuffer.append(c);
337                     }
338                 }
339             }
340         }
341         return new ByteArrayInputStream JavaDoc(outBuffer.toString().getBytes(
342                 fProject.getDefaultCharset()));
343     }
344     
345     private String JavaDoc getReplacementString(String JavaDoc fileName, String JavaDoc key) {
346         if (key.equals(KEY_PRODUCT_NAME)) {
347             return fProduct.getName();
348         }
349         return key;
350     }
351
352     public Object JavaDoc getValue(String JavaDoc variable) {
353         return null;
354     }
355 }
356
Popular Tags