001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.support;
016:
017: import java.awt.geom.AffineTransform;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023:
024: /**
025: * Event detailing a change in the blackboard.
026: *
027: * @author jones
028: * @since 1.1.0
029: */
030: public class EditBlackboardEvent {
031:
032: public static enum EventType {
033: /**
034: * A point was added to the blackboard.
035: * <ul>
036: * <li>The source is the {@link PrimitiveShape} that the point was added to</li>
037: * <li>The OldValue is null</li>
038: * <li>The NewValue is a the new {@link Point}</li>
039: * </ul>
040: */
041: ADD_POINT,
042: /**
043: * A point was removed from the blackboard.
044: * <ul>
045: * <li>The source is the {@link PrimitiveShape} that the point was removed from</li>
046: * <li>The OldValue is the {@link Point} removed</li>
047: * <li>The NewValue is null</li>
048: * </ul>
049: */
050: REMOVE_POINT,
051: /**
052: * A point was moved in the blackboard.
053: * <ul>
054: * <li>The source is a Collection of {@link PrimitiveShape} that were affected by the change</li>
055: * <li>The OldValue is the {@link Point} moved</li>
056: * <li>The NewValue is the new {@link Point}</li>
057: * </ul>
058: */
059: MOVE_POINT,
060: /**
061: * One or more Geometries were added to the blackboard.
062: * <ul>
063: * <li>The source is the {@link EditBlackboard} that the geometries were added to</li>
064: * <li>The OldValue is a null</li>
065: * <li>The NewValue is a Collection of the EditGeoms that were added</li>
066: * </ul>
067: */
068: ADD_GEOMS,
069: /**
070: * The geometries in the EditBlackboard were set.
071: * <ul>
072: * <li>The source is the {@link EditBlackboard} that was modified</li>
073: * <li>The OldValue is the Collection of {@link EditGeom} objects that were in the blackboard
074: * before the change</li>
075: * <li>The NewValue is a Collection of {@link EditGeom} objects</li>
076: * </ul>
077: */
078: SET_GEOMS,
079: /**
080: * One or more Geometries were removed from the blackboard.
081: * <ul>
082: * <li>The source is the {@link EditBlackboard} that the geometries were removed from</li>
083: * <li>The OldValue is a Collection of {@link EditGeom} </li>
084: * <li>The NewValue is a null</li>
085: * </ul>
086: */
087: REMOVE_GEOMS,
088: /**
089: * The Map to Screen transform has changed
090: * <ul>
091: * <li>The source is a {@link EditBlackboard} </li>
092: * <li>The OldValue is the previous AffineTransform of the Blackboard</li>
093: * <li>The NewValue is a Map<Point, List<Point>> where the key is the original point and the value
094: * is the new location(s) of the point after translation on a zoom in a single point can
095: * become more than one point.</li>
096: * </ul>
097: */
098: TRANFORMATION,
099: /**
100: * The Selection has changed
101: * <ul>
102: * <li>The source is a {@link EditBlackboard} </li>
103: * <li>The OldValue is the previous selection</li>
104: * <li>The NewValue is a the new selection</li>
105: * </ul>
106: */
107: SELECTION,
108: /**
109: * A special case where a single point was added to many geometries.
110: * <ul>
111: * <li>The source is a Collection of PrimitiveShapes that had the point added </li>
112: * <li>The OldValue is null</li>
113: * <li>The NewValue is a added Point</li>
114: * </ul>
115: */
116: ADD_POINT_TO_MANY, CLEARED
117: }
118:
119: private final Object source;
120: private final Object before;
121: private final Object after;
122:
123: private final EventType type;
124: private EditBlackboard editBlackboard;
125: /**
126: * Some data where information can be squirreled away for use by the framework. this should not be depended on by
127: * non-uDig developers.
128: */
129: public Object privateData;
130:
131: public EditBlackboardEvent(EditBlackboard bb, Object source2,
132: EventType type2, Object before2, Object after2) {
133: assertLegalValues(source2, type2, before2, after2);
134: this .editBlackboard = bb;
135: source = source2;
136: type = type2;
137: this .before = before2;
138: this .after = after2;
139: }
140:
141: /**
142: * @return Returns the source of the change this is determine by the type of the event.
143: * @see EventType
144: */
145: public Object getSource() {
146: return source;
147: }
148:
149: /**
150: * @return Returns the value after the change.
151: * @see EventType
152: */
153: public Object getNewValue() {
154: return after;
155: }
156:
157: /**
158: * @return Returns the value before the change.
159: * @see EventType
160: */
161: public Object getOldValue() {
162: return before;
163: }
164:
165: /**
166: * @return Returns the type.
167: */
168: public EventType getType() {
169: return type;
170: }
171:
172: @SuppressWarnings("unchecked")
173: private void assertLegalValues(Object source2, EventType type2,
174: Object before2, Object after2) {
175: switch (type2) {
176: case ADD_GEOMS:
177: if (!(source2 instanceof EditBlackboard))
178: throw new IllegalArgumentException(
179: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
180: "EditBlackboard was "
181: + source2.getClass().getName()); //$NON-NLS-1$
182: if (before2 != null)
183: throw new IllegalArgumentException(
184: "The before value of a " + type2 + " event be null was " + before2.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
185: if (!(after2 instanceof Collection))
186: throw new IllegalArgumentException(
187: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
188: "be a Collection was "
189: + after2.getClass().getName()); //$NON-NLS-1$
190: if (((Collection) after2).size() < 1)
191: throw new IllegalArgumentException(
192: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
193: "be larger than 0"); //$NON-NLS-1$
194: for (Iterator<Object> iter = ((Collection<Object>) after2)
195: .iterator(); iter.hasNext();) {
196: Object obj = iter.next();
197: if (!(obj instanceof EditGeom))
198: throw new IllegalArgumentException(
199: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
200: "be a Collection of EditGeoms was "
201: + obj.getClass().getName()); //$NON-NLS-1$
202: }
203: break;
204: case ADD_POINT:
205: if (!(source2 instanceof PrimitiveShape))
206: throw new IllegalArgumentException(
207: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
208: "PrimitiveShape was "
209: + source2.getClass().getName()); //$NON-NLS-1$
210: if (before2 != null)
211: throw new IllegalArgumentException(
212: "The before value of a " + type2 + " event be null was " + before.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
213: if (!(after2 instanceof Point))
214: throw new IllegalArgumentException(
215: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
216: "be a Point was "
217: + after2.getClass().getName()); //$NON-NLS-1$
218: break;
219: case MOVE_POINT:
220: if (!(source2 instanceof Set))
221: throw new IllegalArgumentException(
222: "The source of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
223: "be a Set of PrimitiveShapes was "
224: + source2.getClass().getName()); //$NON-NLS-1$
225: if (((Set) source2).size() < 1)
226: throw new IllegalArgumentException(
227: "The source value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
228: "be larger than 0"); //$NON-NLS-1$
229: for (Iterator<Object> iter = ((Set<Object>) source2)
230: .iterator(); iter.hasNext();) {
231: Object obj = iter.next();
232: if (!(obj instanceof PrimitiveShape))
233: throw new IllegalArgumentException(
234: "The source of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
235: "be a Set of PrimitiveShape was "
236: + obj.getClass().getName()); //$NON-NLS-1$
237: }
238:
239: if (!(before2 instanceof Point))
240: throw new IllegalArgumentException(
241: "The before value of a " + type2 + " event be a Point was " + before.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
242: if (!(after2 instanceof Point))
243: throw new IllegalArgumentException(
244: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
245: "be a Point was "
246: + after2.getClass().getName()); //$NON-NLS-1$
247: break;
248: case REMOVE_GEOMS:
249: if (!(source2 instanceof EditBlackboard))
250: throw new IllegalArgumentException(
251: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
252: "EditBlackboard was "
253: + source2.getClass().getName()); //$NON-NLS-1$
254: if (!(before2 instanceof Collection))
255: throw new IllegalArgumentException(
256: "The before value of a " + type2 + " event be a EditGeom was " + before.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
257: if (after2 != null)
258: throw new IllegalArgumentException(
259: "The after value of a " + type2 + " event must be null"); //$NON-NLS-1$ //$NON-NLS-2$
260: break;
261:
262: case REMOVE_POINT:
263: if (!(source2 instanceof Set))
264: throw new IllegalArgumentException(
265: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
266: "Set was "
267: + source2.getClass().getName()); //$NON-NLS-1$
268: if (!(before2 instanceof Point))
269: throw new IllegalArgumentException(
270: "The before value of a " + type2 + " event be a Point was " + before.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
271: if (after2 != null)
272: throw new IllegalArgumentException(
273: "The after value of a " + type2 + " event must be null"); //$NON-NLS-1$ //$NON-NLS-2$
274: break;
275: case SET_GEOMS:
276: if (!(source2 instanceof EditBlackboard))
277: throw new IllegalArgumentException(
278: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
279: "EditBlackboard was "
280: + source2.getClass().getName()); //$NON-NLS-1$
281: if (!(before2 instanceof Collection))
282: throw new IllegalArgumentException(
283: "The before value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
284: "be a Collection was "
285: + before2.getClass().getName()); //$NON-NLS-1$
286: if (((Collection) before2).size() < 1)
287: throw new IllegalArgumentException(
288: "The before value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
289: "be larger than 0"); //$NON-NLS-1$
290: for (Iterator<Object> iter = ((Collection<Object>) before2)
291: .iterator(); iter.hasNext();) {
292: Object obj = iter.next();
293: if (!(obj instanceof EditGeom))
294: throw new IllegalArgumentException(
295: "The before value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
296: "be a Collection of EditGeoms found a "
297: + obj.getClass().getName()); //$NON-NLS-1$
298: }
299:
300: if (!(after2 instanceof Collection))
301: throw new IllegalArgumentException(
302: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
303: "be a Collection was "
304: + after2.getClass().getName()); //$NON-NLS-1$
305: if (((Collection) after2).size() < 1)
306: throw new IllegalArgumentException(
307: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
308: "be larger than 0"); //$NON-NLS-1$
309: for (Iterator<Object> iter = ((Collection<Object>) after2)
310: .iterator(); iter.hasNext();) {
311: Object obj = iter.next();
312: if (!(obj instanceof EditGeom))
313: throw new IllegalArgumentException(
314: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
315: "be a Collection of EditGeoms was "
316: + obj.getClass().getName()); //$NON-NLS-1$
317: }
318: break;
319: case TRANFORMATION:
320: if (!(source2 instanceof EditBlackboard))
321: throw new IllegalArgumentException(
322: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
323: "EditBlackboard was "
324: + source2.getClass().getName()); //$NON-NLS-1$
325: if (!(before2 instanceof AffineTransform))
326: throw new IllegalArgumentException(
327: "The before value of a " + type2 + " event be an Affine transform" + //$NON-NLS-1$ //$NON-NLS-2$
328: " but was :"
329: + before2.getClass().getName()); //$NON-NLS-1$
330: if (!(after2 instanceof Map))
331: throw new IllegalArgumentException(
332: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
333: " a java.util.Map was: "
334: + after2.getClass().getName()); //$NON-NLS-1$
335:
336: break;
337:
338: case SELECTION:
339: if (!(source2 instanceof Selection))
340: throw new IllegalArgumentException(
341: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
342: "Selection was "
343: + source2.getClass().getName()); //$NON-NLS-1$
344: // TODO check before and after
345: break;
346: case ADD_POINT_TO_MANY:
347: if (!(source2 instanceof Collection))
348: throw new IllegalArgumentException(
349: "The source of a " + type2 + " event must be a " + //$NON-NLS-1$ //$NON-NLS-2$
350: "java.util.Collection was "
351: + source2.getClass().getName()); //$NON-NLS-1$
352: for (Iterator<Object> iter = ((Collection<Object>) source2)
353: .iterator(); iter.hasNext();) {
354: Object obj = iter.next();
355: if (!(obj instanceof PrimitiveShape))
356: throw new IllegalArgumentException(
357: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
358: "be a Collection of EditGeoms was "
359: + obj.getClass().getName()); //$NON-NLS-1$
360: }
361: if (before2 != null)
362: throw new IllegalArgumentException(
363: "The before value of a " + type2 + " event be null");//$NON-NLS-1$ //$NON-NLS-2$
364: if (!(after2 instanceof Point))
365: throw new IllegalArgumentException(
366: "The after value of a " + type2 + " event must be " + //$NON-NLS-1$ //$NON-NLS-2$
367: " a Point was: "
368: + after2.getClass().getName()); //$NON-NLS-1$
369: break;
370:
371: default:
372: break;
373: }
374:
375: }
376:
377: @SuppressWarnings("unchecked")
378: public Map<Point, List<Point>> getTransformationMap() {
379: if (type != EventType.TRANFORMATION) {
380: throw new IllegalStateException(
381: "Event must be a TRANSFORMATION event but was " + type); //$NON-NLS-1$
382: }
383: return (Map<Point, List<Point>>) after;
384: }
385:
386: @Override
387: public String toString() {
388: return getType() + " before: " + before + " after:" + after; //$NON-NLS-1$//$NON-NLS-2$
389: }
390:
391: public EditBlackboard getEditBlackboard() {
392: return editBlackboard;
393: }
394: }
|