001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.build;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.operation.IRunnableWithProgress;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.pde.internal.core.PDECore;
024: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
025: import org.eclipse.pde.internal.core.isite.ISiteFeature;
026: import org.eclipse.pde.internal.core.isite.ISiteModel;
027: import org.eclipse.pde.internal.core.site.WorkspaceSiteModel;
028: import org.eclipse.pde.internal.ui.PDEPlugin;
029: import org.eclipse.pde.internal.ui.PDEPluginImages;
030: import org.eclipse.pde.internal.ui.editor.site.SiteEditor;
031: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
032: import org.eclipse.ui.IObjectActionDelegate;
033: import org.eclipse.ui.IWorkbenchPart;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.progress.IProgressConstants;
036:
037: public class BuildSiteAction implements IObjectActionDelegate {
038:
039: private ISiteModel fModel;
040:
041: private IFile fSiteXML;
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
047: * org.eclipse.ui.IWorkbenchPart)
048: */
049: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
056: */
057: public void run(IAction action) {
058: if (fModel == null)
059: return;
060: ISiteFeature[] sbFeatures = fModel.getSite().getFeatures();
061: IFeatureModel[] models = getFeatureModels(sbFeatures);
062:
063: if (models.length > 0) {
064: try {
065: ensureContentSaved();
066: fModel.load();
067: } catch (CoreException e) {
068: PDEPlugin.logException(e);
069: }
070: BuildSiteJob job = new BuildSiteJob(models, fModel);
071: job.setUser(true);
072: job.schedule();
073: job.setProperty(IProgressConstants.ICON_PROPERTY,
074: PDEPluginImages.DESC_SITE_OBJ);
075: }
076: }
077:
078: private IFeatureModel[] getFeatureModels(ISiteFeature[] sFeatures) {
079: ArrayList list = new ArrayList();
080: for (int i = 0; i < sFeatures.length; i++) {
081: ISiteFeature siteFeature = sFeatures[i];
082: IFeatureModel model = PDECore.getDefault()
083: .getFeatureModelManager().findFeatureModelRelaxed(
084: siteFeature.getId(),
085: siteFeature.getVersion());
086: if (model != null)
087: list.add(model);
088: }
089: return (IFeatureModel[]) list.toArray(new IFeatureModel[list
090: .size()]);
091: }
092:
093: public void selectionChanged(IAction action, ISelection selection) {
094: if (selection instanceof IStructuredSelection) {
095: Object obj = ((IStructuredSelection) selection)
096: .getFirstElement();
097: if (obj != null && obj instanceof IFile) {
098: fSiteXML = (IFile) obj;
099: fModel = new WorkspaceSiteModel(fSiteXML);
100: try {
101: fModel.load();
102: ISiteFeature[] features = fModel.getSite()
103: .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()
116: .getProject();
117: final SiteEditor editor = PDEModelUtility
118: .getOpenUpdateSiteEditor(project);
119: if (editor != null && editor.isDirty()) {
120: try {
121: IRunnableWithProgress op = new IRunnableWithProgress() {
122: public void run(IProgressMonitor monitor) {
123: editor.doSave(monitor);
124: }
125: };
126: PlatformUI
127: .getWorkbench()
128: .getProgressService()
129: .runInUI(
130: PDEPlugin
131: .getActiveWorkbenchWindow(),
132: op,
133: PDEPlugin.getWorkspace().getRoot());
134: } catch (InvocationTargetException e) {
135: PDEPlugin.logException(e);
136: } catch (InterruptedException e) {
137: }
138: }
139: }
140: }
141:
142: }
|