/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 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.1 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
 */

package com.jstatcom.table;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

import com.jstatcom.model.JSCData;
import com.jstatcom.model.JSCSArray;
import com.jstatcom.model.JSCTypeDef;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.Symbol;
import com.jstatcom.model.SymbolTable;

/**
 * JSCAbstractDataTable implementation for JSCSArray
 * data objects.
 * 
 * @author Markus Kraetzig 
 */
public class JSCSArrayTable extends JSCAbstractDataTable {
    private boolean isTransposed = false;
    
    // just to have a component for the focusLost event
    private final JTextField textField = new JTextField() {

        protected void processKeyEvent(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                if (isEditing()) {
                    getCellEditor().cancelCellEditing();
                }
                e.consume();
            }
            super.processKeyEvent(e);
        }
    };

    private TableCellEditor stringEditor = new DefaultCellEditor(textField);

    /**
     * Default constructor initializes this with a default model.
     */
    public JSCSArrayTable() {
        super();
        setDefaultEditor(String.class, stringEditor);
        setCellRenderer(JSCCellRendererTypes.DEFAULT);
        textField.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent evt) {
                stringEditor.stopCellEditing();
            }
        });
    }

    /*
     * @see com.jstatcom.component.JSCAbstractDataTable#setModelForJSCData(com.jstatcom.model.JSCData)
     */
    public void setModelForJSCData(JSCData data) {
        if (data == null) {
            JSCSArrayTableModel model = new JSCSArrayTableModel(null);
            model.setTransposed(isDisplayTransposed());
            model.setEditable(isEditable());
            setModel(model);
        } else {
            JSCSArrayTableModel model = new JSCSArrayTableModel(
                    new Symbol(data));
            model.setTransposed(isDisplayTransposed());
            model.setEditable(isEditable());
            setModel(model);
        }

    }

    /*
     * @see com.jstatcom.component.JSCAbstractDataTable#setModelForSymbolName(com.jstatcom.model.SymbolTable,
     *      String)
     */
    public void setModelForSymbolName(SymbolTable table, String name) {
        if (name == null || table == null) {
            JSCSArrayTableModel model = new JSCSArrayTableModel(null);
            model.setTransposed(isDisplayTransposed());
            model.setEditable(isEditable());
            setModel(model);
        } else {
            JSCSArrayTableModel model = new JSCSArrayTableModel(table
                    .get(new JSCTypeDef(name, JSCTypes.SARRAY)));
            model.setTransposed(isDisplayTransposed());
            model.setEditable(isEditable());
            setModel(model);
        }

    }

    /**
     * Gets a reference of the underlying data object with the correct type.
     * Clients should be aware that this table might display the data
     * transposed, which means that row and column indices are flipped.
     * 

* Usage note:
* The returned data is a reference to the underlying JSCData * object only until a new data object has been set. It should be used read * only. Changes that should not affect this table must be made on a copy of * the returned object. * * @return reference to data */ public JSCSArray getJSCSArray() { return (JSCSArray) getJSCData(); } /** * Gets whether the data in this table should be displayed transposed. This * property has only effect if the underlying model is a * JSCSArrayTableModel. * * @return true if transposed */ public boolean isDisplayTransposed() { return isTransposed; } /** * Sets whether the data in this table should be displayed transposed. This * property has only effect if the underlying model is a * JSCSArrayTableModel. * * @param transposed * true if transposed */ public void setDisplayTransposed(boolean transposed) { if (getModel() instanceof JSCAbstractTableModel) ((JSCSArrayTableModel) getModel()).setTransposed(transposed); isTransposed = transposed; } }

Ads help maintain this website.