1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.basic.editors; 27 28 import java.awt.BorderLayout ; 29 import java.awt.EventQueue ; 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyVetoException ; 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Collections ; 36 import javax.swing.JPanel ; 37 import javax.swing.plaf.basic.BasicBorders ; 38 import javax.swing.tree.TreeSelectionModel ; 39 40 import org.openide.explorer.ExplorerManager; 41 import org.openide.explorer.view.BeanTreeView; 42 import org.openide.nodes.Node; 43 import org.openide.util.Lookup; 44 import org.openide.util.NbBundle; 45 46 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 47 import org.netbeans.modules.xml.schema.model.SchemaComponent; 48 import org.netbeans.modules.xml.schema.model.SchemaModel; 49 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 50 import org.netbeans.modules.xml.schema.ui.basic.UIUtilities; 51 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode; 52 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory; 53 import org.netbeans.modules.xml.schema.ui.nodes.categorized.PrimitiveSimpleType; 54 55 59 public class SchemaComponentSelectionPanel<T extends ReferenceableSchemaComponent> extends JPanel 60 implements ExplorerManager.Provider, PropertyChangeListener { 61 62 static final long serialVersionUID = 1L; 63 public static final String PROPERTY_SELECTION = "selectionChanged"; 64 private Class <T> type; 65 private transient SchemaModel model; 66 private transient T currentSelection; 67 private Collection <? extends SchemaComponent> exclude; 68 private BeanTreeView typeView; 69 private boolean includePrimitives; 70 71 80 public SchemaComponentSelectionPanel(SchemaModel model, Class <T> type, 81 T initialSelection, Collection <? extends SchemaComponent> exclude, 82 boolean primitives) { 83 this.type = type; 84 if(exclude==null) { 85 this.exclude = Collections.emptyList(); 86 } else { 87 this.exclude = exclude; 88 } 89 this.includePrimitives = primitives; 90 initComponents(); 91 this.model=model; 92 initialize(initialSelection); 93 } 94 95 100 private void initComponents() { 102 typeSelectionPanel = new EmbededPanel(this); 103 cvPanel = new javax.swing.JPanel (); 104 selectedLbl = new javax.swing.JLabel (); 105 jScrollPane1 = new javax.swing.JScrollPane (); 106 descriptionPane = new javax.swing.JTextPane (); 107 108 cvPanel.setLayout(new java.awt.BorderLayout ()); 109 110 selectedLbl.setLabelFor(descriptionPane); 111 org.openide.awt.Mnemonics.setLocalizedText(selectedLbl, org.openide.util.NbBundle.getMessage(SchemaComponentSelectionPanel.class, "LBL_Currently_Selected", new Object [] {"", ""})); 112 selectedLbl.setToolTipText(org.openide.util.NbBundle.getMessage(SchemaComponentSelectionPanel.class, "HINT_Currently_Selected")); 113 114 jScrollPane1.setBorder(null); 115 jScrollPane1.setMinimumSize(new java.awt.Dimension (20, 28)); 116 descriptionPane.setBorder(null); 117 descriptionPane.setEditable(false); 118 descriptionPane.setBackground(getBackground()); 119 jScrollPane1.setViewportView(descriptionPane); 120 121 org.jdesktop.layout.GroupLayout typeSelectionPanelLayout = new org.jdesktop.layout.GroupLayout(typeSelectionPanel); 122 typeSelectionPanel.setLayout(typeSelectionPanelLayout); 123 typeSelectionPanelLayout.setHorizontalGroup( 124 typeSelectionPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 125 .add(typeSelectionPanelLayout.createSequentialGroup() 126 .add(selectedLbl) 127 .addContainerGap()) 128 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE) 129 .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE) 130 ); 131 typeSelectionPanelLayout.setVerticalGroup( 132 typeSelectionPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 133 .add(typeSelectionPanelLayout.createSequentialGroup() 134 .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) 135 .add(6, 6, 6) 136 .add(selectedLbl) 137 .add(0, 0, 0) 138 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)) 139 ); 140 141 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 142 this.setLayout(layout); 143 layout.setHorizontalGroup( 144 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 145 .add(layout.createSequentialGroup() 146 .addContainerGap() 147 .add(typeSelectionPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 148 .addContainerGap()) 149 ); 150 layout.setVerticalGroup( 151 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 152 .add(layout.createSequentialGroup() 153 .addContainerGap() 154 .add(typeSelectionPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 155 .addContainerGap()) 156 ); 157 } 159 private void initialize(final T initialSelection) { 160 ArrayList <Class <? extends SchemaComponent>> childTypes = 161 new ArrayList <Class <? extends SchemaComponent>>(); 162 childTypes.add(SchemaModelReference.class); 163 childTypes.add(type); 164 if(includePrimitives&&!type.isAssignableFrom(PrimitiveSimpleType.class)) 165 childTypes.add(PrimitiveSimpleType.class); 166 CategorizedSchemaNodeFactory factory = new CategorizedSchemaNodeFactory( 167 model, childTypes, Lookup.EMPTY); 168 final Node rootNode = factory.createRootNode(); 169 typeView = new BeanTreeView(); 171 typeView.setBorder(BasicBorders.getTextFieldBorder()); 172 typeView.setPopupAllowed(false); 173 typeView.setDefaultActionAllowed(false); 174 typeView.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 175 typeView.setRootVisible(false); 176 typeView.getAccessibleContext().setAccessibleName( 177 org.openide.util.NbBundle.getMessage( 178 SchemaComponentSelectionPanel.class, "LBL_GlobalReferenceTree")); 179 typeView.getAccessibleContext().setAccessibleDescription( 180 org.openide.util.NbBundle.getMessage( 181 SchemaComponentSelectionPanel.class, "LBL_GlobalReferenceTree")); 182 cvPanel.add(typeView, BorderLayout.CENTER); 183 getExplorerManager().setRootContext(rootNode); 184 if(initialSelection!=null) { 185 EventQueue.invokeLater(new Runnable () { 186 public void run() { 187 setInitialSelection(initialSelection); 188 } 189 }); 190 } 191 } 192 193 public void setInitialSelection(T initialSelection) { 194 currentSelection = initialSelection; 195 if(currentSelection!=null) { 196 Node node = UIUtilities.findNode(getExplorerManager(). 197 getRootContext(),currentSelection, model); 198 if(node!=null) { 199 setDescription(node); 200 try { 201 getExplorerManager().setSelectedNodes(new Node[]{node}); 202 } catch (PropertyVetoException ex) { 203 } 204 } 205 } 206 } 207 208 public void propertyChange(PropertyChangeEvent evt) { 209 if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) { 210 Node[] nodes = getExplorerManager().getSelectedNodes(); 211 if (nodes.length > 0) { 212 SchemaComponentNode<T> scn = findReferenceableNode(nodes[0]); 213 setCurrentSelection(scn==null?null:scn.getReference().get()); 214 setDescription(scn); 215 return; 216 } 217 setCurrentSelection(null); 218 } 219 } 220 221 private void setCurrentSelection(T sc) { 222 T oldValue = currentSelection; 223 currentSelection = sc==null||exclude.contains(sc)?null:sc; 224 firePropertyChange(PROPERTY_SELECTION,oldValue,currentSelection); 225 } 226 227 public T getCurrentSelection() { 228 return currentSelection; 229 } 230 231 public ExplorerManager getExplorerManager() { 232 return ((EmbededPanel)getTypeSelectionPanel()).getExplorerManager(); 233 } 234 235 public void setEnabled(boolean flag) { 236 super.setEnabled(flag); 237 typeView.setEnabled(flag); 238 selectedLbl.setEnabled(flag); 239 descriptionPane.setEnabled(flag); 240 } 241 242 public JPanel getTypeSelectionPanel() { 243 return typeSelectionPanel; 244 } 245 246 @SuppressWarnings ("unchecked") 247 private SchemaComponentNode<T> findReferenceableNode(Node node) { 248 Node parent = node; 249 SchemaComponentNode scn = null; 250 while(parent!=null) { 251 scn = (SchemaComponentNode)parent 252 .getCookie(SchemaComponentNode.class); 253 if(scn!=null && type.isInstance(scn.getReference().get())) 254 return (SchemaComponentNode<T>)scn; 255 parent=parent.getParentNode(); 256 } 257 return null; 258 } 259 260 private void setDescription(Node selected) { 261 String arg0 = ""; 262 String arg1 = ""; 263 if(currentSelection!=null) { 264 arg0 = currentSelection.getName(); 265 SchemaComponentNode scn = (SchemaComponentNode)selected. 266 getCookie(SchemaComponentNode.class); 267 arg1 = scn==null?"":"("+scn.getTypeDisplayName()+")"; 268 } 269 selectedLbl.setText(NbBundle.getMessage( 270 SchemaComponentSelectionPanel.class, 271 "LBL_Currently_Selected", new Object [] {arg0,arg1})); 272 if(selected!=null&&selected.getShortDescription()!=null) 273 descriptionPane.setText(selected.getShortDescription()); 274 else 275 descriptionPane.setText(""); 276 descriptionPane.setCaretPosition(0); 277 } 278 279 public javax.swing.JPanel cvPanel; 281 private javax.swing.JTextPane descriptionPane; 282 private javax.swing.JScrollPane jScrollPane1; 283 private javax.swing.JLabel selectedLbl; 284 public javax.swing.JPanel typeSelectionPanel; 285 287 private static class EmbededPanel extends JPanel 288 implements ExplorerManager.Provider { 289 private static final long serialVersionUID = 1L; 290 private ExplorerManager explorerManager; 291 private PropertyChangeListener listener; 292 public EmbededPanel(PropertyChangeListener listener) { 293 super(); 294 this.listener = listener; 295 explorerManager = new ExplorerManager(); 296 } 297 public ExplorerManager getExplorerManager() { 298 return explorerManager; 299 } 300 301 public void removeNotify() { 302 super.removeNotify(); 303 getExplorerManager().removePropertyChangeListener(listener); 304 } 305 306 public void addNotify() { 307 super.addNotify(); 308 getExplorerManager().addPropertyChangeListener(listener); 309 } 310 } 311 } 312 | Popular Tags |