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.Frame;
026: import java.awt.event.ActionEvent;
027: import java.awt.geom.Point2D;
028: import java.math.BigInteger;
029: import java.util.Enumeration;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: import javax.swing.AbstractAction;
036: import javax.swing.JPopupMenu;
037: import javax.swing.SwingUtilities;
038:
039: import org.jgraph.JGraph;
040: import org.jgraph.graph.DefaultEdge;
041: import org.jgraph.graph.DefaultPort;
042: import org.jgraph.graph.EdgeView;
043: import org.jgraph.graph.GraphConstants;
044:
045: import biz.hammurapi.diagram.data.Edge;
046: import biz.hammurapi.diagram.data.Property;
047:
048: public class DiagramEdge extends DefaultEdge implements DiagramElement {
049:
050: private DiagramModel owner;
051: protected Edge data;
052:
053: public DiagramEdge(DiagramModel owner,
054: biz.hammurapi.diagram.data.Edge data) {
055: this .owner = owner;
056: if (data == null) {
057: id = owner.nextElementId();
058: } else {
059: BigInteger elementId = data.getElementId();
060: if (elementId != null) {
061: id = elementId.intValue();
062: }
063: setName(data.getName());
064: setDescription(data.getDescription());
065:
066: Property[] pa = data.getPropertyArray();
067: for (int i = 0; pa != null && i < pa.length; ++i) {
068: setProperty(pa[i].getName(), pa[i].getStringValue());
069: }
070:
071: pa = data.getViewPropertyArray();
072: for (int i = 0; pa != null && i < pa.length; ++i) {
073: setViewProperty(pa[i].getName(), pa[i].getStringValue());
074: }
075:
076: this .data = data;
077: }
078: }
079:
080: private String description;
081: private String name;
082: private int id;
083:
084: public void setUserObject(Object userObject) {
085: setName(userObject.toString());
086: }
087:
088: public Object getUserObject() {
089: return getName();
090: }
091:
092: public String getDescription() {
093: return description;
094: }
095:
096: public String getName() {
097: return name;
098: }
099:
100: public DiagramModel getModel() {
101: return owner;
102: }
103:
104: public void setDescription(String description) {
105: this .description = description;
106: }
107:
108: public void setName(String name) {
109: this .name = name;
110: super .setUserObject(name);
111: }
112:
113: public boolean acceptsSource(Object port) {
114: return true;
115: }
116:
117: public boolean acceptsTarget(Object port) {
118: return true;
119: }
120:
121: public void setAttributes(Map attributes) {
122: GraphConstants.setBendable(attributes, true);
123: //GraphConstants.setDisconnectable(attributes, true);
124: }
125:
126: public int getId() {
127: return id;
128: }
129:
130: public void store(JGraph graph,
131: biz.hammurapi.diagram.data.Edge data, boolean skipSourceInfo) {
132: data.setName(getName());
133: data.setDescription(getDescription());
134: data.setElementId(new BigInteger(String.valueOf(id)));
135: data.setType(getClass().getName());
136:
137: Enumeration pne = getPropertyNames();
138: while (pne.hasMoreElements()) {
139: String name = (String) pne.nextElement();
140: Property property = data.addNewProperty();
141: property.setName(name);
142: property.setStringValue(properties.getProperty(name));
143: }
144:
145: Enumeration vpne = getViewPropertyNames();
146: while (vpne.hasMoreElements()) {
147: String name = (String) vpne.nextElement();
148: Property property = data.addNewViewProperty();
149: property.setName(name);
150: property.setStringValue(viewProperties.getProperty(name));
151: }
152:
153: EdgeView edgeView = (EdgeView) graph.getGraphLayoutCache()
154: .getMapping(this , false);
155: List points = edgeView.getPoints();
156: if (points != null) {
157: Iterator pit = points.iterator();
158: while (pit.hasNext()) {
159: Object point = pit.next();
160: if ((point instanceof Point2D)) {
161: Point2D thePoint = (Point2D) point;
162: biz.hammurapi.diagram.data.Point pointData = data
163: .addNewPoint();
164: pointData.setX(thePoint.getX());
165: pointData.setY(thePoint.getY());
166: }
167: }
168: }
169:
170: Point2D labelPosition = edgeView.getLabelPosition();
171: if (labelPosition != null) {
172: biz.hammurapi.diagram.data.Point pointData = data
173: .addNewLabelPosition();
174: pointData.setX(labelPosition.getX());
175: pointData.setY(labelPosition.getY());
176: }
177:
178: if (!skipSourceInfo) {
179: Object source = getSource();
180: if (source instanceof DefaultPort) {
181: DefaultPort sdp = (DefaultPort) source;
182: if (sdp.getParent() instanceof DiagramElement) {
183: Object uo = sdp.getUserObject();
184: if (!DiagramCell.isBlank(uo)) {
185: data.setSourcePort(uo.toString());
186: }
187: data.setSource(new BigInteger(String
188: .valueOf(((DiagramElement) sdp.getParent())
189: .getId())));
190: }
191: }
192: }
193:
194: Object target = getTarget();
195: if (target instanceof DefaultPort) {
196: DefaultPort tdp = (DefaultPort) target;
197: if (tdp.getParent() instanceof DiagramElement) {
198: Object uo = tdp.getUserObject();
199: if (!DiagramCell.isBlank(uo)) {
200: data.setTargetPort(uo.toString());
201: }
202: data.setTarget(new BigInteger(String
203: .valueOf(((DiagramElement) tdp.getParent())
204: .getId())));
205: }
206: }
207:
208: }
209:
210: protected Properties properties = new Properties();
211:
212: public void setProperty(String name, String value) {
213: properties.setProperty(name, value);
214: }
215:
216: public String getProperty(String name) {
217: return properties.getProperty(name);
218: }
219:
220: public Enumeration getPropertyNames() {
221: return properties.propertyNames();
222: }
223:
224: protected Properties viewProperties = new Properties();
225:
226: public void setViewProperty(String name, String value) {
227: viewProperties.setProperty(name, value);
228: }
229:
230: public String getViewProperty(String name) {
231: return viewProperties.getProperty(name);
232: }
233:
234: public Enumeration getViewPropertyNames() {
235: return viewProperties.propertyNames();
236: }
237:
238: public void populatePopupMenu(final DiagramApplet applet,
239: final java.awt.Point pt, final JPopupMenu menu) {
240: // Edit
241: menu.add(new AbstractAction("Edit") {
242: public void actionPerformed(ActionEvent e) {
243: Frame frame = (Frame) SwingUtilities
244: .getAncestorOfClass(Frame.class, applet
245: .getGraph());
246: DiagramPropertiesDialog pd = new DiagramPropertiesDialog(
247: frame, DiagramEdge.this);
248: if (pd.edit()) {
249: applet.getGraph().repaint();
250: }
251: }
252: });
253: }
254:
255: }
|