KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
18 import javax.swing.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * WorkFlow Layout Manager
24  *
25  * @author Jorg Janke
26  * @version $Id: WFLayoutManager.java,v 1.3 2002/12/05 05:29:30 jjanke Exp $
27  */

28 public class WFLayoutManager implements LayoutManager
29 {
30
31     /**
32      * Constructor
33      */

34     public WFLayoutManager()
35     {
36     } // WFLayoutManager
37

38     /** Cached Size */
39     private Dimension m_size = null;
40
41     /**
42      * If the layout manager uses a per-component string,
43      * adds the component <code>comp</code> to the layout,
44      * associating it
45      * with the string specified by <code>name</code>.
46      *
47      * @param name the string to be associated with the component
48      * @param comp the component to be added
49      */

50     public void addLayoutComponent (String JavaDoc name, Component comp)
51     {
52         invalidateLayout();
53     } // addLayoutComponent
54

55     /**
56      * Removes the specified component from the layout.
57      * @param comp the component to be removed
58      */

59     public void removeLayoutComponent(Component comp)
60     {
61         if (comp == null)
62             return;
63         invalidateLayout();
64     } // removeLayoutComponent
65

66     /**
67      * Calculates the preferred size dimensions for the specified
68      * container, given the components it contains.
69      * @param parent the container to be laid out
70      * @return preferred size
71      * @see #minimumLayoutSize
72      */

73     public Dimension preferredLayoutSize(Container parent)
74     {
75         if (m_size == null)
76             layoutContainer(parent);
77         return m_size;
78     } // preferredLayoutSize
79

80     /**
81      * Calculates the minimum size dimensions for the specified
82      * container, given the components it contains.
83      * @param parent the component to be laid out
84      * @return preferred size
85      * @see #preferredLayoutSize
86      */

87     public Dimension minimumLayoutSize(Container parent)
88     {
89         return preferredLayoutSize(parent);
90     } // minimumLayoutSize
91

92     /**
93      * Lays out the specified container.
94      * @param parent the container to be laid out
95      */

96     public void layoutContainer (Container parent)
97     {
98         Insets insets = parent.getInsets();
99         //
100
int width = insets.left;
101         int height = insets.top;
102
103     // WFPanel panel = (WFPanel)parent;
104

105         // We need to layout
106
if (needLayout(parent))
107         {
108             // Go through all components
109
for (int i = 0; i < parent.getComponentCount(); i++)
110             {
111                 Component comp = parent.getComponent(i);
112                 if (comp.isVisible() && comp instanceof WFNode)
113                 {
114                     Dimension ps = comp.getPreferredSize();
115                     int x = width;
116                     int y = height;
117                     comp.setBounds(x, y, ps.width, ps.height);
118                     //
119
width += ps.getWidth();
120                     height += ps.getHeight();
121                 }
122             }
123         }
124         else // we have an Layout
125
{
126             // Go through all components
127
for (int i = 0; i < parent.getComponentCount(); i++)
128             {
129                 Component comp = parent.getComponent(i);
130                 if (comp.isVisible() && comp instanceof WFNode)
131                 {
132                     Dimension ps = comp.getPreferredSize();
133                     Point loc = comp.getLocation();
134                     int maxWidth = comp.getX() + ps.width;
135                     int maxHeight = comp.getY() + ps.height;
136                     if (width < maxWidth)
137                         width = maxWidth;
138                     if (height < maxHeight)
139                         height = maxHeight;
140                     comp.setBounds(loc.x, loc.y, ps.width, ps.height);
141                 }
142             } // for all components
143
} // have layout
144

145         // Create Lines
146
for (int i = 0; i < parent.getComponentCount(); i++)
147         {
148             Component comp = parent.getComponent(i);
149             if (comp.isVisible() && comp instanceof WFLine)
150             {
151                 WFLine line = (WFLine)comp;
152                 Rectangle from = findBounds (parent, line.getAD_WF_Node_ID());
153                 Rectangle to = findBounds (parent, line.getAD_WF_Next_ID());
154                 line.setFrom(from);
155                 line.setTo(to);
156                 Dimension ps = line.getPreferredSize();
157                 line.setBounds(0, 0, ps.width, ps.height);
158             }
159         } // for all components
160

161         // return size
162
width += insets.right;
163         height += insets.bottom;
164         m_size = new Dimension(width, height);
165     } // layoutContainer
166

167     /**
168      * Need Layout
169      * @param parent parent
170      * @return true if we need to layout
171      */

172     private boolean needLayout (Container parent)
173     {
174         Point p0 = new Point(0,0);
175         // Go through all components
176
for (int i = 0; i < parent.getComponentCount(); i++)
177         {
178             Component comp = parent.getComponent(i);
179             if (comp instanceof WFNode && comp.getLocation().equals(p0))
180             {
181                 Log.trace(Log.l6_Database, "WFLayout.needLayout", comp);
182                 return true;
183             }
184         }
185         return false;
186     } // needLayout
187

188     /**
189      * Get Bounds of WF Node Icon
190      * @param parent parent (WFPanel)
191      * @param AD_WF_Node_ID node id
192      * @return bounds of node with ID or null
193      */

194     private Rectangle findBounds (Container parent, int AD_WF_Node_ID)
195     {
196         for (int i = 0; i < parent.getComponentCount(); i++)
197         {
198             Component comp = parent.getComponent(i);
199             if (comp instanceof WFNode)
200             {
201                 WFNode node = (WFNode)comp;
202                 if (node.getAD_WF_Node_ID() == AD_WF_Node_ID)
203                     return node.getIconBounds();
204             }
205         }
206         return null;
207     } // findBounds
208

209     /**
210      * Invalidates the layout, indicating that if the layout manager
211      * has cached information it should be discarded.
212      */

213     private void invalidateLayout()
214     {
215         m_size = null;
216     } // invalidateLayout
217

218 } // WFLayoutManager
219
Popular Tags