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.data.Record;
013: import com.gwtext.client.widgets.grid.event.RowSelectionListener;
014:
015: /**
016: * A Grid's row based selection model.
017: */
018: public class RowSelectionModel extends AbstractSelectionModel {
019:
020: /**
021: * Cretes a new RowSelectionModel.
022: */
023: public RowSelectionModel() {
024: this (false);
025: }
026:
027: /**
028: * Cretes a new RowSelectionModel.
029: *
030: * @param singleSelect true to allow selection of only one row at a time (defaults to false)
031: */
032: public RowSelectionModel(boolean singleSelect) {
033: jsObj = create(singleSelect);
034: }
035:
036: protected native JavaScriptObject create(boolean singleSelect) /*-{
037: return new $wnd.Ext.grid.RowSelectionModel({singleSelect: singleSelect});
038: }-*/;
039:
040: public RowSelectionModel(JavaScriptObject jsObj) {
041: super (jsObj);
042: }
043:
044: /**
045: * Clears all selections.
046: */
047: public native void clearSelections() /*-{
048: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
049: sm.clearSelections();
050: }-*/;
051:
052: /**
053: * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
054: *
055: * @param startRow the index of the first row in the range
056: * @param endRow the index of the last row in the range
057: */
058: public native void deselectRange(int startRow, int endRow) /*-{
059: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
060: sm.deselectRange(startRow, endRow);
061: }-*/;
062:
063: /**
064: * Deselects a row.
065: *
066: * @param row the index of the row to deselect
067: */
068: public native void deselectRow(int row) /*-{
069: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
070: sm.deselectRow(row);
071: }-*/;
072:
073: /**
074: * Gets the number of selected rows.
075: *
076: * @return the number of selected rows
077: */
078: public native int getCount() /*-{
079: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
080: return sm.getCount();
081: }-*/;
082:
083: /**
084: * Returns the first selected record.
085: *
086: * @return the first selected record
087: */
088: public native Record getSelected() /*-{
089: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
090: var selected = sm.getSelected();
091: return selected == null ? null : @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(selected);
092: }-*/;
093:
094: /**
095: * Returns the selected records.
096: *
097: * @return the selected records
098: */
099: public native Record[] getSelections() /*-{
100: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
101: var records = sm.getSelections();
102: return records == null ? null : @com.gwtext.client.data.Store::convertFromNativeRecordsArray(Lcom/google/gwt/core/client/JavaScriptObject;)(records);
103: }-*/;
104:
105: /**
106: * Returns true if there is a selection.
107: *
108: * @return true if there is a selection
109: */
110: public native boolean hasSelection() /*-{
111: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
112: return sm.hasSelection();
113: }-*/;
114:
115: /**
116: * Returns true if the specified record id is selected.
117: *
118: * @param id the id of record to check
119: * @return true if selected
120: */
121: public native boolean isIdSelected(String id) /*-{
122: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
123: return sm.isIdSelected(id);
124: }-*/;
125:
126: /**
127: * Returns true if the selections are locked.
128: *
129: * @return true if selections are locked
130: */
131: public native boolean isLocked() /*-{
132: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
133: return sm.isLocked();
134: }-*/;
135:
136: /**
137: * Returns true if row is selected.
138: *
139: * @param row the row index
140: * @return true if selected
141: */
142: public native boolean isSelected(int row) /*-{
143: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
144: return sm.isSelected(row);
145: }-*/;
146:
147: /**
148: * Returns true if the record is selected.
149: *
150: * @param record the record to check
151: * @return true if selected
152: */
153: public native boolean isSelected(Record record) /*-{
154: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
155: var recordJ = record.@com.gwtext.client.core.JsObject::getJsObj()();
156: return sm.isSelected(recordJ);
157: }-*/;
158:
159: /**
160: * Selects all rows.
161: */
162: public native void selectAll() /*-{
163: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
164: sm.selectAll();
165: }-*/;
166:
167: /**
168: * Selects the first row.
169: */
170: public native void selectFirstRow() /*-{
171: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
172: sm.selectFirstRow();
173: }-*/;
174:
175: /**
176: * Selects the last row.
177: */
178: public native void selectLastRow() /*-{
179: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
180: sm.selectLastRow();
181: }-*/;
182:
183: /**
184: * Seletcs the last row.
185: *
186: * @param keepExisting true to keep existing selection
187: */
188: public native void selectLastRow(boolean keepExisting) /*-{
189: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
190: sm.selectLastRow(keepExisting);
191: }-*/;
192:
193: /**
194: * Selects the row immediately following the last selected row.
195: */
196: public native void selectNext() /*-{
197: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
198: sm.selectNext();
199: }-*/;
200:
201: /**
202: * Selects the row that precedes the last selected row.
203: */
204: public native void selectPrevious() /*-{
205: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
206: sm.selectPrevious();
207: }-*/;
208:
209: /**
210: * Selects a range of rows. All rows in between startRow and endRow are also selected.
211: *
212: * @param startRow the index of the first row
213: * @param endRow the index of the last row
214: */
215: public native void selectRange(int startRow, int endRow) /*-{
216: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
217: sm.selectRange(startRow, endRow);
218: }-*/;
219:
220: /**
221: * Selects a range of rows. All rows in between startRow and endRow are also selected.
222: *
223: * @param startRow the index of the first row
224: * @param endRow the index of the last row
225: * @param keepExisting keep the existing selection
226: */
227: public native void selectRange(int startRow, int endRow,
228: boolean keepExisting) /*-{
229: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
230: sm.selectRange(startRow, endRow, keepExisting);
231: }-*/;
232:
233: /**
234: * Selects a row.
235: *
236: * @param row the row index to select
237: */
238: public native void selectRow(int row) /*-{
239: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
240: sm.selectRow(row);
241: }-*/;
242:
243: /**
244: * Selects a row.
245: *
246: * @param row the row index to select
247: * @param keepExisting keep existing selection
248: */
249: public native void selectRow(int row, boolean keepExisting) /*-{
250: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
251: sm.selectRow(row, keepExisting);
252: }-*/;
253:
254: /**
255: * Selects multiple rows.
256: *
257: * @param rows array of the indexes of the row to select
258: */
259: public native void selectRows(int[] rows) /*-{
260: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
261: var rowsJS = @com.gwtext.client.util.JavaScriptObjectHelper::convertToJavaScriptArray([I)(rows);
262: sm.selectRows(rowsJS);
263: }-*/;
264:
265: /**
266: * Selects multiple rows.
267: *
268: * @param rows array of the indexes of the row to select
269: * @param keepExisting keep the existing selection
270: */
271: public native void selectRows(int[] rows, boolean keepExisting) /*-{
272: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
273: var rowsJS = @com.gwtext.client.util.JavaScriptObjectHelper::convertToJavaScriptArray([I)(rows);
274: sm.selectRows(rowsJS, keepExisting);
275: }-*/;
276:
277: /**
278: * Add a Row selection listener.
279: *
280: * @param listener the listener
281: */
282: public native void addListener(RowSelectionListener listener) /*-{
283: var sm = this.@com.gwtext.client.core.JsObject::getJsObj()();
284: var smJ = this;
285:
286: sm.addListener('beforerowselect',
287: function(source, rowIndex, keepExisting, record) {
288: if(keepExisting === undefined) {
289: keepExisting = false;
290: }
291: var recordJ = @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(record);
292: return listener.@com.gwtext.client.widgets.grid.event.RowSelectionListener::doBeforeRowSelect(Lcom/gwtext/client/widgets/grid/RowSelectionModel;IZLcom/gwtext/client/data/Record;)(smJ, rowIndex, keepExisting, recordJ);
293: }
294: );
295:
296: sm.addListener('rowselect',
297: function(source, rowIndex, record) {
298: var recordJ = @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(record);
299: listener.@com.gwtext.client.widgets.grid.event.RowSelectionListener::onRowSelect(Lcom/gwtext/client/widgets/grid/RowSelectionModel;ILcom/gwtext/client/data/Record;)(smJ, rowIndex, recordJ);
300: }
301: );
302:
303: sm.addListener('rowdeselect',
304: function(source, rowIndex, record) {
305: var recordJ = @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(record);
306: listener.@com.gwtext.client.widgets.grid.event.RowSelectionListener::onRowDeselect(Lcom/gwtext/client/widgets/grid/RowSelectionModel;ILcom/gwtext/client/data/Record;)(smJ, rowIndex, recordJ);
307: }
308: );
309:
310: sm.addListener('selectionchange',
311: function(source) {
312: listener.@com.gwtext.client.widgets.grid.event.RowSelectionListener::onSelectionChange(Lcom/gwtext/client/widgets/grid/RowSelectionModel;)(smJ);
313: }
314: );
315: }-*/;
316: }
|