001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets.grid;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.core.JsObject;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014: import com.gwtext.client.widgets.grid.event.ColumnModelListener;
015:
016: /**
017: * The column model class for a {@link GridPanel}.
018: */
019: public class ColumnModel extends JsObject {
020:
021: private BaseColumnConfig[] columnConfigs;
022:
023: /**
024: * Construct a new ColumnModel using a native object.
025: *
026: * @param jsObj the native object
027: */
028: public ColumnModel(JavaScriptObject jsObj) {
029: super (jsObj);
030: }
031:
032: /**
033: * Construct a new ColumnModel.
034: *
035: * @param columnConfigs the configuration for the columns
036: */
037: public ColumnModel(BaseColumnConfig[] columnConfigs) {
038: JavaScriptObject[] config = new JavaScriptObject[columnConfigs.length];
039: for (int i = 0; i < columnConfigs.length; i++) {
040: BaseColumnConfig column = columnConfigs[i];
041: config[i] = column.getJsObj();
042: }
043: JavaScriptObject configJS = JavaScriptObjectHelper
044: .convertToJavaScriptArray(config);
045: jsObj = create(configJS);
046: this .columnConfigs = columnConfigs;
047: }
048:
049: public native JavaScriptObject create(JavaScriptObject configJS) /*-{
050: return new $wnd.Ext.grid.ColumnModel(configJS);
051: }-*/;
052:
053: public BaseColumnConfig[] getColumnConfigs() {
054: return columnConfigs;
055: }
056:
057: /**
058: * Default sortable of columns which have no sortable specified (defaults to false).
059: *
060: * @param defaultSortable true to enable sortable for all columns
061: */
062: public native void setDefaultSortable(boolean defaultSortable)/*-{
063: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
064: cm.defaultSortable = defaultSortable;
065: }-*/;
066:
067: /**
068: * The width of columns which have no width specified (defaults to 100)
069: *
070: * @param defaultWidth the default column width
071: */
072: public native void setDefaultWidth(int defaultWidth)/*-{
073: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
074: cm.defaultWidth = defaultWidth;
075: }-*/;
076:
077: //TODO
078: //http://extjs.com/forum/showthread.php?t=6025&highlight=getCellEditor
079: //need to support way to allow user to plug in logic based cell editors
080: //public GridEditor getCellEditor(int colIndex, int rowIndex)
081:
082: /**
083: * Returns the number of columns.
084: *
085: * @return the number of columns
086: */
087: public native int getColumnCount()/*-{
088: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
089: return cm.getColumnCount();
090: }-*/;
091:
092: /**
093: * Returns the column header.
094: *
095: * @param colID column ID
096: * @return header
097: */
098: public String getColumnHeader(String colID) {
099: int colIndex = getIndexById(colID);
100: return colIndex == -1 ? null : getColumnHeader(colIndex);
101: }
102:
103: /**
104: * Returns the header for the specified column.
105: *
106: * @param colIndex the column index
107: * @return the column header
108: */
109: public native String getColumnHeader(int colIndex)/*-{
110: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
111: return cm.getColumnHeader(colIndex);
112: }-*/;
113:
114: /**
115: * Returns the column ID.
116: *
117: * @param colIndex column index
118: * @return the column id set by the user using {@link com.gwtext.client.widgets.grid.ColumnConfig#setId(String)}. If the
119: * user did not set the id for the column, then the column position is returned as a String.
120: */
121: public native String getColumnId(int colIndex)/*-{
122: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
123: return cm.getColumnId(colIndex).toString();
124: }-*/;
125:
126: /**
127: * Returns the tooltip for the specified column.
128: *
129: * @param colID the column ID
130: * @return the column tooltip
131: */
132: public String getColumnTooltip(String colID) {
133: int colIndex = getIndexById(colID);
134: return colIndex == -1 ? null : getColumnTooltip(colIndex);
135: }
136:
137: /**
138: * Returns the tooltip for the specified column.
139: *
140: * @param colIndex the column index
141: * @return the column tooltip
142: */
143: public native String getColumnTooltip(int colIndex)/*-{
144: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
145: return cm.getColumnTooltip(colIndex);
146: }-*/;
147:
148: /**
149: * Returns the width for the specified column.
150: *
151: * @param colID the column ID
152: * @return the column width
153: */
154: public int getColumnWidth(String colID) {
155: int colIndex = getIndexById(colID);
156: return colIndex == -1 ? -1 : getColumnWidth(colIndex);
157: }
158:
159: /**
160: * Returns the width for the specified column.
161: *
162: * @param colIndex the column index
163: * @return the column width
164: */
165: public native int getColumnWidth(int colIndex)/*-{
166: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
167: return cm.getColumnWidth(colIndex);
168: }-*/;
169:
170: //TODO
171: //getColumnsBy(Function fn, [Object scope]) : Array
172:
173: /**
174: * Returns the dataIndex for the specified column.
175: *
176: * @param colID the column ID
177: * @return the data index of the Store
178: */
179: public String getDataIndex(String colID) {
180: int colIndex = getIndexById(colID);
181: return colIndex == -1 ? null : getDataIndex(colIndex);
182: }
183:
184: /**
185: * Returns the dataIndex for the specified column.
186: *
187: * @param colIndex the column idex
188: * @return the data index of the Store
189: */
190: public native String getDataIndex(int colIndex)/*-{
191: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
192: return cm.getDataIndex(colIndex).toString();
193: }-*/;
194:
195: /**
196: * Returns the index for a specified column ID.
197: *
198: * @param id column id used in ColumnConfig when defining the ColumnModel
199: * @return column index (0 based), -1 if not found
200: * @see ColumnConfig#setId(String)
201: */
202: public native int getIndexById(String id) /*-{
203: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
204: return cm.getIndexById(id);
205: }-*/;
206:
207: //TODO getRenderer is required??
208:
209: public native int getTotalWidth()/*-{
210: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
211: return cm.getTotalWidth();
212: }-*/;
213:
214: /**
215: * Returns the total width of all columns.
216: *
217: * @param includeHidden true to include hidden column widths
218: * @return total width
219: */
220: public native int getTotalWidth(boolean includeHidden)/*-{
221: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
222: return cm.getTotalWidth(includeHidden);
223: }-*/;
224:
225: /**
226: * Returns true if the cell is editable.
227: *
228: * @param colID the column ID
229: * @param rowIndex the row index
230: * @return true if editable
231: */
232: public boolean isCellEditable(String colID, int rowIndex) {
233: int colIndex = getIndexById(colID);
234: return colIndex != -1 && isCellEditable(colIndex, rowIndex);
235: }
236:
237: //TODO support pluggable strategy for logic of editable cells
238:
239: /**
240: * Returns true if the cell is editable.
241: *
242: * @param colIndex the column index
243: * @param rowIndex the row index
244: * @return true if editable
245: */
246: public native boolean isCellEditable(int colIndex, int rowIndex)/*-{
247: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
248: return cm.isCellEditable(colIndex, rowIndex);
249: }-*/;
250:
251: /**
252: * Returns true if the column width cannot be changed.
253: *
254: * @param colID the column ID
255: * @return true if fixed
256: */
257: public boolean isFixed(String colID) {
258: int colIndex = getIndexById(colID);
259: return colIndex != -1 && isFixed(colIndex);
260: }
261:
262: /**
263: * Returns true if the column width cannot be changed
264: *
265: * @param colIndex the column index
266: * @return true if fixed
267: */
268: public native boolean isFixed(int colIndex)/*-{
269: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
270: return cm.isFixed(colIndex);
271: }-*/;
272:
273: /**
274: * Returns true if the column is hidden.
275: *
276: * @param colID the column ID
277: * @return true if hidden
278: */
279: public boolean isHidden(String colID) {
280: int colIndex = getIndexById(colID);
281: return colIndex != -1 && isHidden(colIndex);
282: }
283:
284: /**
285: * Returns true if the column is hidden.
286: *
287: * @param colIndex the column index
288: * @return true if hidden
289: */
290: public native boolean isHidden(int colIndex)/*-{
291: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
292: var hidden = cm.isHidden(colIndex);
293: if(hidden === undefined || hidden == null) return false;
294: return hidden;
295: }-*/;
296:
297: /**
298: * Returns true if the column is locked.
299: *
300: * @param colID the column ID
301: * @return true if locked
302: */
303: public boolean isLocked(String colID) {
304: int colIndex = getIndexById(colID);
305: return colIndex != -1 && isLocked(colIndex);
306: }
307:
308: /**
309: * Returns if the column is locked.
310: *
311: * @param colIndex column index (0 based)
312: * @return locked
313: */
314: public native boolean isLocked(int colIndex)/*-{
315: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
316:
317: var locked = null;
318: try {
319: locked = cm.isLocked(colIndex);
320: } catch(e) {
321: locked = false;
322: }
323: return locked === undefined || locked == null? false : locked;
324: }-*/;
325:
326: /**
327: * Returns true if the column is resizable.
328: *
329: * @param colID the column ID
330: * @return true if resizable
331: */
332: public boolean isResizable(String colID) {
333: int colIndex = getIndexById(colID);
334: return colIndex != -1 && isResizable(colIndex);
335: }
336:
337: /**
338: * Returns true if the column is resizable.
339: *
340: * @param colIndex the column index
341: * @return true if resizable
342: */
343: public native boolean isResizable(int colIndex)/*-{
344: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
345: return cm.isResizable(colIndex);
346: }-*/;
347:
348: /**
349: * Returns true if the column is sortable.
350: *
351: * @param colID the column ID
352: * @return true of sortable
353: */
354: public boolean isSortable(String colID) {
355: int colIndex = getIndexById(colID);
356: return colIndex != -1 && isSortable(colIndex);
357: }
358:
359: /**
360: * Returns true if the column is sortable.
361: *
362: * @param colIndex the column index
363: * @return true if sortable
364: */
365: public native boolean isSortable(int colIndex)/*-{
366: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
367: return cm.isSortable(colIndex);
368: }-*/;
369:
370: /**
371: * Sets the header for a column.
372: *
373: * @param colID the column ID
374: * @param header the column header
375: */
376: public void setColumnHeader(String colID, String header) {
377: int colIndex = getIndexById(colID);
378: if (colIndex != -1) {
379: setColumnHeader(colIndex, header);
380: }
381: }
382:
383: /**
384: * Sets the header for a column.
385: *
386: * @param colIndex the column index
387: * @param header the column header
388: */
389: public native void setColumnHeader(int colIndex, String header) /*-{
390: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
391: cm.setColumnHeader(colIndex, header);
392: }-*/;
393:
394: /**
395: * Sets the width for a column.
396: *
397: * @param colID the column ID
398: * @param width the column width
399: */
400: public void setColumnWidth(String colID, int width) {
401: int colIndex = getIndexById(colID);
402: if (colIndex != -1) {
403: setColumnWidth(colIndex, width);
404: }
405: }
406:
407: /**
408: * Sets the width for a column.
409: *
410: * @param colIndex the column index
411: * @param width the column width
412: */
413: public native void setColumnWidth(int colIndex, int width) /*-{
414: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
415: cm.setColumnWidth(colIndex, width);
416: }-*/;
417:
418: /**
419: * Sets the dataIndex for a column.
420: *
421: * @param colID the column ID
422: * @param dataIndex the Store's data index
423: */
424: public void setDataIndex(String colID, int dataIndex) {
425: int colIndex = getIndexById(colID);
426: if (colIndex != -1) {
427: setDataIndex(colIndex, dataIndex);
428: }
429: }
430:
431: /**
432: * Sets the dataIndex for a column.
433: *
434: * @param colIndex the column index
435: * @param dataIndex the data index
436: */
437: public native void setDataIndex(int colIndex, int dataIndex) /*-{
438: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
439: cm.setDataIndex(colIndex, dataIndex);
440: }-*/;
441:
442: /**
443: * Sets the dataIndex for a column.
444: *
445: * @param colID the column ID
446: * @param dataIndex the Store's data index
447: */
448: public void setDataIndex(String colID, String dataIndex) {
449: int colIndex = getIndexById(colID);
450: if (colIndex != -1) {
451: setDataIndex(colIndex, dataIndex);
452: }
453: }
454:
455: /**
456: * Sets the dataIndex for a column.
457: *
458: * @param colIndex the column index
459: * @param dataIndex the data index
460: */
461: public native void setDataIndex(int colIndex, String dataIndex) /*-{
462: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
463: cm.setDataIndex(colIndex, dataIndex);
464: }-*/;
465:
466: /**
467: * Sets if a column is editable.
468: *
469: * @param colID the column ID
470: * @param editable true if editable
471: */
472: public void setEditable(String colID, boolean editable) {
473: int colIndex = getIndexById(colID);
474: if (colIndex != -1) {
475: setEditable(colIndex, editable);
476: }
477: }
478:
479: /**
480: * Sets if a column is editable.
481: *
482: * @param colIndex the column index
483: * @param editable true if editable
484: */
485: public native void setEditable(int colIndex, boolean editable) /*-{
486: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
487: cm.setEditable(colIndex, editable);
488: }-*/;
489:
490: /**
491: * Sets the editor for a column.
492: *
493: * @param colID the column ID
494: * @param editor the editor
495: */
496: public void setEditor(String colID, GridEditor editor) {
497: int colIndex = getIndexById(colID);
498: if (colIndex != -1) {
499: setEditor(colIndex, editor);
500: }
501: }
502:
503: /**
504: * Sets the editor for a column.
505: *
506: * @param colIndex the column index
507: * @param editor the editor
508: */
509: public native void setEditor(int colIndex, GridEditor editor) /*-{
510: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
511: cm.setEditor(colIndex, editor.@com.gwtext.client.core.JsObject::jsObj);
512: }-*/;
513:
514: /**
515: * Sets if a column is hidden.
516: *
517: * @param colID the column ID
518: * @param hidden true if hidden
519: */
520: public void setHidden(String colID, boolean hidden) {
521: int colIndex = getIndexById(colID);
522: if (colIndex != -1) {
523: setHidden(colIndex, hidden);
524: }
525: }
526:
527: /**
528: * Sets if a column is hidden.
529: *
530: * @param colIndex the column index
531: * @param hidden true if hidden
532: */
533: public native void setHidden(int colIndex, boolean hidden) /*-{
534: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
535: cm.setHidden(colIndex, hidden);
536: }-*/;
537:
538: private static CellMetadata createCellMetadata(
539: final JavaScriptObject jsObj) {
540: return new CellMetadata() {
541: public void setCssClass(String cssClass) {
542: JavaScriptObjectHelper.setAttribute(jsObj, "css",
543: cssClass);
544: }
545:
546: public void setHtmlAttribute(String htmlAttribute) {
547: JavaScriptObjectHelper.setAttribute(jsObj, "attr",
548: htmlAttribute);
549: }
550: };
551: }
552:
553: /**
554: * Sets the rendering (formatting) function for a column.
555: *
556: * @param colID the column ID
557: * @param renderer the column renderer
558: */
559: public void setRenderer(String colID, Renderer renderer) {
560: int colIndex = getIndexById(colID);
561: if (colIndex != -1) {
562: setRenderer(colIndex, renderer);
563: }
564: }
565:
566: /**
567: * Sets the rendering (formatting) function for a column.
568: *
569: * @param colIndex the column idex
570: * @param renderer the column renderer
571: */
572: public native void setRenderer(int colIndex, Renderer renderer) /*-{
573: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
574: cm.setRenderer(colIndex, function(val, cell, r, rowIndex, colNum, store) {
575: var valJ = (val == null || val === undefined ) ? null : $wnd.GwtExt.convertToJavaType(val);
576: var recJ = @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(r);
577: var cellJ = @com.gwtext.client.widgets.grid.ColumnModel::createCellMetadata(Lcom/google/gwt/core/client/JavaScriptObject;)(cell);
578: var storeJ = @com.gwtext.client.data.Store::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(store);
579: return renderer.@com.gwtext.client.widgets.grid.Renderer::render(Ljava/lang/Object;Lcom/gwtext/client/widgets/grid/CellMetadata;Lcom/gwtext/client/data/Record;IILcom/gwtext/client/data/Store;)(valJ, cellJ, recJ, rowIndex, colNum, storeJ);
580: });
581: }-*/;
582:
583: /**
584: * Add a ColumnModel listener.
585: *
586: * @param listener the listener
587: */
588: public native void addListener(ColumnModelListener listener) /*-{
589: var cm = this.@com.gwtext.client.core.JsObject::getJsObj()();
590: var cmJ = this;
591:
592: cm.addListener('columnlockchange',
593: function(source, colIndex, locked) {
594: listener.@com.gwtext.client.widgets.grid.event.ColumnModelListener::onColumnLockChange(Lcom/gwtext/client/widgets/grid/ColumnModel;IZ)(cmJ, colIndex, locked);
595: }
596: );
597:
598: cm.addListener('columnmoved',
599: function(source, oldIndex, newIndex) {
600: listener.@com.gwtext.client.widgets.grid.event.ColumnModelListener::onColumnMoved(Lcom/gwtext/client/widgets/grid/ColumnModel;II)(cmJ, oldIndex, newIndex);
601: }
602: );
603:
604: cm.addListener('headerchange',
605: function(source, colIndex, newText) {
606: listener.@com.gwtext.client.widgets.grid.event.ColumnModelListener::onHeaderChange(Lcom/gwtext/client/widgets/grid/ColumnModel;ILjava/lang/String;)(cmJ, colIndex, newText);
607: }
608: );
609:
610: cm.addListener('hiddenchange',
611: function(source, colIndex, hidden) {
612: listener.@com.gwtext.client.widgets.grid.event.ColumnModelListener::onHiddenChange(Lcom/gwtext/client/widgets/grid/ColumnModel;IZ)(cmJ, colIndex, hidden);
613: }
614: );
615:
616: cm.addListener('widthchange',
617: function(source, colIndex, newWidth) {
618: listener.@com.gwtext.client.widgets.grid.event.ColumnModelListener::onWidthChange(Lcom/gwtext/client/widgets/grid/ColumnModel;II)(cmJ, colIndex, newWidth);
619: }
620: );
621: }-*/;
622: }
|