1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.jface.window.Window; 23 import org.eclipse.ui.internal.services.ExpressionAuthority; 24 25 40 final class MenuAuthority extends ExpressionAuthority { 41 42 49 private final Map menuContributionsByElement = new HashMap (); 50 51 59 private final Set [] showingContributionsBySourcePriority = new Set [33]; 60 61 65 private final Window window; 66 67 75 MenuAuthority(final Window window) { 76 this.window = window; 77 } 78 79 86 final void contributeMenu(final IMenuContribution contribution) { 87 final MenuElement element = contribution.getMenuElement(); 89 final Object value = menuContributionsByElement.get(element); 90 if (value instanceof Collection ) { 91 final Collection menuContributions = (Collection ) value; 92 if (!menuContributions.contains(contribution)) { 93 menuContributions.add(contribution); 94 } 95 } else if (value instanceof IMenuContribution) { 96 if (value != contribution) { 97 final Collection menuContributions = new ArrayList (2); 98 menuContributions.add(value); 99 menuContributions.add(contribution); 100 menuContributionsByElement.put(element, menuContributions); 101 } 102 } else { 103 menuContributionsByElement.put(element, contribution); 104 } 105 106 if (contribution.getMenuElement().isShowing(window)) { 108 final int sourcePriority = contribution.getSourcePriority(); 109 for (int i = 1; i <= 32; i++) { 110 if ((sourcePriority & (1 << i)) != 0) { 111 Set contributions = showingContributionsBySourcePriority[i]; 112 if (contributions == null) { 113 contributions = new HashSet (1); 114 showingContributionsBySourcePriority[i] = contributions; 115 } 116 contributions.add(contribution); 117 } 118 } 119 } 120 } 121 122 129 final void removeContribution(final IMenuContribution contribution) { 130 final MenuElement element = contribution.getMenuElement(); 132 final Object value = menuContributionsByElement.get(element); 133 if (value instanceof Collection ) { 134 final Collection menuContributions = (Collection ) value; 135 if (menuContributions.contains(contribution)) { 136 menuContributions.remove(contribution); 137 if (menuContributions.isEmpty()) { 138 menuContributionsByElement.remove(element); 139 140 } else if (menuContributions.size() == 1) { 141 final IMenuContribution remainingContribution = (IMenuContribution) menuContributions 142 .iterator().next(); 143 menuContributionsByElement.put(element, 144 remainingContribution); 145 146 } 147 } 148 } else if (value instanceof IMenuContribution) { 149 if (value == contribution) { 150 menuContributionsByElement.remove(element); 151 } 152 } 153 154 if (element.isShowing(window)) { 156 final int sourcePriority = contribution.getSourcePriority(); 157 for (int i = 1; i <= 32; i++) { 158 if ((sourcePriority & (1 << i)) != 0) { 159 final Set contributions = showingContributionsBySourcePriority[i]; 160 if (contributions == null) { 161 continue; 162 } 163 contributions.remove(contribution); 164 if (contributions.isEmpty()) { 165 showingContributionsBySourcePriority[i] = null; 166 } 167 } 168 } 169 } 170 } 171 172 180 protected final void sourceChanged(final int sourcePriority) { 181 187 final Set contributionsToRecompute = new HashSet (); 188 for (int i = 1; i <= 32; i++) { 189 if ((sourcePriority & (1 << i)) != 0) { 190 final Collection contributions = showingContributionsBySourcePriority[i]; 191 if (contributions != null) { 192 final Iterator contributionItr = contributions.iterator(); 193 while (contributionItr.hasNext()) { 194 contributionsToRecompute.add(contributionItr.next()); 195 } 196 } 197 } 198 } 199 200 205 final Iterator contributionItr = contributionsToRecompute.iterator(); 206 while (contributionItr.hasNext()) { 207 final IMenuContribution contribution = (IMenuContribution) contributionItr 208 .next(); 209 final boolean currentlyVisible = evaluate(contribution); 210 contribution.clearResult(); 211 final boolean newVisible = evaluate(contribution); 212 if (newVisible != currentlyVisible) { 213 contribution.getMenuElement().setVisible(window, newVisible); 214 } 215 } 216 } 217 } 218 | Popular Tags |