Package org.swingml.component

Source Code of org.swingml.component.JTableComponent

/* SwingML
* Copyright (C) 2002 SwingML Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors:
*     Ezequiel Cuellar <ecuellar@crosslogic.com>
*     Robert Morris <robertj@morris.net>
*/

package org.swingml.component;

import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import org.swingml.*;
import org.swingml.event.*;
import org.swingml.model.*;
import org.swingml.model.TableColumnModel;

public class JTableComponent extends JTable implements XMLTranslatable, MouseListener, ListSelectionListener, XMLStateTranslatable {

    private EventHandler eventHandler = EventHandler.getInstance();

    private JTableModel m_tableModel = null;
    private StringBuffer xmlValue = null;

    public JTableComponent (JTableModel aModel) {
        this.m_tableModel = aModel;
        super.setModel(aModel);
        super.setName(aModel.getName());
        super.setToolTipText(aModel.getTooltip());
        super.addMouseListener(this);
        super.getSelectionModel().setSelectionMode(aModel.getMode());
        ToolTipManager.sharedInstance().registerComponent(this);
        applyColumnEditors();
    }

    private TableRowModel getRow (int rowIndex) {
        TableRowModel result = null;
        if (rowIndex >= 0) {
            JTableModel model = (JTableModel) getModel();
            if (model != null) {
                List rows = model.getRows();
                if (rows != null && rows.size() > 0) {
                    result = (TableRowModel) rows.get(rowIndex);
                }
            }
        }
        return result;
    }

    private boolean isSelected (int rowIndex) {
        boolean result = false;
        if (rowIndex >= 0) {
            int[] selectedRows = getSelectedRows();
            if (selectedRows != null && selectedRows.length > 0) {
                for (int x = 0; x < selectedRows.length; x++) {
                    if (selectedRows[x] == rowIndex) {
                        result = true;
                        break;
                    }
                }
            }
        }

        return result;
    }

    private String xmlCol (int rowIndex, int colIndex) {
        String result = "";
        result += xmlColStart(colIndex);
        result += xmlColValue(rowIndex, colIndex);
        result += xmlColEnd();

        return result;
    }

    private String xmlColEnd () {
        return "</TD>";
    }

    private String xmlColStart (int colIndex) {
        return "<TD index=\"" + colIndex + "\">";
    }

    private String xmlColumnState () {
        StringBuffer result = new StringBuffer();
        JTableModel theModel = (JTableModel) super.getModel();
        List cols = theModel.getColumns();
        for (int columnIndex = 0; columnIndex < cols.size(); columnIndex++) {
            TableColumnModel colModel = (TableColumnModel) cols.get(columnIndex);
            result.append("<COLUMN ");
            result.append(Constants.TEXT);
            result.append("=\"");
            result.append(colModel.getText());
            result.append("\" ");
            result.append(Constants.COLUMN_ORDER);
            result.append("=\"");
            result.append(convertColumnIndexToView(columnIndex));
            result.append("\" ");
            result.append(Constants.WIDTH);
            result.append("=\"");
            result.append(getColumn(colModel.getText()).getWidth());
            result.append("\" ");
            result.append(Constants.SORT);
            result.append("=\"");
            result.append("ASC");
            result.append("\" ");
            result.append(Constants.SORTABLE);
            result.append("=\"");
            result.append(colModel.isSortable()?Constants.TRUE:Constants.FALSE);
            result.append("\" ");
            result.append(Constants.SORTORDER);
            result.append("=\"");
            result.append(-1);
            result.append("\" />");
        }
        return result.toString();
    }

    private String xmlColValue (int rowIndex, int colIndex) {
        return super.getModel().getValueAt(rowIndex, colIndex).toString();
    }

    private String xmlRowEnd () {
        return "</TR>";
    }

    private String xmlRowStart (int rowIndex, TableRowModel row, boolean selected) {
        String value = "";
        if (row != null) {
            value = row.getValue();
        }
        return "<TR index=\"" + rowIndex + "\" selected=\"" + selected + "\" value=\"" + value + "\">";
    }

    private String xmlTableEnd () {
        return "</TABLE>";
    }

    private String xmlTableStart () {
        return "<TABLE NAME=\"" + getName() + "\">";
    }

    public void applyColumnEditors () {
        JTableModel aModel = (JTableModel) getModel();
        for (int i = 0; i < aModel.getColumnCount(); i++) {
            TableColumnModel m = (TableColumnModel) aModel.getColumns().get(i);
            if (m.getCustomEditor() != null) {
                getColumnModel().getColumn(i).setCellEditor(m.getCustomEditor());
            }

        }
    }

    public TableCellRenderer getCellRenderer (int row, int column) {
        return super.getCellRenderer(row, column);
    }

