1 19 20 package org.netbeans.core.windows.view.ui.toolbars; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Cursor ; 24 import java.awt.Dialog ; 25 import java.awt.Dimension ; 26 import java.awt.datatransfer.DataFlavor ; 27 import java.awt.datatransfer.Transferable ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.io.IOException ; 31 import java.lang.ref.WeakReference ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import javax.swing.Action ; 35 import javax.swing.BorderFactory ; 36 import javax.swing.JButton ; 37 import javax.swing.JLabel ; 38 import javax.swing.JScrollPane ; 39 import javax.swing.JSeparator ; 40 import javax.swing.SwingUtilities ; 41 import org.netbeans.core.windows.services.ToolbarFolderNode; 42 import org.openide.DialogDescriptor; 43 import org.openide.DialogDisplayer; 44 import org.openide.awt.Actions; 45 import org.openide.awt.ToolbarPool; 46 import org.openide.cookies.InstanceCookie; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileSystem; 49 import org.openide.filesystems.Repository; 50 import org.openide.loaders.DataFilter; 51 import org.openide.loaders.DataFolder; 52 import org.openide.loaders.DataObject; 53 import org.openide.loaders.DataObjectNotFoundException; 54 import org.openide.nodes.AbstractNode; 55 import org.openide.nodes.FilterNode; 56 import org.openide.nodes.Node; 57 import org.openide.util.Exceptions; 58 import org.openide.util.NbBundle; 59 import org.openide.util.datatransfer.ExTransferable; 60 import org.openide.util.datatransfer.NewType; 61 62 68 public class ConfigureToolbarPanel extends javax.swing.JPanel implements Runnable { 69 70 private static WeakReference <Dialog > dialogRef; 72 private Node root; 73 74 75 private ConfigureToolbarPanel() { 76 initComponents(); 77 78 setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) ); 79 80 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 81 FileObject paletteFolder = fs.findResource( "Actions" ); DataFolder df = DataFolder.findFolder( paletteFolder ); 83 root = new FolderActionNode( new AbstractNode( df.createNodeChildren( new ActionIconDataFilter() ) ) ); 84 85 final JLabel lblWait = new JLabel ( getBundleString("LBL_PleaseWait") ); 86 lblWait.setHorizontalAlignment( JLabel.CENTER ); 87 palettePanel.setPreferredSize( new Dimension ( 440, 350 ) ); 88 palettePanel.add( lblWait ); 89 getAccessibleContext().setAccessibleDescription( getBundleString("ACSD_ToolbarCustomizer") ); 90 } 91 92 public void run() { 93 ActionsTree tree = new ActionsTree( root ); 94 tree.getAccessibleContext().setAccessibleDescription( getBundleString("ACSD_ActionsTree") ); 95 tree.getAccessibleContext().setAccessibleName( getBundleString("ACSN_ActionsTree") ); 96 palettePanel.removeAll(); 97 palettePanel.setBorder( BorderFactory.createEmptyBorder() ); 98 JScrollPane scrollPane = new JScrollPane (tree); 99 scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleName( getBundleString("ACSN_ActionsScrollBar") ); 100 scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleDescription( getBundleString("ACSD_ActionsScrollBar") ); 101 palettePanel.add( scrollPane, BorderLayout.CENTER ); 102 lblHint.setLabelFor( tree ); 103 invalidate(); 104 validate(); 105 repaint(); 106 setCursor( Cursor.getDefaultCursor() ); 107 } 108 109 public static void showConfigureDialog() { 110 java.awt.Dialog dialog = null; 111 if (dialogRef != null) 112 dialog = dialogRef.get(); 113 if (dialog == null) { 114 JButton closeButton = new JButton (); 115 closeButton.getAccessibleContext().setAccessibleDescription( getBundleString("ACSD_Close") ); 116 org.openide.awt.Mnemonics.setLocalizedText( 117 closeButton, getBundleString("CTL_Close")); 118 DialogDescriptor dd = new DialogDescriptor( 119 new ConfigureToolbarPanel(), 120 getBundleString("CustomizerTitle"), 121 false, 122 new Object [] { closeButton }, 123 closeButton, 124 DialogDescriptor.DEFAULT_ALIGN, 125 null, 126 null); 127 dialog = DialogDisplayer.getDefault().createDialog(dd); 128 dialogRef = new WeakReference <Dialog >(dialog); 129 } 130 dialog.addWindowListener( new WindowAdapter () { 131 public void windowClosed(WindowEvent e) { 132 endToolbarEditMode(); 133 } 134 }); 135 dialog.setVisible(true); 136 startToolbarEditMode(); 137 } 138 139 static void startToolbarEditMode() { 140 ToolbarPool.getDefault().putClientProperty( "editMode", new Object () ); } 142 143 static void endToolbarEditMode() { 144 ToolbarPool.getDefault().putClientProperty( "editMode", null ); DataFolder folder = ToolbarPool.getDefault().getFolder(); 147 DataObject[] children = folder.getChildren(); 148 for( int i=0; i<children.length; i++ ) { 149 final DataFolder subFolder = (DataFolder)children[i].getCookie( DataFolder.class ); 150 if( null != subFolder && subFolder.getChildren().length == 0 ) { 151 SwingUtilities.invokeLater( new Runnable () { 152 153 public void run() { 154 try { 155 subFolder.delete(); 156 } 157 catch (IOException e) { 158 Logger.getLogger(ConfigureToolbarPanel.class.getName()).log(Level.WARNING, null, e); 159 } 160 } 161 }); 162 } 163 } 164 } 165 166 167 static final String getBundleString (String bundleStr) { 168 return NbBundle.getMessage(ConfigureToolbarPanel.class, bundleStr); 169 } 170 171 private boolean firstTimeInit = true; 172 public void paint(java.awt.Graphics g) { 173 super.paint(g); 174 if( firstTimeInit ) { 175 firstTimeInit = false; 179 SwingUtilities.invokeLater( new Runnable () { 180 public void run() { 181 Node[] categories = root.getChildren().getNodes( true ); 183 for( int i=0; i<categories.length; i++ ) { 184 categories[i].getChildren().getNodes( true ); 185 } 186 SwingUtilities.invokeLater( ConfigureToolbarPanel.this ); 188 } 189 }); 190 } 191 } 192 193 198 private void initComponents() { 200 java.awt.GridBagConstraints gridBagConstraints; 201 202 lblHint = new javax.swing.JLabel (); 203 palettePanel = new javax.swing.JPanel (); 204 checkSmallIcons = new javax.swing.JCheckBox (); 205 btnNewToolbar = new javax.swing.JButton (); 206 btnReset = new javax.swing.JButton (); 207 208 FormListener formListener = new FormListener(); 209 210 setLayout(new java.awt.GridBagLayout ()); 211 212 setMinimumSize(new java.awt.Dimension (453, 68)); 213 org.openide.awt.Mnemonics.setLocalizedText(lblHint, getBundleString("CTL_TreeLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.gridwidth = 2; 216 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 217 gridBagConstraints.insets = new java.awt.Insets (10, 10, 1, 10); 218 add(lblHint, gridBagConstraints); 219 220 palettePanel.setLayout(new java.awt.BorderLayout ()); 221 222 palettePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 223 gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridx = 0; 225 gridBagConstraints.gridy = 1; 226 gridBagConstraints.gridwidth = 3; 227 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 228 gridBagConstraints.weightx = 1.0; 229 gridBagConstraints.weighty = 1.0; 230 gridBagConstraints.insets = new java.awt.Insets (1, 10, 5, 10); 231 add(palettePanel, gridBagConstraints); 232 233 org.openide.awt.Mnemonics.setLocalizedText(checkSmallIcons, getBundleString("CTL_SmallIcons")); checkSmallIcons.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 235 checkSmallIcons.setMargin(new java.awt.Insets (0, 0, 0, 0)); 236 checkSmallIcons.setSelected( ToolbarPool.getDefault().getPreferredIconSize() == 16 ); 237 checkSmallIcons.addActionListener(formListener); 238 239 gridBagConstraints = new java.awt.GridBagConstraints (); 240 gridBagConstraints.gridx = 1; 241 gridBagConstraints.gridy = 2; 242 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 243 gridBagConstraints.insets = new java.awt.Insets (10, 10, 0, 10); 244 add(checkSmallIcons, gridBagConstraints); 245 checkSmallIcons.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SmallIcons")); 247 org.openide.awt.Mnemonics.setLocalizedText(btnNewToolbar, getBundleString("CTL_NewToolbar")); btnNewToolbar.addActionListener(formListener); 249 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.gridx = 0; 252 gridBagConstraints.gridy = 2; 253 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 254 gridBagConstraints.insets = new java.awt.Insets (10, 10, 0, 10); 255 add(btnNewToolbar, gridBagConstraints); 256 btnNewToolbar.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_NewToolbar")); 258 org.openide.awt.Mnemonics.setLocalizedText(btnReset, getBundleString("CTL_ResetToolbarsButton")); btnReset.addActionListener(formListener); 260 261 gridBagConstraints = new java.awt.GridBagConstraints (); 262 gridBagConstraints.gridx = 2; 263 gridBagConstraints.gridy = 2; 264 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 265 gridBagConstraints.insets = new java.awt.Insets (10, 10, 0, 10); 266 add(btnReset, gridBagConstraints); 267 268 } 269 270 272 private class FormListener implements java.awt.event.ActionListener { 273 FormListener() {} 274 public void actionPerformed(java.awt.event.ActionEvent evt) { 275 if (evt.getSource() == checkSmallIcons) { 276 ConfigureToolbarPanel.this.switchIconSize(evt); 277 } 278 else if (evt.getSource() == btnNewToolbar) { 279 ConfigureToolbarPanel.this.newToolbar(evt); 280 } 281 else if (evt.getSource() == btnReset) { 282 ConfigureToolbarPanel.this.resetToolbars(evt); 283 } 284 } 285 } 287 private void resetToolbars(java.awt.event.ActionEvent evt) { new ResetToolbarsAction().actionPerformed( evt ); 289 } 291 private void newToolbar(java.awt.event.ActionEvent evt) { ToolbarFolderNode tf = new ToolbarFolderNode(); 293 NewType[] newTypes = tf.getNewTypes(); 294 if( null != newTypes && newTypes.length > 0 ) { 295 try { 296 newTypes[0].create(); 297 } catch (IOException e) { 298 Exceptions.printStackTrace(e); 299 } 300 } 301 } 303 private void switchIconSize(java.awt.event.ActionEvent evt) { boolean state = checkSmallIcons.isSelected(); 305 if (state) { 306 ToolbarPool.getDefault().setPreferredIconSize(16); 307 } else { 308 ToolbarPool.getDefault().setPreferredIconSize(24); 309 } 310 String name = ToolbarPool.getDefault().getConfiguration(); 313 ToolbarConfiguration tbConf = ToolbarConfiguration.findConfiguration(name); 314 if (tbConf != null) { 315 tbConf.refresh(); 316 } 317 } 319 320 private javax.swing.JButton btnNewToolbar; 322 private javax.swing.JButton btnReset; 323 private javax.swing.JCheckBox checkSmallIcons; 324 private javax.swing.JLabel lblHint; 325 private javax.swing.JPanel palettePanel; 326 328 private static class FolderActionNode extends FilterNode { 329 public FolderActionNode( Node original ) { 330 super( original, new MyChildren( original ) ); 331 } 332 333 public String getDisplayName() { 334 return Actions.cutAmpersand( super.getDisplayName() ); 335 } 336 337 public Transferable drag() throws IOException { 338 return Node.EMPTY.drag(); 339 } 340 341 public Transferable clipboardCut() throws IOException { 342 return Node.EMPTY.clipboardCut(); 343 } 344 345 public Transferable clipboardCopy() throws IOException { 346 return Node.EMPTY.clipboardCopy(); 347 } 348 349 private static class MyChildren extends FilterNode.Children { 350 351 public MyChildren(Node original) { 352 super(original); 353 } 354 355 protected Node copyNode(Node node) { 356 DataFolder df = (DataFolder)node.getCookie( DataFolder.class ); 357 if( null == df ) 358 return new ItemActionNode( node ); 359 return new FolderActionNode( node ); 360 } 361 } 362 } 363 364 private static class ItemActionNode extends FilterNode { 365 366 private static DataFlavor nodeDataFlavor = new DataFlavor ( Node.class, "Action Node" ); 368 public ItemActionNode( Node original ) { 369 super( original, Children.LEAF ); 370 } 371 372 public Transferable drag() throws IOException { 373 return new ExTransferable.Single( nodeDataFlavor ) { 374 public Object getData() { 375 return ItemActionNode.this; 376 } 377 }; 378 } 379 380 public String getDisplayName() { 381 return Actions.cutAmpersand( super.getDisplayName() ); 382 } 383 } 384 385 388 private static class ActionIconDataFilter implements DataFilter { 389 private InstanceCookie instanceCookie; 390 391 public boolean acceptDataObject( DataObject obj ) { 392 instanceCookie = (InstanceCookie)obj.getCookie( InstanceCookie.class ); 393 if( null != instanceCookie ) { 394 try { 395 Object instance = instanceCookie.instanceCreate(); 396 if( null != instance ) { 397 if( instance instanceof Action ) { 398 Action action = (Action )instance; 399 try { 400 if( null == action.getValue( "iconBase" ) ) { 401 return false; 402 } 403 } catch( AssertionError aE ) { 404 } 407 } else if( instance instanceof JSeparator ) { 408 return false; 409 } 410 } 411 } catch( Throwable e ) { 412 Logger.getLogger(ConfigureToolbarPanel.class.getName()).log(Level.WARNING, null, e); 413 } 414 return true; 415 } else { 416 FileObject fo = obj.getPrimaryFile(); 417 if( fo.isFolder() ) { 418 boolean hasChildWithIcon = false; 419 FileObject[] children = fo.getChildren(); 420 for( int i=0; i<children.length; i++ ) { 421 DataObject child = null; 422 try { 423 child = DataObject.find( children[i] ); 424 } catch (DataObjectNotFoundException e) { 425 continue; 426 } 427 if( null != child && acceptDataObject( child ) ) { 428 hasChildWithIcon = true; 429 break; 430 } 431 } 432 return hasChildWithIcon; 433 } 434 } 435 return true; 436 } 437 } 438 } 439 | Popular Tags |