001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.gwt.widget;
022:
023: import org.onemind.swingweb.client.core.ui.EventInterceptor;
024: import com.google.gwt.user.client.DOM;
025: import com.google.gwt.user.client.Event;
026: import com.google.gwt.user.client.ui.*;
027:
028: public class Table extends Container implements EventInterceptor,
029: TableListener {
030:
031: public static class Cell {
032:
033: private int _row;
034:
035: private int _col;
036:
037: /**
038: * Return the col
039: * @return the col.
040: */
041: public final int getCol() {
042: return _col;
043: }
044:
045: /**
046: * Return the row
047: * @return the row.
048: */
049: public final int getRow() {
050: return _row;
051: }
052:
053: public Cell(int row, int col) {
054: _row = row;
055: _col = col;
056: }
057: }
058:
059: private TableDataListenerCollection _dataListeners = new TableDataListenerCollection();
060:
061: private Grid _grid = new Grid();
062:
063: private Cell _lastSelected;
064:
065: private ClickDispatch _clickDispatch = new ClickDispatch();
066:
067: private String _lastStylename;
068:
069: /**
070: * Constructor
071: */
072: public Table() {
073: super ();
074: DOM.setStyleAttribute(_grid.getElement(), "width", "100%");
075: DOM.setStyleAttribute(_grid.getElement(), "height", "100%");
076: _grid.setCellPadding(0);
077: _grid.setCellSpacing(0);
078: _grid.setBorderWidth(1);
079: _grid.addTableListener(this );
080: setWidget(1, 0, _grid);
081: super .sinkEvents(Event.ONDBLCLICK | Event.ONCLICK);
082: }
083:
084: public void onBrowserEvent(Event event) {
085: int eventType = DOM.eventGetType(event);
086: switch (eventType) {
087: case Event.ONMOUSEDOWN: //only clear it on mouse down
088: _lastSelected = null;
089: break;
090: case Event.ONMOUSEUP:
091: if (_lastSelected != null) {
092: //_clickDispatch.processClick(_lastSelected.getWidget());
093: _clickDispatch.processClick(this );
094: }
095: }
096: super .onBrowserEvent(event);
097: }
098:
099: public void resize(int rows, int col) {
100: _grid.resize(rows + 1, col);
101: }
102:
103: public void setHeaderWidget(int col, Widget header) {
104: _grid.setWidget(0, col, header);
105: _grid.getRowFormatter().addStyleName(0, "header_row");
106: }
107:
108: public void setCellWidget(int row, int col, Widget widget) {
109: _grid.setWidget(row + 1, col, widget);
110: if (row % 2 == 0) {
111: _grid.getRowFormatter().setStyleName(row + 1, "odd_row");
112: } else {
113: _grid.getRowFormatter().setStyleName(row + 1, "even_row");
114: }
115: DOM.setStyleAttribute(widget.getElement(), "backgroundColor",
116: null);
117: }
118:
119: public void addTableDataListener(TableDataListener l) {
120: _dataListeners.add(l);
121: //_grid.addTableListener(l);
122: }
123:
124: public boolean interceptEvent(Object eventSource, Object data) {
125: _dataListeners.fireDataChangedEvent(this , "true");
126: System.out.println("data change ");
127: return true;
128: }
129:
130: public void addDoubleClickListener(DoubleClickListener listener) {
131: _clickDispatch.addDoubleClickListener(listener);
132: }
133:
134: public void onCellClicked(SourcesTableEvents sender, int row,
135: int cell) {
136: System.out.println("Cell click at " + row + ", " + cell);
137: if (_lastSelected != null) {
138: if (_lastSelected.getRow() == 0) {
139: _grid.getColumnFormatter().removeStyleName(
140: _lastSelected.getCol(), "selected_col");
141: } else {
142: _grid.getRowFormatter().removeStyleName(
143: _lastSelected.getRow(), "selected_row");
144: }
145: }
146: _lastSelected = new Cell(row, cell);
147: if (_lastSelected.getRow() == 0) {
148: _grid.getColumnFormatter().addStyleName(cell,
149: "selected_col");
150: } else {
151: _grid.getRowFormatter().addStyleName(row, "selected_row");
152: }
153: }
154: }
|