KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > wf > WFLine


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.wf;
15
16 import java.awt.*;
17 import javax.swing.*;
18
19 import org.compiere.model.*;
20
21
22 /**
23  * Work Flow Line between Nodes
24  *
25  * @author Jorg Janke
26  * @version $Id: WFLine.java,v 1.3 2002/12/05 05:42:03 jjanke Exp $
27  */

28 public class WFLine extends JComponent
29 {
30     /**
31      * Create Line
32      * @param next model
33      */

34     public WFLine (MWFNodeNext next)
35     {
36         m_next = next;
37         setOpaque(true);
38     } // WFLine
39

40     /** Model */
41     private MWFNodeNext m_next = null;
42
43     /** From Node */
44     private Rectangle m_from = null;
45     /** To Node */
46     private Rectangle m_to = null;
47
48     /**
49      * Get From
50      * @return from node
51      */

52     public Rectangle getFrom()
53     {
54         return m_from;
55     } // getFrom
56

57     /**
58      * Set From
59      * @param from from node
60      */

61     public void setFrom(Rectangle from)
62     {
63         m_from = from;
64     } // setFrom
65

66     /**
67      * Get To
68      * @return to node
69      */

70     public Rectangle getTo()
71     {
72         return m_to;
73     } // getTo
74

75     /**
76      * Set To
77      * @param to to node
78      */

79     public void setTo(Rectangle to)
80     {
81         m_to = to;
82     } // setTo
83

84     /**
85      * Get From Node ID
86      * @return from node id
87      */

88     public int getAD_WF_Node_ID()
89     {
90         return m_next.getAD_WF_Node_ID();
91     } // getAD_WF_Node_ID
92

93     /**
94      * Get To Node ID
95      * @return to node id
96      */

97     public int getAD_WF_Next_ID()
98     {
99         return m_next.getAD_WF_Next_ID();
100     } // getAD_WF_Next_ID
101

102     /*************************************************************************/
103
104     /**
105      * Paint it
106      * @param g Graph
107      */

108     protected void paintComponent (Graphics g)
109     {
110         if (m_from == null || m_to == null)
111             return;
112
113         Polygon arrow = new Polygon();
114         // left \ down
115
if (m_from.x+m_from.width <= m_to.x
116             && m_from.y+m_from.height <= m_to.y)
117         {
118             addPoint (arrow, m_from, SwingConstants.RIGHT, true);
119             addPoint (arrow, m_to, SwingConstants.TOP, false);
120         }
121         // top | down
122
else if (m_from.y+m_from.height <= m_to.y)
123         {
124             addPoint (arrow, m_from, SwingConstants.BOTTOM, true);
125             addPoint (arrow, m_to, SwingConstants.TOP, false);
126         }
127         // bottom / up
128
else if (m_to.y+m_to.height <= m_from.y)
129         {
130             addPoint (arrow, m_from, SwingConstants.TOP, true);
131             addPoint (arrow, m_to, SwingConstants.BOTTOM, false);
132         }
133         // right o <- o left
134
else if (m_to.x+m_to.width <= m_from.x)
135         {
136             addPoint (arrow, m_from, SwingConstants.LEFT, true);
137             addPoint (arrow, m_to, SwingConstants.RIGHT, false);
138         }
139         // left o -> o right
140
else // if (isL_R())
141
{
142             addPoint (arrow, m_from, SwingConstants.RIGHT, true);
143             addPoint (arrow, m_to, SwingConstants.LEFT, false);
144         }
145
146         // Paint it
147
g.setColor(Color.cyan);
148         g.fillPolygon(arrow);
149         g.setColor(Color.blue);
150         g.drawPolygon(arrow);
151     } // paintComponent
152

153
154     /**
155      * Get Point of Rectangle
156      * @param arrow Polygon to draw arrow
157      * @param rect Rectangle
158      * @param pos SwingConstants.BOTTOM / TOP / RIGHT / LEFT
159      * @param from if true from (base) else to (tip) of arrow
160      */

161     private void addPoint (Polygon arrow, Rectangle rect, int pos, boolean from)
162     {
163         int x = rect.x;
164         int y = rect.y;
165
166         if (pos == SwingConstants.TOP)
167         {
168             x += rect.width/2;
169             if (from)
170             {
171                 arrow.addPoint(x-2, y);
172                 arrow.addPoint(x+2, y);
173             }
174             else
175                 arrow.addPoint(x, y);
176         }
177         else if (pos == SwingConstants.RIGHT)
178         {
179             x += rect.width;
180             y += rect.height/2;
181             if (from)
182             {
183                 arrow.addPoint(x, y-2);
184                 arrow.addPoint(x, y+2);
185             }
186             else
187                 arrow.addPoint(x, y);
188         }
189         else if (pos == SwingConstants.LEFT)
190         {
191             y += rect.height/2;
192             if (from)
193             {
194                 arrow.addPoint(x, y-2);
195                 arrow.addPoint(x, y+2);
196             }
197             else
198                 arrow.addPoint(x, y);
199         }
200         else // if (pos == SwingConstants.BOTTOM)
201
{
202             x += rect.width/2;
203             y += rect.height;
204             if (from)
205             {
206                 arrow.addPoint(x-2, y);
207                 arrow.addPoint(x+2, y);
208             }
209             else
210                 arrow.addPoint(x, y);
211         }
212     } // getPoint
213

214     /**
215      * Get Preferred Size
216      * @return size
217      */

218     public Dimension getPreferredSize()
219     {
220         if (m_from == null || m_to == null)
221             return super.getPreferredSize();
222         //
223
int width = Math.max(m_from.x + m_from.width, m_to.x + m_to.width);
224         int height = Math.max(m_from.y + m_from.height, m_to.y + m_to.height);
225         //
226
return new Dimension(width, height);
227     } // getPreferredSize
228

229     /**
230      * String Representation
231      * @return info
232      */

233     public String JavaDoc toString()
234     {
235         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("WFLine[");
236         sb.append(getAD_WF_Node_ID()).append("->").append(getAD_WF_Next_ID());
237         sb.append("]");
238         return sb.toString();
239     } // toString
240

241 } // WFLine
Popular Tags