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