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.treetable;
009:
010: import net.mygwt.ui.client.Events;
011: import net.mygwt.ui.client.Style;
012: import net.mygwt.ui.client.event.BaseEvent;
013: import net.mygwt.ui.client.widget.ToolTip;
014: import net.mygwt.ui.client.widget.tree.Tree;
015: import net.mygwt.ui.client.widget.tree.TreeItem;
016: import net.mygwt.ui.client.widget.tree.TreeItemUI;
017:
018: import com.google.gwt.user.client.Element;
019:
020: /**
021: * A item in a <code>TreeTable</code>. All events are bubbled to the item's
022: * parent treetable.
023: *
024: * <dl>
025: * <dt><b>Events:</b></dt>
026: *
027: * <dd><b>BeforeAdd</b> : (widget, item, index)<br>
028: * <div>Fires before a item is added or inserted. Listeners can set the
029: * <code>doit</code> field to <code>false</code> to cancel the action.</div>
030: * <ul>
031: * <li>widget : this</li>
032: * <li>item : the item being added</li>
033: * <li>index : the index at which the item will be added</li>
034: * </ul>
035: * </dd>
036: *
037: * <dd><b>BeforeRemove</b> : (widget, item)<br>
038: * <div>Fires before a item is removed. Listeners can set the <code>doit</code>
039: * field to <code>false</code> to cancel the action.</div>
040: * <ul>
041: * <li>widget : this</li>
042: * <li>item : the item being removed</li>
043: * </ul>
044: * </dd>
045: *
046: * <dd><b>BeforeExpand</b> : (widget, item)<br>
047: * <div>Fires before a item is expanded. Listeners can set the <code>doit</code>
048: * field to <code>false</code> to cancel the expand.</div>
049: * <ul>
050: * <li>widget : this</li>
051: * <li>item : the item being expanded</li>
052: * </ul>
053: * </dd>
054: *
055: * <dd><b>BeforeCollapse</b> : (widget, item)<br>
056: * <div>Fires before a item is collapsed. Listeners can set the
057: * <code>doit</code> field to <code>false</code> to cancel the collapse.</div>
058: * <ul>
059: * <li>widget : this</li>
060: * <li>item : the item being expanded</li>
061: * </ul>
062: * </dd>
063: *
064: * <dd><b>Add</b> : (widget, item, index)<br>
065: * <div>Fires after a item has been added or inserted.</div>
066: * <ul>
067: * <li>widget : this</li>
068: * <li>item : the item that was added</li>
069: * <li>index : the index at which the item will be added</li>
070: * </ul>
071: * </dd>
072: *
073: * <dd><b>Remove</b> : (widget, item)<br>
074: * <div>Fires after a item has been removed.</div>
075: * <ul>
076: * <li>widget : this</li>
077: * <li>item : the item being removed</li>
078: * </ul>
079: * </dd>
080: *
081: * <dd><b>Expand</b> : (widget, item)<br>
082: * <div>Fires after a item has been expanded.</div>
083: * <ul>
084: * <li>widget : this</li>
085: * <li>item : the item being expanded</li>
086: * </ul>
087: * </dd>
088: *
089: * <dd><b>Collapse</b> : (widget, item)<br>
090: * <div>Fires ater a item is collapsed.</div>
091: * <ul>
092: * <li>widget : this</li>
093: * <li>item : the item being collapsed</li>
094: * </ul>
095: * </dd>
096: *
097: * <dd><b>CheckChange</b> : (widget)<br>
098: * <div>Fires after a check state change.</div>
099: * <ul>
100: * <li>widget : this</li>
101: * </ul>
102: * </dd>
103: *
104: * <dd><b>CellClick</b> : (widget, item, index)<br>
105: * <div>Fires after a cell has been clicked.</div>
106: * <ul>
107: * <li>widget : tree table</li>
108: * <li>item : this</li>
109: * <li>index : cell column index</li>
110: * </ul>
111: * </dd>
112: *
113: * <dd><b>CellDoubleClick</b> : (widget, item, index)<br>
114: * <div>Fires after a cell has been double clicked.</div>
115: * <ul>
116: * <li>widget : tree table</li>
117: * <li>item : this</li>
118: * <li>index : cell column index</li>
119: * </ul>
120: * </dd>
121: *
122: * <dd><b>RowClick</b> : (widget, item, index)<br>
123: * <div>Fires after a cell has been clicked.</div>
124: * <ul>
125: * <li>widget : tree table</li>
126: * <li>item : item that represents the row</li>
127: * <li>index : cell column index</li>
128: * </ul>
129: * </dd>
130: *
131: * <dd><b>RowDoubleClick</b> : (widget, item, index)<br>
132: * <div>Fires after a cell has been double clicked.</div>
133: * <ul>
134: * <li>widget : tree table</li>
135: * <li>item : item that represents the row</li>
136: * <li>index : cell column index</li>
137: * </ul>
138: * </dd>
139: *
140: * <dt><b>CSS:</b></dt>
141: * <dd>.my-tree-item (the item itself)</dd>
142: * <dd>.my-tree-item-text span (the tree item text)</dd>
143: * </dl>
144: */
145: public class TreeTableItem extends TreeItem {
146:
147: protected TreeTable treeTable;
148:
149: String[] toolTips;
150: ToolTip cellToolTip;
151: boolean hasWidgets;
152: String[] cellStyles;
153: boolean cellsRendered;
154: private Object[] values;
155:
156: /**
157: * Creates a new tree table item.
158: *
159: * @param values the cell values
160: */
161: public TreeTableItem(Object[] values) {
162: this .values = values;
163: }
164:
165: /**
166: * Creates a new tree table item.
167: *
168: * @param text the text
169: * @param values the values
170: */
171: public TreeTableItem(String text, Object[] values) {
172: super (text);
173: this .values = values;
174: }
175:
176: /**
177: * Returns the item's cell tooltip.
178: *
179: * @return the tooltip
180: */
181: public ToolTip getCellToolTip() {
182: return cellToolTip;
183: }
184:
185: /**
186: * Returns the item's tree table.
187: *
188: * @return the tree table
189: */
190: public TreeTable getTreeTable() {
191: return treeTable;
192: }
193:
194: /**
195: * Returns a cell value.
196: *
197: * @param index the cell index
198: * @return the value
199: */
200: public Object getValue(int index) {
201: return values[index];
202: }
203:
204: /**
205: * Returns the item's values.
206: *
207: * @return the values
208: */
209: public Object[] getValues() {
210: return values;
211: }
212:
213: public void onBaseEvent(BaseEvent be) {
214: if (be.widget != this ) {
215: return;
216: }
217:
218: if (ui != null) {
219: getUI().getListener().handleEvent(be);
220: }
221:
222: if (cellToolTip != null) {
223: cellToolTip.handleEvent(be);
224: }
225:
226: switch (be.type) {
227: case Events.Click:
228: onClick(be);
229: break;
230: case Events.DoubleClick:
231: onDoubleClick(be);
232: break;
233: case Events.MouseOver:
234: onMouseOver(be);
235: break;
236: }
237:
238: }
239:
240: /**
241: * Sets the style for a table cell.
242: *
243: * @param index the column index
244: * @param style the new style
245: */
246: public void setCellStyle(int index, String style) {
247: if (cellStyles == null)
248: cellStyles = new String[values.length];
249: cellStyles[index] = style;
250: if (isRendered()) {
251: treeTable.getView().setCellStyle(this , index, style);
252: }
253: }
254:
255: /**
256: * Sets a cell tooltip.
257: *
258: * @param index the column index
259: * @param text the text of the tool tip
260: */
261: public void setCellToolTip(int index, String text) {
262: if (toolTips == null)
263: toolTips = new String[values.length];
264: toolTips[index] = text;
265: initCellToolTips();
266: }
267:
268: /**
269: * Sets all of the cell tooltips
270: *
271: * @param toolTips the tool tips to use
272: */
273: public void setCellToolTips(String[] toolTips) {
274: this .toolTips = toolTips;
275: initCellToolTips();
276: }
277:
278: /**
279: * Sets a cell value.
280: *
281: * @param index the column index
282: * @param text the text
283: */
284: public void setText(int index, String text) {
285: setValue(index, text);
286: }
287:
288: /**
289: * Sets a cell value.
290: *
291: * @param index the column index
292: * @param value the value
293: */
294: public void setValue(int index, Object value) {
295: values[index] = value;
296: if (rendered) {
297: treeTable.getView().renderItemValue(this , index, value);
298: }
299: }
300:
301: /**
302: * Sets the item's values.
303: *
304: * @param values the values
305: */
306: public void setValues(Object[] values) {
307: this .values = values;
308: }
309:
310: protected String[] getRenderedValues() {
311: String[] svalues = new String[values.length];
312: for (int i = 0; i < values.length; i++) {
313: svalues[i] = treeTable.getRenderedValue(i, values[i]);
314: }
315: return svalues;
316: }
317:
318: /**
319: * Subclasses may override.
320: *
321: * @return the ui
322: */
323: protected TreeItemUI getTreeItemUI() {
324: return new TreeTableItemUI(this );
325: }
326:
327: protected void init(TreeTable treeTable) {
328: this .tree = treeTable;
329: this .treeTable = treeTable;
330: }
331:
332: protected void initCellToolTips() {
333: if (cellToolTip == null && isRendered()) {
334: cellToolTip = new ToolTip(this );
335: cellToolTip.setTrackMouse(true);
336: }
337: }
338:
339: public void setElement(Element elem) {
340: super .setElement(elem);
341: }
342:
343: protected void onMouseOver(BaseEvent be) {
344: onCellMouseOver(be);
345: }
346:
347: protected void onCellMouseOver(BaseEvent be) {
348: Element target = be.getTarget();
349:
350: int index = treeTable.getView().getCellIndex(target);
351: if (index == Style.DEFAULT) {
352: return;
353: }
354:
355: if (cellToolTip != null) {
356: if (toolTips != null && toolTips[index] != null
357: && toolTips[index].length() > 0) {
358: cellToolTip.setText(null, toolTips[index]);
359: cellToolTip.setVisible(true);
360: } else {
361: cellToolTip.setVisible(false);
362: }
363: }
364: }
365:
366: protected void onClick(BaseEvent be) {
367: Element target = be.getTarget();
368:
369: int index = treeTable.getView().getCellIndex(target);
370: if (index == Style.DEFAULT) {
371: return;
372: }
373:
374: be.widget = getTreeTable();
375: be.item = this ;
376: be.index = index;
377:
378: treeTable.fireEvent(Events.CellClick, be);
379: treeTable.fireEvent(Events.RowClick, be);
380: }
381:
382: protected void onDoubleClick(BaseEvent be) {
383: Element target = be.getTarget();
384:
385: int index = treeTable.getView().getCellIndex(target);
386: if (index == Style.DEFAULT) {
387: return;
388: }
389: BaseEvent evt = new BaseEvent();
390: evt.widget = getTreeTable();
391: evt.item = this ;
392: evt.index = index;
393:
394: treeTable.fireEvent(Events.CellDoubleClick, evt);
395: treeTable.fireEvent(Events.RowDoubleClick, evt);
396: }
397:
398: protected void setTree(Tree tree) {
399: super .setTree(tree);
400: treeTable = (TreeTable) tree;
401: }
402:
403: }
|