1 19 20 package org.netbeans.modules.java.editor.codegen.ui; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.beans.PropertyVetoException ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.lang.model.element.Element; 28 import javax.swing.BorderFactory ; 29 import javax.swing.JPanel ; 30 import org.netbeans.api.java.source.ElementHandle; 31 import org.openide.explorer.ExplorerManager; 32 import org.openide.explorer.view.BeanTreeView; 33 import org.openide.nodes.Node; 34 35 39 public class ElementSelectorPanel extends JPanel implements ExplorerManager.Provider { 40 41 private ExplorerManager manager = new ExplorerManager(); 42 private BeanTreeView elementView; 43 44 45 public ElementSelectorPanel(ElementNode.Description elementDescription, boolean singleSelection ) { 46 setLayout(new BorderLayout ()); 47 elementView = new CheckTreeView(); 48 elementView.setRootVisible(false); 49 elementView.setBorder(BorderFactory.createLineBorder(Color.gray)); 50 add(elementView, BorderLayout.CENTER); 51 setRootElement(elementDescription, singleSelection); 52 } 53 54 public List <ElementHandle<? extends Element>> getTreeSelectedElements() { 55 ArrayList <ElementHandle<? extends Element>> handles = new ArrayList <ElementHandle<? extends Element>>(); 56 57 for (Node node : manager.getSelectedNodes()) { 58 if (node instanceof ElementNode) { 59 ElementNode.Description description = node.getLookup().lookup(ElementNode.Description.class); 60 handles.add(description.getElementHandle()); 61 } 62 } 63 64 return handles; 65 } 66 67 public List <ElementHandle<? extends Element>> getSelectedElements() { 68 ArrayList <ElementHandle<? extends Element>> handles = new ArrayList <ElementHandle<? extends Element>>(); 69 70 Node n = manager.getRootContext(); 71 ElementNode.Description description = n.getLookup().lookup(ElementNode.Description.class); 72 getSelectedHandles( description, handles ); 73 74 return handles; 75 } 76 77 public void setRootElement(ElementNode.Description elementDescription, boolean singleSelection) { 78 79 Node n; 80 if ( elementDescription != null ) { 81 ElementNode en = new ElementNode(elementDescription); 82 en.setSingleSelection(singleSelection); 83 n = en; 84 } 85 else { 86 n = Node.EMPTY; 87 } 88 manager.setRootContext(n); 89 90 } 91 92 public void doInitialExpansion( int howMuch ) { 93 Node root = getExplorerManager().getRootContext(); 94 Node[] subNodes = root.getChildren().getNodes(true); 95 96 boolean someNodeSelected = false; 97 98 for( int i = 0; subNodes != null && i < (howMuch == - 1 ? subNodes.length : howMuch) ; i++ ) { 99 elementView.expandNode(subNodes[i]); 100 if ( !someNodeSelected ) { 101 Node[] ssn = subNodes[i].getChildren().getNodes( true ); 102 if ( ssn != null && ssn.length > 0 ) { 103 try { 104 getExplorerManager().setSelectedNodes(new org.openide.nodes.Node[]{ssn[0]}); 105 someNodeSelected = true; 106 } 107 catch (PropertyVetoException ex) { 108 } 110 } 111 } 112 } 113 } 114 115 117 public ExplorerManager getExplorerManager() { 118 return manager; 119 } 120 121 private void getSelectedHandles( ElementNode.Description description, 122 ArrayList <ElementHandle<? extends Element>> target) { 123 124 List <ElementNode.Description> subs = description.getSubs(); 125 126 if ( subs == null ) { 127 return; 128 } 129 130 for( ElementNode.Description d : subs ) { 131 if ( d.isSelectable() && d.isSelected() ) { 132 target.add(d.getElementHandle() ); 133 } 134 else { 135 getSelectedHandles( d, target ); 136 } 137 } 138 } 139 140 } 141 | Popular Tags |