KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > displaycomponents > table > TableLayout


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.him.displaycomponents.table;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.LayoutManager JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.JSplitPane JavaDoc;
33
34 /**
35  * Layout manager for the table view. Manages the ordering of table
36  * entries and the flow of the entries when the table is resized or an
37  * entry is expanded/collapsed.
38  *
39  * @author Matthew Large
40  * @version $Revision: 1.2 $
41  *
42  */

43 public class TableLayout implements LayoutManager JavaDoc {
44
45     /**
46      * X axis size of the table view.
47      */

48     private int m_nContainerSizeX = 400;
49
50     /**
51      * Y axis size of the table view.
52      */

53     private int m_nContainerSizeY = 400;
54
55     /**
56      * Parent size???
57      */

58     private Dimension JavaDoc m_parentSize = null;
59     
60     /**
61      * Location of divider in table view.
62      */

63     private int m_nDividerLoc = -1;
64
65     /**
66      * The table view to be laid out.
67      */

68     private TableView m_table = null;
69     
70
71     /**
72      * Constructs a new table layout.
73      *
74      * @param table Table view to be laid out
75      */

76     public TableLayout(TableView table) {
77         super();
78         this.m_table = table;
79     }
80
81     /* (non-Javadoc)
82      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
83      */

84     public void removeLayoutComponent(Component JavaDoc arg0) {
85         // NO-OP
86
}
87
88     /* (non-Javadoc)
89      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
90      */

91     public void layoutContainer(Container JavaDoc container) {
92         Component JavaDoc parent = container.getParent().getParent().getParent();
93         if( parent instanceof JSplitPane JavaDoc) {
94             Dimension JavaDoc dim = parent.getSize();
95             int nDividerLoc = ((JSplitPane JavaDoc)parent).getDividerLocation();
96             
97             if(this.m_parentSize==null) {
98                 
99             }
100             if(this.m_nDividerLoc!=nDividerLoc) {
101                 
102             }
103             if(this.m_parentSize!=null && this.m_parentSize.getHeight()!=dim.getHeight()) {
104                 
105             }
106             if(this.m_parentSize!=null && this.m_parentSize.getWidth()!=dim.getWidth()) {
107                 
108             }
109             
110             if( this.m_parentSize==null || this.m_nDividerLoc!=nDividerLoc || this.m_parentSize.getHeight()!=dim.getHeight() || this.m_parentSize.getWidth()!=dim.getWidth() ) {
111                 this.reflowContainer(container);
112                 this.m_parentSize=dim;
113                 this.m_nDividerLoc = nDividerLoc;
114             } else {
115                 this.resizeContainer(container);
116             }
117         }
118     }
119     
120     /**
121      * Reflows the columns of table entries. Usually called after the
122      * table view or table entries have changed size.
123      *
124      * @param container Container
125      */

126     public void reflowContainer(Container JavaDoc container) {
127         
128         List JavaDoc colRowMap = new ArrayList JavaDoc();
129         
130         Component JavaDoc parent = container.getParent();
131         if( parent instanceof JScrollPane JavaDoc) {
132             parent = parent.getParent();
133         }
134         
135         this.m_nContainerSizeX = parent.getWidth();
136         this.m_nContainerSizeY = parent.getHeight();
137         
138         int nCurrentX = 0;
139         int nCurrentY = 0;
140         
141         List JavaDoc order = this.m_table.getOrderedList();
142         Iterator JavaDoc itor = order.iterator();
143         List JavaDoc col = new ArrayList JavaDoc();
144         while (itor.hasNext()) {
145             String JavaDoc sPath = (String JavaDoc) itor.next();
146             Component JavaDoc comp = this.m_table.getEntry(sPath);
147             comp.setSize(comp.getPreferredSize());
148             int nHeight = comp.getPreferredSize().height;
149             if( nCurrentY + nHeight < this.m_nContainerSizeY ) {
150                 comp.setLocation(nCurrentX, nCurrentY);
151                 nCurrentY = nCurrentY + nHeight;
152                 col.add(((TableEntry)comp).getPath());
153             } else {
154                 colRowMap.add(col);
155                 col = new ArrayList JavaDoc();
156                 col.add(((TableEntry)comp).getPath());
157                 int nWidth = comp.getPreferredSize().width;
158                 nCurrentY = 0;
159                 nCurrentX = nCurrentX + nWidth;
160                 comp.setLocation(nCurrentX, nCurrentY);
161                 nCurrentY = nCurrentY + nHeight;
162             }
163         }
164         if(col.size()>0) {
165             colRowMap.add(col);
166         }
167         
168         nCurrentX = nCurrentX + 200;
169         
170         this.m_nContainerSizeX = nCurrentX;
171         this.m_nContainerSizeY = nCurrentY;
172         
173         container.repaint();
174         this.m_table.setColRowMap(colRowMap);
175     }
176     
177     /**
178      * Reflows a column. Usually called after the
179      * table view or table entries have changed size.
180      *
181      * @param container Container
182      * @param component Column
183      */

184     public void reflowColumn(Container JavaDoc container, Component JavaDoc component) {
185         int nCompX = component.getLocation().x;
186         int nCurrentY = 0;
187         
188         List JavaDoc order = this.m_table.getOrderedList();
189         Iterator JavaDoc itor = order.iterator();
190         while (itor.hasNext()) {
191             String JavaDoc sPath = (String JavaDoc) itor.next();
192             Component JavaDoc comp = this.m_table.getEntry(sPath);
193             comp.setSize(comp.getPreferredSize());
194             if(comp.getLocation().x==nCompX) {
195                 comp.setLocation(nCompX, nCurrentY);
196
197                 nCurrentY = nCurrentY + comp.getHeight();
198             }
199         }
200         
201         this.m_nContainerSizeY = nCurrentY;
202         container.repaint();
203     }
204     
205     /**
206      * Resizes the container.
207      *
208      * @param container Container
209      */

210     public void resizeContainer(Container JavaDoc container) {
211         int nCurrentX = 0;
212         int nCurrentY = 0;
213         
214         Component JavaDoc[] comps = container.getComponents();
215         for(int i=0; i<comps.length; i++) {
216             Component JavaDoc comp = comps[i];
217             if(comp instanceof TableEntry) {
218                 comp.setSize(comp.getPreferredSize());
219                 int nPosX = comp.getLocation().x;
220                 int nPosY = comp.getLocation().y;
221                 int nHeight = comp.getPreferredSize().height;
222                 int nWidth = comp.getPreferredSize().width;
223             
224                 if(nPosX + nWidth > nCurrentX) {
225                     nCurrentX = nPosX + nWidth;
226                 }
227                 if(nPosY + nHeight > nCurrentY) {
228                     nCurrentY = nPosY + nHeight;
229                 }
230             }
231         }
232         
233         nCurrentX = nCurrentX + 200;
234         
235         this.m_nContainerSizeX = nCurrentX;
236         this.m_nContainerSizeY = nCurrentY;
237         
238         container.repaint();
239     }
240
241     /* (non-Javadoc)
242      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
243      */

244     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
245         // NO-OP
246
}
247
248     /* (non-Javadoc)
249      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
250      */

251     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
252         return this.getContainerSize();
253     }
254
255     /* (non-Javadoc)
256      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
257      */

258     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
259         this.layoutContainer(arg0);
260         return this.getContainerSize();
261     }
262     
263     /**
264      * Returns the size of the container.
265      *
266      * @return Container dimensions
267      */

268     private Dimension JavaDoc getContainerSize() {
269         return new Dimension JavaDoc(this.m_nContainerSizeX, this.m_nContainerSizeY);
270     }
271
272 }
Popular Tags