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.MyDOM;
011: import net.mygwt.ui.client.MyGWT;
012: import net.mygwt.ui.client.Style;
013: import net.mygwt.ui.client.widget.table.TableColumnModel;
014: import net.mygwt.ui.client.widget.table.TableItem;
015: import net.mygwt.ui.client.widget.tree.TreeItem;
016:
017: import com.google.gwt.user.client.DOM;
018: import com.google.gwt.user.client.Element;
019: import com.google.gwt.user.client.Event;
020: import com.google.gwt.user.client.ui.Widget;
021: import com.google.gwt.user.client.ui.WidgetHelper;
022:
023: /**
024: * This class encapsulates the user interface of a {@link TreeTable}.
025: */
026: public class TreeTableView {
027:
028: private static String bodyHTML;
029:
030: static {
031: StringBuffer sb = new StringBuffer();
032: sb.append("<div style='overflow: hidden;'>");
033: sb.append("<div style='overflow: scroll;'>");
034: sb.append("<div class='my-treetbl-data'>");
035: sb.append("<div class='my-treetbl-tree'></div>");
036: sb.append("</div></div></div>");
037: bodyHTML = sb.toString();
038: }
039:
040: // styles
041: protected String baseStyle = "my-tbl-item";
042: protected String overStyle = baseStyle + "-over";
043: protected String selStyle = baseStyle + "-sel";
044: protected String cellStyle = baseStyle + "-" + "cell";
045: protected String cellOverflowStyle = baseStyle + "-" + "overflow";
046: protected String textStyle = cellStyle + "-text";
047: protected String widgetStyle = cellStyle + "-widget";
048:
049: protected TableColumnModel cm;
050: protected Element scrollElem, dataElem, treeDiv;
051: protected TreeTable treeTable;
052: protected int scrollBarWidth;
053:
054: protected void applyCellStyles(TreeTableItem item) {
055: if (item.cellStyles != null) {
056: for (int i = 0; i < item.cellStyles.length; i++) {
057: setCellStyle(item, i, item.cellStyles[i]);
058: }
059: }
060: }
061:
062: protected void clearHoverStyles() {
063: TreeItem[] allItems = treeTable.getAllItems();
064: for (int i = 0; i < allItems.length; i++) {
065: ((TreeTableItem) allItems[i]).getUI().onMouseOut(null);
066: }
067: }
068:
069: protected int getCellIndex(Element target) {
070: String index = MyDOM.getAnyElementProperty(target, "index");
071: if (index == null) {
072: target = DOM.getParent(target);
073: while (target != null) {
074: index = MyDOM.getAnyElementProperty(target, "index");
075: if (index == null) {
076: target = DOM.getParent(target);
077: } else {
078: break;
079: }
080: }
081: }
082: return index == null ? Style.DEFAULT : Integer.parseInt(index);
083: }
084:
085: public Element getScrollElement() {
086: return scrollElem;
087: }
088:
089: public Element getTextCellElement(TreeTableItem item, int cell) {
090: return ((TreeTableItemUI) item.getUI())
091: .getTextCellElement(cell);
092: }
093:
094: public void init(TreeTable treeTable) {
095: this .treeTable = treeTable;
096: this .cm = treeTable.getColumnModel();
097: }
098:
099: protected void removeItem(TableItem item) {
100: DOM.removeChild(dataElem, item.getElement());
101: }
102:
103: protected void render() {
104: scrollBarWidth = MyDOM.getScrollBarWidth();
105:
106: Element div = DOM.createDiv();
107: DOM.setInnerHTML(div, bodyHTML.toString());
108: scrollElem = MyDOM.getSubChild(div, 2);
109: dataElem = DOM.getFirstChild(scrollElem);
110: treeDiv = DOM.getFirstChild(dataElem);
111: DOM.appendChild(treeDiv, treeTable.getRootItem().getElement());
112: DOM.appendChild(treeTable.getElement(), DOM.getFirstChild(div));
113:
114: if (!MyGWT.isIE) {
115: DOM.setElementPropertyInt(treeTable.getElement(),
116: "tabIndex", 0);
117: }
118:
119: treeTable.disableTextSelection(true);
120:
121: DOM.sinkEvents(scrollElem, Event.ONSCROLL);
122: }
123:
124: protected void renderItemValue(TreeTableItem item, int index,
125: Object value) {
126: Element textElem = getTextCellElement(item, index);
127: if (textElem != null) {
128: DOM.setInnerHTML(textElem, "");
129: if (value instanceof Widget) {
130: Widget widget = (Widget) value;
131: MyDOM.setStyleName(textElem, widgetStyle);
132: DOM.appendChild(textElem, widget.getElement());
133: if (treeTable.isAttached()) {
134: WidgetHelper.doAttach(widget);
135: }
136: } else {
137: String s = treeTable.getRenderedValue(index, value);
138: MyDOM.setInnerHTML(textElem, s);
139: }
140: }
141: applyCellStyles(item);
142: }
143:
144: public void resize() {
145: int width = treeTable.getOffsetWidth();
146: int headerHeight = treeTable.getTableHeader().getOffsetHeight();
147: int bodyHeight = treeTable.getOffsetHeight() - headerHeight;
148: int bodyWidth = width;
149:
150: MyDOM.setWidth(dataElem, cm.getTotalWidth());
151:
152: boolean vscroll = MyDOM.getHeight(dataElem) > bodyHeight;
153: int adj = vscroll ? scrollBarWidth : 0;
154:
155: if ((treeTable.getStyle() & Style.HORIZONTAL) != 0) {
156: if (MyDOM.getWidth(dataElem) < (width - adj)) {
157: bodyHeight += scrollBarWidth;
158: MyDOM.setLeft(treeTable.getTableHeader().getElement(),
159: 0);
160: }
161: } else {
162: bodyHeight += scrollBarWidth;
163: }
164: if (MyDOM.getHeight(dataElem) > bodyHeight) {
165: width -= scrollBarWidth;
166: }
167:
168: bodyHeight -= MyDOM.getBorderWidth(treeTable.getElement(),
169: Style.TOP | Style.BOTTOM);
170: bodyWidth -= MyDOM.getBorderWidth(treeTable.getElement(),
171: Style.LEFT | Style.RIGHT);
172:
173: MyDOM.setSize(scrollElem, bodyWidth, bodyHeight);
174:
175: int w = cm.getTotalWidth();
176:
177: if (w < width) {
178: adj = width - w;
179: }
180: MyDOM.setWidth(dataElem, cm.getTotalWidth() + adj);
181: }
182:
183: public void resizeCells(int columnIndex) {
184: TreeTableColumn c = (TreeTableColumn) cm.getColumn(columnIndex);
185: int w = ((TreeTableColumnModel) cm).getWidthInPixels(c
186: .getIndex());
187: String sel = "." + treeTable.getId() + "-col-" + columnIndex;
188: String rule = "width:" + w + "px;"
189: + (!c.isHidden() ? "" : "display: none;");
190: treeTable.styleTemplate.set(sel, rule);
191: treeTable.styleTemplate.apply();
192: }
193:
194: protected void setCellStyle(TreeTableItem item, int index,
195: String style) {
196: if (item.cellsRendered) {
197: Element cell = getTextCellElement(item, index);
198: MyDOM.setStyleName(cell, textStyle + " " + style);
199: }
200: }
201:
202: protected void showColumn(int columnIndex, boolean show) {
203: TreeTableColumn c = (TreeTableColumn) cm.getColumn(columnIndex);
204: int w = ((TreeTableColumnModel) cm).getWidthInPixels(c
205: .getIndex());
206: String sel = "." + treeTable.getId() + "-col-" + columnIndex;
207: String rule = "width:" + w + "px;"
208: + (!c.isHidden() ? "" : "display: none;");
209: treeTable.styleTemplate.set(sel, rule);
210: treeTable.styleTemplate.apply();
211: }
212: }
|