001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.him.displaycomponents.table;
021:
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.LayoutManager;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.swing.JScrollPane;
032: import javax.swing.JSplitPane;
033:
034: /**
035: * Layout manager for the table view. Manages the ordering of table
036: * entries and the flow of the entries when the table is resized or an
037: * entry is expanded/collapsed.
038: *
039: * @author Matthew Large
040: * @version $Revision: 1.2 $
041: *
042: */
043: public class TableLayout implements LayoutManager {
044:
045: /**
046: * X axis size of the table view.
047: */
048: private int m_nContainerSizeX = 400;
049:
050: /**
051: * Y axis size of the table view.
052: */
053: private int m_nContainerSizeY = 400;
054:
055: /**
056: * Parent size???
057: */
058: private Dimension m_parentSize = null;
059:
060: /**
061: * Location of divider in table view.
062: */
063: private int m_nDividerLoc = -1;
064:
065: /**
066: * The table view to be laid out.
067: */
068: private TableView m_table = null;
069:
070: /**
071: * Constructs a new table layout.
072: *
073: * @param table Table view to be laid out
074: */
075: public TableLayout(TableView table) {
076: super ();
077: this .m_table = table;
078: }
079:
080: /* (non-Javadoc)
081: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
082: */
083: public void removeLayoutComponent(Component arg0) {
084: // NO-OP
085: }
086:
087: /* (non-Javadoc)
088: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
089: */
090: public void layoutContainer(Container container) {
091: Component parent = container.getParent().getParent()
092: .getParent();
093: if (parent instanceof JSplitPane) {
094: Dimension dim = parent.getSize();
095: int nDividerLoc = ((JSplitPane) parent)
096: .getDividerLocation();
097:
098: if (this .m_parentSize == null) {
099:
100: }
101: if (this .m_nDividerLoc != nDividerLoc) {
102:
103: }
104: if (this .m_parentSize != null
105: && this .m_parentSize.getHeight() != dim.getHeight()) {
106:
107: }
108: if (this .m_parentSize != null
109: && this .m_parentSize.getWidth() != dim.getWidth()) {
110:
111: }
112:
113: if (this .m_parentSize == null
114: || this .m_nDividerLoc != nDividerLoc
115: || this .m_parentSize.getHeight() != dim.getHeight()
116: || this .m_parentSize.getWidth() != dim.getWidth()) {
117: this .reflowContainer(container);
118: this .m_parentSize = dim;
119: this .m_nDividerLoc = nDividerLoc;
120: } else {
121: this .resizeContainer(container);
122: }
123: }
124: }
125:
126: /**
127: * Reflows the columns of table entries. Usually called after the
128: * table view or table entries have changed size.
129: *
130: * @param container Container
131: */
132: public void reflowContainer(Container container) {
133:
134: List colRowMap = new ArrayList();
135:
136: Component parent = container.getParent();
137: if (parent instanceof JScrollPane) {
138: parent = parent.getParent();
139: }
140:
141: this .m_nContainerSizeX = parent.getWidth();
142: this .m_nContainerSizeY = parent.getHeight();
143:
144: int nCurrentX = 0;
145: int nCurrentY = 0;
146:
147: List order = this .m_table.getOrderedList();
148: Iterator itor = order.iterator();
149: List col = new ArrayList();
150: while (itor.hasNext()) {
151: String sPath = (String) itor.next();
152: Component comp = this .m_table.getEntry(sPath);
153: comp.setSize(comp.getPreferredSize());
154: int nHeight = comp.getPreferredSize().height;
155: if (nCurrentY + nHeight < this .m_nContainerSizeY) {
156: comp.setLocation(nCurrentX, nCurrentY);
157: nCurrentY = nCurrentY + nHeight;
158: col.add(((TableEntry) comp).getPath());
159: } else {
160: colRowMap.add(col);
161: col = new ArrayList();
162: col.add(((TableEntry) comp).getPath());
163: int nWidth = comp.getPreferredSize().width;
164: nCurrentY = 0;
165: nCurrentX = nCurrentX + nWidth;
166: comp.setLocation(nCurrentX, nCurrentY);
167: nCurrentY = nCurrentY + nHeight;
168: }
169: }
170: if (col.size() > 0) {
171: colRowMap.add(col);
172: }
173:
174: nCurrentX = nCurrentX + 200;
175:
176: this .m_nContainerSizeX = nCurrentX;
177: this .m_nContainerSizeY = nCurrentY;
178:
179: container.repaint();
180: this .m_table.setColRowMap(colRowMap);
181: }
182:
183: /**
184: * Reflows a column. Usually called after the
185: * table view or table entries have changed size.
186: *
187: * @param container Container
188: * @param component Column
189: */
190: public void reflowColumn(Container container, Component component) {
191: int nCompX = component.getLocation().x;
192: int nCurrentY = 0;
193:
194: List order = this .m_table.getOrderedList();
195: Iterator itor = order.iterator();
196: while (itor.hasNext()) {
197: String sPath = (String) itor.next();
198: Component comp = this .m_table.getEntry(sPath);
199: comp.setSize(comp.getPreferredSize());
200: if (comp.getLocation().x == nCompX) {
201: comp.setLocation(nCompX, nCurrentY);
202:
203: nCurrentY = nCurrentY + comp.getHeight();
204: }
205: }
206:
207: this .m_nContainerSizeY = nCurrentY;
208: container.repaint();
209: }
210:
211: /**
212: * Resizes the container.
213: *
214: * @param container Container
215: */
216: public void resizeContainer(Container container) {
217: int nCurrentX = 0;
218: int nCurrentY = 0;
219:
220: Component[] comps = container.getComponents();
221: for (int i = 0; i < comps.length; i++) {
222: Component comp = comps[i];
223: if (comp instanceof TableEntry) {
224: comp.setSize(comp.getPreferredSize());
225: int nPosX = comp.getLocation().x;
226: int nPosY = comp.getLocation().y;
227: int nHeight = comp.getPreferredSize().height;
228: int nWidth = comp.getPreferredSize().width;
229:
230: if (nPosX + nWidth > nCurrentX) {
231: nCurrentX = nPosX + nWidth;
232: }
233: if (nPosY + nHeight > nCurrentY) {
234: nCurrentY = nPosY + nHeight;
235: }
236: }
237: }
238:
239: nCurrentX = nCurrentX + 200;
240:
241: this .m_nContainerSizeX = nCurrentX;
242: this .m_nContainerSizeY = nCurrentY;
243:
244: container.repaint();
245: }
246:
247: /* (non-Javadoc)
248: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
249: */
250: public void addLayoutComponent(String arg0, Component arg1) {
251: // NO-OP
252: }
253:
254: /* (non-Javadoc)
255: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
256: */
257: public Dimension minimumLayoutSize(Container arg0) {
258: return this .getContainerSize();
259: }
260:
261: /* (non-Javadoc)
262: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
263: */
264: public Dimension preferredLayoutSize(Container arg0) {
265: this .layoutContainer(arg0);
266: return this .getContainerSize();
267: }
268:
269: /**
270: * Returns the size of the container.
271: *
272: * @return Container dimensions
273: */
274: private Dimension getContainerSize() {
275: return new Dimension(this.m_nContainerSizeX,
276: this.m_nContainerSizeY);
277: }
278:
279: }
|