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;
025:
026: import jacareto.record.ReadAccessRecord;
027: import jacareto.record.Recordable;
028: import jacareto.record.VectorRecord;
029: import jacareto.struct.StructureElement;
030: import jacareto.system.EnvironmentMember;
031: import jacareto.toolkit.InsertPoint;
032:
033: import java.util.Arrays;
034: import java.util.Hashtable;
035: import java.util.Vector;
036:
037: /**
038: * A clipboard for storing record parts.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.00
042: */
043: public class ClipBoard extends EnvironmentMember {
044: /** The CleverPHL instance. */
045: private CleverPHL cleverPHL;
046:
047: /** The content in the clipboard. */
048: private StructureElement[] content;
049:
050: /** Maps structure elements to the recordables in their original order. */
051: private Hashtable recordables;
052:
053: /**
054: * Creates the clipboard
055: *
056: * @param cleverPHL DOCUMENT ME!
057: */
058: public ClipBoard(CleverPHL cleverPHL) {
059: super (cleverPHL.getEnvironment());
060: this .cleverPHL = cleverPHL;
061: content = null;
062: }
063:
064: /**
065: * Sets the content of the clipboard. All structure elements will be cloned before they are
066: * stored in the clipboard.
067: *
068: * @param newContent the array of structure elements to add
069: * @param record the record with the original order of the recordables contained in the
070: * structure elements
071: */
072: public void setContent(StructureElement[] newContent,
073: ReadAccessRecord record) {
074: content = getClones(newContent);
075: recordables = new Hashtable();
076:
077: for (int i = 0; i < newContent.length; i++) {
078: StructureElement element = newContent[i];
079: Recordable[] recordablesStructureOrder = element
080: .getRecordables();
081: int numberOfRecordables = recordablesStructureOrder.length;
082: Recordable[] recordablesRecordOrder = element
083: .getRecordablesInRecordOrder(record);
084: Recordable[] contentRecordablesStructureOrder = content[i]
085: .getRecordables();
086: Recordable[] contentRecordablesRecordOrder = new Recordable[numberOfRecordables];
087:
088: contentRecordablesRecordOrder = getClonedRecordablesInRecordOrder(
089: recordablesStructureOrder, recordablesRecordOrder,
090: contentRecordablesStructureOrder);
091:
092: recordables.put(content[i], contentRecordablesRecordOrder);
093: }
094: }
095:
096: /**
097: * Returns the content of the clipboard, or <code>null</code> if nothing is contained. All
098: * elements returned are clones of the elements in the clipboard.
099: *
100: * @return DOCUMENT ME!
101: */
102: public StructureElement[] getContent() {
103: StructureElement[] result = null;
104:
105: if (!isEmpty()) {
106: result = getClones(content);
107: }
108:
109: return result;
110: }
111:
112: /**
113: * Pastes the content of the clipboard into a structure with the given record at the specified
114: * insert point.
115: *
116: * @param record the record belonging to the structure
117: * @param insertPoint the object containing the indices.
118: */
119: public void paste(VectorRecord record, InsertPoint insertPoint) {
120: if (!isEmpty()) {
121: try {
122: StructureElement[] clonedElements = getClones(content);
123:
124: for (int i = size() - 1; i >= 0; i--) {
125: StructureElement element = content[i];
126: StructureElement clonedElement = clonedElements[i];
127: ;
128:
129: Recordable[] contentRecordablesStructureOrder = element
130: .getRecordables();
131: Recordable[] contentRecordablesRecordOrder = (Recordable[]) this .recordables
132: .get(element);
133: Recordable[] clonedRecordablesStructureOrder = clonedElement
134: .getRecordables();
135: Recordable[] clonedRecordablesRecordOrder = getClonedRecordablesInRecordOrder(
136: contentRecordablesStructureOrder,
137: contentRecordablesRecordOrder,
138: clonedRecordablesStructureOrder);
139: record.insert(clonedRecordablesRecordOrder,
140: insertPoint.getRecordIndexToAdd());
141: insertPoint.getStructureToAdd().insertChild(
142: clonedElement,
143: insertPoint.getStructureIndexToAdd());
144: }
145:
146: cleverPHL.getSessionList().getActual()
147: .selectStructureElements(clonedElements);
148: } catch (Exception ex) {
149: logger.error(ex);
150: }
151: }
152: }
153:
154: /**
155: * Returns whether or not the clipboard contains elements.
156: *
157: * @return DOCUMENT ME!
158: */
159: public boolean isEmpty() {
160: return (content == null) || (content.length == 0);
161: }
162:
163: /**
164: * Returns the number of elements in the clipboard.
165: *
166: * @return DOCUMENT ME!
167: */
168: public int size() {
169: if (content != null) {
170: return content.length;
171: }
172:
173: return 0;
174: }
175:
176: /**
177: * Returns an array with cloned elements of the given array
178: *
179: * @param source DOCUMENT ME!
180: *
181: * @return DOCUMENT ME!
182: */
183: private StructureElement[] getClones(StructureElement[] source) {
184: StructureElement[] result = new StructureElement[source.length];
185:
186: for (int i = 0; i < source.length; i++) {
187: result[i] = (StructureElement) source[i].clone();
188: }
189:
190: return result;
191: }
192:
193: /**
194: * Returns cloned recordables in record order.
195: *
196: * @param sourceStructureOrder the source recordables in structure order
197: * @param sourceRecordOrder the source recordables in record order
198: * @param targetStructureOrder the cloned recordables in structure order
199: *
200: * @return the clones in the record order.
201: */
202: private Recordable[] getClonedRecordablesInRecordOrder(
203: Recordable[] sourceStructureOrder,
204: Recordable[] sourceRecordOrder,
205: Recordable[] targetStructureOrder) {
206: int numberOfRecordables = sourceStructureOrder.length;
207: Vector indexVector = new Vector(Arrays
208: .asList(sourceStructureOrder));
209: Recordable[] result = new Recordable[numberOfRecordables];
210:
211: for (int i = 0; i < numberOfRecordables; i++) {
212: int index = indexVector.indexOf(sourceRecordOrder[i]);
213: result[i] = targetStructureOrder[index];
214: }
215:
216: return result;
217: }
218: }
|