KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > widget > ArrowWidget


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget;
20
21 import java.awt.BasicStroke JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.Polygon JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Stroke JavaDoc;
27
28 import org.netbeans.api.visual.widget.Scene;
29 import org.netbeans.api.visual.widget.Widget;
30
31 /**
32  * Temporary widget which draws a arrow. Can be dashed, and either pointing east or west.
33  * May be replaced by connection widget in future.
34  *
35  * @author skini
36  *
37  */

38 public class ArrowWidget extends Widget {
39
40     private int _width = -1;
41     private int _height = 10;
42     private int _x = 0;
43     private int _y = 0;
44     private int _thickness = 1;
45     private Color JavaDoc _color = WidgetConstants.INPUT_OUTPUT_ARROW_COLOR;
46     private boolean mToEast;
47     private ParameterType _type;
48     private Stroke JavaDoc _stroke;
49     
50     public static enum ParameterType {
51
52         INPUT, OUTPUT, FAULT
53     }
54   
55     public ArrowWidget(Scene scene) {
56         super(scene);
57     }
58     
59     
60     
61     public ArrowWidget(Scene scene, boolean toEast, ParameterType type) {
62         this(scene);
63         this.mToEast = toEast;
64         _type = type;
65         if (_type == ParameterType.INPUT) {
66             _stroke = new BasicStroke JavaDoc(_thickness);
67         } else {
68             _stroke = new BasicStroke JavaDoc(_thickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1, new float[]{5, 10, 5,10}, 0);
69         }
70     }
71     
72     
73     
74     @Override JavaDoc
75     protected Rectangle JavaDoc calculateClientArea() {
76         Rectangle JavaDoc r = super.calculateClientArea();
77         r.height = _height;
78         return r;
79     }
80
81     @Override JavaDoc
82     protected void paintWidget() {
83         Graphics2D JavaDoc gr = getGraphics();
84         drawArrow(gr, _x, _y, getBounds().width, _height, _color, _stroke);
85     }
86     
87     private void drawArrow(Graphics2D JavaDoc g, int x, int y , int width, int height, Color JavaDoc color, Stroke JavaDoc stroke) {
88         double heightOfTriangle = Math.sqrt(height * height * 5 / 4);
89         int heightOfTriangleInInt = new Double JavaDoc(heightOfTriangle).intValue();
90
91         int x1 = x;
92         int width1 = width - heightOfTriangleInInt;
93         if (!mToEast) {
94             x1 = x + heightOfTriangleInInt;
95         }
96         Color JavaDoc origColor = g.getColor();
97         Stroke JavaDoc origStroke = g.getStroke();
98         
99         g.setColor(color);
100         g.setStroke(stroke);
101         int y1 = y + height / 2;
102         g.drawLine(x1, y1, x1 + width1, y1);
103
104         Polygon JavaDoc p = new Polygon JavaDoc();
105         
106         int px1 = mToEast ? x + width1 : x + heightOfTriangleInInt;
107         int px2 = mToEast ? x + width : x;
108         int py1 = y;
109         int py2 = y + height;
110         int pyMidPoint = y + height / 2;
111         
112         p.addPoint(px1, py1);
113         p.addPoint(px2, pyMidPoint); //midpoint
114
p.addPoint(px1, py2);
115         g.fillPolygon(p);
116         g.setColor(origColor);
117         g.setStroke(origStroke);
118     }
119
120
121
122
123     public Color JavaDoc getColor() {
124         return _color;
125     }
126
127
128
129
130     public void setColor(Color JavaDoc color) {
131         this._color = color;
132     }
133
134
135
136
137     public int getHeight() {
138         return _height;
139     }
140
141
142
143
144     public void setHeight(int height) {
145         this._height = height;
146     }
147
148
149
150
151     public int getThickness() {
152         return _thickness;
153     }
154
155
156
157
158     public void setThickness(int thickness) {
159         this._thickness = thickness;
160     }
161
162
163
164
165     public int getWidth() {
166         return _width;
167     }
168
169
170
171
172     public void setWidth(int width) {
173         this._width = width;
174     }
175
176
177
178
179     public int getX() {
180         return _x;
181     }
182
183
184
185
186     public void setX(int x) {
187         this._x = x;
188     }
189
190
191
192
193     public int getY() {
194         return _y;
195     }
196
197
198
199
200     public void setY(int y) {
201         this._y = y;
202     }
203
204 }
205
Popular Tags