All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openide.explorer.view.NodeTableModel Maven / Gradle / Ivy

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.explorer.view;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.Iterator;

import org.openide.nodes.Node;
import org.openide.nodes.Node.Property;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.NbBundle;

/** 
* Table model with properties (Node.Property) as columns and nodes (Node) as rows.
* It is used as model for displaying node properties in table. Each column is represented by
* Node.Property object. Each row is represented by Node object.
* Each cell contains Node.Property property which equals with column object
* and should be in property sets of row representant (Node).
*
* @author Jan Rojcek
* @since 1.7
*/
public class NodeTableModel extends AbstractTableModel {

    private static final String ATTR_INVISIBLE = "InvisibleInTreeTableView"; // NOI18N
    static final String ATTR_COMPARABLE_COLUMN = "ComparableColumnTTV"; // NOI18N
    static final String ATTR_SORTING_COLUMN = "SortingColumnTTV"; // NOI18N
    static final String ATTR_DESCENDING_ORDER = "DescendingOrderTTV"; // NOI18N
    private static final String ATTR_ORDER_NUMBER = "OrderNumberTTV"; // NOI18N
    private static final String ATTR_TREE_COLUMN = "TreeColumnTTV"; // NOI18N
    
    /** all columns of model */
    private ArrayColumn[] allPropertyColumns = new ArrayColumn[]{};
    /** visible columns of model */
    private int[] propertyColumns = new int[]{};
    /** rows of model */
    private Node[] nodeRows = new Node[]{};
    /** sorting column */
    private int sortColumn = -1;
    /** if true, at least one column can be used to sort */
    private boolean existsComparableColumn = false;
    
    private Property treeColumnProperty = null;
    
    /** listener on node properties changes, recreates displayed data */
    private PropertyChangeListener pcl = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            //fireTableDataChanged();
            int row = rowForNode((Node)evt.getSource());
            if (row == -1) {
                return;
            }

