1 12 13 package org.eclipse.ui.views.properties; 14 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.help.IContext; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.IStatusLineManager; 19 import org.eclipse.jface.action.IToolBarManager; 20 import org.eclipse.jface.action.MenuManager; 21 import org.eclipse.jface.action.Separator; 22 import org.eclipse.jface.viewers.CellEditor; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.SelectionChangedEvent; 27 import org.eclipse.swt.dnd.Clipboard; 28 import org.eclipse.swt.dnd.DND; 29 import org.eclipse.swt.dnd.DragSource; 30 import org.eclipse.swt.dnd.DragSourceAdapter; 31 import org.eclipse.swt.dnd.DragSourceEvent; 32 import org.eclipse.swt.dnd.DragSourceListener; 33 import org.eclipse.swt.dnd.TextTransfer; 34 import org.eclipse.swt.dnd.Transfer; 35 import org.eclipse.swt.events.HelpEvent; 36 import org.eclipse.swt.events.HelpListener; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Menu; 40 import org.eclipse.swt.widgets.Shell; 41 import org.eclipse.ui.IActionBars; 42 import org.eclipse.ui.IPartListener; 43 import org.eclipse.ui.ISaveablePart; 44 import org.eclipse.ui.ISharedImages; 45 import org.eclipse.ui.IWorkbenchPart; 46 import org.eclipse.ui.PlatformUI; 47 import org.eclipse.ui.help.IContextComputer; 48 import org.eclipse.ui.help.IWorkbenchHelpSystem; 49 import org.eclipse.ui.internal.views.ViewsPlugin; 50 import org.eclipse.ui.internal.views.properties.PropertiesMessages; 51 import org.eclipse.ui.part.CellEditorActionHandler; 52 import org.eclipse.ui.part.Page; 53 54 81 public class PropertySheetPage extends Page implements IPropertySheetPage, IAdaptable { 82 86 public static final String HELP_CONTEXT_PROPERTY_SHEET_PAGE = "org.eclipse.ui.property_sheet_page_help_context"; 88 private PropertySheetViewer viewer; 89 90 private PropertySheetSorter sorter; 91 92 private IPropertySheetEntry rootEntry; 93 94 private IPropertySourceProvider provider; 95 96 private DefaultsAction defaultsAction; 97 98 private FilterAction filterAction; 99 100 private CategoriesAction categoriesAction; 101 102 private CopyPropertyAction copyAction; 103 104 private ICellEditorActivationListener cellEditorActivationListener; 105 106 private CellEditorActionHandler cellEditorActionHandler; 107 108 private Clipboard clipboard; 109 110 private IWorkbenchPart sourcePart; 111 112 118 private class PartListener implements IPartListener { 119 public void partActivated(IWorkbenchPart part) { 120 } 121 122 public void partBroughtToTop(IWorkbenchPart part) { 123 } 124 125 public void partClosed(IWorkbenchPart part) { 126 if (sourcePart == part) { 127 sourcePart = null; 128 if (viewer != null && !viewer.getControl().isDisposed()) { 129 viewer.setInput(new Object [0]); 130 } 131 } 132 } 133 134 public void partDeactivated(IWorkbenchPart part) { 135 } 136 137 public void partOpened(IWorkbenchPart part) { 138 } 139 } 140 141 private PartListener partListener = new PartListener(); 142 143 146 public PropertySheetPage() { 147 super(); 148 } 149 150 153 public void createControl(Composite parent) { 154 viewer = new PropertySheetViewer(parent); 156 viewer.setSorter(sorter); 157 158 if (rootEntry == null) { 160 PropertySheetEntry root = new PropertySheetEntry(); 162 if (provider != null) { 163 root.setPropertySourceProvider(provider); 165 } 166 rootEntry = root; 167 } 168 viewer.setRootEntry(rootEntry); 169 viewer.addActivationListener(getCellEditorActivationListener()); 170 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 172 public void selectionChanged(SelectionChangedEvent event) { 173 handleEntrySelection(event.getSelection()); 174 } 175 }); 176 initDragAndDrop(); 177 makeActions(); 178 179 MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.add(copyAction); 182 menuMgr.add(new Separator()); 183 menuMgr.add(defaultsAction); 184 Menu menu = menuMgr.createContextMenu(viewer.getControl()); 185 viewer.getControl().setMenu(menu); 186 187 viewer.getControl().addHelpListener(new HelpListener() { 189 192 public void helpRequested(HelpEvent e) { 193 IStructuredSelection selection = (IStructuredSelection) viewer 195 .getSelection(); 196 if (!selection.isEmpty()) { 197 IPropertySheetEntry entry = (IPropertySheetEntry) selection 198 .getFirstElement(); 199 Object helpContextId = entry.getHelpContextIds(); 200 if (helpContextId != null) { 201 if (helpContextId instanceof String ) { 202 PlatformUI.getWorkbench() 203 .getHelpSystem().displayHelp( 204 (String ) helpContextId); 205 return; 206 } 207 208 Object [] contexts = null; 214 if (helpContextId instanceof IContextComputer) { 215 contexts = ((IContextComputer) helpContextId) 217 .getLocalContexts(e); 218 } else { 219 contexts = (Object []) helpContextId; 220 } 221 IWorkbenchHelpSystem help = PlatformUI.getWorkbench().getHelpSystem(); 222 if (contexts[0] instanceof IContext) { 224 help.displayHelp((IContext) contexts[0]); 225 } else { 226 help.displayHelp((String ) contexts[0]); 227 } 228 return; 229 } 230 } 231 232 PlatformUI.getWorkbench().getHelpSystem().displayHelp(HELP_CONTEXT_PROPERTY_SHEET_PAGE); 234 } 235 }); 236 } 237 238 242 public void dispose() { 243 super.dispose(); 244 if (sourcePart != null) { 245 sourcePart.getSite().getPage().removePartListener(partListener); 246 } 247 if (rootEntry != null) { 248 rootEntry.dispose(); 249 rootEntry = null; 250 } 251 if (clipboard != null) { 252 clipboard.dispose(); 253 clipboard = null; 254 } 255 } 256 257 263 public Object getAdapter(Class adapter) { 264 if (ISaveablePart.class.equals(adapter)) { 265 return getSaveablePart(); 266 } 267 return null; 268 } 269 270 278 protected ISaveablePart getSaveablePart() { 279 if (sourcePart instanceof ISaveablePart) { 280 return (ISaveablePart) sourcePart; 281 } 282 return null; 283 } 284 285 289 private ICellEditorActivationListener getCellEditorActivationListener() { 290 if (cellEditorActivationListener == null) { 291 cellEditorActivationListener = new ICellEditorActivationListener() { 292 public void cellEditorActivated(CellEditor cellEditor) { 293 if (cellEditorActionHandler != null) { 294 cellEditorActionHandler.addCellEditor(cellEditor); 295 } 296 } 297 298 public void cellEditorDeactivated(CellEditor cellEditor) { 299 if (cellEditorActionHandler != null) { 300 cellEditorActionHandler.removeCellEditor(cellEditor); 301 } 302 } 303 }; 304 } 305 return cellEditorActivationListener; 306 } 307 308 311 public Control getControl() { 312 if (viewer == null) { 313 return null; 314 } 315 return viewer.getControl(); 316 } 317 318 323 public void handleEntrySelection(ISelection selection) { 324 if (defaultsAction != null) { 325 if (selection.isEmpty()) { 326 defaultsAction.setEnabled(false); 327 return; 328 } 329 boolean editable = viewer.getActiveCellEditor() != null; 331 defaultsAction.setEnabled(editable); 332 } 333 } 334 335 338 protected void initDragAndDrop() { 339 int operations = DND.DROP_COPY; 340 Transfer[] transferTypes = new Transfer[] { TextTransfer.getInstance() }; 341 DragSourceListener listener = new DragSourceAdapter() { 342 public void dragSetData(DragSourceEvent event) { 343 performDragSetData(event); 344 } 345 346 public void dragFinished(DragSourceEvent event) { 347 } 349 }; 350 DragSource dragSource = new DragSource( 351 viewer.getControl(), operations); 352 dragSource.setTransfer(transferTypes); 353 dragSource.addDragListener(listener); 354 } 355 356 361 void performDragSetData(DragSourceEvent event) { 362 IStructuredSelection selection = (IStructuredSelection) viewer 364 .getSelection(); 365 if (selection.isEmpty()) { 366 return; 367 } 368 IPropertySheetEntry entry = (IPropertySheetEntry) selection 370 .getFirstElement(); 371 372 StringBuffer buffer = new StringBuffer (); 374 buffer.append(entry.getDisplayName()); 375 buffer.append("\t"); buffer.append(entry.getValueAsString()); 377 378 event.data = buffer.toString(); 379 } 380 381 384 private void makeActions() { 385 ISharedImages sharedImages = PlatformUI.getWorkbench() 386 .getSharedImages(); 387 388 defaultsAction = new DefaultsAction(viewer, "defaults"); defaultsAction.setText(PropertiesMessages.Defaults_text); 391 defaultsAction.setToolTipText(PropertiesMessages.Defaults_toolTip); 392 defaultsAction 393 .setImageDescriptor(ViewsPlugin.getViewImageDescriptor("elcl16/defaults_ps.gif")); defaultsAction 395 .setDisabledImageDescriptor(ViewsPlugin.getViewImageDescriptor("dlcl16/defaults_ps.gif")); defaultsAction.setEnabled(false); 397 398 filterAction = new FilterAction(viewer, "filter"); filterAction.setText(PropertiesMessages.Filter_text); 401 filterAction.setToolTipText(PropertiesMessages.Filter_toolTip); 402 filterAction 403 .setImageDescriptor(ViewsPlugin.getViewImageDescriptor("elcl16/filter_ps.gif")); filterAction.setChecked(false); 405 406 categoriesAction = new CategoriesAction(viewer, "categories"); categoriesAction.setText(PropertiesMessages.Categories_text); 409 categoriesAction.setToolTipText(PropertiesMessages.Categories_toolTip); 410 categoriesAction 411 .setImageDescriptor(ViewsPlugin.getViewImageDescriptor("elcl16/tree_mode.gif")); categoriesAction.setChecked(true); 413 414 Shell shell = viewer.getControl().getShell(); 416 clipboard = new Clipboard(shell.getDisplay()); 417 copyAction = new CopyPropertyAction(viewer, "copy", clipboard); copyAction.setText(PropertiesMessages.CopyProperty_text); 419 copyAction.setImageDescriptor(sharedImages 420 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); 421 } 422 423 426 public void makeContributions(IMenuManager menuManager, 427 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 428 429 toolBarManager.add(categoriesAction); 431 toolBarManager.add(filterAction); 432 toolBarManager.add(defaultsAction); 433 434 menuManager.add(categoriesAction); 436 menuManager.add(filterAction); 437 438 viewer.setStatusLineManager(statusLineManager); 440 } 441 442 449 public void refresh() { 450 if (viewer == null) { 451 return; 452 } 453 viewer.setInput(viewer.getInput()); 455 } 456 457 460 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 461 if (viewer == null) { 462 return; 463 } 464 465 if (sourcePart != null) { 466 sourcePart.getSite().getPage().removePartListener(partListener); 467 sourcePart = null; 468 } 469 470 if (selection instanceof IStructuredSelection) { 472 sourcePart = part; 473 viewer.setInput(((IStructuredSelection) selection).toArray()); 474 } 475 476 if (sourcePart != null) { 477 sourcePart.getSite().getPage().addPartListener(partListener); 478 } 479 } 480 481 489 public void setActionBars(IActionBars actionBars) { 490 super.setActionBars(actionBars); 491 cellEditorActionHandler = new CellEditorActionHandler(actionBars); 492 cellEditorActionHandler.setCopyAction(copyAction); 493 } 494 495 498 public void setFocus() { 499 viewer.getControl().setFocus(); 500 } 501 502 511 public void setPropertySourceProvider(IPropertySourceProvider newProvider) { 512 provider = newProvider; 513 if (rootEntry instanceof PropertySheetEntry) { 514 ((PropertySheetEntry) rootEntry) 515 .setPropertySourceProvider(provider); 516 viewer.setRootEntry(rootEntry); 518 } 519 } 520 521 526 public void setRootEntry(IPropertySheetEntry entry) { 527 rootEntry = entry; 528 if (viewer != null) { 529 viewer.setRootEntry(rootEntry); 531 } 532 } 533 534 544 protected void setSorter(PropertySheetSorter sorter) { 545 this.sorter = sorter; 546 if (viewer != null) { 547 viewer.setSorter(sorter); 548 549 if(null != viewer.getRootEntry()) { 551 viewer.setRootEntry(rootEntry); 552 } 553 } 554 } 555 556 } 557 | Popular Tags |