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.table;
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.Format;
017: import net.mygwt.ui.client.widget.Component;
018: import net.mygwt.ui.client.widget.SplitBar;
019:
020: import com.google.gwt.user.client.DOM;
021: import com.google.gwt.user.client.Element;
022: import com.google.gwt.user.client.Event;
023:
024: public class TableColumnUI extends Component {
025:
026: protected static String html;
027:
028: static {
029: StringBuffer sb = new StringBuffer();
030: sb.append("<div class=my-tbl-col-overflow style='align: {0}'>");
031: sb.append("<div class=my-tbl-col-text>{1}</div>");
032: sb.append("</div>");
033: html = sb.toString();
034: }
035:
036: protected int lastWidth;
037: protected boolean end;
038: protected int index;
039: protected ITable table;
040: protected TableColumn column;
041: protected TableHeader header;
042: protected SplitBar splitBar;
043: protected Listener splitBarListener = new Listener() {
044: public void handleEvent(BaseEvent be) {
045: switch (be.type) {
046: case Events.DragStart:
047: header.setEnabled(false);
048: break;
049: case Events.DragEnd:
050: header.setEnabled(true);
051: break;
052: }
053: }
054: };
055:
056: protected TableColumnUI(ITable table, int index) {
057: this .table = table;
058: this .index = index;
059: column = table.getColumn(index);
060: header = table.getTableHeader();
061: }
062:
063: public void onBaseEvent(BaseEvent be) {
064: switch (be.type) {
065: case Events.MouseOver:
066: onMouseOver(be);
067: break;
068: case Events.MouseOut:
069: onMouseOut(be);
070: break;
071: case Events.Click:
072: header.onColumnClick(this );
073: break;
074: }
075: }
076:
077: public void onBrowserEvent(Event event) {
078: super .onBrowserEvent(event);
079: if (DOM.eventGetType(event) == Event.ONMOUSEUP) {
080: if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT
081: || (MyGWT.isMac && DOM.eventGetCtrlKey(event))) {
082: header.onRightClick(column, event);
083: }
084: }
085: }
086:
087: protected void onColumnResize(BaseEvent be) {
088: if (be.size < 1) {
089: return;
090: }
091: if (column.getWidth() < 1.1) {
092: int pixWidth = be.size - 6;
093: float percent = (float) pixWidth
094: / table.getColumnModel().getVariableWidth();
095: table.getColumnModel().setWidthAsPercent(column.index,
096: percent);
097: header.resizeColumns(true, true);
098: } else {
099: column.setWidth(be.size);
100: header.resizeColumn(index, true);
101: }
102: }
103:
104: protected void onMouseMove(BaseEvent be) {
105: header.onColumnMouseMove(this , be);
106: }
107:
108: protected void onMouseOut(BaseEvent be) {
109: removeStyleName("my-tbl-col-over");
110: }
111:
112: protected void onMouseOver(BaseEvent be) {
113: addStyleName("my-tbl-col-over");
114: }
115:
116: protected void onRender() {
117: String[] params = null;
118: if (end) {
119: params = new String[] { "", "" };
120: } else {
121: params = new String[] { getAlignment(), column.getText() };
122: }
123: String s = Format.substitute(html, params);
124: setElement(MyDOM.create(s));
125: MyDOM.setHeight(getElement(), 24);
126:
127: if (!end && column.isResizable()) {
128: splitBar = header.createSplitBar(Style.EAST, this );
129: splitBar.setBarWidth(6);
130: splitBar.setXOffset(-2);
131: splitBar.setAutoSize(false);
132: splitBar.addListener(Events.Resize, new Listener() {
133: public void handleEvent(BaseEvent be) {
134: onColumnResize(be);
135: }
136: });
137: splitBar.addListener(Events.DragStart, splitBarListener);
138: splitBar.addListener(Events.DragEnd, splitBarListener);
139: }
140:
141: sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS);
142: }
143:
144: protected void onSortChange(int sortDir) {
145: Element td = DOM.getParent(getElement());
146: switch (sortDir) {
147: case Style.ASC:
148: MyDOM.addStyleName(td, "my-tbl-col-sort");
149: addStyleName("my-tbl-col-asc");
150: break;
151: case Style.DESC:
152: MyDOM.addStyleName(td, "my-tbl-col-sort");
153: addStyleName("my-tbl-col-desc");
154: break;
155: default:
156: MyDOM.removeStyleName(td, "my-tbl-col-sort");
157: removeStyleName("my-tbl-col-asc");
158: removeStyleName("my-tbl-col-desc");
159: break;
160: }
161: }
162:
163: protected void onTextChange(String text) {
164: Element textEl = MyDOM.findChild("my-tbl-col-text",
165: getElement());
166: MyDOM.setInnerHTML(textEl, text);
167: }
168:
169: private String getAlignment() {
170: String align = "left";
171: if (column != null) {
172: if (column.getAlignment() == Style.CENTER) {
173: align = "center";
174: } else if (column.getAlignment() == Style.RIGHT) {
175: align = "right";
176: }
177: }
178: return align;
179: }
180:
181: }
|