Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To add a node to the FTree it is necessary to send an FTreeNodeAddedNotification to the tree specifying the type of the node and its location within the tree (optionally it is possible to provide the object contained in the node). The path can either be a String in the standard unix notation (directories separated by "/") or an FTreePath. The type defines the behavior of the node; two default type are provided: FTreeLeafNode for leaves in the tree, and FTreeFolderNode for folders.

tree.treeChanged( new FTreeNodeAddedNotification(this, "/baseDir/subDir", FTreeLeafNode.class) );

When adding a node to the tree, intermediate folders will be automatically created if missing. Try the following code for a complete example. To remove an FTreeNode from the tree use an FTreeNodeRemovedNotification as shown in the following

Code Block

...

Adding nodes on two navigation trees

As mentioned earlier the tree provider allows the user to create new navigation tree; the following code shows how to create two navigation trees adding nodes to them.

Create adapter to define double click on tree node

In the above examples the nodes added on the tree had very little functionality. The code in this example shows how to create an adapter to add the double click action to the leaf nodes. In the example we create a new node adapter by extending the DefaultFTreeNodeAdapter, the default implementation of the FTreeNodeAdapter, overwriting its doubleClick method. When double clicking on the nodes created when running the example an empty editor window will appear whose title is the node's name.

Adding a new sorting algorithm

...

titleAddNodesToTree.java
borderStylesolid

import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.tree.FTree;
import org.freehep.jas.plugin.tree.FTreeFolderNode;
import org.freehep.jas.plugin.tree.FTreeLeafNode;
import org.freehep.jas.plugin.tree.FTreeNodeAddedNotification;
import org.freehep.jas.plugin.tree.FTreePath;
import org.freehep.jas.plugin.tree.FTreeProvider;
import org.freehep.util.FreeHEPLookup;

public class AddNodesToTree {
    
    
    public static void main(String[] args) {
        
        // Get the Studio, the FTreeProvider and the default FTree
        Studio studio = (Studio) Studio.getApplication();
        FTreeProvider treeProvider = (FTreeProvider) studio.getLookup().lookup(FTreeProvider.class);
        FTree tree = treeProvider.tree();
        
        // Add nodes using FTreePath
        FTreePath path = new FTreePath("baseDir");
        tree.treeChanged( new FTreeNodeAddedNotification(studio, path, FTreeFolderNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, path.pathByAddingChild("subDir-a"), FTreeLeafNode.class) );
        path = path.pathByAddingChild("subDir-b").pathByAddingChild("subSubDir-a");
        tree.treeChanged( new FTreeNodeAddedNotification(studio, path, FTreeLeafNode.class) );
        
        // Add nodes using strings
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1", FTreeFolderNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1/subDir-a", FTreeLeafNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1/subDir-b/subSubDir-a", FTreeLeafNode.class) );
    }
    
}

To remove an FTreeNode from the tree use an FTreeNodeRemovedNotification as shown in the following code.

Code Block
titleRemoveNodesFromTree.java
borderStylesolid

import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.tree.FTree;
import org.freehep.jas.plugin.tree.FTreeFolderNode;
import org.freehep.jas.plugin.tree.FTreeNodeRemovedNotification;
import org.freehep.jas.plugin.tree.FTreePath;
import org.freehep.jas.plugin.tree.FTreeProvider;
import org.freehep.util.FreeHEPLookup;

public class RemoveNodesFromTree {
    
    
    public static void main(String[] args) {
        
        // Get the Studio, the FTreeProvider and the default FTree
        Studio studio = (Studio) Studio.getApplication();
        FTreeProvider treeProvider = (FTreeProvider) studio.getLookup().lookup(FTreeProvider.class);
        FTree tree = treeProvider.tree();
        
        // Remove nodes using FTreePath
        FTreePath path = new FTreePath("baseDir");
        path = path.pathByAddingChild("subDir-b").pathByAddingChild("subSubDir-a");
        tree.treeChanged( new FTreeNodeRemovedNotification(studio, path) );
        
        // Remove nodes using strings
        tree.treeChanged( new FTreeNodeRemovedNotification(studio, "baseDir-1/subDir-b") );
    }
    
}

Adding nodes on two navigation trees

As mentioned earlier the tree provider allows the user to create new navigation tree; the following code shows how to create two navigation trees adding nodes to them.

Code Block
titleTwoNavigationTrees.java
borderStylesolid

import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.tree.FTree;
import org.freehep.jas.plugin.tree.FTreeFolderNode;
import org.freehep.jas.plugin.tree.FTreeLeafNode;
import org.freehep.jas.plugin.tree.FTreeNodeAddedNotification;
import org.freehep.jas.plugin.tree.FTreePath;
import org.freehep.jas.plugin.tree.FTreeProvider;
import org.freehep.util.FreeHEPLookup;

