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.MyDOM;
012: import net.mygwt.ui.client.MyGWT;
013: import net.mygwt.ui.client.Style;
014: import net.mygwt.ui.client.event.BaseEvent;
015: import net.mygwt.ui.client.event.Listener;
016: import net.mygwt.ui.client.util.Markup;
017: import net.mygwt.ui.client.util.Rectangle;
018: import net.mygwt.ui.client.widget.ToolTip;
019: import net.mygwt.ui.client.widget.table.TableColumnModel;
020: import net.mygwt.ui.client.widget.tree.TreeItemUI;
021:
022: import com.google.gwt.user.client.DOM;
023: import com.google.gwt.user.client.Element;
024:
025: public class TreeTableItemUI extends TreeItemUI {
026:
027: protected static Listener listener = new Listener() {
028: public void handleEvent(BaseEvent be) {
029: TreeTableItem item = (TreeTableItem) be.widget;
030: TreeItemUI ui = item.getUI();
031: Element target = be.getTarget();
032: int type = be.type;
033: switch (type) {
034: case Events.MouseOver:
035: case Events.MouseOut:
036: treeTableMouseListener.handleEvent(be);
037: break;
038: case Events.Click:
039: case Events.DoubleClick:
040: be.stopEvent();
041: if (DOM.isOrHasChild(ui.getCheckEl(), target)) {
042: item.setChecked(!item.isChecked());
043: } else {
044: clickListener.handleEvent(be);
045: }
046: break;
047: }
048: }
049:
050: };
051:
052: protected static Listener treeTableMouseListener = new Listener() {
053: public void handleEvent(BaseEvent be) {
054: TreeTableItem item = (TreeTableItem) be.widget;
055: TreeTableItemUI treeUI = (TreeTableItemUI) item.getUI();
056: Rectangle rect = MyDOM.getBounds(DOM.getFirstChild(item
057: .getElement()));
058: if (rect.contains(be.getClientX(), be.getClientY())) {
059: if (!treeUI.hovering) {
060: treeUI.hovering = true;
061: treeUI.onMouseOver(be);
062: }
063: } else {
064: treeUI.hovering = false;
065: treeUI.onMouseOut(be);
066: }
067:
068: }
069: };
070:
071: boolean hovering;
072: private Element tableItemEl;
073: private Element rowTableEl;
074: private Element rowTrEl;
075: private Element[] cells;
076:
077: public TreeTableItemUI(TreeTableItem item) {
078: super (item);
079: styleTreeOver = "my-treetbl-over";
080: styleTreeJointOver = "my-treetbl-joint-over";
081: styleTreeChecked = "my-treetbl-checked";
082: styleTreeNotChecked = "my-treetbl-notchecked";
083: styleTreeLoading = "my-treetbl-loading";
084: styleTreeSelected = "my-treetbl-sel";
085: classTreeOpen = "my-treetbl-open";
086: classTreeClose = "my-treetbl-close";
087: render();
088: }
089:
090: public Element getContainer() {
091: return containerEl;
092: }
093:
094: public Listener getListener() {
095: return listener;
096: }
097:
098: public TreeTableItem getTreeTableItem() {
099: return (TreeTableItem) item;
100: }
101:
102: public void onMouseOut(BaseEvent be) {
103: ToolTip tooltip = getTreeTableItem().getCellToolTip();
104: if (tooltip != null && tooltip.isShowing()) {
105: tooltip.hide();
106: }
107: if (!item.isRoot()) {
108: MyDOM.setStyleName(tableItemEl, "my-treetbl-item-over",
109: false);
110: }
111: }
112:
113: public void onSelectedChange(boolean selected) {
114: if (item.isRendered()) {
115: MyDOM.setStyleName(tableItemEl, "my-treetbl-item-sel",
116: selected);
117: }
118: }
119:
120: public void setContainer(Element container) {
121: containerEl = container;
122: }
123:
124: protected Element getTextCellElement(int column) {
125: if (column == 0) {
126: return textSpanEl;
127: } else {
128: return MyDOM.getSubChild(cells[column], 2);
129: }
130: }
131:
132: protected void onMouseOver(BaseEvent be) {
133: if (!item.isRoot()) {
134: MyDOM.setStyleName(tableItemEl, "my-treetbl-item-over",
135: true);
136: }
137: }
138:
139: protected void onValuesChanged(TreeTable table, String[] values) {
140: onTextChange(item.getText());
141: for (int i = 1; i < cells.length; i++) {
142: updateText(i, values[i]);
143: }
144: }
145:
146: protected void render() {
147: if (item.isRoot() == true) {
148: return;
149: }
150:
151: item.setElement(DOM.createDiv());
152: item.setStyleName("my-treeitem");
153:
154: TableColumnModel cm = getTreeTableItem().getTreeTable()
155: .getColumnModel();
156:
157: DOM.appendChild(item.getParentItem().getContainer(), item
158: .getElement());
159: DOM.setInnerHTML(item.getElement(), Markup.TREETABLE_ITEM);
160: tableItemEl = DOM.getFirstChild(item.getElement());
161:
162: rowTableEl = DOM.getFirstChild(tableItemEl);
163: rowTrEl = MyDOM.getSubChild(rowTableEl, 2);
164: updateCellValues(0, DOM.getFirstChild(rowTrEl), cm.getColumn(0)
165: .getAlignment());
166:
167: itemEl = MyDOM.getSubChild(rowTrEl, 4);
168: Element td = MyDOM.getSubChild(itemEl, 3);
169: indentEl = DOM.getFirstChild(td);
170: jointEl = DOM.getNextSibling(td);
171: jointDivEl = DOM.getFirstChild(jointEl);
172: checkEl = DOM.getNextSibling(DOM.getNextSibling(jointEl));
173: checkDivEl = DOM.getFirstChild(checkEl);
174: iconEl = DOM.getNextSibling(checkEl);
175: iconDivEl = DOM.getFirstChild(iconEl);
176: textEl = DOM.getNextSibling(iconEl);
177: textSpanEl = DOM.getFirstChild(textEl);
178: Element tbl = DOM.getFirstChild(item.getElement());
179: containerEl = DOM.getNextSibling(tbl);
180:
181: int numColumns = cm.getColumnCount();
182: cells = new Element[numColumns];
183:
184: for (int i = 1; i < numColumns; i++) {
185: cells[i] = DOM.createTD();
186: DOM.appendChild(rowTrEl, cells[i]);
187:
188: DOM.setElementProperty(cells[i], "className",
189: "my-treetbl-cell");
190: DOM.setElementAttribute(cells[i], "index", String
191: .valueOf(i));
192:
193: Element overflowDiv = DOM.createDiv();
194: DOM.setElementProperty(overflowDiv, "className",
195: "my-treetbl-cell-overflow");
196: DOM.appendChild(cells[i], overflowDiv);
197: Element textDiv = DOM.createDiv();
198: DOM.setElementProperty(textDiv, "className",
199: "my-treetbl-cell-text");
200: DOM.appendChild(overflowDiv, textDiv);
201: updateCellValues(i, cells[i], cm.getColumn(i)
202: .getAlignment());
203: }
204:
205: int style = getTreeTableItem().getTreeTable().getStyle();
206: if ((style & Style.CHECK) != 0) {
207: MyDOM.setVisible(checkEl, true);
208: } else {
209: MyDOM.setVisible(checkEl, false);
210: }
211:
212: onValuesChanged(getTreeTableItem().getTreeTable(),
213: getTreeTableItem().getRenderedValues());
214:
215: onIconStyleChange(item.getIconStyle());
216:
217: if (item.isChecked()) {
218: onCheckChange(true);
219: }
220:
221: MyDOM.setWidth(indentEl, getIndent());
222:
223: if (!MyGWT.isIE) {
224: DOM.setElementPropertyInt(item.getElement(), "tabIndex", 0);
225: }
226:
227: getTreeTableItem().initCellToolTips();
228:
229: updateJoint();
230: item.disableTextSelection(true);
231:
232: }
233:
234: protected void updateCellValues(int col, Element cell, int align) {
235: String salign = "left";
236: if (align == Style.CENTER) {
237: salign = "center";
238: } else if (align == Style.RIGHT) {
239: salign = "right";
240: }
241:
242: String widthClassName = ((TreeTableItem) item).treeTable
243: .getId()
244: + "-col-" + col;
245:
246: String className = DOM.getElementProperty(cell, "className");
247: className = (className == null) ? widthClassName : className
248: + " " + widthClassName;
249: DOM.setElementProperty(cell, "className", className);
250:
251: className = DOM.getElementProperty(DOM.getFirstChild(cell),
252: "className");
253: className = (className == null) ? widthClassName : className
254: + " " + widthClassName;
255: DOM.setElementProperty(DOM.getFirstChild(cell), "className",
256: className);
257:
258: DOM.setStyleAttribute(MyDOM.getSubChild(cell, 2), "textAlign",
259: salign);
260: }
261:
262: protected void updateText(int column, String value) {
263: Element textElem = getTextCellElement(column);
264: if (textElem != null) {
265: MyDOM.setInnerHTML(textElem, value);
266: }
267: }
268: }
|