|
| 1 | +/** |
| 2 | + * Copyright 2005 Bushe Enterprises, Inc., Hopkinton, MA, USA, www.bushe.com |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.scijava.event.bushe; |
| 17 | + |
| 18 | +import java.awt.Component; |
| 19 | +import javax.swing.JComponent; |
| 20 | +import javax.swing.JPopupMenu; |
| 21 | +import javax.swing.RootPaneContainer; |
| 22 | + |
| 23 | +/** |
| 24 | + * This class finds a component's container event service, and creates one if necessary and possible. |
| 25 | + * <p/> |
| 26 | + * A Container EventService is, unlike the EventBus, an EventService that is container specific, in other words, it is |
| 27 | + * shared only amongst components within a container. For example, a Form component can supply an EventService used |
| 28 | + * only by components in the form. The Form's components can publish value change events on their Container's Event |
| 29 | + * Service. The Form's Model and Validator may listen to these events to collect data and show errors, respectively. |
| 30 | + * <p/> |
| 31 | + * Most importantly, Container EventService's cuts down event traffic, avoid naming and listener clashes, promotes |
| 32 | + * componentization, and splits events usage into logical subsets. |
| 33 | + * <p/> |
| 34 | + * The finder will walk up a component's hierarchy searching for a parent that implements ContainerEventServiceSupplier. |
| 35 | + * If it find one, it returns it. If it doesn't find one, the top level JComponent (specifically, the highest parent in |
| 36 | + * the hierarchy, typically a JRootPane) has a client property added to it (if not already set) that has the value of a |
| 37 | + * new SwingEventService, which is then returned. The EventBus is never returned. |
| 38 | + * |
| 39 | + * @author Michael Bushe michael@bushe.com |
| 40 | + */ |
| 41 | +public class ContainerEventServiceFinder { |
| 42 | + /** The client property used to put a new SwingEventService on top-level components. */ |
| 43 | + public static final String CLIENT_PROPERTY_KEY_TOP_LEVEL_EVENT_SERVICE = "ContainerEventServiceFinder.createdService"; |
| 44 | + |
| 45 | + /** |
| 46 | + * Walks the component's parents until it find an ContainerEventServiceSupplier and returns the supplier's |
| 47 | + * EventService. If the component in the tree is a JPopupMenu, then the menu's invoker is walked. |
| 48 | + * |
| 49 | + * @param component any component |
| 50 | + * |
| 51 | + * @return the ContainerEventService of the nearest parent |
| 52 | + */ |
| 53 | + public static EventService getEventService(Component component) { |
| 54 | + while (component != null) { |
| 55 | + if (component instanceof ContainerEventServiceSupplier) { |
| 56 | + return ((ContainerEventServiceSupplier) component).getContainerEventService(); |
| 57 | + } |
| 58 | + if (component instanceof JPopupMenu) { |
| 59 | + component = ((JPopupMenu) component).getInvoker(); |
| 60 | + } else { |
| 61 | + if (component.getParent() == null) { |
| 62 | + //There is no supplier. Instead of returning null, make an event service |
| 63 | + //and stick it in the client properties of the top level container. |
| 64 | + if (component instanceof RootPaneContainer) { |
| 65 | + component = ((RootPaneContainer) component).getRootPane(); |
| 66 | + } |
| 67 | + if (!(component instanceof JComponent)) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + JComponent jComp = ((JComponent) component); |
| 71 | + SwingEventService eventService = (SwingEventService) jComp.getClientProperty(CLIENT_PROPERTY_KEY_TOP_LEVEL_EVENT_SERVICE); |
| 72 | + if (eventService == null) { |
| 73 | + eventService = new SwingEventService(); |
| 74 | + jComp.putClientProperty(CLIENT_PROPERTY_KEY_TOP_LEVEL_EVENT_SERVICE, eventService); |
| 75 | + } |
| 76 | + return eventService; |
| 77 | + } else { |
| 78 | + component = component.getParent(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | +} |
0 commit comments