summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/AbstractComponentContainer.java
blob: e7b142f82866c1aa89e3f4ce18066f1e563f594a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* *************************************************************************
 
                               IT Mill Toolkit 

               Development of Browser User Intarfaces Made Easy

                    Copyright (C) 2000-2006 IT Mill Ltd
                     
   *************************************************************************

   This product is distributed under commercial license that can be found
   from the product package on license/license.txt. Use of this product might 
   require purchasing a commercial license from IT Mill Ltd. For guidelines 
   on usage, see license/licensing-guidelines.html

   *************************************************************************
   
   For more information, contact:
   
   IT Mill Ltd                           phone: +358 2 4802 7180
   Ruukinkatu 2-4                        fax:   +358 2 4802 7181
   20540, Turku                          email:  info@itmill.com
   Finland                               company www: www.itmill.com
   
   Primary source for information and releases: www.itmill.com

   ********************************************************************** */

package com.itmill.toolkit.ui;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Iterator;

/** Extension to {@link AbstractComponent} that defines the default
 * implementation for the methods in {@link ComponentContainer}. Basic UI
 * components that need to contain other components inherit this class to
 * easily qualify as a MillStone component container.
 * 
 * @author  IT Mill Ltd
 * @version @VERSION@
 * @since 3.0
 */
public abstract class AbstractComponentContainer 
extends AbstractComponent implements ComponentContainer {

	/** Constructs a new component container. */
	public AbstractComponentContainer() {
		super();
	}

    /** Removes all components from the container. This should probably be
     * reimplemented in extending classes for a more powerfu
     * implementation. 
     */
    public void removeAllComponents() {
        LinkedList l = new LinkedList();

		// Add all components
        for (Iterator i = getComponentIterator(); i.hasNext();)
        	l.add(i.next());
        	
        // Remove all component
        for (Iterator i = l.iterator(); i.hasNext();)
        	removeComponent((Component)i.next());
    }
    
    /* Moves all components from an another container into this container.
	 * Don't add a JavaDoc comment here, we use the default documentation
	 * from implemented interface.
	 */
	public void moveComponentsFrom(ComponentContainer source) {
		LinkedList components = new LinkedList();
		for (Iterator i = source.getComponentIterator(); i.hasNext();) 
			components.add(i.next());
		
		for (Iterator i = components.iterator(); i.hasNext();) {
			Component c = (Component) i.next();
			source.removeComponent(c);
			addComponent(c);
		}
    }
    
	/** Notifies all contained components that the container is attached to
	 * a window.
	 * 
	 * @see com.itmill.toolkit.ui.Component#attach()
	 */
	public void attach() {
		super.attach();
		
		for (Iterator i = getComponentIterator(); i.hasNext();)
			((Component)i.next()).attach();
	}

	/** Notifies all contained components that the container is detached
	 * from a window.
	 * 
	 * @see com.itmill.toolkit.ui.Component#detach()
	 */
	public void detach() {
		super.detach();
		
		for (Iterator i = getComponentIterator(); i.hasNext();)
			((Component)i.next()).detach();
	}


	/* Events ************************************************************ */

	private static final Method COMPONENT_ATTACHED_METHOD;
	private static final Method COMPONENT_DETACHED_METHOD;

	static {
		try {
			COMPONENT_ATTACHED_METHOD =
				ComponentAttachListener.class.getDeclaredMethod(
					"componentAttachedToContainer",
					new Class[] { ComponentAttachEvent.class });
			COMPONENT_DETACHED_METHOD =
				ComponentDetachListener.class.getDeclaredMethod(
					"componentDetachedFromContainer",
					new Class[] { ComponentDetachEvent.class });
		} catch (java.lang.NoSuchMethodException e) {
			// This should never happen
			throw new java.lang.RuntimeException();
		}
	}

	/* documented in interface */
	public void addListener(ComponentAttachListener listener) {
		addListener(ComponentContainer.ComponentAttachEvent.class, listener, COMPONENT_ATTACHED_METHOD);
	}

	/* documented in interface */
	public void addListener(ComponentDetachListener listener) {
		addListener(ComponentContainer.ComponentDetachEvent.class, listener, COMPONENT_DETACHED_METHOD);
	}

	/* documented in interface */
	public void removeListener(ComponentAttachListener listener) {
		removeListener(ComponentContainer.ComponentAttachEvent.class, listener, COMPONENT_ATTACHED_METHOD);
	}

	/* documented in interface */
	public void removeListener(ComponentDetachListener listener) {
		removeListener(ComponentContainer.ComponentDetachEvent.class, listener, COMPONENT_DETACHED_METHOD);
	}

	/** Fire component attached event. This should be called by the addComponent 
	 * methods after the component have been added to this container. 
	 * @param component The component that has been added to this container.
	 */
	protected void fireComponentAttachEvent(Component component) {
		fireEvent(new ComponentAttachEvent(this,component));
	}

	/** Fire component detached event. This should be called by the removeComponent 
	 * methods after the component have been removed from this container. 
	 * @param component The component that has been removed from this container.
	 */
	protected void fireComponentDetachEvent(Component component) {
		fireEvent(new ComponentAttachEvent(this,component));
	}
	
	/** This only implements the events and component parent calls. The extending
	 * classes must implement component list maintenance and call this method 
	 * after component list maintenance.
	 * @see com.itmill.toolkit.ui.ComponentContainer#addComponent(Component)
	 */
	public void addComponent(Component c) {
		c.setParent(this);
		if (getApplication() != null)
			c.attach();
		fireComponentAttachEvent(c);
	}

	/** This only implements the events and component parent calls. The extending
	 * classes must implement component list maintenance and call this method 
	 * before component list maintenance.
	 * @see com.itmill.toolkit.ui.ComponentContainer#removeComponent(Component)
	 */
	public void removeComponent(Component c) {
		if (getApplication() != null)
			c.detach();
		c.setParent(null);
		fireComponentDetachEvent(c);
	}
}