KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > xhtml > XHTMLConversionOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.wizards.xhtml;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.pde.core.IBaseModel;
26 import org.eclipse.pde.core.plugin.IExtensions;
27 import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
28 import org.eclipse.pde.core.plugin.IPluginElement;
29 import org.eclipse.pde.core.plugin.IPluginExtension;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.core.plugin.IPluginObject;
32 import org.eclipse.pde.internal.ui.PDEUIMessages;
33 import org.eclipse.pde.internal.ui.util.ModelModification;
34 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
35 import org.eclipse.pde.internal.ui.wizards.xhtml.TocReplaceTable.TocReplaceEntry;
36 import org.eclipse.swt.widgets.Shell;
37
38
39 public class XHTMLConversionOperation implements IWorkspaceRunnable {
40     
41     private class AddDynamicHelpExtensions extends ModelModification {
42
43         private String JavaDoc F_CP_POINT = "org.eclipse.help.contentProducer"; //$NON-NLS-1$
44
private String JavaDoc F_LSP_POINT = "org.eclipse.help.base.luceneSearchParticipants"; //$NON-NLS-1$
45
private String JavaDoc F_BIND_ELEMENT = "binding"; //$NON-NLS-1$
46
private String JavaDoc F_PROD_ATT_NAME = "producerId"; //$NON-NLS-1$
47
private String JavaDoc F_PROD_ATT_VALUE = "org.eclipse.help.dynamic"; //$NON-NLS-1$
48
private String JavaDoc F_PART_ATT_NAME = "participantId"; //$NON-NLS-1$
49
private String JavaDoc F_PART_ATT_VALUE = "org.eclipse.help.base.xhtml"; //$NON-NLS-1$
50

51         public AddDynamicHelpExtensions(IProject project) {
52             super(project);
53         }
54
55         protected void modifyModel(IBaseModel baseModel, IProgressMonitor monitor) throws CoreException {
56             if (!(baseModel instanceof IPluginModelBase))
57                 return;
58             IPluginModelBase model = (IPluginModelBase)baseModel;
59             IExtensions ext = model.getExtensions();
60             IPluginExtension[] extensions = ext.getExtensions();
61             
62             boolean contentProducerFound = false;
63             boolean luceneSearchParticipantFound = false;
64             for (int i = 0; i < extensions.length; i++) {
65                 String JavaDoc point = extensions[i].getPoint();
66                 if (point.equals(F_CP_POINT)) {
67                     contentProducerFound = true;
68                     ensureExists(extensions[i], F_BIND_ELEMENT, F_PROD_ATT_NAME, F_PROD_ATT_VALUE);
69                 } else if (point.equals(F_LSP_POINT)) {
70                     luceneSearchParticipantFound = true;
71                     ensureExists(extensions[i], F_BIND_ELEMENT, F_PART_ATT_NAME, F_PART_ATT_VALUE);
72                 }
73                 if (luceneSearchParticipantFound && contentProducerFound)
74                     break;
75             }
76             
77             IExtensionsModelFactory factory = model.getFactory();
78             if (!contentProducerFound) {
79                 IPluginExtension extension = factory.createExtension();
80                 extension.setPoint(F_CP_POINT);
81                 IPluginElement element = factory.createElement(extension);
82                 element.setName(F_BIND_ELEMENT);
83                 element.setAttribute(F_PROD_ATT_NAME, F_PROD_ATT_VALUE);
84                 extension.add(element);
85                 model.getPluginBase().add(extension);
86             }
87             if (!luceneSearchParticipantFound) {
88                 IPluginExtension extension = factory.createExtension();
89                 extension.setPoint(F_LSP_POINT);
90                 IPluginElement element = factory.createElement(extension);
91                 element.setName(F_BIND_ELEMENT);
92                 element.setAttribute(F_PART_ATT_NAME, F_PART_ATT_VALUE);
93                 extension.add(element);
94                 model.getPluginBase().add(extension);
95             }
96         }
97         
98         private void ensureExists(IPluginExtension extension, String JavaDoc elementName, String JavaDoc attrName, String JavaDoc attrValue) throws CoreException {
99             IPluginObject[] children = extension.getChildren();
100             boolean foundElement = false;
101             for (int i = 0; i < children.length; i++) {
102                 if (children[i] instanceof IPluginElement) {
103                     IPluginElement element = (IPluginElement)children[i];
104                     if (element.getName().equals(elementName)) {
105                         foundElement = attrValue.equals(element.getAttribute(attrName));
106                         if (foundElement)
107                             break;
108                     }
109                 }
110             }
111             if (!foundElement) {
112                 IPluginElement element = extension.getPluginModel().getFactory().createElement(extension);
113                 element.setName(elementName);
114                 element.setAttribute(attrName, attrValue);
115                 extension.add(element);
116             }
117         }
118         
119     }
120     
121     private TocReplaceEntry[] fEntries;
122     private Shell fShell;
123     private XHTMLConverter fConverter;
124     
125     public XHTMLConversionOperation(TocReplaceEntry[] entries, Shell shell) {
126         fShell = shell;
127         fEntries = entries;
128         fConverter = new XHTMLConverter(XHTMLConverter.XHTML_TRANSITIONAL);
129     }
130
131     public void run(IProgressMonitor monitor) throws CoreException {
132         MultiStatus ms = new MultiStatus(
133                 "org.eclipse.pde.ui", IStatus.OK, //$NON-NLS-1$
134
PDEUIMessages.XHTMLConversionOperation_failed, null);
135         
136         monitor.beginTask(PDEUIMessages.XHTMLConversionOperation_taskName, fEntries.length * 3);
137         
138         HashSet JavaDoc touchedProjects = new HashSet JavaDoc();
139         for (int i = 0; i < fEntries.length; i++)
140             convert(fEntries[i], ms, monitor, touchedProjects);
141         
142         Iterator JavaDoc it = touchedProjects.iterator();
143         while (it.hasNext())
144             PDEModelUtility.modifyModel(new AddDynamicHelpExtensions((IProject)it.next()), monitor);
145         monitor.worked(1);
146         
147         checkFailed(ms);
148     }
149     
150     private void checkFailed(final MultiStatus ms) {
151         if (ms.getChildren().length > 0) {
152             fShell.getDisplay().syncExec(new Runnable JavaDoc() {
153                 public void run() {
154                     String JavaDoc message;
155                     if (ms.getChildren().length == 1)
156                         message = PDEUIMessages.XHTMLConversionOperation_1prob;
157                     else
158                         message = NLS.bind(PDEUIMessages.XHTMLConversionOperation_multiProb, Integer.toString(ms.getChildren().length));
159                     ErrorDialog.openError(fShell, PDEUIMessages.XHTMLConversionOperation_title, message, ms);
160                 }
161             });
162         }
163     }
164     
165     private void convert(TocReplaceEntry entry, MultiStatus ms, IProgressMonitor monitor, HashSet JavaDoc touchedProjects) {
166         if (monitor.isCanceled())
167             return;
168         monitor.subTask(NLS.bind(PDEUIMessages.XHTMLConversionOperation_createXHTML, entry.getHref()));
169         try {
170             fConverter.convert(entry.getEntryFile(), monitor);
171             IProject project = entry.getEntryFile().getProject();
172             if (!touchedProjects.contains(project))
173                 touchedProjects.add(project);
174         } catch (CoreException e) {
175             ms.add(new Status(
176                     IStatus.WARNING, "org.eclipse.pde.ui", //$NON-NLS-1$
177
IStatus.OK, entry.getTocFile().getName(), e));
178         }
179     }
180     
181
182 }
183
Popular Tags