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.Comparator;
011: import java.util.HashMap;
012:
013: import net.mygwt.ui.client.Style;
014: import net.mygwt.ui.client.util.DefaultComparator;
015:
016: /**
017: * A column in a <code>Table</code>. Column sizes can be specified as either
018: * pixels or percentanges. Width values less than or equal to 1 are treated as
019: * percentages.
020: *
021: * <p>
022: * Note: Public members should not be modified after the column is added to a
023: * column model.
024: * </p>
025: */
026: public class TableColumn {
027:
028: public static final Comparator DEFAULT_COMPARATOR = new DefaultComparator();
029:
030: int sortDir = Style.NONE;
031: int index;
032:
033: private boolean hidden;
034: private int align = Style.LEFT;
035: private int maxWidth = 500;
036: private int minWidth = 20;
037: private float width = minWidth;
038: private CellRenderer renderer;
039: private boolean resizable = true;
040: private boolean sortable = true;
041: private Comparator comparator;
042: private String id, text;
043: private Object data;
044: private HashMap dataMap;
045:
046: /**
047: * Creates a new column instance.
048: *
049: * @param id the column id
050: * @param width the column width, widths that are 1 or less are treated as
051: * percentages.
052: */
053: public TableColumn(String id, float width) {
054: this .id = id;
055: this .text = id;
056: this .width = width;
057: }
058:
059: /**
060: * Creates a new column instance.
061: *
062: * @param id the column id
063: * @param text the column text
064: */
065: public TableColumn(String id, String text) {
066: this .id = id;
067: this .text = text;
068: }
069:
070: /**
071: * Creates a new column instance.
072: *
073: * @param id the column id
074: * @param text the column text
075: * @param width the width
076: */
077: public TableColumn(String id, String text, float width) {
078: this (id, text);
079: this .width = width;
080: }
081:
082: /**
083: * Returns the column's alignment.
084: *
085: * @return the alignment
086: */
087: public int getAlignment() {
088: return align;
089: }
090:
091: /**
092: * returns the table's comparator.
093: *
094: * @return the comparator
095: */
096: public Comparator getComparator() {
097: if (comparator == null)
098: return DEFAULT_COMPARATOR;
099: return comparator;
100: }
101:
102: /**
103: * Returns the application defined data associated with the column, or
104: * <code>null</code> if it has not been set.
105: */
106: public Object getData() {
107: return data;
108: }
109:
110: /**
111: * Returns the application defined property for the given name, or
112: * <code>null</code> if it has not been set.
113: *
114: * @param key the name of the property
115: * @return the value or <code>null</code> if it has not been set
116: */
117: public Object getData(String key) {
118: if (dataMap == null)
119: return null;
120: return dataMap.get(key);
121: }
122:
123: /**
124: * Returns the column's id.
125: *
126: * @return the id
127: */
128: public String getID() {
129: return id;
130: }
131:
132: /**
133: * Returns the column's maximum width.
134: *
135: * @return the max width
136: */
137: public int getMaxWidth() {
138: return maxWidth;
139: }
140:
141: /**
142: * Returns the column's minimum width.
143: *
144: * @return the minimum width
145: */
146: public int getMinWidth() {
147: return minWidth;
148: }
149:
150: /**
151: * Returns the column's cell rendered.
152: *
153: * @return the cell renderer
154: */
155: public CellRenderer getRenderer() {
156: return renderer;
157: }
158:
159: /**
160: * Returns the column's current sort direction.
161: *
162: * @return the sort direction (NONE, ASC, DESC)
163: */
164: public int getSortDir() {
165: return sortDir;
166: }
167:
168: /**
169: * Returns the column's text.
170: *
171: * @return the text
172: */
173: public String getText() {
174: return text;
175: }
176:
177: /**
178: * Returns the column's width.
179: *
180: * @return the column width
181: */
182: public float getWidth() {
183: return width;
184: }
185:
186: /**
187: * Returns <code>true</code> if hidden, <code>false</code> otherwise.
188: *
189: * @return the hidden state
190: */
191: public boolean isHidden() {
192: return hidden;
193: }
194:
195: /**
196: * Returns <code>true</code> if the column is resizable.
197: *
198: * @return the resizable state
199: */
200: public boolean isResizable() {
201: return resizable;
202: }
203:
204: /**
205: * Returns <code>true</code> if the column is sortable.
206: *
207: * @return the sortable state
208: */
209: public boolean isSortable() {
210: return sortable;
211: }
212:
213: /**
214: * Sets the column's alignment. Valid values are LEFT, CENTER, and RIGHT.
215: * Default value is LEFT.
216: *
217: * @param align the alignment
218: */
219: public void setAlignment(int align) {
220: this .align = align;
221: }
222:
223: /**
224: * Sets the column's comparator.
225: *
226: * @param comparator the comparator
227: */
228: public void setComparator(Comparator comparator) {
229: this .comparator = comparator;
230: }
231:
232: /**
233: * Sets the application defined column data.
234: *
235: * @param data the widget data
236: */
237: public void setData(Object data) {
238: this .data = data;
239: }
240:
241: /**
242: * Sets the application defined property with the given name.
243: *
244: * @param key the name of the property
245: * @param data the new value for the property
246: */
247: public void setData(String key, Object data) {
248: if (dataMap == null)
249: dataMap = new HashMap();
250: dataMap.put(key, data);
251: }
252:
253: /**
254: * Sets the column's hidden state.
255: *
256: * @param hidden <code>true</code> to hide, <code>false</code> otherwise
257: */
258: public void setHidden(boolean hidden) {
259: this .hidden = hidden;
260: }
261:
262: /**
263: * Sets the column's maximum width. Default value = 500.
264: *
265: * @param maxWidth the max width
266: */
267: public void setMaxWidth(int maxWidth) {
268: this .maxWidth = maxWidth;
269: }
270:
271: /**
272: * Sets the column's minimum width. Default value is 20.
273: *
274: * @param minWidth the min width
275: */
276: public void setMinWidth(int minWidth) {
277: this .minWidth = minWidth;
278: }
279:
280: /**
281: * Sets the column's cell renderer.
282: *
283: * @param renderer the cell renderer
284: */
285: public void setRenderer(CellRenderer renderer) {
286: this .renderer = renderer;
287: }
288:
289: /**
290: * Specifies if the column may be resized. Default value is <code>true</code>.
291: *
292: * @param resizable the resizeable state
293: */
294: public void setResizable(boolean resizable) {
295: this .resizable = resizable;
296: }
297:
298: /**
299: * Sets the sortable state.
300: *
301: * @param sortable <code>true</code> to enable sorting
302: */
303: public void setSortable(boolean sortable) {
304: this .sortable = sortable;
305: }
306:
307: /**
308: * Sets the column's width. Widths that are 1 or less are treated as
309: * percentages.
310: *
311: * @param width the width
312: */
313: public void setWidth(float width) {
314: this .width = width;
315: }
316:
317: protected int getIndex() {
318: return index;
319: }
320:
321: protected void setIndex(int index) {
322: this.index = index;
323: }
324: }
|