1 11 package org.eclipse.pde.internal.ui.views.dependencies; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.action.IMenuListener; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.action.IStatusLineManager; 18 import org.eclipse.jface.action.IToolBarManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.action.Separator; 21 import org.eclipse.jface.dialogs.IDialogSettings; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 import org.eclipse.jface.viewers.DoubleClickEvent; 25 import org.eclipse.jface.viewers.IContentProvider; 26 import org.eclipse.jface.viewers.IDoubleClickListener; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.StructuredViewer; 30 import org.eclipse.jface.viewers.Viewer; 31 import org.eclipse.jface.viewers.ViewerFilter; 32 import org.eclipse.jface.window.Window; 33 import org.eclipse.osgi.service.resolver.BaseDescription; 34 import org.eclipse.osgi.service.resolver.BundleDescription; 35 import org.eclipse.osgi.service.resolver.BundleSpecification; 36 import org.eclipse.osgi.service.resolver.ExportPackageDescription; 37 import org.eclipse.osgi.service.resolver.ImportPackageSpecification; 38 import org.eclipse.osgi.util.NLS; 39 import org.eclipse.pde.core.plugin.IPluginBase; 40 import org.eclipse.pde.core.plugin.IPluginImport; 41 import org.eclipse.pde.core.plugin.IPluginModelBase; 42 import org.eclipse.pde.core.plugin.IPluginObject; 43 import org.eclipse.pde.core.plugin.ISharedPluginModel; 44 import org.eclipse.pde.core.plugin.PluginRegistry; 45 import org.eclipse.pde.internal.ui.IPreferenceConstants; 46 import org.eclipse.pde.internal.ui.PDEPlugin; 47 import org.eclipse.pde.internal.ui.PDEUIMessages; 48 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor; 49 import org.eclipse.pde.internal.ui.refactoring.RenamePluginAction; 50 import org.eclipse.pde.internal.ui.search.dependencies.DependencyExtentAction; 51 import org.eclipse.pde.internal.ui.search.dependencies.UnusedDependenciesAction; 52 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog; 53 import org.eclipse.swt.widgets.Composite; 54 import org.eclipse.swt.widgets.Control; 55 import org.eclipse.swt.widgets.Menu; 56 import org.eclipse.ui.IActionBars; 57 import org.eclipse.ui.IWorkbenchActionConstants; 58 import org.eclipse.ui.part.Page; 59 60 public abstract class DependenciesViewPage extends Page { 61 class FocusOnSelectionAction extends Action { 62 public void run() { 63 handleFocusOn(getSelectedObject()); 64 } 65 66 public void update(Object object) { 67 setEnabled(object != null); 68 String name = ((LabelProvider) fViewer.getLabelProvider()) 69 .getText(object); 70 setText(NLS.bind(PDEUIMessages.DependenciesViewPage_focusOnSelection, name)); 71 } 72 } 73 74 private Action fFocusOnAction; 75 76 private FocusOnSelectionAction fFocusOnSelectionAction; 77 78 private Action fOpenAction; 79 80 protected RenamePluginAction fRefactorAction; 81 82 private IPropertyChangeListener fPropertyListener; 83 84 private DependenciesView fView; 85 86 protected StructuredViewer fViewer; 87 88 protected IContentProvider fContentProvider; 89 90 private Action fHideFragmentFilterAction; 91 92 protected Action fHideOptionalFilterAction; 93 94 private FragmentFilter fHideFragmentFilter = new FragmentFilter(); 95 96 private static final String HIDE_FRAGMENTS = "hideFrags"; 98 private static final String HIDE_OPTIONAL = "hideOptional"; 100 class FragmentFilter extends ViewerFilter { 101 102 public boolean select(Viewer v, Object parent, Object element) { 103 BundleDescription desc = null; 104 if (element instanceof BundleSpecification) { 105 BaseDescription supplier = ((BundleSpecification)element).getSupplier(); 106 if (supplier instanceof BundleDescription) 107 desc = (BundleDescription) supplier; 108 } else if (element instanceof BundleDescription) { 109 desc = (BundleDescription)element; 110 } else if (element instanceof ImportPackageSpecification) { 111 BaseDescription export = ((ImportPackageSpecification)element).getSupplier(); 112 desc = ((ExportPackageDescription)export).getExporter(); 113 } 114 if (desc != null) { 115 return desc.getHost() == null; 116 } 117 return true; 118 } 119 } 120 121 124 public DependenciesViewPage(DependenciesView view, IContentProvider contentProvider) { 125 this.fView = view; 126 this.fContentProvider = contentProvider; 127 fPropertyListener = new IPropertyChangeListener() { 128 public void propertyChange(PropertyChangeEvent event) { 129 String property = event.getProperty(); 130 if (property.equals(IPreferenceConstants.PROP_SHOW_OBJECTS)) { 131 fViewer.refresh(); 132 } 133 } 134 }; 135 } 136 137 142 public void createControl(Composite parent) { 143 fViewer = createViewer(parent); 144 fViewer.setComparator(DependenciesViewComparator.getViewerComparator()); 145 PDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener( 146 fPropertyListener); 147 getSite().setSelectionProvider(fViewer); 148 } 149 150 protected abstract StructuredViewer createViewer(Composite parent); 151 152 public void dispose() { 153 PDEPlugin.getDefault().getPreferenceStore() 154 .removePropertyChangeListener(fPropertyListener); 155 super.dispose(); 156 } 157 158 private void fillContextMenu(IMenuManager manager) { 159 IStructuredSelection selection = (IStructuredSelection) fViewer 160 .getSelection(); 161 162 if (selection.size() == 1) { 163 manager.add(fOpenAction); 164 manager.add(new Separator()); 165 } 166 fFocusOnSelectionAction.update(getSelectedObject()); 167 if (fFocusOnSelectionAction.isEnabled()) 168 manager.add(fFocusOnSelectionAction); 169 manager.add(fFocusOnAction); 170 Object selectionElement = selection.getFirstElement(); 171 172 manager.add(new Separator()); 173 if (selection.size() == 1 && !fView.isShowingCallers()) { 175 String id = null; 176 if (selectionElement instanceof BundleSpecification) { 177 id = ((BundleSpecification)selectionElement).getName(); 178 } else if (selectionElement instanceof BundleDescription) { 179 id = ((BundleDescription)selectionElement).getSymbolicName(); 180 } 181 if (id != null && PluginRegistry.findModel(id) != null) { 183 Object input = fViewer.getInput(); 184 if (input instanceof IPluginBase) 185 input = ((IPluginBase)input).getModel(); 186 if (input instanceof IPluginModelBase) { 187 IPluginModelBase base = (IPluginModelBase)input; 188 IResource res = (base == null) ? null : base.getUnderlyingResource(); 189 if (res != null) 190 manager.add(new DependencyExtentAction(res 191 .getProject(), id)); 192 } 193 } 194 } 195 ISharedPluginModel model = null; 197 if (selectionElement instanceof BundleSpecification) { 198 model = PluginRegistry.findModel(((BundleSpecification)selectionElement).getName()); 199 } else if (selectionElement instanceof BundleDescription) { 200 model = PluginRegistry.findModel((BundleDescription)selectionElement); 201 } else if (selectionElement instanceof IPluginBase) { 202 model = ((IPluginBase)selectionElement).getModel(); 204 } 205 if (model != null && model.getUnderlyingResource() != null) { 206 manager.add(new UnusedDependenciesAction( 207 (IPluginModelBase) model, true)); 208 } 209 if (enableRename(selection)) { 210 manager.add(new Separator()); 211 manager.add(fRefactorAction); 212 } 213 214 manager.add(new Separator()); 215 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 216 } 217 218 223 public Control getControl() { 224 return fViewer.getControl(); 225 } 226 227 private Object getSelectedObject() { 228 IStructuredSelection selection = getSelection(); 229 if (selection.isEmpty() || selection.size() != 1) 230 return null; 231 return selection.getFirstElement(); 232 } 233 234 protected IStructuredSelection getSelection() { 235 return (IStructuredSelection) fViewer.getSelection(); 236 } 237 238 protected void setSelection(IStructuredSelection selection) { 239 if (selection != null && !selection.isEmpty()) 240 fViewer.setSelection(selection, true); 241 } 242 243 246 public DependenciesView getView() { 247 return fView; 248 } 249 250 private void handleDoubleClick() { 251 Object obj = getSelectedObject(); 252 BundleDescription desc = null; 253 if (obj instanceof BundleSpecification) { 254 desc = (BundleDescription)((BundleSpecification)obj).getSupplier(); 255 } else if (obj instanceof BundleDescription) { 256 desc = (BundleDescription)obj; 257 258 } else if (obj instanceof IPluginBase) { 259 desc = ((IPluginModelBase)((IPluginBase)obj).getModel()).getBundleDescription(); 261 } else if (obj instanceof ImportPackageSpecification) { 262 BaseDescription export = ((ImportPackageSpecification)obj).getSupplier(); 263 desc = ((ExportPackageDescription)export).getExporter(); 264 } 265 if (desc != null) 266 ManifestEditor.openPluginEditor(desc.getSymbolicName()); 267 } 268 269 private void handleFocusOn() { 270 PluginSelectionDialog dialog = new PluginSelectionDialog(fViewer 271 .getControl().getShell(), true, false); 272 dialog.create(); 273 if (dialog.open() == Window.OK) { 274 handleFocusOn(dialog.getFirstResult()); 275 } 276 } 277 278 private void handleFocusOn(Object newFocus) { 279 if (newFocus instanceof IPluginModelBase) { 280 fView.openTo(newFocus); 281 } 282 if (newFocus instanceof IPluginBase) { 283 fView.openTo(((IPluginBase) newFocus).getModel()); 284 } 285 if (newFocus instanceof IPluginImport) { 286 IPluginImport pluginImport = ((IPluginImport) newFocus); 287 String id = pluginImport.getId(); 288 IPluginModelBase model = PluginRegistry.findModel(id); 289 if (model != null) { 290 fView.openTo(model); 291 } else { 292 fView.openTo(null); 293 } 294 } 295 BundleDescription desc = null; 296 if (newFocus instanceof BundleSpecification) { 297 desc = (BundleDescription)((BundleSpecification)newFocus).getSupplier(); 298 if (desc == null) 299 fView.openTo(null); 300 } 301 if (newFocus instanceof BundleDescription) { 302 desc = (BundleDescription)newFocus; 303 } 304 if (desc != null) 305 fView.openTo(PluginRegistry.findModel(desc.getSymbolicName())); 306 } 307 308 private void hookContextMenu() { 309 MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); 311 menuMgr.addMenuListener(new IMenuListener() { 312 public void menuAboutToShow(IMenuManager manager) { 313 DependenciesViewPage.this.fillContextMenu(manager); 314 } 315 }); 316 Menu menu = menuMgr.createContextMenu(fViewer.getControl()); 317 fViewer.getControl().setMenu(menu); 318 319 getSite() 320 .registerContextMenu(fView.getSite().getId(), menuMgr, fViewer); 321 } 322 323 private void hookDoubleClickAction() { 324 fViewer.addDoubleClickListener(new IDoubleClickListener() { 325 public void doubleClick(DoubleClickEvent event) { 326 handleDoubleClick(); 327 } 328 }); 329 } 330 331 private void makeActions() { 332 fOpenAction = new Action() { 333 public void run() { 334 handleDoubleClick(); 335 } 336 }; 337 fOpenAction.setText(PDEUIMessages.DependenciesView_open); 338 339 fFocusOnSelectionAction = new FocusOnSelectionAction(); 340 341 fFocusOnAction = new Action() { 342 public void run() { 343 handleFocusOn(); 344 } 345 }; 346 fFocusOnAction.setText(PDEUIMessages.DependenciesViewPage_focusOn); 347 348 fRefactorAction = new RenamePluginAction(); 349 350 fHideFragmentFilterAction = new Action() { 351 public void run() { 352 boolean checked = fHideFragmentFilterAction.isChecked(); 353 if (checked) 354 fViewer.removeFilter(fHideFragmentFilter); 355 else 356 fViewer.addFilter(fHideFragmentFilter); 357 getSettings().put(HIDE_FRAGMENTS, !checked); 358 } 359 }; 360 fHideFragmentFilterAction.setText(PDEUIMessages.DependenciesViewPage_showFragments); 361 362 fHideOptionalFilterAction = new Action() { 363 public void run() { 364 boolean checked = isChecked(); 365 handleShowOptional(isChecked(), true); 366 getSettings().put(HIDE_OPTIONAL, !checked); 367 } 368 }; 369 fHideOptionalFilterAction.setText(PDEUIMessages.DependenciesViewPage_showOptional); 370 } 371 372 protected abstract void handleShowOptional(boolean checked, boolean refreshIfNecessary); 373 374 protected abstract boolean isShowingOptional(); 375 376 383 public void makeContributions(IMenuManager menuManager, 384 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 385 super.makeContributions(menuManager, toolBarManager, statusLineManager); 386 makeActions(); 387 hookContextMenu(); 388 hookDoubleClickAction(); 389 contributeToActionBars(getSite().getActionBars()); 390 } 391 392 397 public void setFocus() { 398 if (fViewer != null) { 399 Control c = fViewer.getControl(); 400 if (!c.isFocusControl()) { 401 c.setFocus(); 402 } 403 } 404 } 405 406 public void setInput(Object object) { 407 if (object != fViewer.getInput()) 408 fViewer.setInput(object); 409 } 410 411 protected boolean enableRename(IStructuredSelection selection) { 413 if (selection.size() == 1) { 414 Object selectionElement = selection.getFirstElement(); 415 IPluginModelBase base = null; 416 if (selectionElement instanceof IPluginImport) { 417 String id = ((IPluginImport)selectionElement).getId(); 418 base = PluginRegistry.findModel(id); 419 } else if (selectionElement instanceof IPluginObject) { 420 base = (IPluginModelBase)((IPluginObject) selectionElement).getModel(); 421 } else if (selectionElement instanceof BundleSpecification) { 422 BundleDescription desc = (BundleDescription)((BundleSpecification)selectionElement).getSupplier(); 423 if (desc != null) 424 base = PluginRegistry.findModel(desc); 425 } else if (selectionElement instanceof BundleDescription) { 426 base = PluginRegistry.findModel((BundleDescription)selectionElement); 427 } 428 if (base != null && base.getUnderlyingResource() != null) { 429 fRefactorAction.setPlugin(base); 430 return true; 431 } 432 } 433 return false; 434 } 435 436 public void setActive(boolean active) { 437 if (active) { 438 if (fView.isShowingCallers()) { 441 fHideOptionalFilterAction.setChecked(true); 443 fHideOptionalFilterAction.setEnabled(false); 444 } else { 445 fHideOptionalFilterAction.setEnabled(true); 446 fHideOptionalFilterAction.setChecked(!getSettings().getBoolean(HIDE_OPTIONAL)); 447 } 448 fHideFragmentFilterAction.setChecked(!getSettings().getBoolean(HIDE_FRAGMENTS)); 449 450 boolean showFragments = fHideFragmentFilterAction.isChecked(); 452 boolean containsFragments = true; 453 ViewerFilter[] filters = fViewer.getFilters(); 454 for (int i = 0; i < filters.length; i++) { 455 if (filters[i].equals(fHideFragmentFilter)) { 456 containsFragments = false; 457 break; 458 } 459 } 460 if (showFragments != containsFragments) 461 if (showFragments) 462 fViewer.removeFilter(fHideFragmentFilter); 463 else 464 fViewer.addFilter(fHideFragmentFilter); 465 466 if (fHideOptionalFilterAction.isChecked() != isShowingOptional()) 468 handleShowOptional(fHideOptionalFilterAction.isChecked(), false); 469 } 470 471 if (fContentProvider instanceof DependenciesViewPageContentProvider) { 472 if (active) { 473 ((DependenciesViewPageContentProvider)fContentProvider).attachModelListener(); 475 fViewer.refresh(); 476 } else 477 ((DependenciesViewPageContentProvider)fContentProvider).removeModelListener(); 480 } 481 } 482 483 private void contributeToActionBars(IActionBars actionBars) { 484 contributeToDropDownMenu(actionBars.getMenuManager()); 485 } 486 487 private void contributeToDropDownMenu(IMenuManager manager) { 488 manager.add(fHideFragmentFilterAction); 489 manager.add(fHideOptionalFilterAction); 490 IDialogSettings settings = getSettings(); 491 boolean hideFragments = settings.getBoolean(HIDE_FRAGMENTS); 492 boolean hideOptional = settings.getBoolean(HIDE_OPTIONAL); 493 fHideFragmentFilterAction.setChecked(!hideFragments); 494 fHideOptionalFilterAction.setChecked(!hideOptional); 495 } 497 498 private IDialogSettings getSettings() { 499 IDialogSettings master = PDEPlugin.getDefault().getDialogSettings(); 500 IDialogSettings section = master.getSection("dependenciesView"); if (section == null) { 502 section = master.addNewSection("dependenciesView"); } 504 return section; 505 } 506 507 } 508 | Popular Tags |