001: /*
002: * $Id: JGraphpadBusinessObject.java,v 1.4 2005/09/11 08:50:27 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.graph;
011:
012: import java.awt.Component;
013: import java.io.Serializable;
014: import java.util.Hashtable;
015: import java.util.Iterator;
016: import java.util.Map;
017:
018: /**
019: * User object with a dynamic set of properties in a hashtable. The property
020: * under {@link #valueKey} is used in {@link #toString()} to represent the
021: * object as a string. This object supports values of type
022: * {@link JGraphpadRichTextValue}.
023: *
024: * @see JGraphpadGraphModel
025: */
026: public class JGraphpadBusinessObject implements Cloneable, Serializable {
027:
028: /**
029: * Key to use for converting this object to a string.
030: */
031: public static String valueKey = "value";
032:
033: /**
034: * Holds the properties as (key, value) pairs.
035: */
036: protected Map properties = new Hashtable();
037:
038: /**
039: * Constructs a business object with an empty string as its value.
040: */
041: public JGraphpadBusinessObject() {
042: this ("");
043: }
044:
045: /**
046: * Constructs a business object with the specified value.
047: *
048: * @param value
049: * The value of the new object.
050: */
051: public JGraphpadBusinessObject(Object value) {
052: setValue(value);
053: }
054:
055: /**
056: * Returns the properties.
057: *
058: * @return Returns the properties.
059: */
060: public Map getProperties() {
061: return properties;
062: }
063:
064: /**
065: * Sets the properties.
066: *
067: * @param properties
068: * The properties to set.
069: */
070: public void setProperties(Map properties) {
071: this .properties = properties;
072: }
073:
074: /**
075: * Sets the value for {@link #valueKey}.
076: *
077: * @param value
078: * The value to set.
079: */
080: public void setValue(Object value) {
081: putProperty(valueKey, value);
082: }
083:
084: /**
085: * Returns the value for {@link #valueKey}.
086: *
087: * @return Returns the value.
088: */
089: public Object getValue() {
090: return getProperty(valueKey);
091: }
092:
093: /**
094: * Returns true if the value is a rich text value.
095: *
096: * @return Returns true if value is rich text.
097: */
098: public boolean isRichText() {
099: return getValue() instanceof JGraphpadRichTextValue;
100: }
101:
102: /**
103: * Returns true if the value is a component.
104: *
105: * @return Returns true if value is a component.
106: */
107: public boolean isComponent() {
108: return getValue() instanceof Component;
109: }
110:
111: /**
112: * Sets the property under the specified key.
113: *
114: * @param key
115: * They key of the property.
116: * @param value
117: * The value of the property.
118: * @return Returns the previous value.
119: */
120: public Object putProperty(Object key, Object value) {
121: if (key != null && value != null)
122: return properties.put(key, value);
123: return null;
124: }
125:
126: /**
127: * Returns the property under the specified key.
128: *
129: * @param key
130: * The key of the property.
131: * @return Returns the specified property.
132: */
133: public Object getProperty(Object key) {
134: if (key != null)
135: return properties.get(key);
136: return null;
137: }
138:
139: /**
140: * Hook for subclassers to create a custom tooltip for this user object.
141: * This is used in {@link JGraphpadGraph#getToolTipForCell(Object)}.
142: *
143: * @return Returns a tooltip for the user object.
144: */
145: public String getTooltip() {
146: String html = "";
147: String title = toString();
148: if (title.length() > 0)
149: html += "<strong>" + chopString(title, 20)
150: + "</strong><br>";
151: Iterator it = getProperties().entrySet().iterator();
152: while (it.hasNext()) {
153: Map.Entry element = (Map.Entry) it.next();
154: if (!element.getKey().equals(valueKey))
155: html += element.getKey()
156: + ": "
157: + chopString(
158: String.valueOf(element.getValue()), 20)
159: + "<br>";
160: }
161: return html;
162: }
163:
164: /**
165: * A helper method that crops string to the specified length, adding 3 dots
166: * if there were more characters.
167: *
168: * @return The chopped string.
169: */
170: protected String chopString(String s, int max) {
171: if (s != null) {
172: if (s.length() > max)
173: s = s.substring(0, max) + "...";
174: }
175: return s;
176: }
177:
178: /**
179: * Returns the string representation of the value or an empty string if no
180: * value exists.
181: *
182: * @return Returns the value as a string.
183: */
184: public String toString() {
185: Object value = getValue();
186: if (value != null)
187: return String.valueOf(value);
188: return "";
189: }
190:
191: /**
192: * Returns a clone of the object. Note: The properties are not cloned, only
193: * a clone of the containing map is used. As a special case, if the user
194: * object is a JSlider or a JTree then a new instance will be put in place
195: * of the old instance.
196: *
197: * @return Returns a clone of this object.
198: */
199: public Object clone() {
200: JGraphpadBusinessObject clone;
201: try {
202: clone = (JGraphpadBusinessObject) super .clone();
203: } catch (CloneNotSupportedException e) {
204: clone = new JGraphpadBusinessObject();
205: }
206: clone.setProperties(new Hashtable(getProperties()));
207: if (isComponent()) {
208: Object value = getValue();
209: if (value instanceof JGraphpadHeavyweightRenderer) {
210: clone.setValue(((JGraphpadHeavyweightRenderer) value)
211: .clone());
212: }
213: }
214: return clone;
215: }
216:
217: }
|