1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.action.IToolBarManager; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.ISelectionChangedListener; 19 import org.eclipse.jface.viewers.ISelectionProvider; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.ui.IEditorPart; 23 import org.eclipse.ui.IViewPart; 24 import org.eclipse.ui.IWorkbenchPart; 25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 26 27 32 public class ViewerActionBuilder extends PluginActionBuilder { 33 34 35 private ISelectionProvider provider; 36 37 private IWorkbenchPart part; 38 39 42 public ViewerActionBuilder() { 43 } 44 45 48 protected ActionDescriptor createActionDescriptor( 49 IConfigurationElement element) { 50 if (part instanceof IViewPart) { 51 return new ActionDescriptor(element, ActionDescriptor.T_VIEW, part); 52 } 53 return new ActionDescriptor(element, ActionDescriptor.T_EDITOR, part); 54 } 55 56 59 protected BasicContribution createContribution() { 60 return new ViewerContribution(provider); 61 } 62 63 66 public void dispose() { 67 if (cache != null) { 68 for (int i = 0; i < cache.size(); i++) { 69 ((BasicContribution) cache.get(i)).dispose(); 70 } 71 cache = null; 72 } 73 } 74 75 78 protected boolean readElement(IConfigurationElement element) { 79 String tag = element.getName(); 80 81 if (currentContribution != null && tag.equals(IWorkbenchRegistryConstants.TAG_VISIBILITY)) { 83 ((ViewerContribution) currentContribution) 84 .setVisibilityTest(element); 85 return true; 86 } 87 88 return super.readElement(element); 89 } 90 91 101 public boolean readViewerContributions(String id, ISelectionProvider prov, 102 IWorkbenchPart part) { 103 Assert.isTrue(part instanceof IViewPart || part instanceof IEditorPart); 104 provider = prov; 105 this.part = part; 106 readContributions(id, IWorkbenchRegistryConstants.TAG_VIEWER_CONTRIBUTION, 107 IWorkbenchRegistryConstants.PL_POPUP_MENU); 108 return (cache != null); 109 } 110 111 115 private static class ViewerContribution extends BasicContribution implements ISelectionChangedListener { 116 private ISelectionProvider selProvider; 117 118 private ActionExpression visibilityTest; 119 120 125 public ViewerContribution(ISelectionProvider selProvider) { 126 super(); 127 this.selProvider = selProvider; 128 if (selProvider != null) { 129 selProvider.addSelectionChangedListener(this); 130 } 131 } 132 133 138 public void setVisibilityTest(IConfigurationElement element) { 139 visibilityTest = new ActionExpression(element); 140 } 141 142 145 public void contribute(IMenuManager menu, boolean menuAppendIfMissing, 146 IToolBarManager toolbar, boolean toolAppendIfMissing) { 147 boolean visible = true; 148 149 if (visibilityTest != null) { 150 ISelection selection = selProvider.getSelection(); 151 if (selection instanceof IStructuredSelection) { 152 visible = visibilityTest 153 .isEnabledFor((IStructuredSelection) selection); 154 } else { 155 visible = visibilityTest.isEnabledFor(selection); 156 } 157 } 158 159 if (visible) { 160 super.contribute(menu, menuAppendIfMissing, toolbar, 161 toolAppendIfMissing); 162 } 163 } 164 165 168 public void dispose() { 169 if (selProvider != null) { 170 selProvider.removeSelectionChangedListener(this); 171 } 172 disposeActions(); 173 super.dispose(); 174 } 175 176 185 public void selectionChanged(SelectionChangedEvent event) { 186 if (actions != null) { 187 if (actions != null) { 188 for (int i = 0; i < actions.size(); i++) { 189 PluginAction proxy = ((ActionDescriptor) actions.get(i)) 190 .getAction(); 191 proxy.selectionChanged(event); 192 } 193 } 194 } 195 } 196 } 197 198 } 199 | Popular Tags |