1 11 package org.eclipse.pde.internal.ui.wizards.tools; 12 13 import java.io.File ; 14 import java.lang.reflect.InvocationTargetException ; 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 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 file = getManifest(); 63 if (file == null || !file.exists()) 64 return; 65 if (fIndexBuilder == null) 66 fIndexBuilder = new HelpIndexBuilder(); 67 fIndexBuilder.setManifest(file); 68 File 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 { 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 (e); 86 } 87 } 88 } 89 }; 90 try { 91 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op); 92 } catch (InvocationTargetException e) { 93 PDEPlugin.logException(e); 94 } catch (InterruptedException 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 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 getManifest(PluginModelManager manager, IPluginModelBase modelBase) { 111 IPluginBase pluginBase = modelBase.getPluginBase(); 112 if (pluginBase instanceof IFragment) { 113 IFragment fragment = (IFragment)pluginBase; 115 String pluginId = fragment.getPluginId(); 116 String 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"); return (pluginFile.exists()) ? pluginFile.getLocation().toFile() : null; 123 } 124 return fProject.getFile("plugin.xml").getLocation().toFile(); } 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 |