Line 0
Link Here
|
|
|
1 |
/*4 |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.modules.project.uiapi; |
15 |
|
16 |
import java.awt.BorderLayout; |
17 |
import java.awt.Dimension; |
18 |
import java.awt.Image; |
19 |
import java.beans.PropertyChangeEvent; |
20 |
import java.beans.PropertyChangeListener; |
21 |
import java.beans.PropertyVetoException; |
22 |
import java.util.Arrays; |
23 |
import java.util.Collection; |
24 |
import java.util.Collections; |
25 |
import javax.swing.JPanel; |
26 |
import javax.swing.tree.TreeSelectionModel; |
27 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer; |
28 |
import org.openide.explorer.ExplorerManager; |
29 |
import org.openide.explorer.view.BeanTreeView; |
30 |
import org.openide.nodes.AbstractNode; |
31 |
import org.openide.nodes.Children; |
32 |
import org.openide.nodes.Node; |
33 |
import org.openide.util.NbBundle; |
34 |
import org.openide.util.Utilities; |
35 |
import org.openide.util.lookup.Lookups; |
36 |
|
37 |
/** |
38 |
* |
39 |
* @author Petr Hrebejk |
40 |
*/ |
41 |
public class CategoryView extends JPanel implements ExplorerManager.Provider, PropertyChangeListener { |
42 |
|
43 |
private ExplorerManager manager; |
44 |
private BeanTreeView btv; |
45 |
private CategoryModel categoryModel; |
46 |
|
47 |
private ProjectCustomizer.Category currentCategory; |
48 |
|
49 |
public CategoryView( CategoryModel categoryModel ) { |
50 |
|
51 |
this.categoryModel = categoryModel; |
52 |
|
53 |
// See #36315 |
54 |
manager = new ExplorerManager(); |
55 |
|
56 |
setLayout( new BorderLayout() ); |
57 |
|
58 |
Dimension size = new Dimension( 220, 4 ); |
59 |
btv = new BeanTreeView(); // Add the BeanTreeView |
60 |
btv.setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); |
61 |
btv.setPopupAllowed( false ); |
62 |
btv.setRootVisible( false ); |
63 |
btv.setDefaultActionAllowed( false ); |
64 |
btv.setMinimumSize( size ); |
65 |
btv.setPreferredSize( size ); |
66 |
btv.setMaximumSize( size ); |
67 |
btv.setDragSource (false); |
68 |
this.add( btv, BorderLayout.CENTER ); |
69 |
manager.setRootContext( createRootNode( categoryModel ) ); |
70 |
manager.addPropertyChangeListener( this ); |
71 |
categoryModel.addPropertyChangeListener( this ); |
72 |
btv.expandAll(); |
73 |
|
74 |
getAccessibleContext().setAccessibleName(NbBundle.getMessage(CategoryView.class,"AN_CatgoryView")); |
75 |
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CategoryView.class,"AD_CategoryView")); |
76 |
|
77 |
} |
78 |
|
79 |
public ExplorerManager getExplorerManager() { |
80 |
return manager; |
81 |
} |
82 |
|
83 |
|
84 |
public void addNotify() { |
85 |
super.addNotify(); |
86 |
btv.expandAll(); |
87 |
} |
88 |
|
89 |
|
90 |
// Private methods ----------------------------------------------------- |
91 |
|
92 |
private void selectNode( ProjectCustomizer.Category category ) { |
93 |
|
94 |
Node node = findNode( category, manager.getRootContext() ); |
95 |
|
96 |
if ( node != null ) { |
97 |
try { |
98 |
manager.setSelectedNodes( new Node[] { node } ); |
99 |
} |
100 |
catch ( PropertyVetoException e ) { |
101 |
// No node will be selected |
102 |
} |
103 |
} |
104 |
|
105 |
} |
106 |
|
107 |
private Node findNode( ProjectCustomizer.Category category, Node node ) { |
108 |
|
109 |
Children ch = node.getChildren();; |
110 |
|
111 |
if ( ch != null && ch != Children.LEAF ) { |
112 |
Node nodes[] = ch.getNodes( true ); |
113 |
|
114 |
if ( nodes != null ) { |
115 |
for( int i = 0; i < nodes.length; i++ ) { |
116 |
ProjectCustomizer.Category cc = (ProjectCustomizer.Category)nodes[i].getLookup().lookup( ProjectCustomizer.Category.class ); |
117 |
|
118 |
if ( cc == category ) { |
119 |
return nodes[i]; |
120 |
} |
121 |
else { |
122 |
Node n = findNode( category, nodes[i] ); |
123 |
if ( n != null ) { |
124 |
return n; |
125 |
} |
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
return null; |
132 |
} |
133 |
|
134 |
|
135 |
private Node createRootNode( CategoryModel categoryModel ) { |
136 |
ProjectCustomizer.Category rootCategory = ProjectCustomizer.Category.create( "root", "root", null, categoryModel.getCategories() ); // NOI18N |
137 |
return new CategoryNode( rootCategory ); |
138 |
} |
139 |
|
140 |
// Implementation of property change listener -------------------------- |
141 |
|
142 |
public void propertyChange(PropertyChangeEvent evt) { |
143 |
|
144 |
Object source = evt.getSource(); |
145 |
String propertyName = evt.getPropertyName(); |
146 |
|
147 |
if ( source== manager && ExplorerManager.PROP_SELECTED_NODES.equals( propertyName ) ) { |
148 |
Node nodes[] = manager.getSelectedNodes(); |
149 |
if ( nodes == null || nodes.length <= 0 ) { |
150 |
return; |
151 |
} |
152 |
Node node = nodes[0]; |
153 |
|
154 |
ProjectCustomizer.Category category = (ProjectCustomizer.Category) node.getLookup().lookup( ProjectCustomizer.Category.class ); |
155 |
if ( category != categoryModel.getCurrentCategory() ) { |
156 |
categoryModel.setCurrentCategory( category ); |
157 |
} |
158 |
} |
159 |
|
160 |
if ( source == categoryModel && CategoryModel.PROP_CURRENT_CATEGORY.equals( propertyName ) ) { |
161 |
selectNode( (ProjectCustomizer.Category)evt.getNewValue() ); |
162 |
} |
163 |
|
164 |
} |
165 |
|
166 |
|
167 |
// Private Inner classes ----------------------------------------------- |
168 |
|
169 |
/** Node to be used for configuration |
170 |
*/ |
171 |
private static class CategoryNode extends AbstractNode { |
172 |
|
173 |
private Image icon = Utilities.loadImage( "org/netbeans/modules/project/uiapi/defaultCategory.gif" ); // NOI18N |
174 |
|
175 |
public CategoryNode( ProjectCustomizer.Category category ) { |
176 |
super( ( category.getSubcategories() == null || category.getSubcategories().length == 0 ) ? |
177 |
Children.LEAF : new CategoryChildren( category.getSubcategories() ), |
178 |
Lookups.fixed( new Object[] { category } ) ); |
179 |
setName( category.getName() ); |
180 |
setDisplayName( category.getDisplayName() ); |
181 |
|
182 |
if ( category.getIcon() != null ) { |
183 |
this.icon = category.getIcon(); |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
public Image getIcon( int type ) { |
189 |
return this.icon; |
190 |
} |
191 |
|
192 |
public Image getOpenedIcon( int type ) { |
193 |
return getIcon( type ); |
194 |
} |
195 |
} |
196 |
|
197 |
/** Children used for configuration |
198 |
*/ |
199 |
private static class CategoryChildren extends Children.Keys { |
200 |
|
201 |
private Collection descriptions; |
202 |
|
203 |
public CategoryChildren( ProjectCustomizer.Category[] descriptions ) { |
204 |
this.descriptions = Arrays.asList( descriptions ); |
205 |
} |
206 |
|
207 |
// Children.Keys impl -------------------------------------------------- |
208 |
|
209 |
public void addNotify() { |
210 |
setKeys( descriptions ); |
211 |
} |
212 |
|
213 |
public void removeNotify() { |
214 |
setKeys( Collections.EMPTY_LIST ); |
215 |
} |
216 |
|
217 |
protected Node[] createNodes( Object key ) { |
218 |
return new Node[] { new CategoryNode( (ProjectCustomizer.Category)key ) }; |
219 |
} |
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
|