view src/MPipeline/src/mpipeline/PlistTableModel.java @ 27:29276498ebdb database-connection-manager

Remove LTPDARepositoryManager implementation * * * Remove GUI helper
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.io.Serializable;
import javax.swing.JDesktopPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

/**
 * A table model of a plist.
 * 
 * @author hewitson
 */
public class PlistTableModel extends AbstractTableModel {

    private Plist pl;
    private String[] columnNames = {"Key", "Value"};

    /**
     * Construct a plist table model from the given plist.
     * @param p
     */
    public PlistTableModel(Plist p) {
        pl = p;

        this.addTableModelListener(new PlistTableModelListener());


    }

    /**
     * Table model listener for the plist table model.
     */
    private class PlistTableModelListener implements Serializable, TableModelListener {

        public void tableChanged(TableModelEvent e) {//            System.out.println("Table changed: " + e.toString());
            MBlock b = pl.getOwner();
            System.out.println("pl = " + pl);
            System.out.println("b = " + b);
            if (b != null) {
                MCanvas c = (MCanvas) b.getParent();
                System.out.println("c = " + c);
                if (c != null) {
                    BlockDiagram diag = (BlockDiagram) c.getRootPane().getParent();
                    System.out.println("diag = " + diag);
                    if (diag != null) {
                        JDesktopPane dp = diag.getDesktopPane();
                        System.out.println("dp = " + dp);
                        if (dp != null) {
                            MainWindow mw = (MainWindow) dp.getRootPane().getParent();
                            System.out.println("mw = " + mw);
                            mw.setSaved(false);
                        }
                    }
                }
            }
        }
    }

    /**
     * Return the plist associated with this plist table model.
     * @return
     */
    public Plist getPlist() {
        return pl;
    }

    /*
     * Handles the user changing the table in a cell.
     */
    @Override
    public void setValueAt(Object value, int row, int col) {

        // in principle we could check the value here
        if (col == 1) {
            pl.setValueForKey((String) getValueAt(row, 0), value);
            fireTableCellUpdated(row, col);
        } else if (col == 0) {
            pl.setKeyForKey((String) getValueAt(row, 0), value);
        }
    }

    /**
     * Return true if the given cell is editable.
     * @param row
     * @param column
     * @return
     */
    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }

    /**
     * Get the name of the given column.
     * @param col
     * @return
     */
    @Override
    public String getColumnName(int col) {
        return columnNames[col].toString();
    }
    
    /**
     * Get a row count.
     * @return
     */
    public int getRowCount() {
        return pl.getNparams();
    }

    /** 
     * Get a column count.
     * @return
     */
    public int getColumnCount() {
        return columnNames.length;
    }

    /**
     * Get the value in the cell at the given row and column.
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            // return the key
            return pl.getKey(rowIndex);
        } else {
            // return value
            return pl.getVal(rowIndex);
        }
    }
}