1 11 package org.eclipse.ui.internal; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.ListenerList; 19 import org.eclipse.ui.IPropertyListener; 20 import org.eclipse.ui.contexts.IContextActivation; 21 import org.eclipse.ui.contexts.IContextService; 22 import org.eclipse.ui.internal.registry.IActionSetDescriptor; 23 import org.eclipse.ui.services.IServiceLocator; 24 25 41 public class ActionSetManager { 42 43 private static class ActionSetRec { 44 int showCount; 45 46 int maskCount; 47 48 public boolean isVisible() { 49 return maskCount == 0 && showCount > 0; 50 } 51 52 public boolean isEmpty() { 53 return maskCount == 0 && showCount == 0; 54 } 55 } 56 57 private HashMap actionSets = new HashMap (); 58 private HashSet visibleItems = new HashSet (); 59 60 public static final int PROP_VISIBLE = 0; 61 public static final int PROP_HIDDEN = 1; 62 public static final int CHANGE_MASK = 0; 63 public static final int CHANGE_UNMASK = 1; 64 public static final int CHANGE_SHOW = 2; 65 public static final int CHANGE_HIDE = 3; 66 67 private ListenerList listeners = new ListenerList(); 68 private IPropertyListener contextListener; 69 private Map activationsById = new HashMap (); 70 private IContextService contextService; 71 72 public ActionSetManager(IServiceLocator locator) { 73 contextService = (IContextService) locator.getService(IContextService.class); 74 addListener(getContextListener()); 75 } 76 77 80 private IPropertyListener getContextListener() { 81 if (contextListener == null) { 82 contextListener = new IPropertyListener() { 83 public void propertyChanged(Object source, int propId) { 84 if (source instanceof IActionSetDescriptor) { 85 IActionSetDescriptor desc = (IActionSetDescriptor) source; 86 String id = desc.getId(); 87 if (propId == PROP_VISIBLE) { 88 activationsById.put(id, contextService 89 .activateContext(id)); 90 } else if (propId == PROP_HIDDEN) { 91 IContextActivation act = (IContextActivation) activationsById 92 .remove(id); 93 if (act != null) { 94 contextService.deactivateContext(act); 95 } 96 } 97 } 98 } 99 }; 100 } 101 return contextListener; 102 } 103 104 public void addListener(IPropertyListener l) { 105 listeners.add(l); 106 } 107 108 public void removeListener(IPropertyListener l) { 109 listeners.remove(l); 110 } 111 112 private void firePropertyChange(IActionSetDescriptor descriptor, int id) { 113 Object [] l = listeners.getListeners(); 114 for (int i=0; i<l.length; i++) { 115 IPropertyListener listener = (IPropertyListener) l[i]; 116 listener.propertyChanged(descriptor, id); 117 } 118 } 119 120 private ActionSetRec getRec(IActionSetDescriptor descriptor) { 121 ActionSetRec rec = (ActionSetRec)actionSets.get(descriptor); 122 123 if (rec == null) { 124 rec = new ActionSetRec(); 125 actionSets.put(descriptor, rec); 126 } 127 128 return rec; 129 } 130 131 public void showAction(IActionSetDescriptor descriptor) { 132 ActionSetRec rec = getRec(descriptor); 133 134 boolean wasVisible = rec.isVisible(); 135 rec.showCount++; 136 if (!wasVisible && rec.isVisible()) { 137 visibleItems.add(descriptor); 138 firePropertyChange(descriptor, PROP_VISIBLE); 139 if (rec.isEmpty()) { 140 actionSets.remove(descriptor); 141 } 142 } 143 } 144 145 public void hideAction(IActionSetDescriptor descriptor) { 146 ActionSetRec rec = getRec(descriptor); 147 148 boolean wasVisible = rec.isVisible(); 149 rec.showCount--; 150 if (wasVisible && !rec.isVisible()) { 151 visibleItems.remove(descriptor); 152 firePropertyChange(descriptor, PROP_HIDDEN); 153 if (rec.isEmpty()) { 154 actionSets.remove(descriptor); 155 } 156 } 157 } 158 159 public void maskAction(IActionSetDescriptor descriptor) { 160 ActionSetRec rec = getRec(descriptor); 161 162 boolean wasVisible = rec.isVisible(); 163 rec.maskCount++; 164 if (wasVisible && !rec.isVisible()) { 165 visibleItems.remove(descriptor); 166 firePropertyChange(descriptor, PROP_HIDDEN); 167 if (rec.isEmpty()) { 168 actionSets.remove(descriptor); 169 } 170 } 171 } 172 173 public void unmaskAction(IActionSetDescriptor descriptor) { 174 ActionSetRec rec = getRec(descriptor); 175 176 boolean wasVisible = rec.isVisible(); 177 rec.maskCount--; 178 if (!wasVisible && rec.isVisible()) { 179 visibleItems.add(descriptor); 180 firePropertyChange(descriptor, PROP_VISIBLE); 181 if (rec.isEmpty()) { 182 actionSets.remove(descriptor); 183 } 184 } 185 } 186 187 public Collection getVisibleItems() { 188 return visibleItems; 189 } 190 191 public void change(IActionSetDescriptor descriptor, int changeType) { 192 switch(changeType) { 193 case CHANGE_SHOW: 194 showAction(descriptor); break; 195 case CHANGE_HIDE: 196 hideAction(descriptor); break; 197 case CHANGE_MASK: 198 maskAction(descriptor); break; 199 case CHANGE_UNMASK: 200 unmaskAction(descriptor); break; 201 } 202 } 203 } 204 | Popular Tags |