1 19 20 package org.netbeans.modules.project.uiapi; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.Image ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.beans.PropertyVetoException ; 28 import javax.swing.JPanel ; 29 import javax.swing.tree.TreeSelectionModel ; 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 42 public class CategoryView extends JPanel implements ExplorerManager.Provider, PropertyChangeListener { 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 manager = new ExplorerManager(); 54 55 setLayout( new BorderLayout () ); 56 57 Dimension size = new Dimension ( 220, 4 ); 58 btv = new BeanTreeView(); 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 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 e ) { 100 } 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() ); return new CategoryNode( rootCategory ); 137 } 138 139 141 public void propertyChange(PropertyChangeEvent evt) { 142 143 Object source = evt.getSource(); 144 String 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 168 170 private static class CategoryNode extends AbstractNode implements PropertyChangeListener { 171 172 private Image icon = org.openide.util.Utilities.loadImage( "org/netbeans/modules/project/uiapi/defaultCategory.gif" ); 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 getHtmlDisplayName() { 191 return category.isValid() ? null : 192 "<html><font color=\"!nb.errorForeground\">" + category.getDisplayName() + "</font></html>"; } 195 196 public Image getIcon( int type ) { 197 return this.icon; 198 } 199 200 public Image getOpenedIcon( int type ) { 201 return getIcon( type ); 202 } 203 204 public void propertyChange(PropertyChangeEvent evt) { 205 if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) { 206 fireDisplayNameChange(null, null); 207 } 208 } 209 210 } 211 212 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 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 |