KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > toolbars > ConfigureToolbarPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui.toolbars;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Cursor JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.datatransfer.DataFlavor JavaDoc;
27 import java.awt.datatransfer.Transferable JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.ref.WeakReference JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import javax.swing.Action JavaDoc;
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JSeparator JavaDoc;
40 import javax.swing.SwingUtilities JavaDoc;
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 /**
63  * Toolbar Customizer showing a tree of all available actions. Users can drag actions
64  * to toolbars to add new toolbar buttons.
65  *
66  * @author Stanislav Aubrecht
67  */

68 public class ConfigureToolbarPanel extends javax.swing.JPanel JavaDoc implements Runnable JavaDoc {
69     
70     private static WeakReference JavaDoc<Dialog JavaDoc> dialogRef; // is weak reference necessary?
71

72     private Node root;
73
74     /** Creates new form ConfigureToolbarPanel */
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" ); // NOI18N
82
DataFolder df = DataFolder.findFolder( paletteFolder );
83         root = new FolderActionNode( new AbstractNode( df.createNodeChildren( new ActionIconDataFilter() ) ) );
84
85         final JLabel JavaDoc lblWait = new JLabel JavaDoc( getBundleString("LBL_PleaseWait") );
86         lblWait.setHorizontalAlignment( JLabel.CENTER );
87         palettePanel.setPreferredSize( new Dimension JavaDoc( 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 JavaDoc scrollPane = new JScrollPane JavaDoc(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 JavaDoc dialog = null;
111         if (dialogRef != null)
112             dialog = dialogRef.get();
113         if (dialog == null) {
114             JButton JavaDoc closeButton = new JButton JavaDoc();
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 JavaDoc[] { closeButton },
123                 closeButton,
124                 DialogDescriptor.DEFAULT_ALIGN,
125                 null,
126                 null);
127             dialog = DialogDisplayer.getDefault().createDialog(dd);
128             dialogRef = new WeakReference JavaDoc<Dialog JavaDoc>(dialog);
129         }
130         dialog.addWindowListener( new WindowAdapter JavaDoc() {
131             public void windowClosed(WindowEvent JavaDoc e) {
132                 endToolbarEditMode();
133             }
134         });
135         dialog.setVisible(true);
136         startToolbarEditMode();
137     }
138     
139     static void startToolbarEditMode() {
140         ToolbarPool.getDefault().putClientProperty( "editMode", new Object JavaDoc() ); // NOI18N
141
}
142     
143     static void endToolbarEditMode() {
144         ToolbarPool.getDefault().putClientProperty( "editMode", null ); // NOI18N
145
//remove empty toolbars
146
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 JavaDoc() {
152
153                     public void run() {
154                         try {
155                             subFolder.delete();
156                         }
157                         catch (IOException JavaDoc e) {
158                             Logger.getLogger(ConfigureToolbarPanel.class.getName()).log(Level.WARNING, null, e);
159                         }
160                     }
161                 });
162             }
163         }
164     }
165     
166     /** @return returns string from bundle for given string pattern */
167     static final String JavaDoc getBundleString (String JavaDoc bundleStr) {
168         return NbBundle.getMessage(ConfigureToolbarPanel.class, bundleStr);
169     }
170
171     private boolean firstTimeInit = true;
172     public void paint(java.awt.Graphics JavaDoc g) {
173         super.paint(g);
174         if( firstTimeInit ) {
175             //this is not very nice but some Actions insist on being accessed
176
//from the event queue only so let's wait till the dialog window is
177
//painted before filtering out Actions without an icon
178
firstTimeInit = false;
179             SwingUtilities.invokeLater( new Runnable JavaDoc() {
180                 public void run() {
181                     //warm up action nodes so that 'expand all' in actions tree is fast
182
Node[] categories = root.getChildren().getNodes( true );
183                     for( int i=0; i<categories.length; i++ ) {
184                         categories[i].getChildren().getNodes( true );
185                     }
186                     //replace 'please wait' message with actions tree
187
SwingUtilities.invokeLater( ConfigureToolbarPanel.this );
188                 }
189             });
190         }
191     }
192     
193     /** This method is called from within the constructor to
194      * initialize the form.
195      * WARNING: Do NOT modify this code. The content of this method is
196      * always regenerated by the Form Editor.
197      */

198     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
199
private void initComponents() {
200         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
201
202         lblHint = new javax.swing.JLabel JavaDoc();
203         palettePanel = new javax.swing.JPanel JavaDoc();
204         checkSmallIcons = new javax.swing.JCheckBox JavaDoc();
205         btnNewToolbar = new javax.swing.JButton JavaDoc();
206         btnReset = new javax.swing.JButton JavaDoc();
207
208         FormListener formListener = new FormListener();
209
210         setLayout(new java.awt.GridBagLayout JavaDoc());
211
212         setMinimumSize(new java.awt.Dimension JavaDoc(453, 68));
213         org.openide.awt.Mnemonics.setLocalizedText(lblHint, getBundleString("CTL_TreeLabel")); // NOI18N
214
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridwidth = 2;
216         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
217         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 1, 10);
218         add(lblHint, gridBagConstraints);
219
220         palettePanel.setLayout(new java.awt.BorderLayout JavaDoc());
221
222         palettePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
223         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(1, 10, 5, 10);
231         add(palettePanel, gridBagConstraints);
232
233         org.openide.awt.Mnemonics.setLocalizedText(checkSmallIcons, getBundleString("CTL_SmallIcons")); // NOI18N
234
checkSmallIcons.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
235         checkSmallIcons.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
236         checkSmallIcons.setSelected( ToolbarPool.getDefault().getPreferredIconSize() == 16 );
237         checkSmallIcons.addActionListener(formListener);
238
239         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
240         gridBagConstraints.gridx = 1;
241         gridBagConstraints.gridy = 2;
242         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
243         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 0, 10);
244         add(checkSmallIcons, gridBagConstraints);
245         checkSmallIcons.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SmallIcons")); // NOI18N
246

