1 11 14 package org.eclipse.pde.internal.ui.editor.site; 15 import java.util.*; 16 import java.util.List ; 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 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 [] getElements(Object 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 [] {PDEPlugin.getResourceString("SiteEditor.add"), PDEPlugin.getResourceString("SiteEditor.buildAll")}); getSection().setText(PDEPlugin 60 .getResourceString("SiteEditor.FeatureSection.header")); getSection().setDescription(PDEPlugin 62 .getResourceString("SiteEditor.FeatureSection.desc")); 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 e1, Object 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 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")) { public void run() { 125 Object []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")) { public void run() { 139 List 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")) { 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 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 () { 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"); 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 272 protected void selectionChanged(IStructuredSelection selection) { 273 getPage().getPDEEditor().setSelection(selection); 274 } 275 276 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 316 public boolean isDirty() { 317 if (fBuildModel!=null && ((WorkspaceSiteBuildModel) fBuildModel).isDirty()) 318 return true; 319 return super.isDirty(); 320 } 321 322 } | Popular Tags |