001: /* SwingML
002: * Copyright (C) 2002 SwingML Team
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 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
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Ezequiel Cuellar <ecuellar@crosslogic.com>
021: * Robert Morris <robertj@morris.net>
022: */
023:
024: package org.swingml.component;
025:
026: import java.awt.event.*;
027: import java.util.*;
028:
029: import javax.swing.*;
030: import javax.swing.event.*;
031: import javax.swing.table.*;
032:
033: import org.swingml.*;
034: import org.swingml.event.*;
035: import org.swingml.model.*;
036: import org.swingml.model.TableColumnModel;
037:
038: public class JTableComponent extends JTable implements XMLTranslatable,
039: MouseListener, ListSelectionListener, XMLStateTranslatable {
040:
041: private EventHandler eventHandler = EventHandler.getInstance();
042:
043: private JTableModel m_tableModel = null;
044: private StringBuffer xmlValue = null;
045:
046: public JTableComponent(JTableModel aModel) {
047: this .m_tableModel = aModel;
048: super .setModel(aModel);
049: super .setName(aModel.getName());
050: super .setToolTipText(aModel.getTooltip());
051: super .addMouseListener(this );
052: super .getSelectionModel().setSelectionMode(aModel.getMode());
053: ToolTipManager.sharedInstance().registerComponent(this );
054: applyColumnEditors();
055: }
056:
057: private TableRowModel getRow(int rowIndex) {
058: TableRowModel result = null;
059: if (rowIndex >= 0) {
060: JTableModel model = (JTableModel) getModel();
061: if (model != null) {
062: List rows = model.getRows();
063: if (rows != null && rows.size() > 0) {
064: result = (TableRowModel) rows.get(rowIndex);
065: }
066: }
067: }
068: return result;
069: }
070:
071: private boolean isSelected(int rowIndex) {
072: boolean result = false;
073: if (rowIndex >= 0) {
074: int[] selectedRows = getSelectedRows();
075: if (selectedRows != null && selectedRows.length > 0) {
076: for (int x = 0; x < selectedRows.length; x++) {
077: if (selectedRows[x] == rowIndex) {
078: result = true;
079: break;
080: }
081: }
082: }
083: }
084:
085: return result;
086: }
087:
088: private String xmlCol(int rowIndex, int colIndex) {
089: String result = "";
090: result += xmlColStart(colIndex);
091: result += xmlColValue(rowIndex, colIndex);
092: result += xmlColEnd();
093:
094: return result;
095: }
096:
097: private String xmlColEnd() {
098: return "</TD>";
099: }
100:
101: private String xmlColStart(int colIndex) {
102: return "<TD index=\"" + colIndex + "\">";
103: }
104:
105: private String xmlColumnState() {
106: StringBuffer result = new StringBuffer();
107: JTableModel theModel = (JTableModel) super .getModel();
108: List cols = theModel.getColumns();
109: for (int columnIndex = 0; columnIndex < cols.size(); columnIndex++) {
110: TableColumnModel colModel = (TableColumnModel) cols
111: .get(columnIndex);
112: result.append("<COLUMN ");
113: result.append(Constants.TEXT);
114: result.append("=\"");
115: result.append(colModel.getText());
116: result.append("\" ");
117: result.append(Constants.COLUMN_ORDER);
118: result.append("=\"");
119: result.append(convertColumnIndexToView(columnIndex));
120: result.append("\" ");
121: result.append(Constants.WIDTH);
122: result.append("=\"");
123: result.append(getColumn(colModel.getText()).getWidth());
124: result.append("\" ");
125: result.append(Constants.SORT);
126: result.append("=\"");
127: result.append("ASC");
128: result.append("\" ");
129: result.append(Constants.SORTABLE);
130: result.append("=\"");
131: result.append(colModel.isSortable() ? Constants.TRUE
132: : Constants.FALSE);
133: result.append("\" ");
134: result.append(Constants.SORTORDER);
135: result.append("=\"");
136: result.append(-1);
137: result.append("\" />");
138: }
139: return result.toString();
140: }
141:
142: private String xmlColValue(int rowIndex, int colIndex) {
143: return super .getModel().getValueAt(rowIndex, colIndex)
144: .toString();
145: }
146:
147: private String xmlRowEnd() {
148: return "</TR>";
149: }
150:
151: private String xmlRowStart(int rowIndex, TableRowModel row,
152: boolean selected) {
153: String value = "";
154: if (row != null) {
155: value = row.getValue();
156: }
157: return "<TR index=\"" + rowIndex + "\" selected=\"" + selected
158: + "\" value=\"" + value + "\">";
159: }
160:
161: private String xmlTableEnd() {
162: return "</TABLE>";
163: }
164:
165: private String xmlTableStart() {
166: return "<TABLE NAME=\"" + getName() + "\">";
167: }
168:
169: public void applyColumnEditors() {
170: JTableModel aModel = (JTableModel) getModel();
171: for (int i = 0; i < aModel.getColumnCount(); i++) {
172: TableColumnModel m = (TableColumnModel) aModel.getColumns()
173: .get(i);
174: if (m.getCustomEditor() != null) {
175: getColumnModel().getColumn(i).setCellEditor(
176: m.getCustomEditor());
177: }
178:
179: }
180: }
181:
182: public TableCellRenderer getCellRenderer(int row, int column) {
183: return super .getCellRenderer(row, column);
184: }
185:
186: public String getToolTipText(MouseEvent e) {
187: String aTip = null;
188: java.awt.Point p = e.getPoint();
189: int rowIndex = rowAtPoint(p);
190: int colIndex = columnAtPoint(p);
191: int realColumnIndex = convertColumnIndexToModel(colIndex);
192: JTableModel model = (JTableModel) getModel();
193: aTip = model.getToolTip(rowIndex, realColumnIndex);
194: if (aTip == null) {
195: aTip = getToolTipText();
196: }
197: return aTip;
198: }
199:
200: public String getXMLState() {
201: StringBuffer xml = new StringBuffer();
202: xml.append(xmlTableStart());
203: xml.append(xmlColumnState());
204: xml.append(xmlTableEnd());
205: return xml.toString();
206: }
207:
208: public String getXMLValue() {
209: int[] thePostColumns = null;
210: JTableModel theTableModel = null;
211: Object theModel = super .getModel();
212: String thePostStyle = Constants.POST_ALL;
213: if (theModel instanceof JTableModel) {
214: theTableModel = (JTableModel) theModel;
215: thePostStyle = theTableModel.getPostStyle();
216: thePostColumns = theTableModel.getPostColumns();
217: }
218: StringBuffer xml = new StringBuffer();
219: if (thePostStyle.equalsIgnoreCase(Constants.POST_ALL)
220: || thePostStyle
221: .equalsIgnoreCase(Constants.POST_PREFERNCE)) {
222: xml.append(xmlTableStart());
223: int rows = super .getModel().getRowCount();
224: int cols = super .getModel().getColumnCount();
225: for (int i = 0; i < rows; i++) {
226: xml.append(xmlRowStart(i, getRow(i), isSelected(i)));
227: for (int j = 0; j < cols; j++) {
228: xml.append(xmlCol(i, j));
229: }
230: xml.append(xmlRowEnd());
231: }
232: xml.append(xmlTableEnd());
233: } else {
234: int[] rowIndexs = super .getSelectedRows();
235: int[] colIndexs = thePostColumns;
236:
237: if (rowIndexs != null) {
238: xml.append(xmlTableStart());
239: for (int i = 0; i < rowIndexs.length; i++) {
240: xml.append(xmlRowStart(rowIndexs[i],
241: getRow(rowIndexs[i]), true));
242: for (int j = 0; j < colIndexs.length; j++) {
243: xml.append(xmlCol(rowIndexs[i], colIndexs[j]));
244: }
245: xml.append(xmlRowEnd());
246: }
247: xml.append(xmlTableEnd());
248: }
249: }
250: this .xmlValue = xml;
251: return this .xmlValue.toString();
252: }
253:
254: public boolean hasState() {
255: return true;
256: }
257:
258: /**
259: * @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
260: */
261: public void mouseClicked(MouseEvent aEvt) {
262: final int DOUBLE_CLICK_COUNT = 2;
263: final int SINGLE_CLICK_COUNT = 1;
264: String theEventType = null;
265: switch (aEvt.getClickCount()) {
266: case DOUBLE_CLICK_COUNT:
267: theEventType = Constants.MOUSE_DOUBLE_CLICKED;
268: break;
269: case SINGLE_CLICK_COUNT:
270: theEventType = Constants.MOUSE_SINGLE_CLICKED;
271: break;
272: default:
273: theEventType = null;
274: break;
275: }
276: if (theEventType != null && theEventType.length() > 0) {
277: this .eventHandler.handleEvent((SwingMLModel) super
278: .getModel(), theEventType, this );
279: }
280: }
281:
282: /**
283: * @see java.awt.event.MouseListener#mouseEntered(MouseEvent)
284: */
285: public void mouseEntered(MouseEvent aEvt) {
286: }
287:
288: /**
289: * @see java.awt.event.MouseListener#mouseExited(MouseEvent)
290: */
291: public void mouseExited(MouseEvent aEvt) {
292: }
293:
294: /**
295: * @see java.awt.event.MouseListener#mousePressed(MouseEvent)
296: */
297: public void mousePressed(MouseEvent aEvt) {
298: }
299:
300: /**
301: * @see java.awt.event.MouseListener#mouseReleased(MouseEvent)
302: */
303: public void mouseReleased(MouseEvent aEvt) {
304: }
305:
306: /**
307: * Sets the column width for the given column. This will automatically turn
308: * off the column auto resizing feature.
309: *
310: * @param index
311: * the index of the column for which the width should be set
312: * @param width
313: * the width for the column.
314: */
315: public void setColumnWidth(int index, int width) {
316: if (width > -1) {
317: // Disable auto resizing
318: this .setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
319: // Get the column by the index
320: TableColumn c = this .getColumnModel().getColumn(index);
321: if (width == 0) {
322: c.setMinWidth(width);
323: c.setMaxWidth(width);
324: c.setResizable(false);
325: }
326: c.setPreferredWidth(width);
327: }
328: }
329:
330: public void valueChanged(ListSelectionEvent aEvt) {
331: super.valueChanged(aEvt);
332: if (super.getTopLevelAncestor() != null) {
333: if (this.m_tableModel != null && this.eventHandler != null) {
334: eventHandler.handleEvent(this.m_tableModel,
335: Constants.LIST_VALUE_CHANGED, this);
336: }
337: }
338: }
339: }
|