001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.domainmodel;
016:
017: import java.awt.Color;
018: import java.awt.Graphics;
019: import java.awt.Graphics2D;
020: import java.awt.Shape;
021: import java.awt.geom.Point2D;
022: import java.awt.geom.Rectangle2D;
023:
024: import org.jgraph.graph.CellView;
025: import org.jgraph.graph.DefaultEdge;
026: import org.jgraph.graph.GraphConstants;
027:
028: import com.metaboss.applications.designstudio.components.DesignEdgeRenderer;
029: import com.metaboss.applications.designstudio.components.DesignGraph;
030: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRoleCardinalityEnum;
031:
032: public class DomainEdgeRenderer extends DesignEdgeRenderer {
033: public void paint(Graphics g) {
034: super .paint(g);
035:
036: Shape edgeShape = view.getShape();
037: Object lCell = view.getCell();
038:
039: if (edgeShape != null && lCell instanceof DefaultEdge) {
040: Graphics2D g2 = (Graphics2D) g;
041: //Rectangle lRect = getPaintBounds(view);
042:
043: int lPointCount = view.getPointCount();
044: if (lPointCount > 1) {
045: g.setColor(fontColor);
046:
047: if (viewCardinalities()) {
048: AssociationRoleCardinalityEnum lFromCardinality = (AssociationRoleCardinalityEnum) ((DefaultEdge) lCell)
049: .getAttributes().get("CardinalityFrom");
050: AssociationRoleCardinalityEnum lToCardinality = (AssociationRoleCardinalityEnum) ((DefaultEdge) lCell)
051: .getAttributes().get("CardinalityTo");
052:
053: if (lFromCardinality != null
054: && lFromCardinality != AssociationRoleCardinalityEnum.ONE)
055: paintCardinality(g2,
056: cardinalityToString(lFromCardinality),
057: view.getPoint(0), view.getPoint(1));
058: if (lToCardinality != null
059: && lToCardinality != AssociationRoleCardinalityEnum.ONE)
060: paintCardinality(g2,
061: cardinalityToString(lToCardinality),
062: view.getPoint(lPointCount - 1), view
063: .getPoint(lPointCount - 2));
064: }
065: if (viewEndsNames()) {
066: String lFromName = (String) ((DefaultEdge) lCell)
067: .getAttributes().get("FromName");
068: String lToName = (String) ((DefaultEdge) lCell)
069: .getAttributes().get("ToName");
070: if (lFromName != null && lFromName.length() > 0)
071: paintRoleName(g2, lFromName, view.getPoint(0),
072: view.getPoint(1));
073: if (lToName != null && lToName.length() > 0)
074: paintRoleName(g2, lToName, view
075: .getPoint(lPointCount - 1), view
076: .getPoint(lPointCount - 2));
077: }
078: }
079: }
080: }
081:
082: public Rectangle2D getBounds(CellView value) {
083: Rectangle2D lRect = super .getBounds(value);
084:
085: if (viewCardinalities() || viewEndsNames()) {
086: int lSize = 40;
087: if (viewEndsNames()) {
088: DefaultEdge lCell = (DefaultEdge) value.getCell();
089: String lFromName = (String) ((DefaultEdge) lCell)
090: .getAttributes().get("FromName");
091: String lToName = (String) ((DefaultEdge) lCell)
092: .getAttributes().get("ToName");
093: int sw = 0;
094: if (lFromName != null && lFromName.length() > 0) {
095: sw = metrics.stringWidth(lFromName);
096: if (sw > lSize)
097: lSize = sw;
098: }
099: if (lToName != null && lToName.length() > 0) {
100: sw = metrics.stringWidth(lToName);
101: if (sw > lSize)
102: lSize = sw;
103: }
104: }
105:
106: lRect = GraphConstants.createRect(lRect.getX() - lSize,
107: lRect.getY() - lSize, lRect.getWidth() + 2 * lSize,
108: lRect.getHeight() + 2 * lSize);
109: }
110: return lRect;
111: }
112:
113: public Point2D getCardinalityPosition(Point2D pStart,
114: Point2D pStop, int pGap, int pWidth) {
115: Point2D lResult = GraphConstants.createPoint(pStart);
116: Point2D lNewStart = GraphConstants.createPoint(pStart);
117:
118: double n = pStop.getY() - pStart.getY();
119: double m = pStop.getX() - pStart.getX();
120: double k = (n != 0 && m != 0) ? n / m : 0;
121: double b = pStart.getY() - k * pStart.getX();
122: double alpha = Math.toDegrees(Math.atan(k));
123: if (alpha != 0 && alpha != 90) {
124: double beta = 90 - alpha;
125: double k1 = Math.tan(Math.toRadians(180 - beta));
126:
127: double lNewX = pStart.getX();
128: if (pStart.getX() <= pStop.getX())
129: lNewX += 15 * Math.cos(Math.toRadians(alpha));
130: else
131: lNewX -= 15 * Math.cos(Math.toRadians(alpha));
132: double lNewY = k * lNewX + b;
133:
134: double b1 = lNewY - k1 * lNewX;
135: double bn = b + pGap / Math.cos(Math.toRadians(alpha));
136: double x = Math.abs((bn - b1) / (k - k1));
137: double y = Math.abs((k1 * x + b1));
138:
139: lResult.setLocation(x, y);
140:
141: if (lResult.getX() < lNewX
142: && lResult.getX() + pWidth > lNewX)
143: lResult.setLocation(lResult.getX() - pWidth, lResult
144: .getY());
145: } else {
146: if (pStart.getY() == pStop.getY()) {
147: lResult.setLocation(lResult.getX(), lResult.getY()
148: + pGap);
149: if (pStart.getX() <= pStop.getX())
150: lResult.setLocation(lResult.getX() + 15, lResult
151: .getY());
152: else
153: lResult.setLocation(lResult.getX() - (15 + pWidth),
154: lResult.getY());
155: } else if (pStart.getX() == pStop.getX()) {
156: lResult.setLocation(lResult.getX() + pGap, lResult
157: .getY());
158: if (pGap > 0)
159: lResult.setLocation(lResult.getX() + pWidth,
160: lResult.getY());
161: else
162: lResult.setLocation(lResult.getX() - pWidth,
163: lResult.getY());
164: if (pStart.getY() <= pStop.getY())
165: lResult.setLocation(lResult.getX(),
166: lResult.getY() + 15);
167: else
168: lResult.setLocation(lResult.getX(),
169: lResult.getY() - 15);
170: }
171: }
172: return lResult;
173: }
174:
175: public void paintLabel(Graphics2D g, String pLabel, Point2D pStart,
176: Point2D pStop, Point2D pPoint) {
177: g.setColor(Color.darkGray);
178: g.drawString(pLabel, (float) pPoint.getX(), (float) pPoint
179: .getY());
180: }
181:
182: public void paintCardinality(Graphics2D g, String pCaption,
183: Point2D pStart, Point2D pStop) {
184: if (g == null || pCaption == null)
185: return;
186:
187: int sw = metrics.stringWidth(pCaption) + 4;
188: if (sw < 20)
189: sw = 20;
190: Point2D lPoint = getCardinalityPosition(pStart, pStop, sw, 0);
191:
192: //???
193: //int sh = metrics.getHeight();
194: //g.setColor(Color.red);
195: //g.fillRect(lPoint.x, lPoint.y, sw, sh);
196: //g.setColor(Color.blue);
197: //g.drawLine(lPoint.x, lPoint.y, pStart.x, pStart.y);
198: //???
199:
200: paintLabel(g, pCaption, pStart, pStop, lPoint);
201: }
202:
203: public void paintRoleName(Graphics2D g, String pCaption,
204: Point2D pStart, Point2D pStop) {
205: int lWidth = metrics.stringWidth(pCaption);
206: int sw = 10;
207: Point2D lPoint = getCardinalityPosition(pStart, pStop, -sw,
208: lWidth);
209:
210: //???
211: //g.setColor(Color.blue);
212: //g.drawLine(lPoint.x, lPoint.y, pStart.x, pStart.y);
213: //???
214:
215: paintLabel(g, pCaption, pStart, pStop, lPoint);
216: }
217:
218: public String cardinalityToString(
219: AssociationRoleCardinalityEnum pCardinality) {
220: if (pCardinality != null) {
221: if (pCardinality == AssociationRoleCardinalityEnum.ONE)
222: return "1";
223: if (pCardinality == AssociationRoleCardinalityEnum.ONE_TO_MANY)
224: return "1..*";
225: if (pCardinality == AssociationRoleCardinalityEnum.ZERO_OR_ONE)
226: return "0,1";
227: if (pCardinality == AssociationRoleCardinalityEnum.ZERO_TO_MANY)
228: return "0..*";
229: }
230: return null;
231: }
232:
233: public boolean viewCardinalities() {
234: DesignGraph lGraph = (DesignGraph) view.getGraph();
235: return (lGraph != null && lGraph.getShowEdgesCardinalities());
236: }
237:
238: public boolean viewEndsNames() {
239: DesignGraph lGraph = (DesignGraph) view.getGraph();
240: return (lGraph != null && lGraph.getShowEdgesEndsNames());
241: }
242: }
|