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.cleverphl.session;
025:
026: import jacareto.record.RandomAccessRecord;
027: import jacareto.record.RecordChangeEvent;
028: import jacareto.record.RecordChangeListener;
029: import jacareto.record.RecordException;
030: import jacareto.record.Recordable;
031: import jacareto.struct.RecordStructureCompound;
032: import jacareto.struct.Structure;
033: import jacareto.struct.StructureElement;
034: import jacareto.system.Environment;
035: import jacareto.system.EnvironmentMember;
036:
037: import java.util.Collection;
038: import java.util.Iterator;
039: import java.util.Vector;
040:
041: /**
042: * A capture helper.
043: *
044: * @author <a href="mailto:markus.bois@web.de">Markus Bois</a>
045: * @version 1.00
046: */
047: public class CaptureHelper extends EnvironmentMember implements
048: RecordChangeListener {
049: /** The vector of the record change listeners to notify when this record has changed. */
050: private Collection recordChangeListeners;
051:
052: /** Whether or not record change listeners will be notified of record changes. */
053: private boolean isNotificationEnabled;
054:
055: /** The session record */
056: private RandomAccessRecord sessionRecord;
057:
058: /** The session structure */
059: private Structure sessionStructure;
060:
061: /** The status of captureing */
062: private boolean isCaptureing;
063:
064: /** The inserting position in the session record */
065: private int recordIndexToAdd;
066:
067: /** The inserting position in the session structure */
068: private int structureIndexToAdd;
069:
070: /** The inserting structure element */
071: private StructureElement structureToAdd;
072:
073: /**
074: * Creates a capture helper.
075: *
076: * @param env the environment.
077: * @param sessionRecord the session's record
078: * @param sessionStructure the session's structure
079: */
080: public CaptureHelper(Environment env,
081: RandomAccessRecord sessionRecord, Structure sessionStructure) {
082: super (env);
083:
084: this .isCaptureing = false;
085: this .sessionRecord = sessionRecord;
086: this .sessionStructure = sessionStructure;
087:
088: recordChangeListeners = new Vector(10, 5);
089: isNotificationEnabled = true;
090: }
091:
092: /**
093: * Called when a record change has occured. Remove the elemnents on the wrong position of the
094: * record and add them on the right position.
095: *
096: * @param event DOCUMENT ME!
097: */
098: public void recordHasChanged(RecordChangeEvent event) {
099: if (event.getID() == RecordChangeEvent.RECORDABLES_ADDED) {
100: Recordable[] recordables = event.getRecordables();
101: int[] indices = new int[recordables.length];
102:
103: for (int i = 0; i < recordables.length; i++) {
104: try {
105: sessionRecord.remove(recordables[i]);
106:
107: structureToAdd.insertChild(recordables[i],
108: structureIndexToAdd);
109: sessionRecord.insert(recordables[i],
110: recordIndexToAdd);
111:
112: indices[i] = recordIndexToAdd;
113:
114: recordIndexToAdd++;
115: structureIndexToAdd++;
116: } catch (RecordException l) {
117: getLogger().error(l.getMessage());
118: }
119: }
120:
121: fireRecordChange(new RecordChangeEvent(
122: RecordChangeEvent.RECORDABLES_INSERTED,
123: recordables, indices));
124: }
125: }
126:
127: /**
128: * Sets a new record structure compound.
129: *
130: * @param compound the new compound.
131: */
132: public void setRecordStructureCompound(
133: RecordStructureCompound compound) {
134: this .sessionRecord = (RandomAccessRecord) compound.getRecord();
135: this .sessionStructure = compound.getStructure();
136: }
137:
138: /**
139: * Returns if the capture helper is listening .
140: *
141: * @return DOCUMENT ME!
142: */
143: public boolean isCaptureing() {
144: return isCaptureing;
145: }
146:
147: /**
148: * Stop the listening on the capture record. Resets the listeners.
149: */
150: public void stopCaptureing() {
151: removeRecordChangeListener(sessionStructure);
152: sessionRecord.removeRecordChangeListener(this );
153: sessionRecord.addRecordChangeListener(sessionStructure);
154: isCaptureing = false;
155: }
156:
157: /**
158: * Starts the listening on the capture record. Sets up listeners und remove other listeners.
159: *
160: * @param recordIndexToAdd the index where the recordables schould be added.
161: * @param structureIndexToAdd the index where the structure elements should added in the
162: * structure.
163: * @param structureToAdd the structure element where structure elements will be added.
164: */
165: public void startCaptureing(int recordIndexToAdd,
166: int structureIndexToAdd, StructureElement structureToAdd) {
167: this .recordIndexToAdd = recordIndexToAdd;
168: this .structureIndexToAdd = structureIndexToAdd;
169: this .structureToAdd = structureToAdd;
170:
171: sessionRecord.addRecordChangeListener(this );
172: sessionRecord.removeRecordChangeListener(sessionStructure);
173: isCaptureing = true;
174:
175: addRecordChangeListener(sessionStructure);
176: }
177:
178: /**
179: * Adds a record change listener to the list of record change listeners. All listeners will be
180: * notified when this record changes.
181: *
182: * @param listener the listener to add
183: */
184: public void addRecordChangeListener(RecordChangeListener listener) {
185: recordChangeListeners.add(listener);
186: }
187:
188: /**
189: * Removes a record change listener from the list of record change listeners.
190: *
191: * @param listener the listener to remove
192: */
193: public void removeRecordChangeListener(RecordChangeListener listener) {
194: recordChangeListeners.remove(listener);
195: }
196:
197: /**
198: * Specifies whether or not record change listeners should be notified of record changes.
199: *
200: * @param isNotificationEnabled DOCUMENT ME!
201: */
202: public void setNotificationEnabled(boolean isNotificationEnabled) {
203: this .isNotificationEnabled = isNotificationEnabled;
204: }
205:
206: /**
207: * Returns whether or not record change listeners will be notified of record changes.
208: *
209: * @return DOCUMENT ME!
210: */
211: public boolean isNotificationEnabled() {
212: return isNotificationEnabled;
213: }
214:
215: /**
216: * Fires a record change (content or state) to all record change listeners.
217: *
218: * @param event the event wich contains the information of the change
219: */
220: protected void fireRecordChange(RecordChangeEvent event) {
221: if (isNotificationEnabled) {
222: Iterator i = recordChangeListeners.iterator();
223:
224: while (i.hasNext()) {
225: ((RecordChangeListener) i.next())
226: .recordHasChanged(event);
227: }
228: }
229: }
230: }
|