1 19 package org.openide.explorer.propertysheet; 20 21 import org.openide.awt.Mnemonics; 22 import org.openide.explorer.ExplorerManager; 23 import org.openide.explorer.view.TreeTableView; 24 import org.openide.nodes.Node; 25 import org.openide.util.Lookup; 26 import org.openide.util.NbBundle; 27 import org.openide.util.actions.NodeAction; 28 import org.openide.util.actions.SystemAction; 29 30 import java.awt.Component ; 31 32 import java.beans.*; 33 34 import javax.swing.Action ; 35 import javax.swing.JPanel ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.Scrollable ; 38 import javax.swing.UIManager ; 39 import javax.swing.border.Border ; 40 41 42 46 class IndexedEditorPanel extends javax.swing.JPanel implements ExplorerManager.Provider, PropertyChangeListener, 47 Lookup.Provider { 48 private ExplorerManager em; 49 50 51 private Lookup selectedLookup; 52 private Action moveUp; 53 private Action moveDown; 54 private Action newAction; 55 private boolean showingDetails = false; 56 private Node rootNode; 57 private Node.Property prop; 58 private JScrollPane jScrollPane1 = new JScrollPane (); 59 private JPanel detailsPanel = new JPanel (); 60 61 private javax.swing.JButton deleteButton; 63 private javax.swing.JButton detailsButton; 64 private javax.swing.JButton downButton; 65 private javax.swing.JPanel jPanel1; 66 private javax.swing.JPanel jPanel2; 67 private javax.swing.JButton newButton; 68 private javax.swing.JLabel propertiesLabel; 69 private javax.swing.JButton upButton; 70 private TreeTableView treeTableView1; 73 74 75 public IndexedEditorPanel(Node node, Node.Property[] props) { 76 treeTableView1 = new TreeTableView(); 77 78 setBorder((Border ) UIManager.get("Nb.ScrollPane.border")); initComponents(); 81 propertiesLabel.setLabelFor(treeTableView1); 82 83 jPanel2.setLayout(new java.awt.BorderLayout ()); 84 jPanel2.add(treeTableView1); 85 86 detailsPanel.setLayout(new java.awt.BorderLayout ()); 87 getExplorerManager().setRootContext(node); 88 89 rootNode = node; 90 prop = props[0]; 91 getExplorerManager().addPropertyChangeListener(this); 92 treeTableView1.setProperties(props); 93 treeTableView1.setRootVisible(false); 94 treeTableView1.setDefaultActionAllowed(false); 95 treeTableView1.setTreePreferredWidth(200); 96 97 node.addPropertyChangeListener(this); 98 99 try { 100 selectedLookup = org.openide.util.lookup.Lookups.proxy(this); 101 102 NodeAction globalMoveUp = SystemAction.get(Class.forName("org.openide.actions.MoveUpAction").asSubclass(NodeAction.class)); NodeAction globalMoveDown = SystemAction.get(Class.forName("org.openide.actions.MoveDownAction").asSubclass(NodeAction.class)); NodeAction globalNewAction = SystemAction.get(Class.forName("org.openide.actions.NewAction").asSubclass(NodeAction.class)); 106 moveUp = globalMoveUp.createContextAwareInstance(selectedLookup); 108 moveDown = globalMoveDown.createContextAwareInstance(selectedLookup); 109 newAction = globalNewAction.createContextAwareInstance(selectedLookup); 110 } catch (ClassNotFoundException cnfe) { 111 } 112 113 java.util.ResourceBundle bundle = NbBundle.getBundle(IndexedEditorPanel.class); 114 treeTableView1.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Properties")); 115 newButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_New")); 116 deleteButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Delete")); 117 upButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MoveUp")); 118 downButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MoveDown")); 119 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_IndexedEditorPanel")); 120 } 121 122 public void addNotify() { 123 super.addNotify(); 124 updateButtonState(); 125 } 126 127 129 public Lookup getLookup() { 130 Node[] arr = getExplorerManager().getSelectedNodes(); 131 132 return (arr.length == 1) ? arr[0].getLookup() : Lookup.EMPTY; 133 } 134 135 140 private void initComponents() { 142 java.awt.GridBagConstraints gridBagConstraints; 143 144 jPanel1 = new javax.swing.JPanel (); 145 newButton = new javax.swing.JButton (); 146 deleteButton = new javax.swing.JButton (); 147 upButton = new javax.swing.JButton (); 148 downButton = new javax.swing.JButton (); 149 detailsButton = new javax.swing.JButton (); 150 propertiesLabel = new javax.swing.JLabel (); 151 jPanel2 = new javax.swing.JPanel (); 152 153 FormListener formListener = new FormListener(); 154 155 setLayout(new java.awt.GridBagLayout ()); 156 157 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 12)); 158 jPanel1.setLayout(new java.awt.GridLayout (5, 1, 0, 5)); 159 160 org.openide.awt.Mnemonics.setLocalizedText(newButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_New")); 161 newButton.addActionListener(formListener); 162 163 jPanel1.add(newButton); 164 165 org.openide.awt.Mnemonics.setLocalizedText(deleteButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_Delete")); 166 deleteButton.addActionListener(formListener); 167 168 jPanel1.add(deleteButton); 169 170 org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_MoveUp")); 171 upButton.addActionListener(formListener); 172 173 jPanel1.add(upButton); 174 175 org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_MoveDown")); 176 downButton.addActionListener(formListener); 177 178 jPanel1.add(downButton); 179 180 org.openide.awt.Mnemonics.setLocalizedText(detailsButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails")); 181 detailsButton.addActionListener(formListener); 182 183 jPanel1.add(detailsButton); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 1; 187 gridBagConstraints.gridy = 1; 188 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 189 gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; 190 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 191 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 192 add(jPanel1, gridBagConstraints); 193 194 org.openide.awt.Mnemonics.setLocalizedText(propertiesLabel, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_Properties")); 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridx = 0; 197 gridBagConstraints.gridy = 0; 198 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 199 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 200 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 11); 201 add(propertiesLabel, gridBagConstraints); 202 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 0; 205 gridBagConstraints.gridy = 1; 206 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 207 gridBagConstraints.weightx = 1.0; 208 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 209 add(jPanel2, gridBagConstraints); 210 211 } 212 213 215 private class FormListener implements java.awt.event.ActionListener { 216 public void actionPerformed(java.awt.event.ActionEvent evt) { 217 if (evt.getSource() == newButton) { 218 IndexedEditorPanel.this.newButtonActionPerformed(evt); 219 } 220 else if (evt.getSource() == deleteButton) { 221 IndexedEditorPanel.this.deleteButtonActionPerformed(evt); 222 } 223 else if (evt.getSource() == upButton) { 224 IndexedEditorPanel.this.upButtonActionPerformed(evt); 225 } 226 else if (evt.getSource() == downButton) { 227 IndexedEditorPanel.this.downButtonActionPerformed(evt); 228 } 229 else if (evt.getSource() == detailsButton) { 230 IndexedEditorPanel.this.detailsButtonActionPerformed(evt); 231 } 232 } 233 } 235 private void detailsButtonActionPerformed(java.awt.event.ActionEvent evt) { showingDetails = !showingDetails; 237 238 if (showingDetails && !this.equals(detailsPanel.getParent())) { 239 initDetails(); 240 } 241 242 updateButtonState(); 243 updateDetailsPanel(); 244 } 245 247 private void newButtonActionPerformed(java.awt.event.ActionEvent evt) { 249 try { 250 getExplorerManager().setSelectedNodes(new Node[] { rootNode }); 251 } catch (PropertyVetoException pve) { 252 PropertyDialogManager.notify(pve); 254 } 255 256 if ((newAction != null) && (newAction.isEnabled())) { 257 newAction.actionPerformed(evt); 258 } 259 } 260 262 private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) { 264 Node[] sn = getExplorerManager().getSelectedNodes(); 265 266 if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) { 267 return; 268 } 269 270 try { 271 sn[0].destroy(); 272 } catch (java.io.IOException ioe) { 273 PropertyDialogManager.notify(ioe); 274 } 275 276 rootNode = getExplorerManager().getRootContext(); 277 } 278 280 private void downButtonActionPerformed(java.awt.event.ActionEvent evt) { 282 Node[] sn = getExplorerManager().getSelectedNodes(); 283 284 if ((moveDown != null) && (moveDown.isEnabled())) { 285 moveDown.actionPerformed(evt); 286 } 287 288 if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) { 289 return; 290 } 291 292 try { 293 getExplorerManager().setSelectedNodes(sn); 294 } catch (PropertyVetoException pve) { 295 } 296 } 297 299 private void upButtonActionPerformed(java.awt.event.ActionEvent evt) { 301 Node[] sn = getExplorerManager().getSelectedNodes(); 302 303 if ((moveUp != null) && (moveUp.isEnabled())) { 304 moveUp.actionPerformed(evt); 305 } 306 307 if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) { 308 return; 309 } 310 311 try { 312 getExplorerManager().setSelectedNodes(sn); 313 } catch (PropertyVetoException pve) { 314 } 315 } 316 318 public synchronized ExplorerManager getExplorerManager() { 319 if (em == null) { 320 em = new ExplorerManager(); 321 } 322 323 return em; 324 } 325 326 private void updateButtonState() { 327 selectedLookup.lookup(Object .class); 329 330 if (showingDetails) { 331 detailsButton.setText(NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails")); 332 } else { 333 detailsButton.setText(NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_ShowDetails")); 334 } 335 336 upButton.setEnabled((moveUp != null) && (moveUp.isEnabled())); 337 downButton.setEnabled((moveDown != null) && (moveDown.isEnabled())); 338 339 Node[] sn = getExplorerManager().getSelectedNodes(); 340 deleteButton.setEnabled((sn != null) && (sn.length == 1) && (sn[0] != rootNode)); 341 detailsButton.setVisible( 342 (prop != null) && (prop.getPropertyEditor() != null) && (prop.getPropertyEditor().supportsCustomEditor()) 343 ); 344 345 if (detailsButton.isVisible()) { 346 if (showingDetails) { 347 Mnemonics.setLocalizedText(detailsButton, NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails")); 348 detailsButton.getAccessibleContext().setAccessibleDescription( 349 NbBundle.getBundle(IndexedEditorPanel.class).getString("ACSD_HideDetails") 350 ); 351 } else { 352 Mnemonics.setLocalizedText(detailsButton, NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_ShowDetails")); 353 detailsButton.getAccessibleContext().setAccessibleDescription( 354 NbBundle.getBundle(IndexedEditorPanel.class).getString("ACSD_ShowDetails") 355 ); 356 } 357 358 detailsButton.setEnabled((sn != null) && (sn.length == 1) && (sn[0] != rootNode)); 359 } 360 } 361 362 private void updateDetailsPanel() { 363 detailsPanel.removeAll(); 364 365 if (!showingDetails) { 366 remove(detailsPanel); 367 revalidateDetailsPanel(); 368 369 return; 370 } 371 372 Node[] selN = getExplorerManager().getSelectedNodes(); 373 374 if ((selN == null) || (selN.length == 0)) { 375 revalidateDetailsPanel(); 376 377 return; 378 } 379 380 Node n = selN[0]; 381 382 if (n == rootNode) { 383 revalidateDetailsPanel(); 384 385 return; 386 } 387 388 if (selN.length > 1) { 389 n = new ProxyNode(selN); 390 } 391 392 Node.Property prop = n.getPropertySets()[0].getProperties()[0]; 396 PropertyPanel p = new PropertyPanel(prop); 397 p.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR); 398 399 if (isEditorScrollable(p)) { 400 detailsPanel.add(p, java.awt.BorderLayout.CENTER); 401 } else { 402 jScrollPane1.setViewportView(p); 403 detailsPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER); 404 } 405 406 revalidateDetailsPanel(); 407 } 408 409 private void revalidateDetailsPanel() { 410 detailsPanel.invalidate(); 411 repaint(); 412 413 if (detailsPanel.getParent() != null) { 414 detailsPanel.getParent().validate(); 415 } else { 416 detailsPanel.validate(); 417 } 418 } 419 420 425 public void propertyChange(PropertyChangeEvent evt) { 426 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 427 updateButtonState(); 428 updateDetailsPanel(); 429 } 430 } 431 432 private void initDetails() { 433 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 434 gridBagConstraints.gridx = 0; 435 gridBagConstraints.gridy = 2; 436 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 437 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 438 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 439 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 440 gridBagConstraints.weighty = 1.0; 441 add(detailsPanel, gridBagConstraints); 442 } 443 444 private boolean isEditorScrollable(PropertyPanel p) { 445 Component [] comps = p.getComponents(); 446 447 for (int i = 0; i < comps.length; i++) { 448 if (comps[i] instanceof Scrollable || isInstanceOfTopComponent(comps[i])) { 449 return true; 450 } 451 } 452 453 return false; 454 } 455 456 460 private static boolean isInstanceOfTopComponent(Object obj) { 461 ClassLoader l = org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 462 463 if (l == null) { 464 l = IndexedEditorPanel.class.getClassLoader(); 465 } 466 467 try { 468 Class c = Class.forName("org.openide.windows.TopComponent", true, l); 470 return c.isInstance(obj); 471 } catch (Exception ex) { 472 return false; 473 } 474 } 475 } 476 | Popular Tags |