1 11 package org.eclipse.ui.internal.navigator.resources.actions; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 import org.eclipse.jface.window.IShellProvider; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.IActionBars; 24 import org.eclipse.ui.actions.ActionContext; 25 import org.eclipse.ui.actions.ActionFactory; 26 import org.eclipse.ui.dialogs.PropertyDialogAction; 27 import org.eclipse.ui.navigator.CommonActionProvider; 28 import org.eclipse.ui.navigator.ICommonActionExtensionSite; 29 import org.eclipse.ui.navigator.ICommonMenuConstants; 30 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 31 32 38 public class PropertiesActionProvider extends CommonActionProvider { 39 40 private PropertyDialogAction propertiesAction; 41 private ISelectionProvider delegateSelectionProvider; 42 43 public void init(final ICommonActionExtensionSite aSite) { 44 45 delegateSelectionProvider = new DelegateSelectionProvider( aSite.getViewSite().getSelectionProvider()); 46 propertiesAction = new PropertyDialogAction(new IShellProvider() { 47 public Shell getShell() { 48 return aSite.getViewSite().getShell(); 49 } 50 },delegateSelectionProvider); 51 propertiesAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PROPERTIES); 52 } 53 54 59 public void fillContextMenu(IMenuManager menu) { 60 super.fillContextMenu(menu); 61 menu.appendToGroup(ICommonMenuConstants.GROUP_PROPERTIES, 62 propertiesAction); 63 } 64 65 68 public void fillActionBars(IActionBars actionBars) { 69 super.fillActionBars(actionBars); 70 71 actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), 72 propertiesAction); 73 } 74 75 80 public void setContext(ActionContext context) { 81 super.setContext(context); 82 83 propertiesAction.selectionChanged(delegateSelectionProvider 84 .getSelection()); 85 86 } 87 88 private class DelegateIAdaptable implements IAdaptable { 89 90 private Object delegate; 91 92 private DelegateIAdaptable(Object o) { 93 delegate = o; 94 } 95 96 101 public Object getAdapter(Class adapter) { 102 if (adapter.isInstance(delegate)) { 103 return delegate; 104 } 105 return Platform.getAdapterManager().getAdapter(delegate, adapter); 106 } 107 } 108 109 private class DelegateSelectionProvider implements ISelectionProvider { 110 111 private ISelectionProvider delegate; 112 113 private DelegateSelectionProvider(ISelectionProvider s) { 114 delegate = s; 115 } 116 117 122 public void addSelectionChangedListener( 123 ISelectionChangedListener listener) { 124 delegate.addSelectionChangedListener(listener); 125 126 } 127 128 133 public ISelection getSelection() { 134 if (delegate.getSelection() instanceof IStructuredSelection) { 135 IStructuredSelection sSel = (IStructuredSelection) delegate 136 .getSelection(); 137 if (sSel.getFirstElement() instanceof IAdaptable) { 138 return sSel; 139 } 140 141 return new StructuredSelection(new DelegateIAdaptable(sSel 142 .getFirstElement())); 143 } 144 return delegate.getSelection(); 145 } 146 147 152 public void removeSelectionChangedListener( 153 ISelectionChangedListener listener) { 154 delegate.removeSelectionChangedListener(listener); 155 156 } 157 158 163 public void setSelection(ISelection selection) { 164 delegate.setSelection(selection); 165 166 } 167 168 } 169 170 } 171 | Popular Tags |