1 19 20 package org.netbeans.modules.palette.ui; 21 22 import java.awt.Dialog ; 23 import javax.swing.*; 24 import javax.swing.text.DefaultEditorKit ; 25 import java.beans.*; 26 import org.netbeans.spi.palette.PaletteActions; 27 import org.netbeans.spi.palette.PaletteController; 28 import org.openide.awt.Mnemonics; 29 30 import org.openide.util.*; 31 import org.openide.explorer.*; 32 import org.openide.explorer.view.BeanTreeView; 33 import org.openide.*; 34 import org.openide.nodes.*; 35 import org.netbeans.modules.palette.*; 36 37 38 44 45 public class Customizer extends JPanel implements ExplorerManager.Provider, 46 Lookup.Provider 47 { 48 private ExplorerManager explorerManager; 49 private Lookup lookup; 50 51 private Node root; 52 private PaletteController controller; 53 private Settings settings; 54 55 private JButton[] customButtons; 56 57 59 64 public static void show( Node paletteRoot, PaletteController controller, Settings settings ) { 65 JButton closeButton = new JButton(); 66 org.openide.awt.Mnemonics.setLocalizedText( 67 closeButton, Utils.getBundleString("CTL_Close_Button")); closeButton.getAccessibleContext().setAccessibleDescription( Utils.getBundleString("ACSD_Close") ); 69 DialogDescriptor dd = new DialogDescriptor( 70 new Customizer( paletteRoot, controller, settings ), 71 Utils.getBundleString("CTL_Customizer_Title"), false, 73 new Object [] { closeButton }, 74 closeButton, 75 DialogDescriptor.DEFAULT_ALIGN, 76 null, 77 null); 78 Dialog dialog = DialogDisplayer.getDefault().createDialog(dd); 79 dialog.setVisible(true); 80 } 81 82 83 public Customizer( Node paletteRoot, PaletteController controller, Settings settings ) { 84 this.root = paletteRoot; 85 this.controller = controller; 86 this.settings = settings; 87 explorerManager = new ExplorerManager(); 88 89 ActionMap map = getActionMap(); 90 map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(explorerManager)); 91 map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager)); 92 map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager)); 93 map.put("delete", ExplorerUtils.actionDelete(explorerManager, true)); 95 lookup = ExplorerUtils.createLookup(explorerManager, map); 96 97 explorerManager.setRootContext(paletteRoot); 98 99 initComponents(); 100 101 CheckTreeView treeView = new CheckTreeView( settings ); 102 treeView.getAccessibleContext().setAccessibleName( 103 Utils.getBundleString("ACSN_PaletteContentsTree")); treeView.getAccessibleContext().setAccessibleDescription( 105 Utils.getBundleString("ACSD_PaletteContentsTree")); infoLabel.setLabelFor( treeView ); 107 treePanel.add(treeView, java.awt.BorderLayout.CENTER); 108 captionLabel.setLabelFor(treeView); 109 110 explorerManager.addPropertyChangeListener(new PropertyChangeListener() { 111 public void propertyChange(PropertyChangeEvent ev) { 112 if (ExplorerManager.PROP_SELECTED_NODES.equals(ev.getPropertyName())) { 113 updateInfoLabel(explorerManager.getSelectedNodes()); 114 updateButtons(); 115 } 116 } 117 }); 118 updateButtons(); 119 } 120 121 public void addNotify() { 122 super.addNotify(); 123 ExplorerUtils.activateActions(explorerManager, true); 124 } 125 126 public void removeNotify() { 127 ExplorerUtils.activateActions(explorerManager, false); 128 super.removeNotify(); 129 } 130 131 public ExplorerManager getExplorerManager() { 133 return explorerManager; 134 } 135 136 public Lookup getLookup() { 138 return lookup; 139 } 140 141 private void updateButtons() { 142 Node[] selNodes = explorerManager.getSelectedNodes(); 143 boolean canRemove = null != selNodes && selNodes.length > 0; 144 boolean canMoveUp = null != selNodes && selNodes.length == 1; 145 boolean canMoveDown = null != selNodes && selNodes.length == 1; 146 147 for( int i=0; null != selNodes && i<selNodes.length; i++ ) { 148 Node node = selNodes[i]; 149 if( !node.canDestroy() ) 150 canRemove = false; 151 152 Node parent = node.getParentNode(); 153 if( null == parent || movePossible( node, parent, true ) < 0 ) 154 canMoveUp = false; 155 if( null == parent || movePossible( node, parent, false ) < 0 ) 156 canMoveDown = false; 157 } 158 removeButton.setEnabled( canRemove ); 159 moveUpButton.setEnabled( canMoveUp ); 160 moveDownButton.setEnabled( canMoveDown ); 161 } 162 164 169 private void initComponents() { 171 java.awt.GridBagConstraints gridBagConstraints; 172 173 captionLabel = new javax.swing.JLabel (); 174 treePanel = new javax.swing.JPanel (); 175 infoLabel = new javax.swing.JLabel (); 176 moveUpButton = new javax.swing.JButton (); 177 moveDownButton = new javax.swing.JButton (); 178 removeButton = new javax.swing.JButton (); 179 newCategoryButton = new javax.swing.JButton (); 180 customActionsPanel = new javax.swing.JPanel (); 181 resetButton = new javax.swing.JButton (); 182 183 setLayout(new java.awt.GridBagLayout ()); 184 185 getAccessibleContext().setAccessibleDescription(null); org.openide.awt.Mnemonics.setLocalizedText(captionLabel, Utils.getBundleString("CTL_Caption")); gridBagConstraints = new java.awt.GridBagConstraints (); 188 gridBagConstraints.gridwidth = 2; 189 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 190 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 191 gridBagConstraints.insets = new java.awt.Insets (6, 10, 0, 10); 192 add(captionLabel, gridBagConstraints); 193 194 treePanel.setLayout(new java.awt.BorderLayout ()); 195 196 treePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 197 treePanel.setPreferredSize(new java.awt.Dimension (288, 336)); 198 gridBagConstraints = new java.awt.GridBagConstraints (); 199 gridBagConstraints.gridx = 0; 200 gridBagConstraints.gridy = 1; 201 gridBagConstraints.gridheight = 7; 202 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 203 gridBagConstraints.weightx = 1.0; 204 gridBagConstraints.weighty = 1.0; 205 gridBagConstraints.insets = new java.awt.Insets (6, 10, 0, 0); 206 add(treePanel, gridBagConstraints); 207 208 infoLabel.setText(" "); 209 gridBagConstraints = new java.awt.GridBagConstraints (); 210 gridBagConstraints.gridx = 0; 211 gridBagConstraints.gridy = 8; 212 gridBagConstraints.gridwidth = 2; 213 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 214 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 215 gridBagConstraints.insets = new java.awt.Insets (10, 10, 0, 10); 216 add(infoLabel, gridBagConstraints); 217 218 org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, Utils.getBundleString("CTL_MoveUp_Button")); moveUpButton.addActionListener(new java.awt.event.ActionListener () { 220 public void actionPerformed(java.awt.event.ActionEvent evt) { 221 moveUpButtonActionPerformed(evt); 222 } 223 }); 224 225 gridBagConstraints = new java.awt.GridBagConstraints (); 226 gridBagConstraints.gridx = 1; 227 gridBagConstraints.gridy = 2; 228 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 229 gridBagConstraints.insets = new java.awt.Insets (28, 12, 0, 10); 230 add(moveUpButton, gridBagConstraints); 231 moveUpButton.getAccessibleContext().setAccessibleDescription(null); 233 org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, Utils.getBundleString("CTL_MoveDown_Button")); moveDownButton.addActionListener(new java.awt.event.ActionListener () { 235 public void actionPerformed(java.awt.event.ActionEvent evt) { 236 moveDownButtonActionPerformed(evt); 237 } 238 }); 239 240 gridBagConstraints = new java.awt.GridBagConstraints (); 241 gridBagConstraints.gridx = 1; 242 gridBagConstraints.gridy = 3; 243 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 244 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 10); 245 add(moveDownButton, gridBagConstraints); 246 moveDownButton.getAccessibleContext().setAccessibleDescription(null); 248 org.openide.awt.Mnemonics.setLocalizedText(removeButton, Utils.getBundleString("CTL_Remove_Button")); removeButton.addActionListener(new java.awt.event.ActionListener () { 250 public void actionPerformed(java.awt.event.ActionEvent evt) { 251 removeButtonActionPerformed(evt); 252 } 253 }); 254 255 gridBagConstraints = new java.awt.GridBagConstraints (); 256 gridBagConstraints.gridx = 1; 257 gridBagConstraints.gridy = 4; 258 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 259 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 10); 260 add(removeButton, gridBagConstraints); 261 removeButton.getAccessibleContext().setAccessibleDescription(null); 263 org.openide.awt.Mnemonics.setLocalizedText(newCategoryButton, Utils.getBundleString("CTL_NewCategory_Button")); newCategoryButton.addActionListener(new java.awt.event.ActionListener () { 265 public void actionPerformed(java.awt.event.ActionEvent evt) { 266 newCategoryButtonActionPerformed(evt); 267 } 268 }); 269 270 gridBagConstraints = new java.awt.GridBagConstraints (); 271 gridBagConstraints.gridx = 1; 272 gridBagConstraints.gridy = 5; 273 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 274 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 10); 275 add(newCategoryButton, gridBagConstraints); 276 newCategoryButton.getAccessibleContext().setAccessibleDescription(null); 278 customActionsPanel.setLayout(new java.awt.GridBagLayout ()); 279 280 createCustomButtons(); 281 gridBagConstraints = new java.awt.GridBagConstraints (); 282 gridBagConstraints.gridx = 1; 283 gridBagConstraints.gridy = 1; 284 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 285 add(customActionsPanel, gridBagConstraints); 286 287 org.openide.awt.Mnemonics.setLocalizedText(resetButton, Utils.getBundleString("CTL_ResetPalette")); resetButton.setActionCommand("Reset Palette"); 289 resetButton.addActionListener(new java.awt.event.ActionListener () { 290 public void actionPerformed(java.awt.event.ActionEvent evt) { 291 resetButtonActionPerformed(evt); 292 } 293 }); 294 295 gridBagConstraints = new java.awt.GridBagConstraints (); 296 gridBagConstraints.gridx = 1; 297 gridBagConstraints.gridy = 6; 298 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 299 gridBagConstraints.insets = new java.awt.Insets (28, 12, 0, 10); 300 add(resetButton, gridBagConstraints); 301 resetButton.getAccessibleContext().setAccessibleName(Utils.getBundleString("ASCN_ResetPalette")); resetButton.getAccessibleContext().setAccessibleDescription(Utils.getBundleString("ASCD_ResetPalette")); 304 } 306 private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) { Utils.resetPalette( controller, settings ); 308 } 310 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Node[] selected = explorerManager.getSelectedNodes(); 312 if (selected.length == 0) 313 return; 314 315 if( selected.length == 1 && !selected[0].canDestroy() ) 316 return; 317 318 NotifyDescriptor desc = new NotifyDescriptor.Confirmation( 320 Utils.getBundleString("MSG_ConfirmPaletteDelete"), Utils.getBundleString("CTL_ConfirmDeleteTitle"), NotifyDescriptor.YES_NO_OPTION); 323 324 if (NotifyDescriptor.YES_OPTION.equals( 325 DialogDisplayer.getDefault().notify(desc))) 326 { 327 try { 328 for (int i=0; i < selected.length; i++) { 329 if( selected[i].canDestroy() ) 330 selected[i].destroy(); 331 } 332 } 333 catch (java.io.IOException e) { 334 ErrorManager.getDefault().notify(e); 335 } 336 } 337 } 339 private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) { moveNode(false); 341 } 343 private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) { moveNode(true); 345 } 347 private void newCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) { new Utils.NewCategoryAction( root ).actionPerformed( evt ); 349 } 351 352 private javax.swing.JLabel captionLabel; 354 private javax.swing.JPanel customActionsPanel; 355 private javax.swing.JLabel infoLabel; 356 private javax.swing.JButton moveDownButton; 357 private javax.swing.JButton moveUpButton; 358 private javax.swing.JButton newCategoryButton; 359 private javax.swing.JButton removeButton; 360 private javax.swing.JButton resetButton; 361 private javax.swing.JPanel treePanel; 362 364 private void moveNode(boolean up) { 365 Node[] selected = explorerManager.getSelectedNodes(); 366 if (selected.length != 1) 367 return; 368 369 Node node = selected[0]; 370 Node parent = node.getParentNode(); 371 if (parent == null) 372 return; 373 374 Index indexCookie = (Index) parent.getCookie(Index.class); 375 if (indexCookie == null) 376 return; 377 378 int index = movePossible(node, parent, up); 379 if (index != -1) { 380 if (up) { 381 indexCookie.moveUp(index); 382 } else { 383 indexCookie.moveDown(index); 384 } 385 } 386 } 387 388 private static int movePossible(Node node, Node parentNode, boolean up) { 389 if (parentNode == null) 390 return -1; 391 392 Node[] nodes = parentNode.getChildren().getNodes(); 393 for (int i=0; i < nodes.length; i++) 394 if (nodes[i].getName().equals(node.getName())) 395 return (up && i > 0) || (!up && i+1 < nodes.length) ? i : -1; 396 397 return -1; 398 } 399 400 private void updateInfoLabel(org.openide.nodes.Node[] nodes) { 401 String text = " "; if (nodes.length == 1) { 403 Item item = nodes[0].getCookie( Item.class ); 404 if (item != null) 405 text = item.getShortDescription(); } 407 infoLabel.setText(text); 408 } 409 410 private void createCustomButtons() { 411 PaletteActions customActions = (PaletteActions)root.getLookup().lookup( PaletteActions.class ); 412 if( null == customActions ) 413 return; 414 415 Action[] actions = customActions.getImportActions(); 416 if( null == actions || actions.length == 0 ) 417 return; 418 419 customButtons = new JButton[actions.length]; 420 for( int i=0; i<actions.length; i++ ) { 421 customButtons[i] = new JButton( actions[i] ); 422 if( null != actions[i].getValue( Action.NAME ) ) 423 Mnemonics.setLocalizedText( customButtons[i], actions[i].getValue( Action.NAME ).toString() ); 424 if( null != actions[i].getValue( Action.LONG_DESCRIPTION ) ) 425 customButtons[i].getAccessibleContext().setAccessibleDescription( actions[i].getValue( Action.LONG_DESCRIPTION ).toString() ); 426 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 427 gridBagConstraints.gridx = 0; 428 gridBagConstraints.gridy = i; 429 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 430 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 10); 431 customActionsPanel.add( customButtons[i], gridBagConstraints); 432 } 433 } 434 435 436 private static class CheckTreeView extends BeanTreeView { 437 438 public CheckTreeView( Settings settings ) { 439 if( settings instanceof DefaultSettings ) { 440 CheckListener l = new CheckListener( (DefaultSettings)settings ); 441 tree.addMouseListener( l ); 442 tree.addKeyListener( l ); 443 444 CheckRenderer check = new CheckRenderer( (DefaultSettings)settings ); 445 tree.setCellRenderer( check ); 446 } 447 } 448 } 449 } 450 | Popular Tags |