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.internal.ui;
016:
017: import javax.xml.transform.TransformerException;
018:
019: import net.refractions.udig.ui.AbstractTextStrategizedTransfer;
020:
021: import org.eclipse.swt.dnd.TextTransfer;
022: import org.eclipse.swt.dnd.Transfer;
023: import org.eclipse.swt.dnd.TransferData;
024: import org.geotools.gml.producer.GeometryTransformer;
025:
026: import com.vividsolutions.jts.geom.Geometry;
027: import com.vividsolutions.jts.io.ParseException;
028: import com.vividsolutions.jts.io.WKTReader;
029: import com.vividsolutions.jts.io.WKTWriter;
030:
031: public class GeometryTextTransfer extends
032: AbstractTextStrategizedTransfer implements UDIGTransfer {
033:
034: private static GeometryTextTransfer _instance = new GeometryTextTransfer();
035:
036: private GeometryTextTransfer() {
037: }
038:
039: /**
040: * Returns the singleton instance of the TextTransfer class.
041: *
042: * @return the singleton instance of the TextTransfer class
043: */
044: public static GeometryTextTransfer getInstance() {
045: return _instance;
046: }
047:
048: private TransferStrategy[] transferStrategies;
049:
050: @Override
051: public synchronized TransferStrategy[] getAllStrategies() {
052: if (transferStrategies == null) {
053: transferStrategies = new TransferStrategy[] { new JtsWktStrategy() };
054: }
055:
056: TransferStrategy[] copy = new TransferStrategy[transferStrategies.length];
057: System.arraycopy(transferStrategies, 0, copy, 0,
058: transferStrategies.length);
059: return copy;
060: }
061:
062: @Override
063: public String[] getStrategyNames() {
064: return new String[] { "JTS WKT" }; //$NON-NLS-1$
065: }
066:
067: @Override
068: public String getTransferName() {
069: return "Geometry"; //$NON-NLS-1$
070: }
071:
072: @Override
073: public TransferStrategy getDefaultStrategy() {
074: return getAllStrategies()[0];
075: }
076:
077: @Override
078: public TransferData[] getSupportedTypes() {
079: return TextTransfer.getInstance().getSupportedTypes();
080: }
081:
082: @Override
083: public boolean isSupportedType(TransferData transferData) {
084: return TextTransfer.getInstance().isSupportedType(transferData);
085: }
086:
087: public boolean validate(Object object) {
088: return object instanceof Geometry;
089: }
090:
091: /**
092: * This strategy exports geometries as Well Known Text as generated by JTS WKTWriter.
093: *
094: * @author jeichar
095: */
096: public static class JtsWktStrategy implements TransferStrategy {
097:
098: /**
099: * @see Transfer#javaToNative
100: */
101: public void javaToNative(Object object,
102: TransferData transferData) {
103: String stringToEncode;
104:
105: Geometry feature = (Geometry) object;
106:
107: WKTWriter writer = new WKTWriter();
108: String geometry = writer.writeFormatted(feature);
109:
110: stringToEncode = geometry;
111: TextTransfer.getInstance().javaToNative(stringToEncode,
112: transferData);
113: }
114:
115: /**
116: * @see Transfer#nativeToJava
117: */
118: @SuppressWarnings("deprecation")//$NON-NLS-1$
119: public Object nativeToJava(TransferData transferData) {
120: String string = (String) TextTransfer.getInstance()
121: .nativeToJava(transferData);
122:
123: WKTReader reader = new WKTReader();
124: try {
125: return reader.read(string);
126: } catch (ParseException e) {
127: // JONES
128: }
129: return null;
130: }
131:
132: }
133:
134: /**
135: * This strategy encodes geometries as GML Geometries.
136: *
137: * @author jeichar
138: */
139: public static class GMLStrategy implements TransferStrategy {
140:
141: /**
142: * @see Transfer#javaToNative
143: */
144: public void javaToNative(Object object,
145: TransferData transferData) {
146: Geometry geometry = (Geometry) object;
147: GeometryTransformer transformer = new GeometryTransformer();
148:
149: transformer.setIndentation(2);
150:
151: try {
152: TextTransfer.getInstance().javaToNative(
153: transformer.transform(geometry), transferData);
154: } catch (TransformerException e) {
155: throw (RuntimeException) new RuntimeException()
156: .initCause(e);
157: }
158: }
159:
160: /**
161: * @see Transfer#nativeToJava
162: */
163: @SuppressWarnings("deprecation")//$NON-NLS-1$
164: public Object nativeToJava(TransferData transferData) {
165: // JONES
166: return null;
167: // String string = (String) TextTransfer.getInstance().nativeToJava(transferData);
168: // InputSource input = new InputSource(new StringReader(string));
169: //
170: // GMLHandlerJTS simpleGeometryHandler=new GMLHandlerJTS(){
171: //
172: // };
173: // GMLFilterGeometry filterGeometry = new GMLFilterGeometry(simpleGeometryHandler);
174: // GMLFilterDocument filterDocument = new GMLFilterDocument(filterGeometry);
175: //
176: // try {
177: // // parse xml
178: // XMLReader reader = XMLReaderFactory.createXMLReader();
179: // reader.setContentHandler(filterDocument);
180: // reader.parse(input);
181: // } catch (Exception e) {
182: // return null;
183: // }
184: //
185: // return simpleGeometryHandler.geometry();
186:
187: }
188:
189: }
190:
191: }
|