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.util;
018:
019: import java.io.IOException;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import net.refractions.udig.catalog.IResolve;
024: import net.refractions.udig.catalog.IResolveChangeEvent;
025: import net.refractions.udig.catalog.IResolveDelta;
026: import net.refractions.udig.catalog.IResolveDeltaVisitor;
027: import net.refractions.udig.catalog.IResolveDelta.Kind;
028:
029: /**
030: * Easy of use visitor for search IResolveDeltas.
031: * <p>
032: * After a run found == Delta that best matches the provided handle.
033: * <p>
034: * Will be null if no matches were found that were interesting. Where: interesting != NO_CHANGE
035: * </p>
036: * <p>
037: * You can use this code as an example of a good IResolveDeltaVisitor.
038: * </p>
039: *
040: * @author jgarnett
041: * @since 0.6.0
042: */
043: public class SearchResolveDeltaVisitor implements IResolveDeltaVisitor {
044: private IResolveDelta found;
045: private List<IResolve> path;
046:
047: public SearchResolveDeltaVisitor(IResolve handle) {
048: path = path(handle);
049: found = null;
050: }
051:
052: /** Find available parents if provided handle */
053: static List<IResolve> path(IResolve handle) {
054: IResolve handle2 = handle;
055:
056: LinkedList<IResolve> path = new LinkedList<IResolve>();
057: while (handle2 != null) {
058: path.addFirst(handle2);
059: try {
060: handle2 = handle2.parent(null);
061: } catch (IOException e) {
062: handle2 = null; // no more parents
063: }
064: }
065: return path;
066: }
067:
068: /**
069: * Best match IResolveDelta for handle, may be null if search came up empty.
070: *
071: * @return Best match IResolveDelta for handle
072: */
073: public IResolveDelta getFound() {
074: return found;
075: }
076:
077: public boolean visit(IResolveDelta delta) {
078: if (delta.getKind() != Kind.NO_CHANGE
079: && path.contains(delta.getResolve())) {
080: found = delta;
081: }
082: return true;
083: }
084:
085: /**
086: * Quick method that uses this visitor to search an event.
087: * <p>
088: * This serves as a good example of using a visitor.
089: * </p>
090: */
091: public static IResolveDelta search(IResolve handle,
092: IResolveChangeEvent event) {
093: if (handle == null || event == null)
094: return null;
095:
096: IResolveDelta delta = event.getDelta();
097: if (delta == null)
098: return null;
099:
100: SearchResolveDeltaVisitor visitor = new SearchResolveDeltaVisitor(
101: handle);
102: try {
103: delta.accept(visitor);
104: return visitor.getFound();
105: } catch (IOException e) {
106: return null; // visitor obviously could not find anything
107: }
108: }
109: }
|