01: /*
02: * ContainerEffectManager.java
03: *
04: * Created on March 31, 2007, 9:31 AM
05: *
06: * Copyright 2006-2007 Nigel Hughes
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
09: * in compliance with the License. You may obtain a copy of the License at http://www.apache.org/
10: * licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
13: * governing permissions and limitations under the License.
14: */
15:
16: package com.blogofbug.swing.components.effects;
17:
18: import java.awt.Container;
19: import java.awt.event.ContainerEvent;
20: import java.awt.event.ContainerListener;
21: import java.lang.UnsupportedOperationException;
22: import java.util.Hashtable;
23: import javax.swing.JComponent;
24:
25: /**
26: *
27: * @author nigel
28: */
29: public class ContainerEffectManager extends EffectEngine implements
30: ContainerListener {
31: protected ComponentEffectSupplier supplier;
32: protected JComponent component;
33: protected Container container;
34: protected Hashtable<JComponent, ComponentEffect> componentEffects = new Hashtable<JComponent, ComponentEffect>();
35:
36: /** Creates a new instance of ContainerEffectManager */
37: public ContainerEffectManager(JComponent containerComponent,
38: ComponentEffectSupplier supplier, boolean paintOnly) {
39: super (containerComponent, paintOnly);
40:
41: if (!(containerComponent instanceof Container)) {
42: throw new UnsupportedOperationException(
43: "Component must be a container");
44: }
45:
46: container = (Container) containerComponent;
47: this .supplier = supplier;
48:
49: container.addContainerListener(this );
50: }
51:
52: public ComponentEffect getEffectFor(JComponent component) {
53: return componentEffects.get(component);
54: }
55:
56: public void componentAdded(ContainerEvent containerEvent) {
57: ComponentEffect newEffect = supplier
58: .getEffect((JComponent) containerEvent.getChild(),
59: container, this );
60: if (newEffect != null) {
61: this .addEffect(newEffect);
62: componentEffects.put(
63: (JComponent) containerEvent.getChild(), newEffect);
64: }
65: }
66:
67: public void componentRemoved(ContainerEvent containerEvent) {
68: ComponentEffect effect = componentEffects.get(containerEvent
69: .getChild());
70: if (effect != null) {
71: componentEffects.remove(containerEvent.getChild());
72: removeEffect(effect);
73: }
74: }
75:
76: public interface ComponentEffectSupplier {
77: public ComponentEffect getEffect(JComponent forComponent,
78: Container inComponent, EffectContainer effectEngine);
79: }
80:
81: }
|