001: /*
002: * $Id: JGraphpadL2FPropertySheet.java,v 1.4 2005/08/28 21:28:32 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.l2fplugin;
011:
012: import java.awt.Component;
013: import java.awt.geom.Point2D;
014: import java.awt.geom.Rectangle2D;
015: import java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.jgraph.JGraph;
022: import org.jgraph.graph.GraphConstants;
023: import org.jgraph.graph.PortView;
024: import org.w3c.dom.Node;
025:
026: import com.jgraph.editor.JGraphEditorResources;
027: import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
028: import com.jgraph.pad.graph.JGraphpadBusinessObject;
029: import com.jgraph.pad.util.JGraphpadFocusManager;
030: import com.l2fprod.common.propertysheet.DefaultProperty;
031: import com.l2fprod.common.propertysheet.Property;
032: import com.l2fprod.common.propertysheet.PropertySheetPanel;
033:
034: /**
035: * Property sheet to display cell and user object properties.
036: */
037: public class JGraphpadL2FPropertySheet extends PropertySheetPanel {
038:
039: /**
040: * Constructs a new property sheet.
041: */
042: public JGraphpadL2FPropertySheet() {
043: setSortingProperties(true);
044: setBorder(null);
045: }
046:
047: /**
048: * Updates the display properties to show the properties of the selection
049: * cell in the specified graph.
050: *
051: * @param graph
052: * The graph to display the selection cell properties for.
053: */
054: public void update(JGraph graph) {
055: if (graph != null && !graph.isSelectionEmpty()) {
056: Object cell = graph.getSelectionCell();
057: Map attrs = graph.getModel().getAttributes(cell);
058: Property[] properties = createProperties(
059: JGraphEditorResources.getString("Graph"), attrs,
060: createPropertyChangeListener(graph, cell));
061:
062: // Adds the business object properties to the array
063: Object obj = graph.getModel().getValue(cell);
064: if (obj instanceof JGraphpadBusinessObject) {
065: Property[] tmp1 = createProperties(
066: JGraphEditorResources.getString("Business"),
067: ((JGraphpadBusinessObject) obj).getProperties(),
068: createPropertyChangeListener(graph, obj));
069: Property[] tmp2 = properties;
070: properties = new Property[tmp1.length + tmp2.length];
071: System.arraycopy(tmp1, 0, properties, 0, tmp1.length);
072: System.arraycopy(tmp2, 0, properties, tmp1.length,
073: tmp2.length);
074: }
075: setProperties(properties);
076: } else {
077: // Resets properties
078: //setProperties(new Property[0]);
079: }
080: }
081:
082: /**
083: * Creates an array of properties out of the key, value pairs in the
084: * specified map using the specified category.
085: *
086: * @param map
087: * The map that contains the key, value pairs to be added.
088: * @param category
089: * The category of the new properties.
090: * @return Returns an array of properties for <code>map</code>.
091: */
092: protected Property[] createProperties(String category, Map map,
093: PropertyChangeListener listener) {
094: Property[] properties = new Property[map.size()];
095: Iterator it = map.entrySet().iterator();
096: int i = 0;
097: while (it.hasNext()) {
098: Map.Entry entry = (Map.Entry) it.next();
099: properties[i++] = createProperty(category, String
100: .valueOf(entry.getKey()), entry.getValue(),
101: listener);
102: }
103: return properties;
104: }
105:
106: /**
107: * Creates a new property using the specified name, value and category.
108: *
109: * @param name
110: * The name of the property to be created.
111: * @param value
112: * The value of the property to be created.
113: * @param category
114: * The category of the new property.
115: * @return Returns a new property.
116: */
117: protected Property createProperty(String category, String name,
118: Object value, PropertyChangeListener listener) {
119: DefaultProperty property = new DefaultProperty();
120: property.setDisplayName(name);
121: property.setShortDescription(name);
122: property.setName(name);
123: property.setType(value.getClass());
124: property.setValue(convertValue(value));
125: property.setEditable(listener != null);
126: property.setCategory(category);
127: if (listener != null)
128: property.addPropertyChangeListener(listener);
129: return property;
130: }
131:
132: /**
133: * Converts the value to a string representation if it is not supported as
134: * an editable value by the property sheet.
135: */
136: public Object convertValue(Object value) {
137: if (value instanceof Rectangle2D) {
138: Rectangle2D rect = (Rectangle2D) value;
139: value = "[x=" + rect.getX() + ", y=" + rect.getY() + ", w="
140: + rect.getWidth() + ", h=" + rect.getHeight() + "]";
141: } else if (value instanceof PortView) {
142: PortView pv = (PortView) value;
143: Point2D p = pv.getLocation();
144: value = "port[null]";
145: if (p != null)
146: value = "port[x=" + p.getX() + ", y=" + p.getY() + "]";
147: } else if (value instanceof Point2D) {
148: Point2D p = (Point2D) value;
149: value = "[x=" + p.getX() + ", y=" + p.getY() + "]";
150: } else if (value instanceof List) {
151: String entries = "";
152: Iterator it = ((List) value).iterator();
153: if (it.hasNext())
154: entries = String.valueOf(convertValue(it.next()));
155: while (it.hasNext())
156: entries += ", "
157: + String.valueOf(convertValue(it.next()));
158: value = entries;
159: }
160: return value;
161: }
162:
163: /**
164: * Creates a property change listener that uses the specified keyCell to add
165: * an entry to the nested map for the changed attributes.
166: *
167: * @param graph
168: * The graph to be used for changing the key cell.
169: * @param keyCell
170: * The object to be used as a key in the nested map.
171: */
172: protected PropertyChangeListener createPropertyChangeListener(
173: final JGraph graph, final Object keyCell) {
174: return new PropertyChangeListener() {
175: public void propertyChange(PropertyChangeEvent arg0) {
176: Property source = (Property) arg0.getSource();
177: Map nested = GraphConstants.createAttributes(keyCell,
178: source.getName(), arg0.getNewValue());
179:
180: // Checks if we're dealing with a business object and
181: // redirect the edit call to the model if we do.
182: if (!graph.getModel().contains(keyCell))
183: graph.getModel().edit(nested, null, null, null);
184: else
185: graph.getGraphLayoutCache().edit(nested, null,
186: null, null);
187: }
188: };
189: }
190:
191: public static class FactoryMethod extends JGraphEditorFactoryMethod {
192:
193: /**
194: * Defines the default name for factory methods of this kind.
195: */
196: public static String NAME = "createPropertySheet";
197:
198: /**
199: * Constructs a new factory method using {@link #NAME}.
200: */
201: public FactoryMethod() {
202: super (NAME);
203: }
204:
205: /*
206: * (non-Javadoc)
207: */
208: public Component createInstance(Node configuration) {
209: final JGraphpadL2FPropertySheet propertySheet = new JGraphpadL2FPropertySheet();
210: propertySheet.setSortingCategories(true);
211: propertySheet.setFocusable(false);
212: // Updates the property sheet on selection, model and layout cache
213: // changes.
214: final JGraphpadFocusManager focusedGraph = JGraphpadFocusManager
215: .getCurrentGraphFocusManager();
216: focusedGraph
217: .addPropertyChangeListener(new PropertyChangeListener() {
218: public void propertyChange(PropertyChangeEvent e) {
219: String prop = e.getPropertyName();
220: JGraph graph = focusedGraph
221: .getFocusedGraph();
222: if (JGraphpadFocusManager.SELECTION_CHANGE_NOTIFICATION
223: .equals(prop)
224: || JGraphpadFocusManager.MODEL_CHANGE_NOTIFICATION
225: .equals(prop)
226: || JGraphpadFocusManager.UNDOABLE_CHANGE_NOTIFICATION
227: .equals(prop)
228: || JGraphpadFocusManager.FOCUSED_GRAPH_PROPERTY
229: .equals(prop)) {
230:
231: // This is fired when switching from the graph
232: // to the property panel for example and should
233: // be ignored.
234: if (prop
235: .equals(JGraphpadFocusManager.FOCUSED_GRAPH_PROPERTY)
236: && e.getNewValue() == null)
237: return;
238: propertySheet.update(graph);
239: }
240: }
241: });
242:
243: return propertySheet;
244: }
245:
246: }
247:
248: }
|