    public String getToolTipText (MouseEvent e) {
        String aTip = null;
        java.awt.Point p = e.getPoint();
        int rowIndex = rowAtPoint(p);
        int colIndex = columnAtPoint(p);
        int realColumnIndex = convertColumnIndexToModel(colIndex);
        JTableModel model = (JTableModel) getModel();
        aTip = model.getToolTip(rowIndex, realColumnIndex);
        if (aTip == null) {
            aTip = getToolTipText();
        }
        return aTip;
    }

    public String getXMLState () {
        StringBuffer xml = new StringBuffer();
        xml.append(xmlTableStart());
        xml.append(xmlColumnState());
        xml.append(xmlTableEnd());
        return xml.toString();
    }

    public String getXMLValue () {
        int[] thePostColumns = null;
        JTableModel theTableModel = null;
        Object theModel = super.getModel();
        String thePostStyle = Constants.POST_ALL;
        if (theModel instanceof JTableModel) {
            theTableModel = (JTableModel) theModel;
            thePostStyle = theTableModel.getPostStyle();
            thePostColumns = theTableModel.getPostColumns();
        }
        StringBuffer xml = new StringBuffer();
        if (thePostStyle.equalsIgnoreCase(Constants.POST_ALL) || thePostStyle.equalsIgnoreCase(Constants.POST_PREFERNCE)) {
            xml.append(xmlTableStart());
            int rows = super.getModel().getRowCount();
            int cols = super.getModel().getColumnCount();
            for (int i = 0; i < rows; i++) {
                xml.append(xmlRowStart(i, getRow(i), isSelected(i)));
                for (int j = 0; j < cols; j++) {
                    xml.append(xmlCol(i, j));
                }
                xml.append(xmlRowEnd());
            }
            xml.append(xmlTableEnd());
        } else {
            int[] rowIndexs = super.getSelectedRows();
            int[] colIndexs = thePostColumns;

            if (rowIndexs != null) {
                xml.append(xmlTableStart());
                for (int i = 0; i < rowIndexs.length; i++) {
                    xml.append(xmlRowStart(rowIndexs[i], getRow(rowIndexs[i]), true));
                    for (int j = 0; j < colIndexs.length; j++) {
                        xml.append(xmlCol(rowIndexs[i], colIndexs[j]));
                    }
                    xml.append(xmlRowEnd());
                }
                xml.append(xmlTableEnd());
            }
        }
        this.xmlValue = xml;
        return this.xmlValue.toString();
    }

    public boolean hasState () {
        return true;
    }

    /**
     * @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
     */
    public void mouseClicked (MouseEvent aEvt) {
        final int DOUBLE_CLICK_COUNT = 2;
        final int SINGLE_CLICK_COUNT = 1;
        String theEventType = null;
        switch (aEvt.getClickCount()) {
            case DOUBLE_CLICK_COUNT:
                theEventType = Constants.MOUSE_DOUBLE_CLICKED;
                break;
            case SINGLE_CLICK_COUNT:
                theEventType = Constants.MOUSE_SINGLE_CLICKED;
                break;
            default:
                theEventType = null;
                break;
        }
        if (theEventType != null && theEventType.length() > 0) {
            this.eventHandler.handleEvent((SwingMLModel) super.getModel(), theEventType, this);
        }
    }

    /**
     * @see java.awt.event.MouseListener#mouseEntered(MouseEvent)
     */
    public void mouseEntered (MouseEvent aEvt) {}

    /**
     * @see java.awt.event.MouseListener#mouseExited(MouseEvent)
     */
    public void mouseExited (MouseEvent aEvt) {}

    /**
     * @see java.awt.event.MouseListener#mousePressed(MouseEvent)
     */
    public void mousePressed (MouseEvent aEvt) {}

    /**
     * @see java.awt.event.MouseListener#mouseReleased(MouseEvent)
     */
    public void mouseReleased (MouseEvent aEvt) {}

    /**
     * Sets the column width for the given column. This will automatically turn
     * off the column auto resizing feature.
     *
     * @param index
     *            the index of the column for which the width should be set
     * @param width
     *            the width for the column.
     */
    public void setColumnWidth (int index, int width) {
        if (width > -1) {
            // Disable auto resizing
            this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            // Get the column by the index
            TableColumn c = this.getColumnModel().getColumn(index);
            if (width == 0) {
                c.setMinWidth(width);
                c.setMaxWidth(width);
                c.setResizable(false);
            }
            c.setPreferredWidth(width);
        }
    }

    public void valueChanged (ListSelectionEvent aEvt) {
        super.valueChanged(aEvt);
        if (super.getTopLevelAncestor() != null) {
            if (this.m_tableModel != null && this.eventHandler != null) {
                eventHandler.handleEvent(this.m_tableModel, Constants.LIST_VALUE_CHANGED, this);
            }
        }
    }
}
TOP

Related Classes of org.swingml.component.JTableComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.