KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
25
26
27 /** Maintains current category in the customizer.
28  *
29  * @author Petr Hrebejk
30  */

31 public class CategoryModel {
32
33     public static final String JavaDoc PROP_CURRENT_CATEGORY = "propCurrentCategory";
34
35     private ProjectCustomizer.Category[] categories;
36
37     private ProjectCustomizer.Category currentCategory;
38
39     // Might be vetoable later
40
private PropertyChangeSupport JavaDoc pcs;
41     
42     public CategoryModel( ProjectCustomizer.Category[] categories ) {
43         
44         if ( categories == null || categories.length == 0 ) {
45             throw new IllegalArgumentException JavaDoc( "Must provide at least one category" ); // NOI18N
46
}
47         
48         this.categories = categories;
49         this.currentCategory = categories[0];
50         this.pcs = new PropertyChangeSupport JavaDoc( this );
51     }
52     
53     
54     public ProjectCustomizer.Category getCurrentCategory() {
55         return this.currentCategory;
56     }
57     
58     public ProjectCustomizer.Category getCategory( String JavaDoc name ) {
59         return findCategoryByName( name, categories );
60     }
61     
62     public void setCurrentCategory( ProjectCustomizer.Category category ) {
63         
64         if ( currentCategory != category ) {
65             ProjectCustomizer.Category oldValue = currentCategory;
66             this.currentCategory = category;
67             firePropertyChange( PROP_CURRENT_CATEGORY, oldValue, category );
68         }
69         
70     }
71     
72     public ProjectCustomizer.Category[] getCategories() {
73         return this.categories;
74     }
75     
76     public void addPropertyChangeListener( String JavaDoc propertyName, PropertyChangeListener JavaDoc l ) {
77         pcs.addPropertyChangeListener( propertyName, l );
78     }
79     
80     public void addPropertyChangeListener( PropertyChangeListener JavaDoc l ) {
81         pcs.addPropertyChangeListener( l );
82     }
83     
84     public void removePropertyChangeListener( String JavaDoc propertyName, PropertyChangeListener JavaDoc l ) {
85         pcs.removePropertyChangeListener( propertyName, l );
86     }
87     
88     public void removePropertyChangeListener( PropertyChangeListener JavaDoc l ) {
89         pcs.removePropertyChangeListener( l );
90     }
91     
92     public void firePropertyChange( String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue ) {
93         pcs.firePropertyChange( propertyName, oldValue, newValue );
94     }
95     
96     // Private methods ---------------------------------------------------------
97

98     private static ProjectCustomizer.Category findCategoryByName( String JavaDoc name, ProjectCustomizer.Category[] categories ) {
99         
100         for( int i = 0; i < categories.length; i++ ) {
101             if ( name.equals( categories[i].getName() ) ) {
102                 return categories[i];
103             }
104             
105             ProjectCustomizer.Category[] subcategories = categories[i].getSubcategories();
106             if ( subcategories != null ) {
107                 ProjectCustomizer.Category category = findCategoryByName( name, subcategories );
108                 if ( category != null ) {
109                     return category;
110                 }
111             }
112             
113         }
114         
115         return null;
116     }
117     
118 }
119
Popular Tags