1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.Separator; 20 import org.eclipse.jface.action.ToolBarManager; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.IStructuredContentProvider; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.jface.viewers.TableViewer; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.pde.core.IModelChangedEvent; 28 import org.eclipse.pde.core.plugin.IPluginBase; 29 import org.eclipse.pde.core.plugin.IPluginModelBase; 30 import org.eclipse.pde.core.plugin.ModelEntry; 31 import org.eclipse.pde.core.plugin.PluginRegistry; 32 import org.eclipse.pde.internal.core.IFeatureModelDelta; 33 import org.eclipse.pde.internal.core.IFeatureModelListener; 34 import org.eclipse.pde.internal.core.IPluginModelListener; 35 import org.eclipse.pde.internal.core.PDECore; 36 import org.eclipse.pde.internal.core.PluginModelDelta; 37 import org.eclipse.pde.internal.core.feature.FeatureImport; 38 import org.eclipse.pde.internal.core.ifeature.IFeature; 39 import org.eclipse.pde.internal.core.ifeature.IFeatureImport; 40 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 41 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin; 42 import org.eclipse.pde.internal.ui.PDEPlugin; 43 import org.eclipse.pde.internal.ui.PDEUIMessages; 44 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 45 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer; 46 import org.eclipse.pde.internal.ui.editor.TableSection; 47 import org.eclipse.pde.internal.ui.editor.actions.SortAction; 48 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 49 import org.eclipse.pde.internal.ui.parts.TablePart; 50 import org.eclipse.pde.internal.ui.wizards.FeatureSelectionDialog; 51 import org.eclipse.pde.internal.ui.wizards.ListUtil; 52 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog; 53 import org.eclipse.swt.SWT; 54 import org.eclipse.swt.custom.BusyIndicator; 55 import org.eclipse.swt.dnd.Clipboard; 56 import org.eclipse.swt.events.DisposeEvent; 57 import org.eclipse.swt.events.DisposeListener; 58 import org.eclipse.swt.graphics.Cursor; 59 import org.eclipse.swt.layout.GridData; 60 import org.eclipse.swt.widgets.Button; 61 import org.eclipse.swt.widgets.Composite; 62 import org.eclipse.swt.widgets.Display; 63 import org.eclipse.swt.widgets.ToolBar; 64 import org.eclipse.ui.actions.ActionFactory; 65 import org.eclipse.ui.forms.widgets.FormToolkit; 66 import org.eclipse.ui.forms.widgets.Section; 67 68 public class RequiresSection extends TableSection implements 69 IPluginModelListener, IFeatureModelListener { 70 private Button fSyncButton; 71 72 private TableViewer fPluginViewer; 73 74 private Action fDeleteAction; 75 76 private SortAction fSortAction; 77 78 class ImportContentProvider extends DefaultContentProvider implements 79 IStructuredContentProvider { 80 public Object [] getElements(Object parent) { 81 if (parent instanceof IFeature) { 82 IFeatureImport[] imports = ((IFeature) parent).getImports(); 83 ArrayList displayable = new ArrayList (); 84 for (int i = 0; i < imports.length; i++) { 85 if (imports[i].isPatch()) 86 continue; 87 displayable.add(imports[i]); 88 } 89 90 return displayable.toArray(); 91 } 92 return new Object [0]; 93 } 94 } 95 96 public RequiresSection(FeatureDependenciesPage page, Composite parent) { 97 super(page, parent, Section.DESCRIPTION, new String [] { 98 PDEUIMessages.FeatureEditor_RequiresSection_plugin, 99 PDEUIMessages.FeatureEditor_RequiresSection_feature, null, 100 PDEUIMessages.FeatureEditor_RequiresSection_compute }); 101 getSection().setText(PDEUIMessages.FeatureEditor_RequiresSection_title); 102 getSection().setDescription(PDEUIMessages.FeatureEditor_RequiresSection_desc); 103 getTablePart().setEditable(false); 104 } 105 106 public void commit(boolean onSave) { 107 super.commit(onSave); 108 } 109 110 public void createClient(Section section, FormToolkit toolkit) { 111 112 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 113 GridData data = new GridData(GridData.FILL_BOTH); 114 section.setLayoutData(data); 115 116 Composite container = createClientContainer(section, 2, toolkit); 117 118 fSyncButton = toolkit.createButton(container, PDEUIMessages.FeatureEditor_RequiresSection_sync, SWT.CHECK); 119 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 121 gd.horizontalSpan = 2; 122 fSyncButton.setLayoutData(gd); 123 124 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 125 126 TablePart tablePart = getTablePart(); 127 fPluginViewer = tablePart.getTableViewer(); 128 fPluginViewer.setContentProvider(new ImportContentProvider()); 129 fPluginViewer.setComparator(ListUtil.NAME_COMPARATOR); 130 fPluginViewer.setLabelProvider(PDEPlugin.getDefault() 131 .getLabelProvider()); 132 133 fDeleteAction = new Action() { 134 public void run() { 135 handleDelete(); 136 } 137 }; 138 fDeleteAction.setText(PDEUIMessages.Actions_delete_label); 139 toolkit.paintBordersFor(container); 140 section.setClient(container); 141 initialize(); 142 createSectionToolbar(section, toolkit); 143 } 144 145 149 private void createSectionToolbar(Section section, FormToolkit toolkit) { 150 151 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 152 ToolBar toolbar = toolBarManager.createControl(section); 153 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 154 toolbar.setCursor(handCursor); 155 toolbar.addDisposeListener(new DisposeListener() { 157 public void widgetDisposed(DisposeEvent e) { 158 if ((handCursor != null) && 159 (handCursor.isDisposed() == false)) { 160 handCursor.dispose(); 161 } 162 } 163 }); 164 fSortAction = new SortAction( 166 getStructuredViewerPart().getViewer(), 167 PDEUIMessages.FeatureEditor_RequiresSection_sortAlpha, 168 ListUtil.NAME_COMPARATOR, 169 null, 170 null); 171 172 toolBarManager.add(fSortAction); 173 174 toolBarManager.update(true); 175 176 section.setTextClient(toolbar); 177 } 178 179 protected void buttonSelected(int index) { 180 switch (index) { 181 case 0: 182 handleNewPlugin(); 183 break; 184 case 1: 185 handleNewFeature(); 186 break; 187 case 2: 188 break; 189 case 3: 190 recomputeImports(); 191 break; 192 } 193 } 194 195 private void handleNewPlugin() { 196 BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(), 197 new Runnable () { 198 public void run() { 199 IPluginModelBase[] allModels = PluginRegistry.getActiveModels(); 200 ArrayList newModels = new ArrayList (); 201 for (int i = 0; i < allModels.length; i++) { 202 if (canAdd(allModels[i])) 203 newModels.add(allModels[i]); 204 } 205 IPluginModelBase[] candidateModels = (IPluginModelBase[]) newModels 206 .toArray(new IPluginModelBase[newModels.size()]); 207 PluginSelectionDialog dialog = new PluginSelectionDialog( 208 fPluginViewer.getTable().getShell(), 209 candidateModels, true); 210 if (dialog.open() == Window.OK) { 211 Object [] models = dialog.getResult(); 212 try { 213 doAdd(models); 214 } catch (CoreException e) { 215 PDEPlugin.log(e); 216 } 217 } 218 } 219 }); 220 } 221 222 private boolean canAdd(IPluginModelBase candidate) { 223 IPluginBase plugin = candidate.getPluginBase(); 224 if (candidate.isFragmentModel()) 225 return false; 226 227 IFeatureModel model = (IFeatureModel) getPage().getModel(); 228 IFeatureImport[] imports = model.getFeature().getImports(); 229 230 for (int i = 0; i < imports.length; i++) { 231 IFeatureImport fimport = imports[i]; 232 if (plugin.getId().equals(fimport.getId())) 233 return false; 234 } 235 IFeaturePlugin[] fplugins = model.getFeature().getPlugins(); 237 for (int i = 0; i < fplugins.length; i++) { 238 IFeaturePlugin fplugin = fplugins[i]; 239 if (plugin.getId().equals(fplugin.getId())) 240 return false; 241 } 242 return true; 243 } 244 245 private void handleNewFeature() { 246 BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(), 247 new Runnable () { 248 public void run() { 249 IFeatureModel[] allModels = PDECore.getDefault() 250 .getFeatureModelManager().getModels(); 251 ArrayList newModels = new ArrayList (); 252 for (int i = 0; i < allModels.length; i++) { 253 if (canAdd(allModels[i])) 254 newModels.add(allModels[i]); 255 } 256 IFeatureModel[] candidateModels = (IFeatureModel[]) newModels 257 .toArray(new IFeatureModel[newModels.size()]); 258 FeatureSelectionDialog dialog = new FeatureSelectionDialog( 259 fPluginViewer.getTable().getShell(), 260 candidateModels, true); 261 if (dialog.open() == Window.OK) { 262 Object [] models = dialog.getResult(); 263 try { 264 doAdd(models); 265 } catch (CoreException e) { 266 PDECore.log(e); 267 } 268 } 269 } 270 }); 271 } 272 273 private void doAdd(Object [] candidates) throws CoreException { 274 IFeatureModel model = (IFeatureModel) getPage().getModel(); 275 IFeature feature = model.getFeature(); 276 IFeatureImport[] added = new IFeatureImport[candidates.length]; 277 for (int i = 0; i < candidates.length; i++) { 278 FeatureImport fimport = (FeatureImport) model.getFactory() 279 .createImport(); 280 if (candidates[i] instanceof IFeatureModel) { 281 IFeatureModel candidate = (IFeatureModel) candidates[i]; 282 fimport.loadFrom(candidate.getFeature()); 283 } else { IPluginModelBase candidate = (IPluginModelBase) candidates[i]; 285 IPluginBase pluginBase = candidate.getPluginBase(); 286 fimport.setId(pluginBase.getId()); 287 } 288 added[i] = fimport; 289 } 290 feature.addImports(added); 291 } 292 293 private boolean canAdd(IFeatureModel candidate) { 294 IFeature cfeature = candidate.getFeature(); 295 296 IFeatureModel model = (IFeatureModel) getPage().getModel(); 297 IFeature feature = model.getFeature(); 298 299 if (cfeature.getId().equals(feature.getId()) 300 && cfeature.getVersion().equals(feature.getVersion())) { 301 return false; 302 } 303 304 IFeatureImport[] features = feature.getImports(); 305 306 for (int i = 0; i < features.length; i++) { 307 if (features[i].getId().equals(cfeature.getId()) 308 && features[i].getVersion().equals(cfeature.getVersion())) 309 return false; 310 } 311 return true; 312 } 313 314 private void handleDelete() { 315 IFeatureModel model = (IFeatureModel) getPage().getModel(); 316 if (!model.isEditable()) { 317 return; 318 } 319 IFeature feature = model.getFeature(); 320 IStructuredSelection selection = (IStructuredSelection) fPluginViewer 321 .getSelection(); 322 if (selection.isEmpty()) 323 return; 324 325 try { 326 IFeatureImport[] deleted = new IFeatureImport[selection.size()]; 327 int i = 0; 328 for (Iterator iter = selection.iterator(); iter.hasNext();) { 329 IFeatureImport iimport = (IFeatureImport) iter.next(); 330 deleted[i++] = iimport; 331 } 332 feature.removeImports(deleted); 333 } catch (CoreException e) { 334 PDEPlugin.logException(e); 335 } 336 } 337 338 private void handleSelectAll() { 339 IStructuredContentProvider provider = (IStructuredContentProvider) fPluginViewer 340 .getContentProvider(); 341 Object [] elements = provider.getElements(fPluginViewer.getInput()); 342 StructuredSelection ssel = new StructuredSelection(elements); 343 fPluginViewer.setSelection(ssel); 344 } 345 346 public void dispose() { 347 IFeatureModel model = (IFeatureModel) getPage().getModel(); 348 if (model != null) 349 model.removeModelChangedListener(this); 350 PDECore.getDefault().getModelManager().removePluginModelListener(this); 351 PDECore.getDefault().getFeatureModelManager().removeFeatureModelListener(this); 352 super.dispose(); 353 } 354 355 public boolean doGlobalAction(String actionId) { 356 if (actionId.equals(ActionFactory.DELETE.getId())) { 357 BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(), 358 new Runnable () { 359 public void run() { 360 handleDelete(); 361 } 362 }); 363 return true; 364 } 365 if (actionId.equals(ActionFactory.CUT.getId())) { 366 handleDelete(); 369 return false; 370 } 371 if (actionId.equals(ActionFactory.PASTE.getId())) { 372 doPaste(); 373 return true; 374 } 375 if (actionId.equals(ActionFactory.SELECT_ALL.getId())) { 376 BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(), 377 new Runnable () { 378 public void run() { 379 handleSelectAll(); 380 } 381 }); 382 return true; 383 } 384 return false; 385 } 386 387 public void expandTo(Object object) { 388 if (object instanceof IFeatureImport) { 389 StructuredSelection ssel = new StructuredSelection(object); 390 fPluginViewer.setSelection(ssel); 391 } 392 } 393 394 protected void fillContextMenu(IMenuManager manager) { 395 IStructuredSelection selection = (StructuredSelection) fPluginViewer 396 .getSelection(); 397 if (!selection.isEmpty()) { 398 manager.add(fDeleteAction); 399 manager.add(new Separator()); 400 } 401 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 402 manager); 403 } 404 405 protected void selectionChanged(IStructuredSelection selection) { 406 getPage().getPDEEditor().setSelection(selection); 407 getPage().getManagedForm().fireSelectionChanged(this, selection); 408 } 409 410 public void initialize() { 411 IFeatureModel model = (IFeatureModel) getPage().getModel(); 412 refresh(); 413 if (model.isEditable() == false) { 414 getTablePart().setButtonEnabled(0, false); 415 getTablePart().setButtonEnabled(1, false); 416 getTablePart().setButtonEnabled(3, false); 417 fSyncButton.setEnabled(false); 418 } 419 model.addModelChangedListener(this); 420 PDECore.getDefault().getModelManager().addPluginModelListener(this); 421 PDECore.getDefault().getFeatureModelManager().addFeatureModelListener( 422 this); 423 } 424 425 public void modelChanged(IModelChangedEvent e) { 426 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 427 markStale(); 428 return; 429 } else if (e.getChangeType() == IModelChangedEvent.CHANGE) { 430 Object obj = e.getChangedObjects()[0]; 431 if (obj instanceof IFeatureImport) { 432 fPluginViewer.refresh(obj); 433 } 434 } else { 435 Object obj = e.getChangedObjects()[0]; 436 if (obj instanceof IFeatureImport) { 437 if (e.getChangeType() == IModelChangedEvent.INSERT) { 438 fPluginViewer.add(e.getChangedObjects()); 439 if (e.getChangedObjects().length > 0) { 440 fPluginViewer.setSelection(new StructuredSelection(e 441 .getChangedObjects()[0])); 442 } 443 } else 444 fPluginViewer.remove(e.getChangedObjects()); 445 } else if (obj instanceof IFeaturePlugin) { 446 if (fSyncButton.getSelection()) { 447 recomputeImports(); 448 } 449 } 450 } 451 } 452 453 private void recomputeImports() { 454 IFeatureModel model = (IFeatureModel) getPage().getModel(); 455 IFeature feature = model.getFeature(); 456 try { 457 feature.computeImports(); 458 } catch (CoreException e) { 459 PDEPlugin.logException(e); 460 } 461 } 462 463 public void modelsChanged(final PluginModelDelta delta) { 464 getSection().getDisplay().asyncExec(new Runnable () { 465 public void run() { 466 if (getSection().isDisposed()) { 467 return; 468 } 469 ModelEntry[] added = delta.getAddedEntries(); 470 ModelEntry[] removed = delta.getRemovedEntries(); 471 ModelEntry[] changed = delta.getChangedEntries(); 472 if (hasModels(added) || hasModels(removed) 473 || hasModels(changed)) 474 markStale(); 475 } 476 }); 477 } 478 479 private boolean hasModels(ModelEntry[] entries) { 480 if (entries == null) 481 return false; 482 return entries.length > 0; 483 } 484 485 public void modelsChanged(final IFeatureModelDelta delta) { 486 getSection().getDisplay().asyncExec(new Runnable () { 487 public void run() { 488 if (getSection().isDisposed()) { 489 return; 490 } 491 IFeatureModel[] added = delta.getAdded(); 492 IFeatureModel[] removed = delta.getRemoved(); 493 IFeatureModel[] changed = delta.getChanged(); 494 if (hasModels(added) || hasModels(removed) 495 || hasModels(changed)) 496 markStale(); 497 } 498 }); 499 } 500 501 private boolean hasModels(IFeatureModel[] models) { 502 if (models == null) 503 return false; 504 IFeatureModel thisModel = (IFeatureModel) getPage().getModel(); 505 for (int i = 0; i < models.length; i++) { 506 if (models[i] != thisModel) { 507 return true; 508 } 509 } 510 return false; 511 } 512 513 public void setFocus() { 514 if (fPluginViewer != null) 515 fPluginViewer.getTable().setFocus(); 516 } 517 518 public void refresh() { 519 IFeatureModel model = (IFeatureModel) getPage().getModel(); 520 IFeature feature = model.getFeature(); 521 fPluginViewer.setInput(feature); 522 super.refresh(); 523 } 524 525 528 public boolean canPaste(Clipboard clipboard) { 529 Object [] objects = (Object []) clipboard.getContents(ModelDataTransfer 530 .getInstance()); 531 if (objects != null && objects.length > 0) { 532 return canPaste(null, objects); 533 } 534 return false; 535 } 536 537 541 protected boolean canPaste(Object target, Object [] objects) { 542 for (int i = 0; i < objects.length; i++) { 543 if (!(objects[i] instanceof FeatureImport)) 544 return false; 545 } 546 return true; 547 } 548 549 552 protected void doPaste() { 553 Clipboard clipboard = getPage().getPDEEditor().getClipboard(); 554 Object [] objects = (Object []) clipboard.getContents(ModelDataTransfer 555 .getInstance()); 556 if (objects != null && canPaste(null, objects)) 557 doPaste(null, objects); 558 } 559 560 564 protected void doPaste(Object target, Object [] objects) { 565 IFeatureModel model = (IFeatureModel) getPage().getModel(); 566 IFeature feature = model.getFeature(); 567 if (!model.isEditable()) { 568 return; 569 } 570 571 IFeatureImport[] imports = new IFeatureImport[objects.length]; 572 try { 573 for (int i = 0; i < objects.length; i++) { 574 FeatureImport fImport = (FeatureImport) objects[i]; 575 fImport.setModel(model); 576 fImport.setParent(feature); 577 imports[i] = fImport; 578 } 579 feature.addImports(imports); 580 } catch (CoreException e) { 581 PDEPlugin.logException(e); 582 } 583 584 } 585 586 void fireSelection() { 587 ISelection sel = fPluginViewer.getSelection(); 588 if (!sel.isEmpty()) { 589 fPluginViewer.setSelection(fPluginViewer.getSelection()); 590 } else if (fPluginViewer.getElementAt(0) != null) { 591 fPluginViewer.setSelection(new StructuredSelection(fPluginViewer 592 .getElementAt(0))); 593 } 594 } 595 596 protected boolean createCount() { return true; } 597 598 } 599 | Popular Tags |