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.trackimpl;
025:
026: import jacareto.track.TrackSelectionEvent;
027: import jacareto.track.TrackSelectionListener;
028: import jacareto.track.TrackSelectionModel;
029: import jacareto.track.block.Block;
030: import jacareto.track.block.BlockType;
031:
032: import org.apache.commons.lang.Validate;
033:
034: import java.util.ArrayList;
035: import java.util.Collections;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.Map;
040:
041: /**
042: * <p>
043: * Implementation of the {@link TrackSelectionModel} interface.
044: * </p>
045: *
046: * @author Oliver Specht
047: * @version $revision$
048: */
049: public class DefaultTrackSelectionModel implements TrackSelectionModel {
050: /** List of all listeners listening to changes in this model */
051: List listeners = new ArrayList();
052:
053: /** HashMap which holds List of selected blocks referenced by BlockType */
054: Map selectedBlocks = new HashMap();
055:
056: /**
057: * Creates a new DefaultTrackSelectionModel
058: */
059: public DefaultTrackSelectionModel() {
060: init();
061: }
062:
063: /**
064: * Initializes the HashMap with all possible types and an empty list as selection
065: */
066: private void init() {
067: Iterator iter = BlockType.iterator();
068:
069: while (iter.hasNext()) {
070: selectedBlocks.put(iter.next(), Collections.EMPTY_LIST);
071: }
072: }
073:
074: /**
075: * {@inheritDoc}
076: */
077: public void addTrackSelectionListener(
078: TrackSelectionListener listener) {
079: Validate.notNull(listener);
080: Validate.isTrue(!listeners.contains(listener));
081:
082: listeners.add(listener);
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: public void removeTrackSelectionListener(
089: TrackSelectionListener listener) {
090: Validate.notNull(listener);
091: Validate.isTrue(listeners.contains(listener));
092:
093: listeners.remove(listener);
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: public void setSelection(Block block) {
100: Validate.notNull(block);
101: Validate.isTrue(selectedBlocks.keySet().contains(
102: block.getType()));
103:
104: List list = new ArrayList();
105: list.add(block);
106: selectedBlocks.put(block.getType(), list);
107: fireBlockSelected(block);
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public List getSelection(BlockType type) {
114: Validate.notNull(type);
115: Validate.isTrue(this .selectedBlocks.keySet().contains(type));
116:
117: return (List) this .selectedBlocks.get(type);
118: }
119:
120: /**
121: * {@inheritDoc}
122: */
123: public void setSelectionRange(Block start, Block end) {
124: Validate.notNull(start);
125: Validate.notNull(end);
126: Validate.notNull(this .selectedBlocks);
127: Validate.isTrue(start.getType() == end.getType());
128:
129: List list = new ArrayList();
130: list.add(start);
131: list.add(end);
132: this .selectedBlocks.put(start.getType(), list);
133:
134: fireRangeSelected(start, end);
135: }
136:
137: public void removeSelection() {
138: Validate.notNull(this .selectedBlocks);
139:
140: Iterator iter = this .selectedBlocks.keySet().iterator();
141:
142: while (iter.hasNext()) {
143: ((List) this .selectedBlocks.get(iter.next())).clear();
144: }
145:
146: fireSelectionRemoved();
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: public void removeSelection(BlockType type) {
153: Validate.notNull(type);
154: Validate.notNull(this .selectedBlocks);
155: Validate.isTrue(this .selectedBlocks.keySet().contains(type));
156:
157: ((List) this .selectedBlocks.get(type)).clear();
158:
159: fireSelectionRemoved();
160: }
161:
162: /**
163: * Informs all listeners that a single block has been selected
164: *
165: * @param block the block that has been selected
166: */
167: void fireBlockSelected(Block block) {
168: Validate.notNull(this .listeners);
169:
170: Iterator iter = this .listeners.iterator();
171:
172: while (iter.hasNext()) {
173: ((TrackSelectionListener) iter.next())
174: .blockSelected(new TrackSelectionEvent(this , block));
175: }
176: }
177:
178: /**
179: * Informs all listeners that a range of blocks has been selected
180: *
181: * @param firstBlock the first selected {@link Block}
182: * @param lastBlock the last selected {@link Block}
183: */
184: void fireRangeSelected(Block firstBlock, Block lastBlock) {
185: Validate.notNull(this .listeners);
186:
187: Iterator iter = this .listeners.iterator();
188:
189: while (iter.hasNext()) {
190: ((TrackSelectionListener) iter.next())
191: .rangeSelected(new TrackSelectionEvent(this ,
192: firstBlock, lastBlock));
193: }
194: }
195:
196: /**
197: * Informs all listeners that the selection has been removed
198: */
199: void fireSelectionRemoved() {
200: Validate.notNull(this .listeners);
201:
202: Iterator iter = this .listeners.iterator();
203:
204: while (iter.hasNext()) {
205: ((TrackSelectionListener) iter.next())
206: .selectionRemoved(new TrackSelectionEvent(this ));
207: }
208: }
209:
210: /**
211: * {@inheritDoc}
212: */
213: public boolean hasSelectedBlocks(BlockType type) {
214: Validate.notNull(type);
215: Validate.notNull(this .selectedBlocks);
216: Validate.isTrue(this .selectedBlocks.keySet().contains(type));
217:
218: if (((List) this .selectedBlocks.get(type)).size() > 0) {
219: return true;
220: }
221:
222: return false;
223: }
224:
225: /**
226: * {@inheritDoc}
227: */
228: public boolean hasSelectedBlocks() {
229: Validate.notNull(this .selectedBlocks);
230:
231: if (this .selectedBlocks.size() == 0) {
232: return false;
233: }
234:
235: return true;
236: }
237: }
|