            int column = columnForProperty(evt.getPropertyName());
            if (column == -1) {
                fireTableRowsUpdated(row, row);
            } else {
                fireTableCellUpdated(row, column);
            }
        }
    };
    
    /** Set rows.
     * @param nodes the rows
     */
    public void setNodes(Node[] nodes) {
        for (int i = 0; i < nodeRows.length; i++)
            nodeRows[i].removePropertyChangeListener(pcl);
        nodeRows = nodes;
        for (int i = 0; i < nodeRows.length; i++)
            nodeRows[i].addPropertyChangeListener(pcl);
        fireTableDataChanged();
    }
    
    /** Set columns.
     * @param props the columns
     */
    public void setProperties(Property[] props) {
        int size = props.length;
        sortColumn = -1;
        int treePosition = -1;
        for ( int i=0; iNode.Property.class
     */
    public Class getColumnClass(int column) {
        return Node.Property.class;
    }

    /** Getter for column name
     * @param column table column index
     * @return display name of property which represents column
     */
    public String getColumnName(int column) {
        return allPropertyColumns[propertyColumns[column]].getProperty().getDisplayName();
    }
    
    /* display panel to set/unset set of visible columns
     */
    boolean selectVisibleColumns(String viewName, String treeColumnName, String treeColumnDesc) {
        boolean changed = false;
        
        javax.swing.JPanel panel = new javax.swing.JPanel();
        panel.setLayout(new GridBagLayout());
        
        ArrayList boxes = new ArrayList(allPropertyColumns.length);
        boolean[] oldvalues = new boolean[ allPropertyColumns.length ];
        int[] sortpointer = new int[ allPropertyColumns.length ];
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 12);
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        
        GridBagConstraints labelConstraints = new GridBagConstraints();
        labelConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        labelConstraints.anchor = java.awt.GridBagConstraints.WEST;
        labelConstraints.insets = new java.awt.Insets(12, 12, 0, 12);
        labelConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        labelConstraints.weightx = 1.0;
        JLabel desc = new JLabel( NbBundle.getBundle(NodeTableModel.class).getString("LBL_ColumnDialogDesc") );
        panel.add(desc, labelConstraints);
        
        GridBagConstraints firstConstraints = new GridBagConstraints();
        firstConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        firstConstraints.anchor = java.awt.GridBagConstraints.WEST;
        firstConstraints.insets = new java.awt.Insets(12, 12, 0, 12);
        firstConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        firstConstraints.weightx = 1.0;
        JCheckBox first = new JCheckBox( treeColumnName + ": " + treeColumnDesc, true );  // NOI18N
        first.setEnabled( false );
        panel.add(first, firstConstraints);

        String boxtext;
        TreeMap sort = new TreeMap();
        for (int i = 0; i < allPropertyColumns.length; i++) {
            oldvalues[i] = isVisible( allPropertyColumns[i].getProperty() );
            boxtext = allPropertyColumns[i].getProperty().getDisplayName()
                + ": " + allPropertyColumns[i].getProperty().getShortDescription(); // NOI18N
            sort.put( boxtext, new Integer( i ));
        }

        Iterator it = sort.keySet().iterator();
        int j = 0;
        while ( it.hasNext() ) {
            boxtext = ((String)it.next());
            int i = ((Integer)sort.get( boxtext )).intValue();
            JCheckBox b = new JCheckBox(
                boxtext,
                oldvalues[i] );
            sortpointer[j] = i;
            panel.add(b, gridBagConstraints);
            boxes.add(b);
            j++;
        }
        
        String title = NbBundle.getBundle(NodeTableModel.class).getString("LBL_ColumnDialogTitle");
        if ( viewName != null && viewName.length() > 0 )
            title = viewName + " - " + title;  // NOI18N
        DialogDescriptor dlg = new DialogDescriptor(
                panel,
                title,
                true,
                DialogDescriptor.OK_CANCEL_OPTION,
                DialogDescriptor.OK_OPTION,
                DialogDescriptor.DEFAULT_ALIGN,
                null,
                null
           );
        
        final Dialog dialog = DialogDisplayer.getDefault().createDialog(dlg);
        dialog.show();
        
        if (dlg.getValue().equals( DialogDescriptor.OK_OPTION )) {

            int num = boxes.size();
            int nv = 0;
            for ( int i = 0; i < num; i++ ) {
                JCheckBox b = (JCheckBox)boxes.get(i);
                
                j = sortpointer[i];
                if ( b.isSelected() != oldvalues[j] ) {
                    setVisible( allPropertyColumns[j].getProperty(), b.isSelected() );
                    changed = true;
                }
                if ( b.isSelected() ) {
                    nv++;
                }
            }

            // Don't allow the user to disable ALL columns
            /*
            if (nv == 0) {
                setVisible( allPropertyColumns[0].getProperty(), true );
                nv = 1;
            }
             */
            if ( changed )
                computeVisiblePorperties( nv );
        }
        return changed;
    }
    
    void moveColumn(int from, int to) {
        int i = propertyColumns[ from ];
        int j = propertyColumns[ to ];
        
        propertyColumns[ from ] = j;
        propertyColumns[ to ] = i;
        
        allPropertyColumns[i].setVisibleIndex( to );
        allPropertyColumns[j].setVisibleIndex( from );

        sortColumn = -1;
    }
    
    /* class representing property column
     */
    private static class ArrayColumn {
        ArrayColumn() {}
        /** Property representing column */
        private Property property;
        
        /** Preferred width of column */
        private int width;
        
        /** Column index in table, if it's visible */
        private int visibleIndex;
        
        /** Getter for property property.
         * @return Value of property property.
         */
        public Property getProperty() {
            return this.property;
        }
        
        /** Setter for property property.
         * @param property New value of property property.
         */
        public void setProperty(Property property) {
            this.property = property;
        }
        
        /** Getter for property width.
         * @return Value of property width.
         */
        public int getWidth() {
            return this.width;
        }
        
        /** Setter for property width.
         * @param width New value of property width.
         */
        public void setWidth(int width) {
            this.width = width;
        }
        
        /** Getter for property visibleIndex.
         * @return Value of property visibleIndex.
         */
        public int getVisibleIndex() {
            return this.visibleIndex;
        }
        
        /** Setter for property visibleIndex.
         * @param visibleIndex New value of property visibleIndex.
         */
        public void setVisibleIndex(int visibleIndex) {
            this.visibleIndex = visibleIndex;
            property.setValue( ATTR_ORDER_NUMBER, new Integer( visibleIndex ) );
        }
        
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy