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 com.vividsolutions.jts.geom.Coordinate;
018:
019: /**
020: * Wraps a Coordinate and calculates its position only requested. This allows the point it maps to
021: * to be moved around without the more time consuming process of calculating the coordinate
022: * location.
023: *
024: * @author jones
025: * @since 1.1.0
026: */
027: public class LazyCoord extends Coordinate {
028:
029: /** long serialVersionUID field */
030: private static final long serialVersionUID = 8814031966871200006L;
031:
032: Point start;
033: private EditBlackboard blackboard;
034: Coordinate coord;
035: PointCoordCalculator pointCoordCalculator;
036: private double differenceX;
037: private double differenceY;
038:
039: private final Object obj = new Object();
040:
041: public LazyCoord(Point point2, Coordinate coord2, EditBlackboard bb2) {
042: this .start = point2;
043: pointCoordCalculator = new PointCoordCalculator(
044: bb2.pointCoordCalculator);
045: this .blackboard = bb2;
046: this .coord = coord2;
047: Coordinate calculated = pointCoordCalculator.toCoord(start);
048: this .differenceX = calculated.x - coord.x;
049: this .differenceY = calculated.y - coord.y;
050: super .x = coord.x;
051: super .y = coord.y;
052: super .z = coord.z;
053: }
054:
055: public LazyCoord(LazyCoord coord2) {
056: this (coord2.start, coord2.coord, coord2.blackboard);
057: }
058:
059: public Coordinate get(Point p) {
060:
061: boolean sameTransform = pointCoordCalculator.toScreen
062: .equals(blackboard.pointCoordCalculator.toScreen);
063:
064: if (p.equals(start) && sameTransform) {
065: super .x = coord.x;
066: super .y = coord.y;
067: super .z = coord.z;
068: return new Coordinate(this );
069: }
070:
071: double[] startEndDelta = new double[2];
072: Coordinate endTransform = blackboard.pointCoordCalculator
073: .toCoord(p);
074:
075: startEndDelta[0] = endTransform.x - coord.x;
076: startEndDelta[1] = endTransform.y - coord.y;
077:
078: coord.x = coord.x + startEndDelta[0] - differenceX;
079: coord.y = coord.y + startEndDelta[1] - differenceY;
080: super .x = coord.x;
081: super .y = coord.y;
082: pointCoordCalculator = new PointCoordCalculator(
083: blackboard.pointCoordCalculator);
084: start = p;
085:
086: return new Coordinate(this );
087: }
088:
089: public void set(Coordinate o, Point point) {
090: coord = o;
091: super .x = o.x;
092: super .y = o.y;
093: super .z = o.z;
094: start = point;
095: }
096:
097: @Override
098: public String toString() {
099: return start.toString();
100: }
101:
102: @Override
103: public boolean equals(Object obj) {
104: return (this == obj);
105: }
106:
107: @Override
108: public int hashCode() {
109: return obj.hashCode();
110: }
111:
112: }
|