01: /*
02: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
03: * for visualizing and manipulating spatial features with geometry and attributes.
04: *
05: * Copyright (C) 2003 Vivid Solutions
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: *
21: * For more information, contact:
22: *
23: * Vivid Solutions
24: * Suite #1A
25: * 2328 Government Street
26: * Victoria BC V8T 5G5
27: * Canada
28: *
29: * (250)385-6040
30: * www.vividsolutions.com
31: */
32: package com.vividsolutions.jump.workbench.ui.plugin.clipboard;
33:
34: import java.awt.datatransfer.DataFlavor;
35: import java.awt.datatransfer.UnsupportedFlavorException;
36: import java.io.StringReader;
37: import java.util.ArrayList;
38: import java.util.Collection;
39: import java.util.Collections;
40: import java.util.Iterator;
41:
42: import com.vividsolutions.jump.feature.Feature;
43: import com.vividsolutions.jump.io.FUTURE_JTS_WKTWriter;
44:
45: public class CollectionOfFeaturesTransferable extends
46: AbstractTransferable {
47: /** A java.util.Collection, not a FeatureCollection */
48: public static final DataFlavor COLLECTION_OF_FEATURES_FLAVOR = new DataFlavor(
49: Collection.class, "Collection of Features") {
50: public boolean equals(DataFlavor that) {
51: //Needed so #equals will return false for COLLECTION_OF_LAYERS_FLAVOR. [Jon Aquino]
52: return super .equals(that)
53: && getHumanPresentableName().equals(
54: that.getHumanPresentableName());
55: }
56: };
57: private static final DataFlavor[] flavors = {
58: DataFlavor.stringFlavor, //plainTextFlavor is deprecated, but JDK 1.3 needs it to paste to
59: //non-Java apps (like Notepad). [Jon Aquino]
60: DataFlavor.plainTextFlavor, COLLECTION_OF_FEATURES_FLAVOR };
61: private Collection features;
62: private FUTURE_JTS_WKTWriter writer = new FUTURE_JTS_WKTWriter();
63:
64: public CollectionOfFeaturesTransferable(Collection features) {
65: super (flavors);
66: this .features = new ArrayList(features);
67: }
68:
69: public Object getTransferData(DataFlavor flavor)
70: throws UnsupportedFlavorException {
71: if (flavor.equals(COLLECTION_OF_FEATURES_FLAVOR)) {
72: return Collections.unmodifiableCollection(features);
73: }
74: if (flavor.equals(DataFlavor.stringFlavor)) {
75: return toString(features);
76: }
77: if (flavor.equals(DataFlavor.plainTextFlavor)) {
78: return new StringReader(toString(features));
79: }
80: throw new UnsupportedFlavorException(flavor);
81: }
82:
83: private String toString(Collection features) {
84: StringBuffer b = new StringBuffer();
85: for (Iterator i = features.iterator(); i.hasNext();) {
86: Feature feature = (Feature) i.next();
87: //Not System.getProperty("line.separator"); otherwise, when you copy
88: //into, say, Notepad, you get garbage characters at the end of each line
89: //(\r\r\n). [Jon Aquino]
90: b.append(writer.writeFormatted(feature.getGeometry())
91: + "\n\n");
92: }
93: return b.toString();
94: }
95: }
|