001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.diagram;
024:
025: import java.awt.Color;
026: import java.awt.Font;
027: import java.awt.Frame;
028: import java.awt.event.ActionEvent;
029: import java.awt.geom.Point2D;
030: import java.awt.geom.Rectangle2D;
031: import java.math.BigInteger;
032: import java.util.ArrayList;
033: import java.util.Enumeration;
034: import java.util.Hashtable;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.Properties;
039:
040: import javax.swing.AbstractAction;
041: import javax.swing.JPopupMenu;
042: import javax.swing.SwingConstants;
043: import javax.swing.SwingUtilities;
044:
045: import org.jgraph.JGraph;
046: import org.jgraph.graph.CellView;
047: import org.jgraph.graph.DefaultGraphCell;
048: import org.jgraph.graph.DefaultPort;
049: import org.jgraph.graph.GraphConstants;
050: import org.jgraph.graph.GraphLayoutCache;
051: import org.jgraph.graph.Port;
052:
053: import biz.hammurapi.diagram.data.Cell;
054: import biz.hammurapi.diagram.data.Point;
055: import biz.hammurapi.diagram.data.Property;
056:
057: public class DiagramCell extends DefaultGraphCell implements
058: DiagramElement {
059:
060: private DiagramModel owner;
061: private String description;
062: private String name;
063: private int id;
064: protected Cell data;
065:
066: public DiagramCell(DiagramModel owner,
067: biz.hammurapi.diagram.data.Cell data) {
068: this .owner = owner;
069: if (data == null) {
070: id = owner.nextElementId();
071: setName("Cell " + id);
072: } else {
073: id = data.getElementId().intValue();
074: setName(data.getName());
075: setDescription(data.getDescription());
076:
077: Property[] pa = data.getPropertyArray();
078: for (int i = 0; pa != null && i < pa.length; ++i) {
079: setProperty(pa[i].getName(), pa[i].getStringValue());
080: }
081:
082: pa = data.getViewPropertyArray();
083: for (int i = 0; pa != null && i < pa.length; ++i) {
084: setViewProperty(pa[i].getName(), pa[i].getStringValue());
085: }
086:
087: this .data = data;
088: }
089: }
090:
091: public void setUserObject(Object userObject) {
092: String newName = userObject.toString();
093: setName(newName);
094: }
095:
096: public Object getUserObject() {
097: return getName();
098: }
099:
100: public String getDescription() {
101: return description;
102: }
103:
104: public String getName() {
105: return name;
106: }
107:
108: public DiagramModel getModel() {
109: return owner;
110: }
111:
112: public void setDescription(String description) {
113: this .description = description;
114: }
115:
116: public void setName(String name) {
117: this .name = name;
118: super .setUserObject(name);
119: }
120:
121: public void store(JGraph graph,
122: biz.hammurapi.diagram.data.Cell data,
123: boolean nestConnections) {
124: data.setName(getName());
125: data.setElementId(new BigInteger(String.valueOf(id)));
126: data.setDescription(getDescription());
127: data.setType(getClass().getName());
128:
129: CellView pageView = graph.getGraphLayoutCache().getMapping(
130: this , false);
131: Rectangle2D bounds = pageView.getBounds();
132: data.setX(bounds.getX());
133: data.setY(bounds.getY());
134:
135: Enumeration pne = getPropertyNames();
136: while (pne.hasMoreElements()) {
137: String name = (String) pne.nextElement();
138: Property property = data.addNewProperty();
139: property.setName(name);
140: property.setStringValue(properties.getProperty(name));
141: }
142:
143: Enumeration vpne = getViewPropertyNames();
144: while (vpne.hasMoreElements()) {
145: String name = (String) vpne.nextElement();
146: Property property = data.addNewViewProperty();
147: property.setName(name);
148: property.setStringValue(viewProperties.getProperty(name));
149: }
150:
151: if (nestConnections) {
152: Iterator it = getChildren().iterator();
153: while (it.hasNext()) {
154: Object next = it.next();
155: if (next instanceof DefaultPort) {
156: DefaultPort pNext = (DefaultPort) next;
157: Object pName = pNext.getUserObject();
158: biz.hammurapi.diagram.data.Port port = data
159: .addNewPort();
160: if (!isBlank(pName)) {
161: port.setName(pName.toString());
162: }
163:
164: Iterator eit = pNext.getEdges().iterator();
165: while (eit.hasNext()) {
166: Object edge = eit.next();
167: if (edge instanceof DiagramEdge) {
168: DiagramEdge dEdge = (DiagramEdge) edge;
169: if (dEdge.getSource() == pNext) {
170: biz.hammurapi.diagram.data.Edge edgeData = port
171: .addNewEdge();
172: dEdge.store(graph, edgeData,
173: nestConnections);
174: }
175: }
176: }
177: }
178: }
179: }
180: }
181:
182: public int getId() {
183: return id;
184: }
185:
186: public void setAttributes(Map attributes) {
187: GraphConstants.setGradientColor(attributes, Color.LIGHT_GRAY);
188: // Add a Border Color Attribute to the Map
189: GraphConstants.setBorderColor(attributes, Color.BLACK);
190:
191: // Add a White Background
192: GraphConstants.setBackground(attributes, Color.white);
193: // Make Vertex Opaque
194: GraphConstants.setOpaque(attributes, true);
195:
196: //GraphConstants.setConstrained(map, true);
197: GraphConstants.setAutoSize(attributes, true);
198:
199: GraphConstants.setInset(attributes, 5);
200: GraphConstants.setHorizontalAlignment(attributes,
201: SwingConstants.CENTER);
202:
203: Font font = GraphConstants.DEFAULTFONT;
204: GraphConstants.setFont(attributes, font.deriveFont(Font.PLAIN,
205: 10));
206: }
207:
208: public static boolean isBlank(Object obj) {
209: return obj == null || obj.toString().trim().length() == 0;
210: }
211:
212: public DefaultPort getPort(String name) {
213: Iterator it = getChildren().iterator();
214: while (it.hasNext()) {
215: Object next = it.next();
216: if (next instanceof DefaultPort) {
217: DefaultPort pNext = (DefaultPort) next;
218: Object pName = pNext.getUserObject();
219: if (isBlank(name)) {
220: if (isBlank(pName)) {
221: return pNext;
222: }
223: } else if (pName != null
224: && name.equals(pName.toString())) {
225: return pNext;
226: }
227: }
228: }
229: return null;
230: }
231:
232: void setId(int id) {
233: this .id = id;
234: }
235:
236: public void connect() {
237: if (data != null) {
238: GraphLayoutCache glc = owner.getGraph()
239: .getGraphLayoutCache();
240: biz.hammurapi.diagram.data.Port[] portArray = data
241: .getPortArray();
242: for (int k = 0; k < portArray.length; ++k) {
243: Port source = getPort(portArray[k].getName());
244: if (source != null) {
245: biz.hammurapi.diagram.data.Edge[] ea = portArray[k]
246: .getEdgeArray();
247: for (int i = 0; i < ea.length; ++i) {
248: DiagramEdge edge = (DiagramEdge) owner
249: .createElement(ea[i]);
250: if (edge != null) {
251: Map edgeAttributes = new Hashtable();
252: ((DiagramElement) edge)
253: .setAttributes(edgeAttributes);
254:
255: edge.getAttributes().applyMap(
256: edgeAttributes);
257:
258: BigInteger targetId = ea[i].getTarget();
259: if (targetId != null) {
260: DiagramCell targetCell = (DiagramCell) owner
261: .getDiagramElement(targetId
262: .intValue());
263: if (targetCell != null) {
264: Port target = targetCell
265: .getPort(ea[i]
266: .getTargetPort());
267:
268: if (target != null) {
269: // Insert the Edge and its Attributes
270: glc.insertEdge(edge, source,
271: target);
272:
273: Point[] pa = ea[i]
274: .getPointArray();
275: if (pa != null && pa.length > 0) {
276: List allPoints = new ArrayList();
277: allPoints.add(source);
278: for (int j = 0; j < pa.length; ++j) {
279: allPoints
280: .add(new Point2D.Double(
281: pa[j]
282: .getX(),
283: pa[j]
284: .getY()));
285: }
286: allPoints.add(target);
287: Map newAttributes = new Hashtable();
288: GraphConstants.setPoints(
289: newAttributes,
290: allPoints);
291: glc.editCell(edge,
292: newAttributes);
293: }
294: }
295: }
296: }
297: }
298: }
299: }
300: }
301: }
302: }
303:
304: protected Properties properties = new Properties();
305:
306: public void setProperty(String name, String value) {
307: properties.setProperty(name, value);
308: }
309:
310: public String getProperty(String name) {
311: return properties.getProperty(name);
312: }
313:
314: public Enumeration getPropertyNames() {
315: return properties.propertyNames();
316: }
317:
318: protected Properties viewProperties = new Properties();
319:
320: public void setViewProperty(String name, String value) {
321: viewProperties.setProperty(name, value);
322: }
323:
324: public String getViewProperty(String name) {
325: return viewProperties.getProperty(name);
326: }
327:
328: public Enumeration getViewPropertyNames() {
329: return viewProperties.propertyNames();
330: }
331:
332: public void populatePopupMenu(final DiagramApplet applet,
333: final java.awt.Point pt, final JPopupMenu menu) {
334: // Edit
335: menu.add(new AbstractAction("Edit") {
336: public void actionPerformed(ActionEvent e) {
337: Frame frame = (Frame) SwingUtilities
338: .getAncestorOfClass(Frame.class, applet
339: .getGraph());
340: DiagramPropertiesDialog pd = new DiagramPropertiesDialog(
341: frame, DiagramCell.this );
342: if (pd.edit()) {
343: JGraph graph = applet.getGraph();
344: GraphLayoutCache graphLayoutCache = graph
345: .getGraphLayoutCache();
346: CellView cellView = graphLayoutCache.getMapping(
347: DiagramCell.this , false);
348: graph.updateAutoSize(cellView);
349: }
350: }
351: });
352: }
353:
354: /**
355: * @return true if cell shall be rendered with rounded corners
356: */
357: public boolean isRounded() {
358: return true;
359: }
360:
361: }
|