alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.viewmodel;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Vector;
import javax.swing.Action;
import org.netbeans.spi.viewmodel.ColumnModel;
import org.netbeans.spi.viewmodel.ComputingException;
import org.netbeans.spi.viewmodel.Models;
import org.netbeans.spi.viewmodel.NoInformationException;
import org.netbeans.spi.viewmodel.NodeActionsProvider;
import org.netbeans.spi.viewmodel.NodeModel;
import org.netbeans.spi.viewmodel.TableModel;
import org.netbeans.spi.viewmodel.TreeModel;
import org.netbeans.spi.viewmodel.TreeModelListener;
import org.netbeans.spi.viewmodel.UnknownTypeException;


/**
 * Default implemetation of {@link CompoundModel}. It delegates all 
 * functionality to given instanceo of 
 * {@link org.netbeans.spi.viewmodel.TreeModel},
 * {@link org.netbeans.spi.viewmodel.NodeModel} and
 * {@link org.netbeans.spi.viewmodel.NodeActionsProvider}.
 *
 * @author   Jan Jancura
 */
public final class CompoundModel implements TreeModel, 
NodeModel, NodeActionsProvider, TableModel {

    private TreeModel       treeModel;
    private NodeModel       nodeModel;
    private NodeActionsProvider nodeActionsProvider;
    private ColumnsModel    columnsModel;
    private TableModel      tableModel;


    // init ....................................................................

    /**
     * Creates a new instance of {@link CompoundModel} for given models.
     *
     * @param treeModel a tree model to delegate on
     * @param nodeModel a node model to delegate on
     * @param nodeActionsProvider a node actions provider to delegate on
     * @param nodeActionsProvider a columns modeol to delegate on
     */
    public CompoundModel (
        TreeModel treeModel, 
        NodeModel nodeModel, 
        NodeActionsProvider nodeActionsProvider,
        List columnModels,
        TableModel tableModel
    ) {
        if (treeModel == null) treeModel = Models.EMPTY_TREE_MODEL;
        
        this.treeModel = treeModel;
        this.nodeModel = nodeModel;
        this.nodeActionsProvider = nodeActionsProvider;
        this.columnsModel = new ColumnsModel (columnModels);
        this.tableModel = tableModel;
    }

    /**
     * Creates a new instance of {@link CompoundModel}.
     */
    CompoundModel (
    ) {
        this (null, null, null, null, null);
    }


    // TreeModel ...............................................................

    /** 
     * Returns the root node of the tree or null, if the tree is empty.
     *
     * @return the root node of the tree or null
     */
    public Object getRoot () {
        return treeModel.getRoot ();
    }

    /** 
     * Returns children for given parent on given indexes.
     *
     * @param   parent a parent of returned nodes
     * @throws  NoInformationException if the set of children can not be 
     *          resolved
     * @throws  ComputingException if the children resolving process 
     *          is time consuming, and will be performed off-line 
     * @throws  UnknownTypeException if this TreeModel implementation is not
     *          able to resolve dchildren for given node type
     *
     * @return  children for given parent on given indexes
     */
    public Object[] getChildren (Object parent, int from, int to) 
        throws NoInformationException, ComputingException, UnknownTypeException {

        return treeModel.getChildren (parent, from, to);
    }
    
    /**
     * Returns number of children for given node.
     * 
     * @param   node the parent node
     * @throws  NoInformationException if the set of children can not be 
     *          resolved
     * @throws  ComputingException if the children resolving process 
     *          is time consuming, and will be performed off-line 
     * @throws  UnknownTypeException if this TreeModel implementation is not
     *          able to resolve children for given node type
     *
     * @return  true if node is leaf
     */
    public int getChildrenCount (Object node) throws NoInformationException, 
    ComputingException, UnknownTypeException {
        return treeModel.getChildrenCount (node);
    }

    /**
     * Returns true if node is leaf.
     * 
     * @throws  UnknownTypeException if this TreeModel implementation is not
     *          able to resolve dchildren for given node type
     * @return  true if node is leaf
     */
    public boolean isLeaf (Object node) throws UnknownTypeException {
        return treeModel.isLeaf (node);
    }


    // NodeModel ...............................................................

    /**
     * Returns display name for given node.
     *
     * @throws  ComputingException if the display name resolving process 
     *          is time consuming, and the value will be updated later
     * @throws  UnknownTypeException if this NodeModel implementation is not
     *          able to resolve display name for given node type
     * @return  display name for given node
     */
    public String getDisplayName (Object node) 
    throws ComputingException, UnknownTypeException {
        if (nodeModel == null) return node.toString ();
        String displayName = nodeModel.getDisplayName (node);
        if (displayName == null) return node.toString ();
        return displayName;
    }

    /**
     * Returns tooltip for given node.
     *
     * @throws  ComputingException if the tooltip resolving process 
     *          is time consuming, and the value will be updated later
     * @throws  UnknownTypeException if this NodeModel implementation is not
     *          able to resolve tooltip for given node type
     * @return  tooltip for given node
     */
    public String getShortDescription (Object node) 
    throws ComputingException, UnknownTypeException {
        if (nodeModel == null) return null;
        String description = nodeModel.getShortDescription (node);
        return description;
    }

    /**
     * Returns icon for given node.
     *
     * @throws  ComputingException if the icon resolving process 
     *          is time consuming, and the value will be updated later
     * @throws  UnknownTypeException if this NodeModel implementation is not
     *          able to resolve icon for given node type
     * @return  icon for given node
     */
    public String getIconBase (Object node) 
    throws ComputingException, UnknownTypeException {
        if (nodeModel == null) return null;
        return nodeModel.getIconBase (node);
    }


    // NodeActionsProvider .....................................................

    /**
     * Performs default action for given node.
     *
     * @throws  UnknownTypeException if this NodeActionsProvider implementation 
     *          is not able to resolve actions for given node type
     * @return  display name for given node
     */
    public void performDefaultAction (Object node) throws UnknownTypeException {
        if (nodeActionsProvider == null) return;
        nodeActionsProvider.performDefaultAction (node);
    }

    /**
     * Returns set of actions for given node.
     *
     * @throws  UnknownTypeException if this NodeActionsProvider implementation 
     *          is not able to resolve actions for given node type
     * @return  display name for given node
     */
    public Action[] getActions (Object node) throws UnknownTypeException {
        if (nodeActionsProvider == null) return new Action [0];
        return nodeActionsProvider.getActions (node);
    }


    // ColumnsModel ............................................................

    /**
     * Returns sorted array of 
     * {@link org.netbeans.spi.viewmodel.ColumnModel}s.
     *
     * @return sorted array of ColumnModels
     */
    public ColumnModel[] getColumns () {
        return columnsModel.getColumns ();
    }


    // TableModel ............................................................

    public Object getValueAt (Object node, String columnID) throws 
    ComputingException, UnknownTypeException {
        if (tableModel == null) return null;
        return tableModel.getValueAt (node, columnID);
    }

    public boolean isReadOnly (Object node, String columnID) throws 
    UnknownTypeException {
        if (tableModel == null) return false;
        return tableModel.isReadOnly (node, columnID);
    }

    public void setValueAt (Object node, String columnID, Object value) throws 
    UnknownTypeException {
        if (tableModel == null) return;
        tableModel.setValueAt (node, columnID, value);
    }


    // listeners ...............................................................

    /** 
     * Registers given listener.
     * 
     * @param l the listener to add
     */
    public void addTreeModelListener (TreeModelListener l) {
        treeModel.addTreeModelListener (l);
        if (nodeModel != null)
            nodeModel.addTreeModelListener (l);
        if (nodeActionsProvider != null)
            nodeActionsProvider.addTreeModelListener (l);
        if (tableModel != null)
            tableModel.addTreeModelListener (l);
    }

    /** 
     * Unregisters given listener.
     *
     * @param l the listener to remove
     */
    public void removeTreeModelListener (TreeModelListener l) {
        treeModel.removeTreeModelListener (l);
        if (nodeModel != null)
            nodeModel.removeTreeModelListener (l);
        if (nodeActionsProvider != null)
            nodeActionsProvider.removeTreeModelListener (l);
        if (tableModel != null)
            tableModel.removeTreeModelListener (l);
    }

    public String toString () {
        return super.toString () + 
               "\n  TreeModel = " + treeModel +
               "\n  NodeModel = " + nodeModel +
               "\n  TableModel = " + tableModel +
               "\n  NodeActionsProvider = " + nodeActionsProvider +
               "\n  ColumnsModel = " + columnsModel;
    }

    // innerclasses ............................................................

    /**
     * Defines model for table view columns. Can be used together with 
     * {@link TreeModel} for tree table view representation.
     *
     * @author   Jan Jancura
     */
    static final class ColumnsModel {

        private ColumnModel[] columns;

        /**
         * Creates a new {@link ColumnsModel} for given {@link ColumnModel}s.
         *
         * @param columns a list of columns
         */
        public ColumnsModel (ColumnModel[] columns) {
            this.columns = columns;
        }

        private static ColumnModel[] convert (List l) {
            ColumnModel[] models = new ColumnModel [l.size ()];
            return (ColumnModel[]) l.toArray (models);
        }

        /**
         * Creates a new {@link ColumnsModel} for given {@link ColumnModel}s.
         *
         * @param columns a list of columns
         */
        public ColumnsModel (List l) {
            this (convert (l));
        }

        /**
         * Returns sorted array of 
         * {@link org.netbeans.spi.viewmodel.ColumnModel}s.
         *
         * @return sorted array of ColumnModels
         */
        public ColumnModel[] getColumns () {
            return columns;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.