KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > FilterXNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.deployment.impl.ui;
21 import java.awt.Image JavaDoc;
22 import org.openide.nodes.Node;
23 import org.openide.nodes.FilterNode;
24 import org.openide.nodes.Sheet;
25 import org.openide.nodes.Node.PropertySet;
26 import javax.swing.Action JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import org.openide.util.Utilities;
31 import org.openide.util.actions.SystemAction;
32
33 /*
34  * A node to filter the original children and extending its action by
35  * an extension node.
36  *
37  * Created on December 19, 2003, 11:21 AM
38  * @author nn136682
39  */

40 public class FilterXNode extends FilterNode {
41     protected Node xnode;
42     private Node original;
43     private boolean extendsActions = true;
44
45     public FilterXNode(Node original, Node xnode, boolean extendsActions) {
46         super(original);
47         this.xnode = xnode;
48         this.original = original;
49         disableDelegation(DELEGATE_GET_ACTIONS);
50         this.extendsActions = extendsActions;
51     }
52     
53     public FilterXNode(Node original, Node xnode, boolean extendsActions, Children xchildren) {
54         super(original, xchildren);
55         this.xnode = xnode;
56         this.original = original;
57         disableDelegation(DELEGATE_GET_ACTIONS);
58         this.extendsActions = extendsActions;
59     }
60     
61     public Node getXNode() {
62         return xnode;
63     }
64     
65     public static class XChildren extends Children {
66         public XChildren(Node original) {
67             super(original);
68         }
69         public void update() {
70             addNotify();
71         }
72     }
73     
74     public Action JavaDoc[] getActions(boolean context) {
75         List JavaDoc actions = new ArrayList JavaDoc();
76         if (extendsActions) {
77             actions.addAll(Arrays.asList(xnode.getActions(context)));
78         }
79         actions.addAll(Arrays.asList(getOriginal().getActions(context)));
80         return (Action JavaDoc[]) actions.toArray(new Action JavaDoc[actions.size()]);
81     }
82
83     public org.openide.nodes.Node.Cookie getCookie(Class JavaDoc type) {
84         org.openide.nodes.Node.Cookie c = null;
85         if (xnode != null)
86             c = xnode.getCookie(type);
87         if (c == null)
88             c = getOriginal().getCookie(type);
89         if (c == null)
90             c = super.getCookie(type);
91         return c;
92     }
93     
94     public PropertySet[] getPropertySets() {
95         return merge(getOriginal().getPropertySets(), xnode.getPropertySets());
96     }
97     
98     public void refreshChildren() {
99         org.openide.nodes.Children c = getChildren();
100         if (c instanceof XChildren)
101             ((XChildren)c).update();
102     }
103
104     public static PropertySet merge(PropertySet overriding, PropertySet base) {
105         Sheet.Set overridden;
106         if (base instanceof Sheet.Set)
107             overridden = (Sheet.Set) base;
108         else {
109             overridden = new Sheet.Set();
110             overridden.put(base.getProperties());
111         }
112         overridden.put(overriding.getProperties());
113         return overridden;
114     }
115     
116     public static PropertySet[] merge(PropertySet[] overridingSets, PropertySet[] baseSets) {
117         java.util.Map JavaDoc ret = new java.util.HashMap JavaDoc();
118         for (int i=0; i<baseSets.length; i++) {
119             ret.put(baseSets[i].getName(), baseSets[i]);
120         }
121         for (int j=0; j<overridingSets.length; j++) {
122             PropertySet base = (PropertySet) ret.get(overridingSets[j].getName());
123             if (base == null) {
124                 ret.put(overridingSets[j].getName(), overridingSets[j]);
125             } else {
126                 base = merge(overridingSets[j], base);
127                 ret.put(base.getName(), base);
128             }
129         }
130
131         PropertySet top = (PropertySet) ret.remove(Sheet.PROPERTIES);
132         List JavaDoc retList = new ArrayList JavaDoc();
133         if (top != null)
134             retList.add(top);
135         retList.addAll(ret.values());
136         return (PropertySet[]) retList.toArray(new PropertySet[retList.size()]);
137     }
138 }
139
Popular Tags