001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.comp.select;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028: import jacareto.system.EnvironmentMember;
029:
030: import java.awt.Component;
031:
032: import java.util.Iterator;
033: import java.util.Vector;
034:
035: /**
036: * <p>
037: * A selection of components.
038: * </p>
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.00
042: */
043: public class ComponentSelection extends EnvironmentMember {
044: /** The instance which knows the components. */
045: private Components components;
046:
047: /** The vector of selected components. */
048: private Vector selectedComponents;
049:
050: /** The component selection listeners. */
051: private Vector componentSelectionListeners;
052:
053: /**
054: * Creates a new ComponentSelection.
055: *
056: * @param env the environment
057: * @param components the instance which knows the components
058: */
059: public ComponentSelection(Environment env, Components components) {
060: super (env);
061: this .components = components;
062: selectedComponents = new Vector(5, 5);
063: componentSelectionListeners = new Vector(5, 5);
064: }
065:
066: /**
067: * Adds a new component to the selection.
068: *
069: * @param component the component to add
070: */
071: public void add(Component component) {
072: if (!selectedComponents.contains(component)) {
073: selectedComponents.add(component);
074:
075: Component[] addedComponents = new Component[1];
076: addedComponents[0] = component;
077: fireComponentSelectionEvent(new ComponentSelectionEvent(
078: this , ComponentSelectionEvent.COMPONENTS_ADDED,
079: addedComponents));
080: }
081: }
082:
083: /**
084: * Adds new components to the selection.
085: *
086: * @param newComponents the components to add
087: */
088: public void add(Component[] newComponents) {
089: int[] addedIndices = new int[newComponents.length];
090: int numberOfAddedComponents = 0;
091:
092: for (int i = 0; i < newComponents.length; i++) {
093: if (!selectedComponents.contains(newComponents[i])) {
094: selectedComponents.add(newComponents[i]);
095: addedIndices[numberOfAddedComponents++] = i;
096: }
097: }
098:
099: if (numberOfAddedComponents > 0) {
100: Component[] addedComponents = new Component[numberOfAddedComponents];
101:
102: for (int i = 0; i < numberOfAddedComponents; i++) {
103: addedComponents[i] = newComponents[addedIndices[i]];
104: }
105:
106: fireComponentSelectionEvent(new ComponentSelectionEvent(
107: this , ComponentSelectionEvent.COMPONENTS_ADDED,
108: addedComponents));
109: }
110: }
111:
112: /**
113: * Removes component from the selection.
114: *
115: * @param component the component to remove
116: */
117: public void remove(Component component) {
118: if (selectedComponents.contains(component)) {
119: selectedComponents.remove(component);
120:
121: Component[] removedComponents = new Component[1];
122: removedComponents[0] = component;
123: fireComponentSelectionEvent(new ComponentSelectionEvent(
124: this , ComponentSelectionEvent.COMPONENTS_REMOVED,
125: removedComponents));
126: }
127: }
128:
129: /**
130: * Removes the specified components from the selection.
131: *
132: * @param componentsToRemove the components to remove
133: */
134: public void remove(Component[] componentsToRemove) {
135: int[] removedIndices = new int[componentsToRemove.length];
136: int numberOfRemovedComponents = 0;
137:
138: for (int i = 0; i < componentsToRemove.length; i++) {
139: if (selectedComponents.contains(componentsToRemove[i])) {
140: selectedComponents.remove(componentsToRemove[i]);
141: removedIndices[numberOfRemovedComponents++] = i;
142: }
143: }
144:
145: if (numberOfRemovedComponents > 0) {
146: Component[] removedComponents = new Component[numberOfRemovedComponents];
147:
148: for (int i = 0; i < numberOfRemovedComponents; i++) {
149: removedComponents[i] = componentsToRemove[removedIndices[i]];
150: }
151:
152: fireComponentSelectionEvent(new ComponentSelectionEvent(
153: this , ComponentSelectionEvent.COMPONENTS_REMOVED,
154: removedComponents));
155: }
156: }
157:
158: /**
159: * Returns whether or not the component is selected
160: *
161: * @param component DOCUMENT ME!
162: *
163: * @return <code>true</code> if the component is contained in the selection, otherwise
164: * <code>false</code>
165: */
166: public boolean contains(Component component) {
167: return selectedComponents.contains(component);
168: }
169:
170: /**
171: * Returns the selected components.
172: *
173: * @return an array containing the selected components
174: */
175: public Component[] getComponents() {
176: Component[] result = new Component[selectedComponents.size()];
177:
178: for (int i = 0; i < selectedComponents.size(); i++) {
179: result[i] = (Component) selectedComponents.get(i);
180: }
181:
182: return result;
183: }
184:
185: /**
186: * Clears the selection.
187: */
188: public void clear() {
189: selectedComponents.clear();
190: fireComponentSelectionEvent(new ComponentSelectionEvent(this ,
191: ComponentSelectionEvent.SELECTION_CLEARED, null));
192: }
193:
194: /**
195: * Updates the selection. If selected components are not known by the components instance
196: * anymore, they are removed from the selection.
197: */
198: public void update() {
199: Vector componentsToBeRemoved = new Vector(5, 5);
200:
201: // Get the components to be removed
202: Iterator it = selectedComponents.iterator();
203:
204: while (it.hasNext()) {
205: Component component = (Component) it.next();
206:
207: if (!components.isKnown(component)) {
208: componentsToBeRemoved.add(component);
209: }
210: }
211:
212: // Remove the components
213: Component[] arrayOfComponents = new Component[componentsToBeRemoved
214: .size()];
215:
216: for (int i = 0; i < arrayOfComponents.length; i++) {
217: arrayOfComponents[i] = (Component) componentsToBeRemoved
218: .get(i);
219: }
220:
221: remove(arrayOfComponents);
222: }
223:
224: /**
225: * Returns the number of selection components
226: *
227: * @return the size of the selection
228: */
229: public int size() {
230: return selectedComponents.size();
231: }
232:
233: /**
234: * Returns whether or not the selection containes components.
235: *
236: * @return <code>true</code> if no component is selected, otherwise <code>false</code>
237: */
238: public boolean isEmpty() {
239: return size() == 0;
240: }
241:
242: /**
243: * Adds a component selection listener
244: *
245: * @param listener the listener to add
246: */
247: public void addComponentSelectionListener(
248: ComponentSelectionListener listener) {
249: if (!componentSelectionListeners.contains(listener)) {
250: componentSelectionListeners.add(listener);
251: }
252: }
253:
254: /**
255: * Removes a component selection listener
256: *
257: * @param listener the listener to remove
258: */
259: public void removeComponentSelectionListener(
260: ComponentSelectionListener listener) {
261: if (componentSelectionListeners.contains(listener)) {
262: componentSelectionListeners.remove(listener);
263: }
264: }
265:
266: /**
267: * Fires a component selection event to all listeners.
268: *
269: * @param event DOCUMENT ME!
270: */
271: protected void fireComponentSelectionEvent(
272: ComponentSelectionEvent event) {
273: Iterator it = componentSelectionListeners.iterator();
274:
275: while (it.hasNext()) {
276: ((ComponentSelectionListener) it.next())
277: .componentSelectionChanged(event);
278: }
279: }
280: }
|