1 19 20 package org.netbeans.modules.project.uiapi; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import org.netbeans.spi.project.ui.support.ProjectCustomizer; 25 26 27 31 public class CategoryModel { 32 33 public static final String PROP_CURRENT_CATEGORY = "propCurrentCategory"; 34 35 private ProjectCustomizer.Category[] categories; 36 37 private ProjectCustomizer.Category currentCategory; 38 39 private PropertyChangeSupport pcs; 41 42 public CategoryModel( ProjectCustomizer.Category[] categories ) { 43 44 if ( categories == null || categories.length == 0 ) { 45 throw new IllegalArgumentException ( "Must provide at least one category" ); } 47 48 this.categories = categories; 49 this.currentCategory = categories[0]; 50 this.pcs = new PropertyChangeSupport ( this ); 51 } 52 53 54 public ProjectCustomizer.Category getCurrentCategory() { 55 return this.currentCategory; 56 } 57 58 public ProjectCustomizer.Category getCategory( String 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 propertyName, PropertyChangeListener l ) { 77 pcs.addPropertyChangeListener( propertyName, l ); 78 } 79 80 public void addPropertyChangeListener( PropertyChangeListener l ) { 81 pcs.addPropertyChangeListener( l ); 82 } 83 84 public void removePropertyChangeListener( String propertyName, PropertyChangeListener l ) { 85 pcs.removePropertyChangeListener( propertyName, l ); 86 } 87 88 public void removePropertyChangeListener( PropertyChangeListener l ) { 89 pcs.removePropertyChangeListener( l ); 90 } 91 92 public void firePropertyChange( String propertyName, Object oldValue, Object newValue ) { 93 pcs.firePropertyChange( propertyName, oldValue, newValue ); 94 } 95 96 98 private static ProjectCustomizer.Category findCategoryByName( String 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 |