public class TwoNavigationTrees {
    
    
    public static void main(String[] args) {
        
        // Get the Studio, the FTreeProvider and the default FTree
        Studio studio = (Studio) Studio.getApplication();
        FTreeProvider treeProvider = (FTreeProvider) studio.getLookup().lookup(FTreeProvider.class);
        FTree tree1 = treeProvider.tree("tree1");
        FTree tree2 = treeProvider.tree("tree2");
        
        // Add nodes to the first tree
        tree1.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1", FTreeFolderNode.class) );
        tree1.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1/subDir-a", FTreeLeafNode.class) );
        tree1.treeChanged( new FTreeNodeAddedNotification(studio, "baseDir-1/subDir-b/subSubDir-a", FTreeLeafNode.class) );

        // Add nodes to the second tree
        tree2.treeChanged( new FTreeNodeAddedNotification(studio, "/home", FTreeFolderNode.class) );
        tree2.treeChanged( new FTreeNodeAddedNotification(studio, "/home/file.txt", FTreeLeafNode.class) );
        tree2.treeChanged( new FTreeNodeAddedNotification(studio, "/home/tmp/out.out", FTreeLeafNode.class) );
    }
    
}

Create adapter to define double click on tree node

In the above examples the nodes added on the tree had very little functionality. The code in this example shows how to create an adapter to add the double click action to the leaf nodes. In the example we create a new node adapter by extending the DefaultFTreeNodeAdapter, the default implementation of the FTreeNodeAdapter, overwriting its doubleClick method. When double clicking on the nodes created when running the example an empty editor window will appear whose title is the node's name.

Code Block
titleBasicDoubleClick.java
borderStylesolid

import java.io.File;
import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.tree.DefaultFTreeNodeAdapter;
import org.freehep.jas.plugin.tree.FTree;
import org.freehep.jas.plugin.tree.FTreeNode;
import org.freehep.jas.plugin.tree.FTreeNodeAdapter;
import org.freehep.jas.plugin.tree.FTreeNodeAddedNotification;
import org.freehep.jas.plugin.tree.FTreeProvider;
import org.freehep.jas.services.TextEditorService;
import org.freehep.util.FreeHEPLookup;

public class BasicDoubleClick {
    
    public static void main(String[] args) {
        
        // Get the Studio, the FTreeProvider and the default FTree
        Studio studio = (Studio) Studio.getApplication();
        FTreeProvider treeProvider = (FTreeProvider) studio.getLookup().lookup(FTreeProvider.class);
        FTree tree = treeProvider.tree();
        
        // Get the text editor service
        TextEditorService textEditor = (TextEditorService) studio.getLookup().lookup(TextEditorService.class);
        
        // Register the adapter for nodes of type MyFile.class
        BasicDoubleClick doubleClick = new BasicDoubleClick();
        treeProvider.treeNodeAdapterRegistry().registerNodeAdapter( doubleClick.adapter(textEditor), MyFile.class );
        
        // Add nodes to the first tree
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "/home/file.txt", MyFile.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "/home/tmp/out.out", MyFile.class) );
    }
    
    FTreeNodeAdapter adapter( TextEditorService service ) {
        return new MyFileAdapter(service);
    }
    
    private class MyFile {
    }
    
    private class MyFileAdapter extends DefaultFTreeNodeAdapter {
        
        private TextEditorService textEditor;
        
        MyFileAdapter(TextEditorService textEditor) {
            super(100);
            this.textEditor = textEditor;
        }
        
        public boolean doubleClick(FTreeNode node) {
            String nodeName = node.path().getLastPathComponent();
            textEditor.show(nodeName,"txt", nodeName );
            return true;
        }        
        
        
    }
    
}

Adding a new sorting algorithm

In the following code a new sorting algorithm is added to the JAS3 lookup table. Running the example a few nodes will be added to the JAS3 navigation tree. By right clicking on the "sortable nodes" folder and selecting the Sorting... menu item you will notice the presence of the algorithm we just added: "Name-length". It sorts nodes based on the length of their name. To achieve this all we had to do was to implement the FTreeNodeSorter interface and register an instance with the lookup table.

Code Block
titleAddSortingAlgorithm.java
borderStylesolid

import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.tree.FTree;
import org.freehep.jas.plugin.tree.FTreeFolderNode;
import org.freehep.jas.plugin.tree.FTreeLeafNode;
import org.freehep.jas.plugin.tree.FTreeNode;
import org.freehep.jas.plugin.tree.FTreeNodeAddedNotification;
import org.freehep.jas.plugin.tree.FTreeNodeSorter;
import org.freehep.jas.plugin.tree.FTreeProvider;

public class AddSortingAlgorithm implements FTreeNodeSorter {
    
    
    public static void main(String[] args) {
        
        // Get the Studio, the FTreeProvider and the default FTree
        Studio studio = (Studio) Studio.getApplication();
        FTreeProvider treeProvider = (FTreeProvider) studio.getLookup().lookup(FTreeProvider.class);
        FTree tree = treeProvider.tree();
        
        // Add some nodes
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "sortable nodes", FTreeFolderNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "sortable nodes/node 1", FTreeLeafNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "sortable nodes/abcdefgh", FTreeLeafNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "sortable nodes/x", FTreeLeafNode.class) );
        tree.treeChanged( new FTreeNodeAddedNotification(studio, "sortable nodes/the longest of them", FTreeLeafNode.class) );
        
        // Add the sorting algorithm to the lookup table.
        studio.getLookup().add( new AddSortingAlgorithm() );
        
    }
    
    public String algorithmName() {
        return "Name-Length";
    }
    
    public String description() {
        return "Sorts nodes based on the length of their name.";
    }
    
    public int sort(FTreeNode node1, FTreeNode node2) {
        return node1.path().getLastPathComponent().length() - node2.path().getLastPathComponent().length();
    }
    
}