001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.products.ad.viewfactory;
043:
044: import org.netbeans.modules.uml.ui.support.viewfactorysupport.DrawEngineArrowheadKindEnum;
045: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo;
046: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IETArrowHead;
047: import com.tomsawyer.editor.TSEColor;
048: import com.tomsawyer.editor.graphics.TSEGraphics; //import com.tomsawyer.util.TSConstPoint;
049: import com.tomsawyer.drawing.geometry.TSConstPoint; //import com.tomsawyer.util.TSTransform;
050: import com.tomsawyer.editor.TSTransform;
051:
052: import java.awt.Color;
053: import java.awt.Point;
054: import java.awt.Shape;
055: import java.awt.geom.GeneralPath;
056:
057: /*
058: *
059: * @author KevinM
060: *
061: * < ><-----
062: *
063: */
064: public class ETUnFilledDiamondNavigable extends ETUnFilledDiamond {
065:
066: IETArrowHead m_pUnfilledArrow = ETArrowHeadFactory
067: .create(DrawEngineArrowheadKindEnum.DEAK_UNFILLEDARROW);
068:
069: public ETUnFilledDiamondNavigable() {
070: super (
071: DrawEngineArrowheadKindEnum.DEAK_UNFILLEDDIAMOND_NAVIGABLE);
072: }
073:
074: protected ETUnFilledDiamondNavigable(int kind) {
075: super (kind);
076: }
077:
078: /* (non-Javadoc)
079: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IETArrowHead#draw(org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo, com.tomsawyer.util.TSConstPoint, com.tomsawyer.util.TSConstPoint, com.tomsawyer.editor.TSEColor)
080: */
081: public boolean draw(IDrawInfo pInfo, TSConstPoint fromPt,
082: TSConstPoint toPt, TSEColor color) {
083: // Draw the Diamond
084: Shape diamondShape = super .getShape(pInfo, fromPt, toPt);
085: if (diamondShape != null) {
086: TSEGraphics graphics = pInfo.getTSEGraphics();
087: Color prevColor = graphics.getColor();
088: Color fillColor = this .isFilled() ? color.getColor()
089: : Color.WHITE;
090: graphics.setColor(fillColor);
091: graphics.fill(diamondShape);
092: graphics.setColor(color);
093: graphics.draw(diamondShape);
094: // Now Draw the arrowHead.
095: Shape arrowShape = m_pUnfilledArrow.getShape(pInfo, fromPt,
096: getDiamondTip(pInfo, fromPt, toPt));
097: if (arrowShape != null) {
098: graphics.draw(arrowShape);
099: }
100: graphics.setColor(prevColor);
101: return true;
102: }
103: return false;
104: }
105:
106: /*
107: * Returns the from tip of the diamond used to calculate the to point of the unfilled arrow.
108: */
109: protected TSConstPoint getDiamondTip(IDrawInfo pInfo,
110: TSConstPoint fromPt, TSConstPoint toPt) {
111: TSTransform transform = pInfo.getTSTransform();
112: double fromX = fromPt.getX();
113: double fromY = fromPt.getY();
114: double toX = toPt.getX();
115: double toY = toPt.getY();
116:
117: float edgeVectorX = transform.widthToDevice(toX - fromX);
118: float edgeVectorY = transform.heightToDevice(toY - fromY);
119: float edgeLength = (float) Math
120: .sqrt((edgeVectorX * edgeVectorX)
121: + (edgeVectorY * edgeVectorY));
122:
123: // avoid divide-by-zero.
124:
125: if (edgeLength == 0.0f) {
126: return null;
127: }
128:
129: int lineFromX = transform.xToDevice(fromX);
130: int lineFromY = transform.yToDevice(fromY);
131: int lineToX = transform.xToDevice(toX);
132: int lineToY = transform.yToDevice(toY);
133:
134: float halfArrowWidth = transform.widthToDevice(this .getWidth()) / 2;
135: float arrowHeight = transform.heightToDevice(this .getHeight());
136:
137: // normalize the edge vector components.
138:
139: edgeVectorX /= edgeLength;
140: edgeVectorY /= edgeLength;
141:
142: // calculate the vector from the center of the base to
143: // the arrow tip
144:
145: float arrowHeightX = arrowHeight * edgeVectorX;
146: float arrowHeightY = arrowHeight * edgeVectorY;
147:
148: // calculate the vector from the base center point to
149: // the edge of the base
150:
151: float halfBaseVectorX = halfArrowWidth * edgeVectorY;
152: float halfBaseVectorY = halfArrowWidth * edgeVectorX;
153: return transform.pointToWorld(new Point(lineToX
154: - (int) (arrowHeightX * 2), lineToY
155: + (int) (arrowHeightY * 2)));
156: }
157:
158: /* (non-Javadoc)
159: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IETArrowHead#getShape(org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo, com.tomsawyer.util.TSConstPoint, com.tomsawyer.util.TSConstPoint)
160: */
161: public GeneralPath getShape(IDrawInfo pInfo, TSConstPoint fromPt,
162: TSConstPoint toPt) {
163: GeneralPath diamond = super .getShape(pInfo, fromPt, toPt);
164:
165: TSConstPoint newToPt = getDiamondTip(pInfo, fromPt, toPt);
166: Shape UnfilledPath = m_pUnfilledArrow.getShape(pInfo, fromPt,
167: newToPt);
168: GeneralPath finalShape = new GeneralPath(
169: GeneralPath.WIND_EVEN_ODD);
170:
171: if (finalShape != null && diamond != null
172: && UnfilledPath != null) {
173: // Build the final comp, object.
174: finalShape.append(diamond, false);
175: finalShape.append(UnfilledPath, false);
176: }
177: return finalShape;
178: }
179:
180: }
|