1 19 20 package org.netbeans.modules.xml.schema.abe.nodes.properties; 21 22 import java.awt.BorderLayout ; 23 import java.awt.EventQueue ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyVetoException ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.List ; 30 import javax.swing.JPanel ; 31 import javax.swing.ListSelectionModel ; 32 import org.netbeans.modules.xml.axi.AXIComponent; 33 import org.netbeans.modules.xml.axi.ContentModel; 34 import org.netbeans.modules.xml.axi.datatype.CustomDatatype; 35 import org.netbeans.modules.xml.axi.datatype.Datatype; 36 import org.netbeans.modules.xml.schema.abe.nodes.ABENodeFactory; 37 import org.netbeans.modules.xml.axi.AXIModel; 38 import org.netbeans.modules.xml.axi.AXIType; 39 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode; 40 import org.netbeans.modules.xml.schema.abe.nodes.ContentModelNode; 41 import org.netbeans.modules.xml.schema.abe.nodes.CustomDatatypeNode; 42 import org.netbeans.modules.xml.schema.abe.nodes.DatatypeNode; 43 import org.netbeans.modules.xml.schema.abe.nodes.GlobalContentModelsNode; 44 import org.netbeans.modules.xml.schema.abe.nodes.PrimitiveSimpleTypesNode; 45 import org.netbeans.modules.xml.schema.abe.nodes.SimpleTypesNode; 46 import org.openide.explorer.ExplorerManager; 47 import org.openide.explorer.view.BeanTreeView; 48 import org.openide.nodes.Node; 49 import org.openide.util.Lookup; 50 51 55 public class AXIComponentSelectionPanel extends JPanel 56 implements ExplorerManager.Provider, PropertyChangeListener { 57 58 static final long serialVersionUID = 1L; 59 public static final String PROPERTY_SELECTION = "selectionChanged"; 60 private transient Object currentSelection; 61 private transient String typeDisplayName; 62 private Collection <? extends AXIComponent> exclude; 63 private BeanTreeView typeView; 64 private ExplorerManager explorerManager; 65 66 private List <Class > filterTypes; 67 68 69 public AXIComponentSelectionPanel(AXIModel model, String typeDisplayName, 70 List <Class > filterTypes, Object initialSelection, 71 Collection <? extends AXIComponent> exclude) { 72 this.currentSelection = initialSelection; 73 this.typeDisplayName = typeDisplayName; 74 this.filterTypes = filterTypes; 75 if(exclude==null) { 76 this.exclude = Collections.emptyList(); 77 } else { 78 this.exclude = exclude; 79 } 80 initComponents(); 81 initialize(model); 82 } 83 84 89 private void initComponents() { 91 jPanel1 = new javax.swing.JPanel (); 92 selectedLbl = new javax.swing.JLabel (); 93 nameTxt = new javax.swing.JTextField (); 94 cvPanel = new javax.swing.JPanel (); 95 96 selectedLbl.setText(org.openide.util.NbBundle.getMessage(AXIComponentSelectionPanel.class, "LBL_Currently_Selected", new Object [] {typeDisplayName})); 97 98 nameTxt.setEditable(false); 99 nameTxt.setText(getCurrentSelectionName()); 100 101 org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); 102 jPanel1.setLayout(jPanel1Layout); 103 jPanel1Layout.setHorizontalGroup( 104 jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 105 .add(jPanel1Layout.createSequentialGroup() 106 .add(selectedLbl) 107 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 108 .add(nameTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)) 109 ); 110 jPanel1Layout.setVerticalGroup( 111 jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 112 .add(jPanel1Layout.createSequentialGroup() 113 .addContainerGap() 114 .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 115 .add(nameTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 116 .add(selectedLbl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)) 117 .addContainerGap()) 118 ); 119 120 cvPanel.setLayout(new java.awt.BorderLayout ()); 121 122 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 123 this.setLayout(layout); 124 layout.setHorizontalGroup( 125 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 126 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 127 .addContainerGap() 128 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 129 .add(org.jdesktop.layout.GroupLayout.LEADING, cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE) 130 .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 131 .addContainerGap()) 132 ); 133 layout.setVerticalGroup( 134 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 135 .add(layout.createSequentialGroup() 136 .addContainerGap() 137 .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 138 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 139 .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE) 140 .addContainerGap()) 141 ); 142 } 144 private void initialize(final AXIModel model) { 145 ABENodeFactory factory = new ABENodeFactory( 148 model, Lookup.EMPTY); 149 final Node rootNode = factory.createRootNode(filterTypes); 150 typeView = new BeanTreeView(); 152 typeView.setPopupAllowed(false); 153 typeView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 154 typeView.setRootVisible(false); 155 cvPanel.add(typeView, BorderLayout.CENTER); 156 explorerManager = new ExplorerManager(); 157 explorerManager.setRootContext(rootNode); 158 EventQueue.invokeLater(new Runnable () { 159 public void run() { 160 selectCurrentSelection(model); 161 } 162 }); 163 } 164 165 private void selectCurrentSelection(final AXIModel model) { 166 if(currentSelection!=null) { 167 Node node = findNode(explorerManager.getRootContext(), 168 currentSelection, model); 169 if(node!=null) { 170 try { 171 getExplorerManager().setSelectedNodes(new Node[]{node}); 172 } catch (PropertyVetoException ex) { 173 } 174 } 175 } 176 getExplorerManager().addPropertyChangeListener(this); 177 } 178 179 public void propertyChange(PropertyChangeEvent evt) { 180 if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) { 181 Node[] nodes = getExplorerManager().getSelectedNodes(); 182 if (nodes.length > 0) { 183 if(nodes[0] instanceof ABEAbstractNode) { 184 setCurrentSelection(((ABEAbstractNode)nodes[0]).getAXIComponent()); 185 return; 186 } 187 188 if(nodes[0] instanceof DatatypeNode) { 189 setCurrentSelection((Datatype) 190 ((DatatypeNode)nodes[0]).getType()); 191 return; 192 } 193 194 if(nodes[0] instanceof PrimitiveSimpleTypesNode.TypeNode) { 195 setCurrentSelection((Datatype) 196 ((PrimitiveSimpleTypesNode.TypeNode)nodes[0]).getType()); 197 return; 198 } 199 } 200 setCurrentSelection(null); 201 } 202 } 203 204 private void setCurrentSelection(Object sc){ 205 if(sc instanceof AXIType) { 206 Object oldValue = currentSelection; 207 currentSelection = sc==null||exclude.contains(sc)?null:sc; 208 nameTxt.setText(getCurrentSelectionName()); firePropertyChange(PROPERTY_SELECTION,oldValue,currentSelection); 210 } 211 } 212 213 public Object getCurrentSelection() { 214 return currentSelection; 215 } 216 217 public String getCurrentSelectionName() { 218 if(currentSelection instanceof AXIType) 219 return ((AXIType)currentSelection).getName(); 220 return ""; 221 } 222 223 public ExplorerManager getExplorerManager() { 224 return explorerManager; 225 } 226 227 private Node findNode(Node root, Object currentSelection, AXIModel model) { 228 Node[] categories = root.getChildren().getNodes(); 229 for(Node category:categories) { 230 if(category instanceof PrimitiveSimpleTypesNode && 231 currentSelection instanceof Datatype) { 232 String name = ((Datatype)currentSelection).getName(); 233 Node[] childs = category.getChildren().getNodes(); 234 for(Node child:childs) { 235 if(((PrimitiveSimpleTypesNode.TypeNode)child).getName().equals(name)) 236 return child; 237 } 238 } else if(category instanceof SimpleTypesNode && 239 currentSelection instanceof CustomDatatype) { 240 String name = ((CustomDatatype)currentSelection).getName(); 241 Node[] childs = category.getChildren().getNodes(); 242 for(Node child:childs) { 243 if(((CustomDatatypeNode)child).getName().equals(name)) 244 return child; 245 } 246 } else if(category instanceof GlobalContentModelsNode && 247 currentSelection instanceof ContentModel) { 248 String name = ((ContentModel)currentSelection).getName(); 249 Node[] childs = category.getChildren().getNodes(); 250 for(Node child:childs) { 251 if(((ContentModelNode)child).getName().equals(name)) 252 return child; 253 } 254 } 255 } 256 return null; 257 } 258 259 private javax.swing.JPanel cvPanel; 261 private javax.swing.JPanel jPanel1; 262 private javax.swing.JTextField nameTxt; 263 private javax.swing.JLabel selectedLbl; 264 266 } 267 | Popular Tags |