1 11 package org.eclipse.ui.internal.part.services; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.ui.internal.components.framework.ComponentException; 18 import org.eclipse.ui.internal.components.framework.IDisposable; 19 import org.eclipse.ui.internal.components.framework.IServiceProvider; 20 import org.eclipse.ui.internal.part.Part; 21 import org.eclipse.ui.internal.part.components.services.IActionBarContributor; 22 import org.eclipse.ui.internal.part.components.services.IActionBarContributorFactory; 23 import org.eclipse.ui.internal.part.components.services.IPartDescriptor; 24 import org.eclipse.ui.internal.part.multiplexer.INestedComponent; 25 import org.eclipse.ui.internal.part.multiplexer.ISharedContext; 26 27 public class ChildActionBarContributorFactory implements INestedComponent, IActionBarContributorFactory, IDisposable { 28 29 private IActionBarContributorFactory parent; 30 private Map activeBars = new HashMap (); 31 private boolean active = false; 32 private IPartDescriptor descriptor; 33 private IActionBarContributor defaultContributor; 34 35 private final class PartMap { 36 PartMap(IActionBarContributor contributor, Part part) { 37 this.contributor = contributor; 38 this.part = part; 39 } 40 41 IActionBarContributor contributor; 42 Part part; 43 }; 44 45 public ChildActionBarContributorFactory(IPartDescriptor descr, ISharedContext shared) throws ComponentException { 46 IServiceProvider sharedContainer = shared.getSharedComponents(); 47 this.parent = (IActionBarContributorFactory)sharedContainer.getService(IActionBarContributorFactory.class); 48 this.descriptor = descr; 49 this.defaultContributor = parent.getContributor(descr); 50 } 51 52 public void activate(Part activePart) { 53 for (Iterator iter = activeBars.values().iterator(); iter.hasNext();) { 54 PartMap next = (PartMap) iter.next(); 55 56 parent.activateBars(next.contributor, next.part); 57 } 58 59 parent.activateBars(defaultContributor, activePart); 61 62 active = true; 63 } 64 65 public void deactivate(Object newActive) { 66 ChildActionBarContributorFactory factory = null; 67 if (newActive instanceof ChildActionBarContributorFactory) { 68 factory = (ChildActionBarContributorFactory)newActive; 69 } 70 71 for (Iterator iter = activeBars.values().iterator(); iter.hasNext();) { 73 PartMap next = (PartMap) iter.next(); 74 75 String nextId = next.contributor.getDescriptor().getId(); 76 if (factory == null 78 || (factory.activeBars.get(nextId) == null 79 && !factory.defaultContributor.getDescriptor().getId().equals(nextId))) { 80 81 parent.deactivateBars(next.contributor); 82 } 83 } 84 85 String nextId = defaultContributor.getDescriptor().getId(); 86 if (factory == null 88 || (factory.activeBars.get(nextId) == null 89 && !factory.defaultContributor.getDescriptor().getId().equals(nextId))) { 90 91 parent.deactivateBars(defaultContributor); 92 } 93 94 active = false; 95 } 96 97 public void activateBars(IActionBarContributor toActivate, Part actualPart) { 98 String key = toActivate.getDescriptor().getId(); 99 PartMap existing = (PartMap) activeBars.get(key); 100 101 if (existing == null) { 102 existing = new PartMap(toActivate, actualPart); 103 activeBars.put(key, existing); 104 } else { 105 existing.contributor = toActivate; 106 existing.part = actualPart; 107 } 108 109 if (active) { 110 parent.activateBars(toActivate, actualPart); 111 } 112 } 113 114 public void deactivateBars(IActionBarContributor toDeactivate) { 115 String key = toDeactivate.getDescriptor().getId(); 116 PartMap existing = (PartMap) activeBars.get(key); 117 118 if (existing == null) { 119 return; 120 } 121 122 activeBars.remove(key); 123 124 if (active) { 125 parent.deactivateBars(toDeactivate); 126 } 127 } 128 129 public IActionBarContributor getContributor(IPartDescriptor descriptor) { 130 return parent.getContributor(descriptor); 131 } 132 133 public void dispose() { 134 defaultContributor.dispose(); 135 } 136 } 137 | Popular Tags |