KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > codegen > ui > ElementSelectorPanel


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.java.editor.codegen.ui;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.lang.model.element.Element;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import org.netbeans.api.java.source.ElementHandle;
31 import org.openide.explorer.ExplorerManager;
32 import org.openide.explorer.view.BeanTreeView;
33 import org.openide.nodes.Node;
34
35 /**
36  *
37  * @author Petr Hrebejk, Dusan Balek
38  */

39 public class ElementSelectorPanel extends JPanel JavaDoc implements ExplorerManager.Provider {
40     
41     private ExplorerManager manager = new ExplorerManager();
42     private BeanTreeView elementView;
43     
44     /** Creates new form ElementSelectorPanel */
45     public ElementSelectorPanel(ElementNode.Description elementDescription, boolean singleSelection ) {
46         setLayout(new BorderLayout JavaDoc());
47         elementView = new CheckTreeView();
48         elementView.setRootVisible(false);
49         elementView.setBorder(BorderFactory.createLineBorder(Color.gray));
50         add(elementView, BorderLayout.CENTER);
51         setRootElement(elementDescription, singleSelection);
52     }
53     
54     public List JavaDoc<ElementHandle<? extends Element>> getTreeSelectedElements() {
55         ArrayList JavaDoc<ElementHandle<? extends Element>> handles = new ArrayList JavaDoc<ElementHandle<? extends Element>>();
56                 
57         for (Node node : manager.getSelectedNodes()) {
58             if (node instanceof ElementNode) {
59                 ElementNode.Description description = node.getLookup().lookup(ElementNode.Description.class);
60                 handles.add(description.getElementHandle());
61             }
62         }
63      
64         return handles;
65     }
66         
67     public List JavaDoc<ElementHandle<? extends Element>> getSelectedElements() {
68         ArrayList JavaDoc<ElementHandle<? extends Element>> handles = new ArrayList JavaDoc<ElementHandle<? extends Element>>();
69             
70         Node n = manager.getRootContext();
71         ElementNode.Description description = n.getLookup().lookup(ElementNode.Description.class);
72         getSelectedHandles( description, handles );
73        
74         return handles;
75     }
76     
77     public void setRootElement(ElementNode.Description elementDescription, boolean singleSelection) {
78         
79         Node n;
80         if ( elementDescription != null ) {
81             ElementNode en = new ElementNode(elementDescription);
82             en.setSingleSelection(singleSelection);
83             n = en;
84         }
85         else {
86             n = Node.EMPTY;
87         }
88         manager.setRootContext(n);
89         
90     }
91     
92     public void doInitialExpansion( int howMuch ) {
93         Node root = getExplorerManager().getRootContext();
94         Node[] subNodes = root.getChildren().getNodes(true);
95         
96         boolean someNodeSelected = false;
97         
98         for( int i = 0; subNodes != null && i < (howMuch == - 1 ? subNodes.length : howMuch) ; i++ ) {
99             elementView.expandNode(subNodes[i]);
100             if ( !someNodeSelected ) {
101                 Node[] ssn = subNodes[i].getChildren().getNodes( true );
102                 if ( ssn != null && ssn.length > 0 ) {
103                     try {
104                         getExplorerManager().setSelectedNodes(new org.openide.nodes.Node[]{ssn[0]});
105                         someNodeSelected = true;
106                     }
107                     catch (PropertyVetoException JavaDoc ex) {
108                         // Ignore
109
}
110                 }
111             }
112         }
113     }
114     
115     // ExplorerManager.Provider imlementation ----------------------------------
116

117     public ExplorerManager getExplorerManager() {
118         return manager;
119     }
120     
121     private void getSelectedHandles( ElementNode.Description description,
122                                      ArrayList JavaDoc<ElementHandle<? extends Element>> target) {
123         
124         List JavaDoc<ElementNode.Description> subs = description.getSubs();
125         
126         if ( subs == null ) {
127             return;
128         }
129         
130         for( ElementNode.Description d : subs ) {
131             if ( d.isSelectable() && d.isSelected() ) {
132                 target.add(d.getElementHandle() );
133             }
134             else {
135                 getSelectedHandles( d, target );
136             }
137         }
138     }
139        
140 }
141
Popular Tags