KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > tools > CreateHelpIndexAction


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.tools;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.SubProgressMonitor;
24 import org.eclipse.help.search.HelpIndexBuilder;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.pde.core.plugin.IFragment;
31 import org.eclipse.pde.core.plugin.IPluginBase;
32 import org.eclipse.pde.core.plugin.IPluginModelBase;
33 import org.eclipse.pde.internal.core.ModelEntry;
34 import org.eclipse.pde.internal.core.PDECore;
35 import org.eclipse.pde.internal.core.PluginModelManager;
36 import org.eclipse.pde.internal.ui.PDEPlugin;
37 import org.eclipse.pde.internal.ui.PDEUIMessages;
38 import org.eclipse.ui.IObjectActionDelegate;
39 import org.eclipse.ui.IWorkbenchPart;
40 import org.eclipse.ui.PlatformUI;
41
42 /**
43  * Creates the help search index by parsing the selected
44  * plugin.xml file and generating index for TOC extensions.
45  *
46  * @since 3.1
47  */

48
49 public class CreateHelpIndexAction implements IObjectActionDelegate {
50
51     private HelpIndexBuilder fIndexBuilder;
52     private IProject fProject;
53     private IStatus fStatus;
54
55     public CreateHelpIndexAction() {
56     }
57
58     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
59     }
60
61     public void run(IAction action) {
62         File JavaDoc file = getManifest();
63         if (file == null || !file.exists())
64             return;
65         if (fIndexBuilder == null)
66             fIndexBuilder = new HelpIndexBuilder();
67         fIndexBuilder.setManifest(file);
68         File JavaDoc target = fProject.getLocation().toFile();
69         if (target == null)
70             return;
71         fIndexBuilder.setDestination(target);
72         IRunnableWithProgress op = new IRunnableWithProgress() {
73             
74             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
75                 try {
76                     monitor.beginTask(PDEUIMessages.CreateHelpIndexAction_creating, 10);
77                     fIndexBuilder.execute(new SubProgressMonitor(monitor, 9));
78                 } catch (CoreException e) {
79                     IStatus s = e.getStatus();
80                     fStatus = new Status(IStatus.WARNING, s.getPlugin(), s.getCode(), s.getMessage(), s.getException());
81                 } finally {
82                     try {
83                         refreshTarget(new SubProgressMonitor(monitor, 1));
84                     } catch (CoreException e) {
85                         throw new InvocationTargetException JavaDoc(e);
86                     }
87                 }
88             }
89         };
90         try {
91             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
92         } catch (InvocationTargetException JavaDoc e) {
93             PDEPlugin.logException(e);
94         } catch (InterruptedException JavaDoc e) {
95             PDEPlugin.logException(e);
96         }
97         if (fStatus != null)
98             ErrorDialog.openError(null, null, null, fStatus);
99         fStatus = null;
100     }
101
102     private File JavaDoc getManifest() {
103         PluginModelManager manager = PDECore.getDefault().getModelManager();
104         ModelEntry entry = manager.findEntry(fProject);
105         if (entry != null)
106             return getManifest(manager, entry.getActiveModel());
107         return null;
108     }
109     
110     private File JavaDoc getManifest(PluginModelManager manager, IPluginModelBase modelBase) {
111         IPluginBase pluginBase = modelBase.getPluginBase();
112         if (pluginBase instanceof IFragment) {
113             // fragment
114
IFragment fragment = (IFragment)pluginBase;
115             String JavaDoc pluginId = fragment.getPluginId();
116             String JavaDoc pluginVersion = fragment.getPluginVersion();
117             int match = fragment.getRule();
118             IPluginModelBase pluginModel = manager.findPlugin(pluginId, pluginVersion, match);
119             if (pluginModel.getUnderlyingResource() == null)
120                 return null;
121             IFile pluginFile = pluginModel.getUnderlyingResource().getProject().getFile("plugin.xml"); //$NON-NLS-1$
122
return (pluginFile.exists()) ? pluginFile.getLocation().toFile() : null;
123         }
124         return fProject.getFile("plugin.xml").getLocation().toFile(); //$NON-NLS-1$
125
}
126
127     private void refreshTarget(IProgressMonitor monitor) throws CoreException {
128         fProject.refreshLocal(IResource.DEPTH_INFINITE, monitor);
129     }
130
131     public void selectionChanged(IAction action, ISelection selection) {
132         fProject = (IProject)((IStructuredSelection) selection).getFirstElement();
133     }
134 }
135
Popular Tags