247         org.openide.awt.Mnemonics.setLocalizedText(btnNewToolbar, getBundleString("CTL_NewToolbar")); // NOI18N
248
btnNewToolbar.addActionListener(formListener);
249
250         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
251         gridBagConstraints.gridx = 0;
252         gridBagConstraints.gridy = 2;
253         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
254         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 0, 10);
255         add(btnNewToolbar, gridBagConstraints);
256         btnNewToolbar.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_NewToolbar")); // NOI18N
257

258         org.openide.awt.Mnemonics.setLocalizedText(btnReset, getBundleString("CTL_ResetToolbarsButton")); // NOI18N
259
btnReset.addActionListener(formListener);
260
261         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
262         gridBagConstraints.gridx = 2;
263         gridBagConstraints.gridy = 2;
264         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
265         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 0, 10);
266         add(btnReset, gridBagConstraints);
267
268     }
269
270     // Code for dispatching events from components to event handlers.
271

272     private class FormListener implements java.awt.event.ActionListener JavaDoc {
273         FormListener() {}
274         public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
286

287     private void resetToolbars(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_resetToolbars
288
new ResetToolbarsAction().actionPerformed( evt );
289     }//GEN-LAST:event_resetToolbars
290

291     private void newToolbar(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_newToolbar
292
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 JavaDoc e) {
298                 Exceptions.printStackTrace(e);
299             }
300         }
301     }//GEN-LAST:event_newToolbar
302

303     private void switchIconSize(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_switchIconSize
304
boolean state = checkSmallIcons.isSelected();
305           if (state) {
306               ToolbarPool.getDefault().setPreferredIconSize(16);
307           } else {
308               ToolbarPool.getDefault().setPreferredIconSize(24);
309           }
310           //Rebuild toolbar panel
311
//#43652: Find current toolbar configuration
312
String JavaDoc name = ToolbarPool.getDefault().getConfiguration();
313           ToolbarConfiguration tbConf = ToolbarConfiguration.findConfiguration(name);
314           if (tbConf != null) {
315               tbConf.refresh();
316           }
317     }//GEN-LAST:event_switchIconSize
318

319     
320     // Variables declaration - do not modify//GEN-BEGIN:variables
321
private javax.swing.JButton JavaDoc btnNewToolbar;
322     private javax.swing.JButton JavaDoc btnReset;
323     private javax.swing.JCheckBox JavaDoc checkSmallIcons;
324     private javax.swing.JLabel JavaDoc lblHint;
325     private javax.swing.JPanel JavaDoc palettePanel;
326     // End of variables declaration//GEN-END:variables
327

328     private static class FolderActionNode extends FilterNode {
329         public FolderActionNode( Node original ) {
330             super( original, new MyChildren( original ) );
331         }
332
333         public String JavaDoc getDisplayName() {
334             return Actions.cutAmpersand( super.getDisplayName() );
335         }
336
337         public Transferable JavaDoc drag() throws IOException JavaDoc {
338             return Node.EMPTY.drag();
339         }
340
341         public Transferable JavaDoc clipboardCut() throws IOException JavaDoc {
342             return Node.EMPTY.clipboardCut();
343         }
344
345         public Transferable JavaDoc clipboardCopy() throws IOException JavaDoc {
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 JavaDoc nodeDataFlavor = new DataFlavor JavaDoc( Node.class, "Action Node" ); // NOI18N
367

368         public ItemActionNode( Node original ) {
369             super( original, Children.LEAF );
370         }
371
372         public Transferable JavaDoc drag() throws IOException JavaDoc {
373             return new ExTransferable.Single( nodeDataFlavor ) {
374                 public Object JavaDoc getData() {
375                    return ItemActionNode.this;
376                 }
377             };
378         }
379
380         public String JavaDoc getDisplayName() {
381             return Actions.cutAmpersand( super.getDisplayName() );
382         }
383     }
384     
385     /**
386      * A filter that does not allow Action instances without an icon.
387      */

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 JavaDoc instance = instanceCookie.instanceCreate();
396                     if( null != instance ) {
397                         if( instance instanceof Action JavaDoc ) {
398                             Action JavaDoc action = (Action JavaDoc)instance;
399                             try {
400                                 if( null == action.getValue( "iconBase" ) ) {
401                                     return false;
402                                 }
403                             } catch( AssertionError JavaDoc aE ) {
404                                 //hack: some action do not allow access outside
405
//event queue - so let's ignore their assertions
406
}
407                         } else if( instance instanceof JSeparator JavaDoc ) {
408                             return false;
409                         }
410                     }
411                 } catch( Throwable JavaDoc 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