KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > uiapi > CategoryView


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.project.uiapi;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyVetoException JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.tree.TreeSelectionModel JavaDoc;
30 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
31 import org.openide.explorer.ExplorerManager;
32 import org.openide.explorer.view.BeanTreeView;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.Node;
36 import org.openide.util.NbBundle;
37 import org.openide.util.lookup.Lookups;
38
39 /**
40  * @author Petr Hrebejk
41  */

42 public class CategoryView extends JPanel JavaDoc implements ExplorerManager.Provider, PropertyChangeListener JavaDoc {
43
44     private ExplorerManager manager;
45     private BeanTreeView btv;
46     private CategoryModel categoryModel;
47
48     public CategoryView( CategoryModel categoryModel ) {
49
50         this.categoryModel = categoryModel;
51
52         // See #36315
53
manager = new ExplorerManager();
54
55         setLayout( new BorderLayout JavaDoc() );
56
57         Dimension JavaDoc size = new Dimension JavaDoc( 220, 4 );
58         btv = new BeanTreeView(); // Add the BeanTreeView
59
btv.setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
60         btv.setPopupAllowed( false );
61         btv.setRootVisible( false );
62         btv.setDefaultActionAllowed( false );
63         btv.setMinimumSize( size );
64         btv.setPreferredSize( size );
65         btv.setMaximumSize( size );
66         btv.setDragSource (false);
67         this.add( btv, BorderLayout.CENTER );
68         manager.setRootContext( createRootNode( categoryModel ) );
69         manager.addPropertyChangeListener( this );
70         categoryModel.addPropertyChangeListener( this );
71         btv.expandAll();
72         selectNode( categoryModel.getCurrentCategory() );
73
74         btv.getAccessibleContext().setAccessibleName(NbBundle.getMessage(CategoryView.class,"AN_CatgoryView"));
75         btv.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CategoryView.class,"AD_CategoryView"));
76
77     }
78
79     public ExplorerManager getExplorerManager() {
80         return manager;
81     }
82
83     public void addNotify() {
84         super.addNotify();
85         btv.expandAll();
86         btv.requestFocusInWindow();
87     }
88
89     // Private methods -----------------------------------------------------
90

91     private void selectNode( ProjectCustomizer.Category category ) {
92
93         Node node = findNode( category, manager.getRootContext() );
94
95         if ( node != null ) {
96             try {
97                 manager.setSelectedNodes( new Node[] { node } );
98             }
99             catch ( PropertyVetoException JavaDoc e ) {
100                 // No node will be selected
101
}
102         }
103
104     }
105
106     private Node findNode( ProjectCustomizer.Category category, Node node ) {
107
108         Children ch = node.getChildren();
109
110         if ( ch != null && ch != Children.LEAF ) {
111             Node nodes[] = ch.getNodes( true );
112
113             if ( nodes != null ) {
114                 for (Node child : nodes) {
115                     ProjectCustomizer.Category cc = child.getLookup().lookup(ProjectCustomizer.Category.class);
116
117                     if ( cc == category ) {
118                         return child;
119                     }
120                     else {
121                         Node n = findNode(category, child);
122                         if ( n != null ) {
123                             return n;
124                         }
125                     }
126                 }
127             }
128         }
129
130         return null;
131     }
132
133
134     private Node createRootNode( CategoryModel categoryModel ) {
135         ProjectCustomizer.Category rootCategory = ProjectCustomizer.Category.create( "root", "root", null, categoryModel.getCategories() ); // NOI18N
136
return new CategoryNode( rootCategory );
137     }
138
139     // Implementation of property change listener --------------------------
140

141     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
142
143         Object JavaDoc source = evt.getSource();
144         String JavaDoc propertyName = evt.getPropertyName();
145
146         if ( source== manager && ExplorerManager.PROP_SELECTED_NODES.equals( propertyName ) ) {
147             Node nodes[] = manager.getSelectedNodes();
148             if ( nodes == null || nodes.length <= 0 ) {
149                 return;
150             }
151             Node node = nodes[0];
152
153             ProjectCustomizer.Category category = node.getLookup().lookup(ProjectCustomizer.Category.class);
154             if ( category != categoryModel.getCurrentCategory() ) {
155                 categoryModel.setCurrentCategory( category );
156             }
157         }
158
159         if ( source == categoryModel && CategoryModel.PROP_CURRENT_CATEGORY.equals( propertyName ) ) {
160             selectNode( (ProjectCustomizer.Category)evt.getNewValue() );
161         }
162
163     }
164
165
166     // Private Inner classes -----------------------------------------------
167

168     /** Node to be used for configuration
169      */

170     private static class CategoryNode extends AbstractNode implements PropertyChangeListener JavaDoc {
171
172         private Image JavaDoc icon = org.openide.util.Utilities.loadImage( "org/netbeans/modules/project/uiapi/defaultCategory.gif" ); // NOI18N
173

174         private ProjectCustomizer.Category category;
175
176         public CategoryNode( ProjectCustomizer.Category category ) {
177             super( ( category.getSubcategories() == null || category.getSubcategories().length == 0 ) ?
178                         Children.LEAF : new CategoryChildren( category.getSubcategories() ),
179                    Lookups.fixed(category));
180             setName( category.getName() );
181             this.category = category;
182             setDisplayName( category.getDisplayName() );
183
184             if ( category.getIcon() != null ) {
185                 this.icon = category.getIcon();
186             }
187             Utilities.getCategoryChangeSupport(category).addPropertyChangeListener(this);
188         }
189
190         public String JavaDoc getHtmlDisplayName() {
191             return category.isValid() ? null :
192                 "<html><font color=\"!nb.errorForeground\">" + // NOI18N
193
category.getDisplayName() + "</font></html>"; // NOI18N
194
}
195
196         public Image JavaDoc getIcon( int type ) {
197             return this.icon;
198         }
199
200         public Image JavaDoc getOpenedIcon( int type ) {
201             return getIcon( type );
202         }
203
204         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
205             if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) {
206                 fireDisplayNameChange(null, null);
207             }
208         }
209
210     }
211
212     /** Children used for configuration
213      */

214     private static class CategoryChildren extends Children.Keys<ProjectCustomizer.Category> {
215
216         private ProjectCustomizer.Category[] descriptions;
217
218         public CategoryChildren( ProjectCustomizer.Category[] descriptions ) {
219             this.descriptions = descriptions;
220         }
221
222         // Children.Keys impl --------------------------------------------------
223

224         public void addNotify() {
225             setKeys( descriptions );
226         }
227
228         public void removeNotify() {
229             setKeys(new ProjectCustomizer.Category[0]);
230         }
231
232         protected Node[] createNodes(ProjectCustomizer.Category c) {
233             return new Node[] {new CategoryNode(c)};
234         }
235     }
236
237 }
238
239
240
Popular Tags