001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Refractions Research Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.catalog.defaults;
018:
019: import org.geotools.catalog.Resolve;
020: import org.geotools.catalog.ResolveDelta;
021: import org.geotools.catalog.ResolveDeltaVisitor;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: /**
030: * Catalog delta.
031: *
032: * @since 0.6.0
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/catalog/defaults/DefaultResolveDelta.java $
034: */
035: public class DefaultResolveDelta implements ResolveDelta {
036: private List children;
037: private Kind kind = Kind.NO_CHANGE;
038: private Resolve handle = null;
039: private Resolve newHandle = null;
040:
041: /**
042: * Delta for a changed handle, ie handle state refresh.
043: *
044: * <p>
045: * Used to communicate that new Info is available and labels should be
046: * refreshed.
047: * </p>
048: *
049: * @param handle DOCUMENT ME!
050: * @param changes DOCUMENT ME!
051: *
052: * @throws IllegalArgumentException DOCUMENT ME!
053: */
054: public DefaultResolveDelta(Resolve handle, List changes) {
055: this .kind = Kind.CHANGED;
056: this .children = Collections.unmodifiableList(changes);
057: this .handle = handle;
058:
059: if (kind == Kind.REPLACED) {
060: throw new IllegalArgumentException(
061: "New handle required in replace event.");
062: }
063:
064: newHandle = null;
065: }
066:
067: /**
068: * Simple change used for Add and Remove with no children
069: *
070: * @param handle DOCUMENT ME!
071: * @param kind DOCUMENT ME!
072: *
073: * @throws IllegalArgumentException DOCUMENT ME!
074: */
075: public DefaultResolveDelta(Resolve handle, Kind kind) {
076: this .kind = kind;
077: this .children = NO_CHILDREN;
078: this .handle = handle;
079:
080: if (kind == Kind.REPLACED) {
081: throw new IllegalArgumentException(
082: "New handle required in replace event.");
083: }
084:
085: newHandle = null;
086: }
087:
088: /**
089: * Delta for a specific change
090: *
091: * @param handle DOCUMENT ME!
092: * @param kind DOCUMENT ME!
093: * @param changes DOCUMENT ME!
094: *
095: * @throws IllegalArgumentException DOCUMENT ME!
096: */
097: public DefaultResolveDelta(Resolve handle, Kind kind, List changes) {
098: this .kind = kind;
099:
100: if (changes == null) {
101: changes = new ArrayList();
102: }
103:
104: this .children = Collections.unmodifiableList(changes);
105: this .handle = handle;
106:
107: if (kind == Kind.REPLACED) {
108: throw new IllegalArgumentException(
109: "New handle required in replace event.");
110: }
111:
112: newHandle = null;
113: }
114:
115: /**
116: * Delta for handle repalcement.
117: *
118: * <p>
119: * Used to indicate the actual connection used by the handle has been
120: * replaced. Layers should foget everything they no and latch onto the
121: * newHandle.
122: * </p>
123: *
124: * @param handle DOCUMENT ME!
125: * @param newHandle DOCUMENT ME!
126: * @param changes DOCUMENT ME!
127: */
128: public DefaultResolveDelta(Resolve handle, Resolve newHandle,
129: List changes) {
130: this .kind = Kind.REPLACED;
131:
132: if (changes == null) {
133: changes = new ArrayList();
134: }
135:
136: this .children = Collections.unmodifiableList(changes);
137: this .handle = handle;
138: this .newHandle = newHandle;
139: }
140:
141: /*
142: * @see net.refractions.udig.catalog.ICatalogDelta#accept(net.refractions.udig.catalog.IServiceVisitor)
143: */
144: public void accept(ResolveDeltaVisitor visitor) throws IOException {
145: visitor.visit(this );
146:
147: for (Iterator itr = children.iterator(); itr.hasNext();) {
148: DefaultResolveDelta delta = (DefaultResolveDelta) itr
149: .next();
150:
151: if ((delta != null) && visitor.visit(delta)) {
152: delta.accept(visitor);
153: }
154: }
155: }
156:
157: /*
158: * @see net.refractions.udig.catalog.ICatalogDelta#getAffected()
159: */
160: public List getChildren() {
161: return children;
162: }
163:
164: /*
165: * @see net.refractions.udig.catalog.ICatalogDelta#getAffected(int, int)
166: */
167: public List getChildren(Set kindMask) {
168: List list = new ArrayList();
169:
170: for (Iterator itr = children.iterator(); itr.hasNext();) {
171: DefaultResolveDelta delta = (DefaultResolveDelta) itr
172: .next();
173:
174: if ((delta != null) && kindMask.contains(delta.getKind())) {
175: list.add(delta);
176: }
177: }
178:
179: return list;
180: }
181:
182: /*
183: * @see net.refractions.udig.catalog.IDelta#getKind()
184: */
185: public Kind getKind() {
186: return kind;
187: }
188:
189: public Resolve getResolve() {
190: return handle;
191: }
192:
193: public Resolve getNewResolve() {
194: return newHandle;
195: }
196:
197: public String toString() {
198: StringBuffer buffer = new StringBuffer();
199:
200: /*
201: * private List<IResolveDelta> children; private Kind kind = Kind.NO_CHANGE; private
202: * IResolve handle = null; private IResolve newHandle = null;
203: */
204: buffer.append(" ResolveDelta: ["); //$NON-NLS-1$
205: buffer.append(kind);
206:
207: if (handle != null) {
208: buffer.append(","); //$NON-NLS-1$
209: buffer.append(handle);
210: }
211:
212: if (newHandle != null) {
213: buffer.append(","); //$NON-NLS-1$
214: buffer.append(newHandle);
215: }
216:
217: if (children != null) {
218: buffer.append("children ["); //$NON-NLS-1$
219:
220: for (int i = 0; i < children.size(); i++) {
221: DefaultResolveDelta delta = (DefaultResolveDelta) children
222: .get(i);
223: buffer.append(delta.getKind());
224: }
225:
226: buffer.append("] "); //$NON-NLS-1$
227: }
228:
229: buffer.append("] "); //$NON-NLS-1$
230:
231: return buffer.toString();
232: }
233: }
|