1 11 14 package org.eclipse.pde.internal.ui.build; 15 16 import java.lang.reflect.InvocationTargetException ; 17 import java.util.ArrayList ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.operation.IRunnableWithProgress; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.pde.internal.core.PDECore; 28 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 29 import org.eclipse.pde.internal.core.isite.ISiteFeature; 30 import org.eclipse.pde.internal.core.isite.ISiteModel; 31 import org.eclipse.pde.internal.core.site.WorkspaceSiteModel; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEPluginImages; 34 import org.eclipse.pde.internal.ui.editor.site.SiteEditor; 35 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 36 import org.eclipse.ui.IObjectActionDelegate; 37 import org.eclipse.ui.IWorkbenchPart; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.progress.IProgressConstants; 40 41 public class BuildSiteAction implements IObjectActionDelegate { 42 43 private ISiteModel fModel; 44 45 private IFile fSiteXML; 46 47 53 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 54 } 55 56 61 public void run(IAction action) { 62 if (fModel == null) 63 return; 64 ISiteFeature[] sbFeatures = fModel.getSite().getFeatures(); 65 IFeatureModel[] models = getFeatureModels(sbFeatures); 66 67 if (models.length > 0) { 68 try { 69 ensureContentSaved(); 70 fModel.load(); 71 } catch (CoreException e) { 72 PDEPlugin.logException(e); 73 } 74 BuildSiteJob job = new BuildSiteJob(models, fModel); 75 job.setUser(true); 76 job.schedule(); 77 job.setProperty(IProgressConstants.ICON_PROPERTY, 78 PDEPluginImages.DESC_SITE_OBJ); 79 } 80 } 81 82 private IFeatureModel[] getFeatureModels(ISiteFeature[] sFeatures) { 83 ArrayList list = new ArrayList (); 84 for (int i = 0; i < sFeatures.length; i++) { 85 ISiteFeature siteFeature = sFeatures[i]; 86 IFeatureModel model = PDECore.getDefault().getFeatureModelManager() 87 .findFeatureModelRelaxed(siteFeature.getId(), 88 siteFeature.getVersion()); 89 if (model != null) 90 list.add(model); 91 } 92 return (IFeatureModel[]) list.toArray(new IFeatureModel[list.size()]); 93 } 94 95 public void selectionChanged(IAction action, ISelection selection) { 96 if (selection instanceof IStructuredSelection) { 97 Object obj = ((IStructuredSelection) selection).getFirstElement(); 98 if (obj != null && obj instanceof IFile) { 99 fSiteXML = (IFile) obj; 100 fModel = new WorkspaceSiteModel(fSiteXML); 101 try { 102 fModel.load(); 103 ISiteFeature[] features = fModel.getSite().getFeatures(); 104 if(features.length <= 0 ) 105 action.setEnabled(false); 106 } catch (CoreException e) { 107 action.setEnabled(false); 108 } 109 } 110 } 111 } 112 113 private void ensureContentSaved() { 114 if(fModel != null && fModel.getUnderlyingResource() != null) { 115 IProject project = fModel.getUnderlyingResource().getProject(); 116 final SiteEditor editor = 117 PDEModelUtility.getOpenUpdateSiteEditor(project); 118 if (editor != null && editor.isDirty()) { 119 try { 120 IRunnableWithProgress op = new IRunnableWithProgress() { 121 public void run(IProgressMonitor monitor) { 122 editor.doSave(monitor); 123 } 124 }; 125 PlatformUI.getWorkbench().getProgressService().runInUI( 126 PDEPlugin.getActiveWorkbenchWindow(), op, 127 PDEPlugin.getWorkspace().getRoot()); 128 } catch (InvocationTargetException e) { 129 PDEPlugin.logException(e); 130 } catch (InterruptedException e) { 131 } 132 } 133 } 134 } 135 136 } 137 | Popular Tags |