KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piagetproject > PiagetNode


1 /*
2  * PiagetNode.java
3  *
4  * Created on July 14, 2005, 5:59 PM
5  *
6  * To change this template, choose Tools | Options and locate the template under
7  * the Source Creation and Management node. Right-click the template and choose
8  * Open. You can then make changes to the template in the Source Editor.
9  */

10
11 package org.netbeans.modules.piagetproject;
12
13 import java.awt.Image JavaDoc;
14 import javax.swing.AbstractAction JavaDoc;
15 import javax.swing.Action JavaDoc;
16 import org.netbeans.modules.piagetproject.treestructure.NodeRetriever;
17 import org.openide.loaders.DataObjectNotFoundException;
18 import org.openide.nodes.Children;
19 import org.openide.nodes.FilterNode;
20 import org.openide.nodes.Node;
21 import org.openide.util.Lookup;
22 import org.openide.util.Utilities;
23 import org.openide.util.lookup.Lookups;
24 import org.openide.util.lookup.ProxyLookup;
25
26
27
28 /**
29  *
30  * @author loicsegapelli
31  */

32 public final class PiagetNode extends FilterNode {
33     
34     private PiagetProject project;
35     private static Node originalNode;
36     private static Node [] userFiles, levels;
37     RefreshAction refresh = new RefreshAction(this);
38     
39     public PiagetNode(Node node, PiagetProject project) throws DataObjectNotFoundException {
40         super(node, getTreeChildren(node), new ProxyLookup(new Lookup[] { Lookups.singleton(project), node.getLookup() }));
41         NodeRetriever.initListeners(this);
42         this.project = project;
43         originalNode = node;
44         setShortDescription("Main node; refresh to view changes");
45     }
46     
47     public Action JavaDoc getPreferredAction() {
48         return refresh;
49     }
50     
51     public void refresh(){
52         refresh.refresh();
53     }
54     
55     private static Children.Array getTreeChildren(Node original) {
56         Children.Array c = new Children.Array();
57         c.add(getTreeNodes(original));
58         return c;
59     }
60     
61     private static Node[] getTreeNodes(Node original) {
62         levels = NodeRetriever.getLevels();
63         userFiles = NodeRetriever.getUserFiles(original);
64         int userLength = 0;
65         if (userFiles!=null) userLength = userFiles.length;
66         Node newNodes [] = new Node [levels.length + userLength];
67         
68         System.arraycopy(levels, 0, newNodes, 0, levels.length);
69         if (userFiles!=null) System.arraycopy(userFiles, 0, newNodes, levels.length, userFiles.length);
70         
71         return newNodes;
72     }
73     
74     public Image JavaDoc getIcon(int type) {
75         return Utilities.loadImage("org/netbeans/modules/piagetproject/resources/project.png");
76     }
77     
78     public Image JavaDoc getOpenedIcon(int type) {
79         return getIcon(type);
80     }
81     
82     public String JavaDoc getDisplayName() {
83         return project.getProjectDirectory().getName();
84     }
85     
86     public Action JavaDoc[] getActions(boolean context) {
87         Action JavaDoc [] orig = super.getActions(context);
88         Action JavaDoc [] result = new Action JavaDoc[orig.length+1];
89         System.arraycopy(orig, 0, result, 1, orig.length);
90         result [0] = refresh;
91         return result;
92         //return super.getActions (context);
93
}
94     
95     private static class RefreshAction extends AbstractAction JavaDoc {
96         
97         PiagetNode piagetNode;
98         
99         public RefreshAction(PiagetNode piagetNode) {
100             putValue(Action.NAME, "refresh");
101             this.piagetNode = piagetNode;
102         }
103         
104         public void refresh(){
105             Children.Array c = (Children.Array) piagetNode.getChildren();
106             if (levels!=null) c.remove(levels);
107             if (userFiles!=null) c.remove(userFiles);
108             c.add(PiagetNode.getTreeNodes(originalNode));
109             NodeRetriever.initListeners(piagetNode);
110         }
111         
112         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
113             refresh();
114         }
115         
116         public boolean isEnabled() {
117             return true;
118         }
119         
120     }
121 }
Popular Tags