001: /*
002: * @(#)GraphTransferable.java 1.0 03-JUL-04
003: *
004: * Copyright (c) 2001-2004 Gaudenz Alder
005: *
006: */
007: package org.jgraph.graph;
008:
009: import java.awt.datatransfer.Clipboard;
010: import java.awt.datatransfer.ClipboardOwner;
011: import java.awt.datatransfer.DataFlavor;
012: import java.awt.datatransfer.Transferable;
013: import java.awt.datatransfer.UnsupportedFlavorException;
014: import java.awt.geom.Rectangle2D;
015: import java.io.Serializable;
016: import java.util.Map;
017:
018: import org.jgraph.plaf.basic.BasicGraphTransferable;
019:
020: /**
021: * An object that represents the clipboard contents for a graph selection.
022: * The object has three representations:
023: * <p>
024: * 1. Richer: The cells, view attributes and connections for this selection are
025: * stored as separate datastructures, which can be inserted using
026: * the GraphModel.insert() method.
027: * 2. HTML: If one cell is selected, the userObject is returned as HTML.
028: * 3. Plain: The userObject of the selected cell is returned as plain text.
029: *
030: * @author Gaudenz Alder
031: * @version 1.0 1/1/02
032: *
033: */
034:
035: public class GraphTransferable extends BasicGraphTransferable implements
036: Serializable, ClipboardOwner {
037:
038: /** Local Machine Reference Data Flavor. */
039: public static DataFlavor dataFlavor;
040:
041: /** Selected cells. */
042: protected Object[] cells;
043:
044: /** Object that describes the connection between cells. */
045: protected ConnectionSet cs;
046:
047: /** Object that describes the group structure between cells. */
048: protected ParentMap pm;
049:
050: /** (Cell, Map) entries that hold the view attributes for the cells. */
051: protected Map attributeMap;
052:
053: /** Rectangle that defines the former bounds of the views. */
054: protected Rectangle2D bounds;
055:
056: /**
057: * Constructs a new transferable selection for <code>cells</code>,
058: * <code>cs</code>and <code>attrMap</code>.
059: */
060: public GraphTransferable(Object[] cells, Map attrMap,
061: Rectangle2D bounds, ConnectionSet cs, ParentMap pm) {
062:
063: attributeMap = attrMap;
064: this .bounds = bounds;
065: this .cells = cells;
066: this .cs = cs;
067: this .pm = pm;
068: }
069:
070: /**
071: * Returns the <code>cells</code> that represent the selection.
072: */
073: public Object[] getCells() {
074: return cells;
075: }
076:
077: /**
078: * Returns the connections between <code>cells</code> (and possibly
079: * other, unselected cells).
080: */
081: public ConnectionSet getConnectionSet() {
082: return cs;
083: }
084:
085: public ParentMap getParentMap() {
086: return pm;
087: }
088:
089: /**
090: * Returns a map of (GraphCell, Map)-pairs that represent the
091: * view attributes for the respecive cells.
092: */
093: public Map getAttributeMap() {
094: return attributeMap;
095: }
096:
097: public Rectangle2D getBounds() {
098: return bounds;
099: }
100:
101: // from ClipboardOwner
102: public void lostOwnership(Clipboard clip, Transferable contents) {
103: // do nothing
104: }
105:
106: // --- Richer ----------------------------------------------------------
107:
108: /**
109: * Returns the jvm-localreference flavors of the transferable.
110: */
111: public DataFlavor[] getRicherFlavors() {
112: return new DataFlavor[] { dataFlavor };
113: }
114:
115: /**
116: * Fetch the data in a jvm-localreference format.
117: */
118: public Object getRicherData(DataFlavor flavor)
119: throws UnsupportedFlavorException {
120: if (flavor.equals(dataFlavor))
121: return this ;
122: else
123: throw new UnsupportedFlavorException(flavor);
124: }
125:
126: // --- Plain ----------------------------------------------------------
127:
128: /**
129: * Returns true if the transferable support a text/plain format.
130: */
131: public boolean isPlainSupported() {
132: return (cells != null && cells.length == 1);
133: }
134:
135: /**
136: * Fetch the data in a text/plain format.
137: */
138: public String getPlainData() {
139: if (cells[0] instanceof DefaultGraphCell) {
140: Object obj = ((DefaultGraphCell) cells[0]).getUserObject();
141: if (obj != null)
142: return obj.toString();
143: }
144: return cells[0].toString();
145: }
146:
147: // --- HTML ---------------------------------------------------------
148:
149: /**
150: * Returns true if the transferable support a text/html format.
151: */
152: public boolean isHTMLSupported() {
153: return isPlainSupported();
154: }
155:
156: /**
157: * Fetch the data in a text/html format.
158: */
159: public String getHTMLData() {
160: StringBuffer buf = new StringBuffer();
161: buf.append("<html><body><p>");
162: buf.append(getPlainData());
163: buf.append("</p></body></html>");
164: return buf.toString();
165: }
166:
167: /* Local Machine Reference Data Flavor. */
168: static {
169: DataFlavor localDataFlavor;
170: try {
171: localDataFlavor = new DataFlavor(
172: DataFlavor.javaJVMLocalObjectMimeType
173: + "; class=org.jgraph.graph.GraphTransferable");
174: } catch (ClassNotFoundException cnfe) {
175: localDataFlavor = null;
176: }
177: dataFlavor = localDataFlavor;
178: }
179:
180: }
|