KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > FeatureSection


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /*
12  * Created on Sep 29, 2003
13  */

14 package org.eclipse.pde.internal.ui.editor.site;
15 import java.util.*;
16 import java.util.List JavaDoc;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.action.*;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.jface.wizard.*;
21 import org.eclipse.pde.core.*;
22 import org.eclipse.pde.internal.core.ifeature.*;
23 import org.eclipse.pde.internal.core.isite.*;
24 import org.eclipse.pde.internal.core.site.*;
25 import org.eclipse.pde.internal.ui.*;
26 import org.eclipse.pde.internal.ui.build.*;
27 import org.eclipse.pde.internal.ui.editor.*;
28 import org.eclipse.pde.internal.ui.elements.*;
29 import org.eclipse.pde.internal.ui.parts.*;
30 import org.eclipse.swt.*;
31 import org.eclipse.swt.custom.*;
32 import org.eclipse.swt.dnd.*;
33 import org.eclipse.swt.widgets.*;
34 import org.eclipse.ui.actions.*;
35 import org.eclipse.ui.forms.widgets.*;
36
37 /**
38  * @author melhem
39  */

40 public class FeatureSection extends TableSection {
41     private TableViewer fFeaturesViewer;
42     private ISiteModel fModel;
43     private ISiteBuildModel fBuildModel;
44     private TablePart fFeaturesTablePart;
45     class FeatureContentProvider extends DefaultContentProvider
46             implements
47                 IStructuredContentProvider {
48         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
49             ISiteBuildModel model = (ISiteBuildModel) inputElement;
50             return model.getSiteBuild().getFeatures();
51         }
52     }
53     public FeatureSection(PDEFormPage formPage, Composite parent) {
54         super(formPage,
55                 parent,
56                 Section.DESCRIPTION,
57                 new String JavaDoc[] {PDEPlugin.getResourceString("SiteEditor.add"), //$NON-NLS-1$
58
PDEPlugin.getResourceString("SiteEditor.buildAll")}); //$NON-NLS-1$
59
getSection().setText(PDEPlugin
60                 .getResourceString("SiteEditor.FeatureSection.header")); //$NON-NLS-1$
61
getSection().setDescription(PDEPlugin
62                 .getResourceString("SiteEditor.FeatureSection.desc")); //$NON-NLS-1$
63
PDEPlugin.getDefault().getLabelProvider().connect(this);
64     }
65     public void dispose() {
66         super.dispose();
67         fModel.removeModelChangedListener(this);
68         if (fBuildModel != null)
69             fBuildModel.removeModelChangedListener(this);
70         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
71     }
72     
73     public void createClient(Section section, FormToolkit toolkit) {
74         fModel = (ISiteModel) getPage().getModel();
75         fModel.addModelChangedListener(this);
76         fBuildModel = fModel.getBuildModel();
77         if (fBuildModel != null)
78             fBuildModel.addModelChangedListener(this);
79         Composite container = createClientContainer(section, 2, toolkit);
80         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
81         fFeaturesTablePart = getTablePart();
82
83         fFeaturesViewer = fFeaturesTablePart.getTableViewer();
84         fFeaturesViewer.setContentProvider(new FeatureContentProvider());
85         fFeaturesViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
86         fFeaturesViewer.setSorter(new ViewerSorter() {
87             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
88                 ISiteBuildFeature f1 = (ISiteBuildFeature) e1;
89                 ISiteBuildFeature f2 = (ISiteBuildFeature) e2;
90                 int compare = super.compare(viewer, f1.getId(), f2.getId());
91                 return (compare == 0) ? super.compare(viewer, f1.getVersion(),
92                         f2.getVersion()) : compare;
93             }
94         });
95         
96         // add drag support
97
Transfer[] transfers = new Transfer[] { ModelDataTransfer.getInstance()};
98         fFeaturesViewer.addDragSupport(DND.DROP_LINK, transfers,
99                 new DragSourceListener() {
100                     public void dragStart(DragSourceEvent event) {
101                         ISelection selection = fFeaturesViewer.getSelection();
102                         if (selection == null || selection.isEmpty()) {
103                             event.doit = false;
104                         }
105                     }
106                     public void dragSetData(DragSourceEvent event) {
107                         IStructuredSelection ssel = (IStructuredSelection)fFeaturesViewer.getSelection();
108                         event.data = ssel.toArray();
109                     }
110                     public void dragFinished(DragSourceEvent event) {
111                     }
112                 });
113
114         fFeaturesViewer.setInput(fBuildModel);
115         toolkit.paintBordersFor(container);
116         section.setClient(container);
117         refresh();
118     }
119
120     public void fillContextMenu(IMenuManager manager) {
121         final ISelection selection = fFeaturesViewer.getSelection();
122         if (selection != null && !selection.isEmpty()) {
123             manager.add(new Action(PDEPlugin.getResourceString("SiteEditor.publish")) { //$NON-NLS-1$
124
public void run() {
125                     Object JavaDoc[]selected = ((IStructuredSelection)selection).toArray();
126                     for (int i = 0; i < selected.length; i++) {
127                         ISiteBuildFeature sbFeature = (ISiteBuildFeature)selected[i];
128                         ISiteFeature feature = findMatchingSiteFeature(fModel, sbFeature);
129                         try {
130                             if (feature == null)
131                                 fModel.getSite().addFeatures(new ISiteFeature[]{createSiteFeature(fModel, sbFeature)});
132                         } catch (CoreException e) {
133                         }
134                     }
135                 }
136             });
137             manager.add(new Action(PDEPlugin.getResourceString("SiteEditor.build")) { //$NON-NLS-1$
138
public void run() {
139                     List JavaDoc list = ((IStructuredSelection)selection).toList();
140                     handleBuild((ISiteBuildFeature[])list.toArray(new ISiteBuildFeature[list.size()]));
141                 }
142             });
143             manager.add(new Separator());
144             manager.add(new Action(PDEPlugin.getResourceString("SiteEditor.remove")) { //$NON-NLS-1$
145
public void run() {
146                     doGlobalAction(ActionFactory.DELETE.getId());
147                 }
148             });
149         }
150         manager.add(getPage().getPDEEditor().getContributor().getGlobalAction(ActionFactory.COPY.getId()));
151         manager.add(getPage().getPDEEditor().getContributor().getGlobalAction(ActionFactory.PASTE.getId()));
152         manager.add(new Separator());
153         manager.add(getPage().getPDEEditor().getContributor().getRevertAction());
154         manager.add(getPage().getPDEEditor().getContributor().getSaveAction());
155     }
156
157     public void refresh() {
158         fFeaturesViewer.refresh();
159         int featureCount = fFeaturesViewer.getTable().getItemCount();
160         fFeaturesTablePart.setButtonEnabled(1, featureCount > 0);
161         super.refresh();
162     }
163     
164     public void modelChanged(IModelChangedEvent event) {
165         markStale();
166     }
167
168     public void commit(boolean onSave) {
169         if (onSave && fBuildModel instanceof WorkspaceSiteBuildModel
170                 && ((WorkspaceSiteBuildModel) fBuildModel).isDirty()) {
171             ((WorkspaceSiteBuildModel) fBuildModel).save();
172         }
173         
174         super.commit(onSave);
175     }
176     
177     public boolean canPaste(Clipboard clipboard) {
178         return false;
179     }
180     
181     public boolean doGlobalAction(String JavaDoc actionId) {
182         if (actionId.equals(ActionFactory.CUT.getId())) {
183             handleRemoveFeature();
184             return false;
185         }
186         if (actionId.equals(ActionFactory.DELETE.getId())) {
187             handleRemoveFeature();
188             return true;
189         }
190         return false;
191     }
192     public void handleNewFeature() {
193         final Control control = fFeaturesViewer.getTable();
194         BusyIndicator.showWhile(control.getDisplay(), new Runnable JavaDoc() {
195             public void run() {
196                 BuiltFeaturesWizard wizard = new BuiltFeaturesWizard(
197                         fBuildModel);
198                 WizardDialog dialog = new WizardDialog(control.getShell(),
199                         wizard);
200                 if (dialog.open() == WizardDialog.OK) {
201                     markDirty();
202                 }
203             }
204         });
205     }
206     private boolean handleRemoveFeature() {
207         try {
208             IStructuredSelection ssel = (IStructuredSelection) fFeaturesViewer
209                     .getSelection();
210             if (ssel != null && ssel.size() > 0) {
211                 ISiteBuildFeature[] sbFeatures = (ISiteBuildFeature[]) ssel
212                         .toList().toArray(new ISiteBuildFeature[ssel.size()]);
213                 for (int i = 0; i < sbFeatures.length; i++) {
214                     ISiteFeature feature = findMatchingSiteFeature(fModel, sbFeatures[i]);
215                     if (feature != null) {
216                         ISite site = fModel.getSite();
217                         site.removeFeatures(new ISiteFeature[]{feature});
218                     }
219                 }
220                 fBuildModel.getSiteBuild().removeFeatures(sbFeatures);
221                 markDirty();
222                 return true;
223             }
224         } catch (CoreException e) {
225         }
226         return false;
227     }
228     
229     public static ISiteFeature findMatchingSiteFeature(ISiteModel model, ISiteBuildFeature sbfeature) {
230         ISiteFeature[] sfeatures = model.getSite().getFeatures();
231         for (int j = 0; j < sfeatures.length; j++) {
232             ISiteFeature sfeature = sfeatures[j];
233             if (matches(sfeature, sbfeature))
234                 return sfeature;
235         }
236         return null;
237     }
238     
239     private static boolean matches(ISiteFeature sfeature,
240             ISiteBuildFeature sbfeature) {
241         return sbfeature.getId().equals(sfeature.getId())
242                 && sbfeature.getVersion().equals(sfeature.getVersion());
243     }
244     
245     public static ISiteFeature createSiteFeature(ISiteModel model, ISiteBuildFeature sbfeature)
246     throws CoreException {
247         ISiteFeature sfeature = model.getFactory().createFeature();
248         sfeature.setId(sbfeature.getId());
249         sfeature.setVersion(sbfeature.getVersion());
250         sfeature.setURL(model.getBuildModel().getSiteBuild().getFeatureLocation() + "/" + sbfeature.getId() + "_" + sbfeature.getVersion()+".jar"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
251
IFeature refFeature = sbfeature.getReferencedFeature();
252         sfeature.setOS(refFeature.getOS());
253         sfeature.setWS(refFeature.getWS());
254         sfeature.setArch(refFeature.getArch());
255         sfeature.setNL(refFeature.getNL());
256         sfeature.setIsPatch(isFeaturePatch(refFeature));
257         return sfeature;
258     }
259     
260
261     private static boolean isFeaturePatch(IFeature feature) {
262         IFeatureImport[] imports = feature.getImports();
263         for (int i = 0; i<imports.length; i++){
264             if (imports[i].isPatch())
265                 return true;
266         }
267         return false;
268     }
269     /* (non-Javadoc)
270      * @see org.eclipse.pde.internal.ui.editor.TableSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
271      */

272     protected void selectionChanged(IStructuredSelection selection) {
273         getPage().getPDEEditor().setSelection(selection);
274     }
275     
276     /* (non-Javadoc)
277      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int)
278      */

279     protected void buttonSelected(int index) {
280         switch(index) {
281             case 0:
282                 handleNewFeature();
283                 break;
284             case 1:
285                 handleBuild(fBuildModel.getSiteBuild().getFeatures());
286         }
287     }
288
289     private void handleBuild(ISiteBuildFeature[] sbFeatures) {
290         if (sbFeatures.length == 0)
291             return;
292         IFeatureModel[] models = getFeatureModels(sbFeatures);
293         if (models.length == 0)
294             return;
295         BuildSiteJob job = new BuildSiteJob(models, fModel
296                 .getUnderlyingResource().getProject(), fBuildModel);
297         job.setUser(true);
298         job.schedule();
299     }
300     
301     private IFeatureModel[] getFeatureModels(ISiteBuildFeature[] sbFeatures) {
302         ArrayList list = new ArrayList();
303         for (int i = 0; i < sbFeatures.length; i++) {
304             IFeature feature = sbFeatures[i].getReferencedFeature();
305             if (feature == null)
306                 continue;
307             IFeatureModel model = feature.getModel();
308             if (model != null && model.getUnderlyingResource() != null)
309                 list.add(model);
310         }
311         return (IFeatureModel[])list.toArray(new IFeatureModel[list.size()]);
312     }
313     /* (non-Javadoc)
314      * @see org.eclipse.ui.forms.AbstractFormPart#isDirty()
315      */

316     public boolean isDirty() {
317         if (fBuildModel!=null && ((WorkspaceSiteBuildModel) fBuildModel).isDirty())
318             return true;
319         return super.isDirty();
320     }
321     
322 }
Popular Tags