001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget.table;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.MyDOM;
015: import net.mygwt.ui.client.event.BaseEvent;
016: import net.mygwt.ui.client.util.Observable;
017: import net.mygwt.ui.client.widget.Component;
018:
019: /**
020: * This is the default implementation of a ColumnModel used by the Table.
021: *
022: * <dl>
023: * <dt>Events:</dt>
024: *
025: * <dd><b>Resize</b> : (this,widget,index)<br>
026: * <div>Fires after a column is resized.</div>
027: * <ul>
028: * <li>source : this</li>
029: * <li>widget : table</li>
030: * <li>index : column index</li>
031: * </ul>
032: * </dd>
033: */
034: public class TableColumnModel extends Observable {
035:
036: Component table;
037: List columns;
038:
039: /**
040: * Constructs a new instance.
041: */
042: public TableColumnModel(TableColumn[] columns) {
043: this .columns = new ArrayList();
044: for (int i = 0; i < columns.length; i++) {
045: columns[i].index = i;
046: this .columns.add(columns[i]);
047: }
048: }
049:
050: /**
051: * Returns the column at the given, zero-relative index or <code>null</code>
052: * if the index is out of range.
053: *
054: * @param index the index of the column to return
055: * @return the column at the given index
056: */
057: public TableColumn getColumn(int index) {
058: if ((index < 0) || (index >= getColumnCount()))
059: return null;
060: return (TableColumn) columns.get(index);
061: }
062:
063: /**
064: * Returns the column with the given name.
065: *
066: * @param name the column name
067: * @return the column or <code>null</code> if no match
068: */
069: public TableColumn getColumn(String name) {
070: for (int i = 0; i < columns.size(); i++) {
071: TableColumn column = getColumn(i);
072: if (column.getID().equals(name)) {
073: return column;
074: }
075: }
076: return null;
077: }
078:
079: /**
080: * Returns the number of columns contained in the table.
081: *
082: * @return the number of columns
083: */
084: public int getColumnCount() {
085: return columns.size();
086: }
087:
088: /**
089: * Returns the total column model width.
090: *
091: * @return the width in pixels
092: */
093: public int getTotalWidth() {
094: int total = 0;
095: int cols = getColumnCount();
096: for (int i = 0; i < cols; i++) {
097: TableColumn col = getColumn(i);
098: if (!col.isHidden()) {
099: total += getWidthInPixels(col.index);
100: }
101:
102: }
103: return total;
104: }
105:
106: /**
107: * Returns the number of visible columns.
108: *
109: * @return the visible column count
110: */
111: public int getVisibleColumnCount() {
112: int count = 0;
113: for (int i = 0; i < getColumnCount(); i++) {
114: TableColumn column = getColumn(i);
115: if (!column.isHidden()) {
116: count++;
117: }
118: }
119: return count;
120: }
121:
122: /**
123: * Returns the index of the column.
124: *
125: * @param column the column
126: * @return the column index
127: */
128: public int indexOf(TableColumn column) {
129: return columns.indexOf(column);
130: }
131:
132: /**
133: * Sets the column's width.
134: *
135: * @param index the column index
136: * @param width the new width
137: */
138: public void setColumnWidth(int index, float width) {
139: TableColumn col = getColumn(index);
140: col.setWidth(width);
141: BaseEvent be = new BaseEvent();
142: be.source = this ;
143: be.widget = table;
144: be.index = index;
145: fireEvent(Events.Resize);
146: }
147:
148: int getFixedWidth() {
149: int cols = getColumnCount();
150: int total = 0;
151: for (int i = 0; i < cols; i++) {
152: TableColumn col = getColumn(i);
153: if (col.isHidden()) {
154: continue;
155: }
156: if (col.getWidth() > 1) {
157: total += col.getWidth();
158: }
159: }
160: return total;
161: }
162:
163: protected int getVariableColumnCount() {
164: int count = 0;
165: for (int i = 0; i < getColumnCount(); i++) {
166: TableColumn col = getColumn(i);
167: if (col.getWidth() <= 1) {
168: count++;
169: }
170: }
171: return count;
172: }
173:
174: protected int getVariableWidth() {
175: return (int) (.99 * (table.getOffsetWidth() - MyDOM
176: .getScrollBarWidth()) - getFixedWidth());
177: }
178:
179: protected int getWidthInPixels(int index) {
180: // TODO: need to stop calcs when column size has not changed
181: TableColumn col = getColumn(index);
182: float width = getColumn(index).getWidth();
183: if (width <= 1) {
184: if (getVariableColumnCount() == 1) {
185: return getVariableWidth() - 25;
186: }
187: int w = (int) (getVariableWidth() * getColumn(index)
188: .getWidth());
189: w = Math.max(col.getMinWidth(), w);
190: w = Math.min(col.getMaxWidth(), w);
191: return w;
192: } else {
193: return (int) getColumn(index).getWidth();
194: }
195: }
196:
197: protected void setWidthAsPercent(int index, float width) {
198: TableColumn col = getColumn(index);
199: col.setWidth(getAdjustedWidth(col, width));
200:
201: float o = 0;
202: for (int i = 0; i < getColumnCount(); i++) {
203: TableColumn tc = getColumn(i);
204: if (tc != col && tc.getWidth() < 1.1) {
205: o += tc.getWidth();
206: }
207: }
208:
209: float dif = 1 - col.getWidth() - o;
210:
211: int afterCols = 0;
212: for (int i = (index + 1); i < getColumnCount(); i++) {
213: TableColumn tc = getColumn(i);
214: if (tc.getWidth() < 1.1) {
215: afterCols++;
216: }
217: }
218:
219: float adj = dif / (afterCols);
220: for (int i = (index + 1); i < getColumnCount(); i++) {
221: TableColumn other = getColumn(i);
222: if (other.getWidth() <= 1) {
223: if (other != col) {
224: other.setWidth(other.getWidth() + adj);
225: }
226: }
227: }
228: }
229:
230: private float getAdjustedWidth(TableColumn col, float width) {
231: int totalWidth = getTotalWidth();
232: int pixels = (int) (width * totalWidth);
233: if (pixels < col.getMinWidth()) {
234: width = (float) col.getMinWidth() / totalWidth;
235: }
236: if (pixels > col.getMaxWidth()) {
237: width = (float) col.getMaxWidth() / totalWidth;
238: }
239:
240: return width;
241: }
242:
243: public Component getTable() {
244: return table;
245: }
246:
247: public void setTable(Component table) {
248: this.table = table;
249: }
250: }
|