Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| org.netbeans.spi.viewmodel/2 1.9.22
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES

## Package org.netbeans.spi.viewmodel

Defines API for sharing of Tree Table View.

See:
Description

Interface Summary
Model | Marker interface for all models.
ModelListener | Notifies about changes in view model.
Models.ActionPerformer | Support interface for `Models.createAction(String,Models.ActionPerformer,int)` method.
NodeActionsProvider | Provides actions and default action for some type of objects.
NodeActionsProviderFilter | Filters actions provided by some original `NodeActionsProvider`.
NodeModel | Provides display name, icon and tooltip value for some type of objects.
NodeModelFilter | Filters content of some existing `NodeModel`.
TableModel | Adds support for columns to basic `TreeModel`.
TableModelFilter | Allows to filter content of some existing `TableModel`.
TreeExpansionModel | This model controlls expansion, collapsion of nodes in tree view, and defindes default expand state for all node in it.
TreeModel | Defines data model for tree.
TreeModelFilter | Filters content of some original tree of nodes (represented by `TreeModel`).

Class Summary
ColumnModel | Defines model for one table view column.
ModelEvent | Encapsulates information describing changes to a model, and used to notify model listeners of the change.
ModelEvent.NodeChanged | Used to notify that one node has been changed (icon, displayName and children).
ModelEvent.TableValueChanged | Used to notify that one cell in table has been changed.
ModelEvent.TreeChanged | Used to notify that whole content of tree has been changed.
Models | Contains various utility methods for various models.
Models.CompoundModel | This model encapsulates all currently supported models.
Models.TreeFeatures | Implements set of tree view features.

Exception Summary
UnknownTypeException | Used by various data models if data model is asked to resolve node of unknown type.

## Package org.netbeans.spi.viewmodel Description

Defines API for sharing of Tree Table View. This API has been designed for sharing Debugger Views (like Callstack View) among different modules. But it does not depends on debugger itself.

## Main features:

- One TreeView, or TreeTableView can be shared among different modules. Different modules can add different types of nodes to one view.
- Hierarchy produced by one module can be changed by another one.
- Allows to split UI and implementation to different modules.
- Performance and memory consumption of this model should be much better than Node based models.
- You do not need one instance of some class (Node) per one node visible in view. And you do not need one class per one node type.

## How to use View Model API

Following example shows how to use viewmodel API to create simple files view.

### Step 1.

In the first step we should create plain tree model (TreeModel).

    public class TreeModelImpl implements TreeModel {

        public Object getRoot () {
            return ROOT;
        }

        public Object[] getChildren (Object parent, int from, int to) {
            if (parent == ROOT)
                return File.listRoots ();
            return ((File) parent).listFiles ();
        }

        public boolean isLeaf (Object node) {
            if (node == ROOT)
                return false;
            return ((File) node).isFile ();
        }
    }

And create a TreeView for this model:

        JComponent treeView = Models.createView (
            Models.createCompoundModel (
            Arrays.asList (new Model[] {
                    new TreeModelImpl (),      // TreeModel
                    new ArrayList ()           // list of ColumnModel s
                })
            )
        );

### Step 2.

NodeModel implementation can define name, icon and tooltip for tree nodes produced by TreeModel.

    public class NodeModelImpl implements NodeModel {

        public String getDisplayName (Object node) {
            if (node == ROOT) return "Name";
            String name = ((File) node).getName ();
            if (name.length () < 1) return ((File) node).getAbsolutePath ();
            return name;
        }

        public String getIconBase (Object node) {
            if (node == ROOT) return "folder";
            if (((File) node).isDirectory ()) return "folder";
            return "file";
        }

        public String getShortDescription (Object node) {
            if (node == ROOT) return "Name";
            return ((File) node).getAbsolutePath ();
        }
    }

### Step 3.

NodeActionsProvider defines set of Actions for each node, and default action..

    public class NodeActionsProviderImpl implements NodeActionsProvider {

        public Action[] getActions (final Object node) {
            return new Action [] {
                new AbstractAction ("Open") {
                    public void actionPerformed (ActionEvent e) {
                        performDefaultAction (node);
                    }
                },
                new AbstractAction ("Delete") {
                    public void actionPerformed (ActionEvent e) {
                        ((File) node).delete ();
                    }
                }
            };
        }

        public void performDefaultAction (Object node) {
            try {
                JFrame f = new JFrame ("View");
                f.getContentPane ().add (new JEditorPane (((File) node).toURL ()));
                f.pack ();
                f.show ();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

### Step 4.

TableModel and ColumnModel adds support for additional columns to tree view.

    public class TableModelImpl implements TableModel {

        public Object getValueAt (Object node, String columnID) {
            try {
                if (node == ROOT) return null;
                if (columnID.equals ("sizeID")) {
                    if (((File) node).isDirectory ()) return "<dir>";
                    return "" + new FileInputStream ((File) node).getChannel ().size ();
                }
            } catch (Exception e) {
                e.printStackTrace ();
            }
            return "";
        }

        public boolean isReadOnly (Object node, String columnID) {
            return true;
        }

        public void setValueAt (Object node, String columnID, Object value) {
        }
    }

And initialization of columns looks like:

        ArrayList columns = new ArrayList ();
        columns.add (new ColumnModel () {
            public String getID () { return "sizeID"; }
            public String getDisplayName () { return "size"; }
            public Class getType () { return String.class; }
        });
        JComponent treeTableView = Models.createView (
            Models.createCompoundModel (
                Arrays.asList (new Model[] {
                    new TreeModelImpl (),           // TreeModel
                    new NodeModelImpl (),           // NodeModel
                    new TableModelImpl (),          // TableModel
                    new NodeActionsProviderImpl (), // NodeActionsProvider
                    columns                         // list of ColumnModel s
                })
            )
        );

### How to use Filters

We can use filters to modify content of tree table view created in our example.

- TreeModelFilter: this filter can be used to hide fome files, to add some virtual filesystems, to add content of zip files to original tree, and so on.
- NodeModelFilter: can be used to change names, icons or tooltips for existing files.
- TableModelFilter: can be used to modify content of columns.
- NodeActionsProviderFilter: can be used to add a new actions to pup-up menus, of to redefine default action.

All these actions can be done in some external module.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| org.netbeans.spi.viewmodel/2 1.9.22
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES

Built on December 7 2006. | Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.