1 11 12 package org.eclipse.ui.internal; 13 14 import org.eclipse.jface.action.ActionContributionItem; 15 import org.eclipse.jface.action.IContributionManager; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.ui.IPluginContribution; 18 import org.eclipse.ui.PlatformUI; 19 import org.eclipse.ui.activities.ActivityManagerEvent; 20 import org.eclipse.ui.activities.IActivityManagerListener; 21 import org.eclipse.ui.activities.IIdentifier; 22 import org.eclipse.ui.activities.IIdentifierListener; 23 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 24 import org.eclipse.ui.activities.IdentifierEvent; 25 import org.eclipse.ui.activities.WorkbenchActivityHelper; 26 27 31 public class PluginActionContributionItem extends ActionContributionItem 32 implements IIdentifierListener, IActivityManagerListener { 33 34 private IIdentifier identifier = null; 35 36 43 public PluginActionContributionItem(PluginAction action) { 44 super(action); 46 } 47 48 53 private void hookListeners() { 54 PlatformUI.getWorkbench().getActivitySupport().getActivityManager() 55 .addActivityManagerListener(this); 56 IIdentifier id = getIdentifier(); 58 if (id != null) { 59 id.addIdentifierListener(this); 60 } 61 } 62 63 68 private void unhookListeners() { 69 PlatformUI.getWorkbench().getActivitySupport().getActivityManager() 70 .removeActivityManagerListener(this); 71 72 IIdentifier id = getIdentifier(); 73 if (id != null) { 74 id.removeIdentifierListener(this); 75 } 76 } 77 78 81 public void setParent(IContributionManager parent) { 82 IContributionManager oldParent = getParent(); 83 super.setParent(parent); 84 if (oldParent == parent) { 85 return; 86 } 87 88 if (parent == null) { 89 unhookListeners(); 90 } else { 91 hookListeners(); 92 } 93 } 94 95 100 private IIdentifier getIdentifier() { 101 if (!WorkbenchActivityHelper.isFiltering()) { 102 return null; 103 } 104 105 if (identifier == null) { 106 IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI 107 .getWorkbench().getActivitySupport(); 108 IPluginContribution contribution = (IPluginContribution) getAction(); 109 identifier = workbenchActivitySupport.getActivityManager() 112 .getIdentifier( 113 WorkbenchActivityHelper 114 .createUnifiedId(contribution)); 115 } 116 return identifier; 117 } 118 119 124 private void disposeIdentifier() { 125 identifier = null; 126 } 127 128 133 public void dispose() { 134 unhookListeners(); 135 disposeIdentifier(); 136 } 137 138 143 public boolean isVisible() { 144 if (identifier != null && !identifier.isEnabled()) { 145 return false; 146 } 147 return super.isVisible(); 148 } 149 150 153 public void identifierChanged(IdentifierEvent identifierEvent) { 154 invalidateParent(); 155 } 156 157 162 private void invalidateParent() { 163 IContributionManager parent = getParent(); 164 if (parent != null) { 165 parent.markDirty(); 166 } 167 } 168 169 172 public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) { 173 if (WorkbenchActivityHelper.isFiltering() && identifier == null) { 176 hookListeners(); 177 invalidateParent(); 178 } else if (!WorkbenchActivityHelper.isFiltering() && identifier != null) { 179 unhookListeners(); 180 disposeIdentifier(); 181 invalidateParent(); 182 } 183 } 184 185 188 public ISelection getSelection() { 189 return ((PluginAction)getAction()).getSelection(); 190 } 191 } 192 | Popular Tags |