KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > build > BuildSiteAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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 /*
12  * Created on Oct 6, 2003
13  */

14 package org.eclipse.pde.internal.ui.build;
15
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.util.ArrayList JavaDoc;
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     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
51      * org.eclipse.ui.IWorkbenchPart)
52      */

53     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
60      */

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 JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc e) {
129                     PDEPlugin.logException(e);
130                 } catch (InterruptedException JavaDoc e) {
131                 }
132             }
133         }
134     }
135
136 }
137
Popular Tags