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