KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > Utils


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
21 package org.netbeans.modules.palette;
22
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.datatransfer.*;
29 import java.io.IOException JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.concurrent.Callable JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import javax.swing.*;
36 import org.netbeans.spi.palette.PaletteController;
37 import org.netbeans.modules.palette.ui.PalettePanel;
38
39 import org.openide.*;
40 import org.openide.loaders.DataObject;
41 import org.openide.nodes.*;
42 import org.openide.filesystems.*;
43 import org.openide.util.*;
44 import org.openide.util.datatransfer.PasteType;
45 import org.openide.util.datatransfer.NewType;
46 import org.openide.util.datatransfer.ExClipboard;
47
48
49 /**
50  * Class providing various useful methods for palette classes.
51  *
52  * @author S Aubrecht
53  */

54 public final class Utils {
55
56     private static final Logger JavaDoc ERR = Logger.getLogger( "org.netbeans.modules.palette" ); // NOI18N
57

58     private Utils() {
59     }
60
61     // -----------
62

63     public static ResourceBundle JavaDoc getBundle() {
64         return NbBundle.getBundle(Utils.class);
65     }
66
67     public static String JavaDoc getBundleString(String JavaDoc key) {
68         return getBundle().getString(key);
69     }
70     
71     public static Action[] mergeActions( Action[] first, Action[] second ) {
72         if( null == first )
73             return second;
74         if( null == second )
75             return first;
76         
77         Action[] res = new Action[first.length+second.length+1];
78         System.arraycopy( first, 0, res, 0, first.length );
79         res[first.length] = null;
80         System.arraycopy( second, 0, res, first.length+1, second.length );
81         return res;
82     }
83     
84     public static boolean isReadonly( Node node ) {
85         Object JavaDoc val = node.getValue( PaletteController.ATTR_IS_READONLY );
86         if( null == val ) {
87             DataObject dobj = (DataObject)node.getCookie( DataObject.class );
88             if( null != dobj ) {
89                 val = dobj.getPrimaryFile().getAttribute( PaletteController.ATTR_IS_READONLY );
90             }
91         }
92         if( null != val ) {
93             return Boolean.valueOf( val.toString() ).booleanValue();
94         } else {
95             return !node.canDestroy();
96         }
97     }
98     
99     public static HelpCtx getHelpCtx( Node node, HelpCtx defaultHelp ) {
100         HelpCtx retValue = defaultHelp;
101         if( null == retValue || HelpCtx.DEFAULT_HELP.equals( retValue ) ) {
102             Object JavaDoc val = node.getValue( PaletteController.ATTR_HELP_ID );
103             if( null == val ) {
104                 DataObject dobj = (DataObject)node.getCookie( DataObject.class );
105                 if( null != dobj ) {
106                     val = dobj.getPrimaryFile().getAttribute( PaletteController.ATTR_HELP_ID );
107                 }
108             }
109         
110             if( null != val )
111                 retValue = new HelpCtx( val.toString() );
112         }
113         return retValue;
114     }
115     
116     public static void addCustomizationMenuItems( JPopupMenu popup, PaletteController controller, Settings settings ) {
117         popup.addSeparator();
118         popup.add( new ShowNamesAction( settings ) );
119         popup.add( new ChangeIconSizeAction( settings ) );
120         addResetMenuItem( popup, controller, settings );
121         popup.addSeparator();
122         popup.add( new ShowCustomizerAction( controller ) );
123     }
124     
125     static void addResetMenuItem( JPopupMenu popup, final PaletteController controller, final Settings settings ) {
126         JMenuItem item = new JMenuItem( getBundleString( "CTL_ResetPalettePopup" ) );
127         item.addActionListener( new ActionListener JavaDoc() {
128             public void actionPerformed(ActionEvent JavaDoc e) {
129                 resetPalette( controller, settings );
130             }
131         });
132         popup.add( item );
133     }
134     
135     /**
136      * Find a Node representing the given category.
137      *
138      * @param root Palette's root node.
139      * @param categoryName Name of the category to search for.
140      * @return Category with the given name or null.
141      */

142     public static Node findCategoryNode( Node root, String JavaDoc categoryName ) {
143         return root.getChildren().findChild( categoryName );
144     }
145     
146     public static void resetPalette( final PaletteController controller, final Settings settings ) {
147         Node rootNode = (Node)controller.getRoot().lookup( Node.class );
148         if( null != rootNode )
149             resetPalette( rootNode, controller, settings );
150     }
151     
152     public static void resetPalette( Node rootNode, PaletteController controller, Settings settings ) {
153         // first user confirmation...
154
NotifyDescriptor desc = new NotifyDescriptor.Confirmation(
155             getBundleString("MSG_ConfirmPaletteReset"), // NOI18N
156
getBundleString("CTL_ConfirmResetTitle"), // NOI18N
157
NotifyDescriptor.YES_NO_OPTION);
158
159         if( NotifyDescriptor.YES_OPTION.equals(
160                     DialogDisplayer.getDefault().notify(desc)) ) {
161             
162             settings.reset();
163             DataObject dob = (DataObject)rootNode.getLookup().lookup( DataObject.class );
164             if( null != dob ) {
165                 FileObject primaryFile = dob.getPrimaryFile();
166                 if( null != primaryFile && primaryFile.isFolder() ) {
167                     final Object JavaDoc cleaner = primaryFile.getAttribute( "removeWritables" ); //NOI18N
168
if( null != cleaner && (cleaner instanceof Callable JavaDoc) ) {
169                         try {
170                             ((Callable JavaDoc)cleaner).call();
171                         } catch (Exception JavaDoc ex) {
172                             ERR.log( Level.INFO, ex.getLocalizedMessage(), ex );
173                         }
174                     }
175                 }
176             }
177             controller.refresh();
178         }
179     }
180
181     /**
182      * An action to create a new palette category.
183      */

184     public static class NewCategoryAction extends AbstractAction {
185         private Node paletteNode;
186         
187         /**
188          * @param paletteRootNode Palette's root node.
189          */

190         public NewCategoryAction( Node paletteRootNode ) {
191             putValue(Action.NAME, getBundleString("CTL_CreateCategory")); // NOI18N
192
this.paletteNode = paletteRootNode;
193         }
194
195         public void actionPerformed(ActionEvent JavaDoc event) {
196             NewType[] newTypes = paletteNode.getNewTypes();
197             try {
198                 if( null != newTypes && newTypes.length > 0 ) {
199                     newTypes[0].create();
200                 }
201             } catch( IOException JavaDoc ioE ) {
202                 ERR.log( Level.INFO, ioE.getLocalizedMessage(), ioE );
203             }
204         }
205     }
206     
207     /**
208      * An action to sort categories alphabetically.
209      */

210     static class SortCategoriesAction extends AbstractAction {
211         private Node paletteNode;
212         public SortCategoriesAction( Node paletteNode ) {
213             putValue(Action.NAME, getBundleString("CTL_SortCategories")); // NOI18N
214
this.paletteNode = paletteNode;
215         }
216         
217         public void actionPerformed(ActionEvent JavaDoc event) {
218             Index order = (Index)paletteNode.getCookie(Index.class);
219             if (order != null) {
220                 final Node[] nodes = paletteNode.getChildren().getNodes( DefaultModel.canBlock() );
221                 Arrays.sort( nodes, new Comparator JavaDoc<Node>() {
222                     public int compare(Node n1, Node n2) {
223                         return n1.getDisplayName().compareTo( n2.getDisplayName() );
224                     }
225                 } );
226                 int[] perm = new int[nodes.length];
227                 for( int i=0; i<perm.length; i++ ) {
228                     perm[i] = order.indexOf( nodes[i] );
229                 }
230                 order.reorder( perm );
231             }
232         }
233         
234         public boolean isEnabled() {
235             return (paletteNode.getCookie(Index.class) != null);
236         }
237     }
238     
239     /**
240      * An action to show/hide palette item names.
241      */

242     private static class ShowNamesAction extends AbstractAction {
243         
244         private Settings settings;
245         
246         public ShowNamesAction( Settings settings ) {
247             this.settings = settings;
248         }
249         
250         public void actionPerformed(ActionEvent JavaDoc event) {
251             settings.setShowItemNames( !settings.getShowItemNames() );
252         }
253         
254         public Object JavaDoc getValue(String JavaDoc key) {
255             if (Action.NAME.equals(key)) {
256                 boolean showNames = settings.getShowItemNames();
257                 return getBundleString(showNames ? "CTL_HideNames" : "CTL_ShowNames"); // NOI18N
258
} else {
259                 return super.getValue(key);
260             }
261         }
262     }
263     
264     /**
265      * An action to change the size of palette icons.
266      */

267     private static class ChangeIconSizeAction extends AbstractAction {
268         
269         private Settings settings;
270         
271         public ChangeIconSizeAction( Settings settings ) {
272             this.settings = settings;
273         }
274         
275         public void actionPerformed(ActionEvent JavaDoc event) {
276             int oldSize = settings.getIconSize();
277             int newSize = (oldSize == BeanInfo.ICON_COLOR_16x16) ?
278                 BeanInfo.ICON_COLOR_32x32 : BeanInfo.ICON_COLOR_16x16;
279             settings.setIconSize( newSize );
280         }
281         
282         public Object JavaDoc getValue(String JavaDoc key) {
283             if (Action.NAME.equals(key)) {
284                 String JavaDoc namePattern = getBundleString("CTL_IconSize"); // NOI18N
285
return MessageFormat.format(namePattern,
286                 new Object JavaDoc[] {Integer.valueOf(settings.getIconSize())});
287             } else {
288                 return super.getValue(key);
289             }
290         }
291     }
292     
293     /**
294      * An action to restore palette's default state.
295      */

296     static class RefreshPaletteAction extends AbstractAction {
297         
298         public RefreshPaletteAction() {
299             putValue(Action.NAME, getBundleString("CTL_RefreshPalette")); // NOI18N
300
}
301         
302         public void actionPerformed(ActionEvent JavaDoc event) {
303             PalettePanel.getDefault().doRefresh();
304         }
305         
306     }
307     
308     /**
309      * An action to remove a category and all items in it.
310      */

311     static class DeleteCategoryAction extends AbstractAction {
312         private Node categoryNode;
313         
314         public DeleteCategoryAction(Node categoryNode) {
315             this.categoryNode = categoryNode;
316             putValue(Action.NAME, getBundleString("CTL_DeleteCategory")); // NOI18N
317
}
318         
319         public void actionPerformed(ActionEvent JavaDoc event) {
320             // first user confirmation...
321
String JavaDoc message = MessageFormat.format(
322                 getBundleString("FMT_ConfirmCategoryDelete"), // NOI18N
323
new Object JavaDoc [] { categoryNode.getName() });
324
325             NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message,
326                 getBundleString("CTL_ConfirmCategoryTitle"), // NOI18N
327
NotifyDescriptor.YES_NO_OPTION);
328
329             if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) {
330                 try {
331                     categoryNode.destroy();
332                 } catch (java.io.IOException JavaDoc e) {
333                     ERR.log( Level.INFO, e.getLocalizedMessage(), e );
334                 }
335             }
336         }
337         
338         public boolean isEnabled() {
339             return categoryNode.canDestroy();
340         }
341     }
342     
343     /**
344      * An action to rename a category.
345      */

346     static class RenameCategoryAction extends AbstractAction {
347         private Node categoryNode;
348         
349         public RenameCategoryAction(Node categoryNode) {
350             this.categoryNode = categoryNode;
351             putValue(Action.NAME, getBundleString("CTL_RenameCategory")); // NOI18N
352
}
353         
354         public void actionPerformed(ActionEvent JavaDoc event) {
355             NotifyDescriptor.InputLine desc = new NotifyDescriptor.InputLine(
356                 getBundleString("CTL_NewName"), // NOI18N
357
getBundleString("CTL_Rename")); // NOI18N
358
desc.setInputText(categoryNode.getDisplayName());
359
360             if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) {
361                 String JavaDoc newName;
362                 try {
363                     newName = desc.getInputText();
364                     if (!"".equals(newName)) // NOI18N
365
categoryNode.setDisplayName(newName);
366                 } catch (IllegalArgumentException JavaDoc e) {
367                     ERR.log( Level.INFO, e.getLocalizedMessage(), e );
368                 }
369             }
370         }
371         
372         public boolean isEnabled() {
373             return categoryNode.canRename();
374         }
375     }
376
377     /**
378      * An action to sort categories alphabetically.
379      */

380     static class SortItemsAction extends AbstractAction {
381         private Node categoryNode;
382         public SortItemsAction( Node categoryNode ) {
383             putValue(Action.NAME, getBundleString("CTL_SortItems")); // NOI18N
384
this.categoryNode = categoryNode;
385         }
386         
387         public void actionPerformed(ActionEvent JavaDoc event) {
388             Index order = (Index)categoryNode.getCookie(Index.class);
389             if (order != null) {
390                 final Node[] nodes = categoryNode.getChildren().getNodes( DefaultModel.canBlock() );
391                 Arrays.sort( nodes, new Comparator JavaDoc<Node>() {
392                     public int compare(Node n1, Node n2) {
393                         return n1.getDisplayName().compareTo( n2.getDisplayName() );
394                     }
395                 } );
396                 int[] perm = new int[nodes.length];
397                 for( int i=0; i<perm.length; i++ ) {
398                     perm[i] = order.indexOf( nodes[i] );
399                 }
400                 order.reorder( perm );
401             }
402         }
403         
404         public boolean isEnabled() {
405             return (categoryNode.getCookie(Index.class) != null);
406         }
407     }
408     
409     /**
410      * An action to create a new palette item from clipboard contents.
411      */

412     public static class PasteItemAction extends AbstractAction {
413         private Node categoryNode;
414         
415         public PasteItemAction(Node categoryNode) {
416             this.categoryNode = categoryNode;
417             putValue(Action.NAME, getBundleString("CTL_Paste")); // NOI18N
418
}
419         
420         public void actionPerformed(ActionEvent JavaDoc event) {
421             PasteType type = getPasteType();
422             if (type != null) {
423                 try {
424                     Transferable trans = type.paste();
425                     if (trans != null) {
426                         ClipboardOwner owner = trans instanceof ClipboardOwner ?
427                             (ClipboardOwner)trans : new StringSelection(""); // NOI18N
428
Clipboard clipboard = (Clipboard)Lookup.getDefault().lookup(ExClipboard.class);
429                         clipboard.setContents(trans, owner);
430                     }
431                 } catch (java.io.IOException JavaDoc e) {
432                     ERR.log( Level.INFO, e.getLocalizedMessage(), e );
433                 }
434             }
435         }
436         
437         public boolean isEnabled() {
438             return (getPasteType() != null);
439         }
440
441         private PasteType getPasteType() {
442             Clipboard clipboard = (Clipboard) Lookup.getDefault().lookup(ExClipboard.class);
443             Transferable trans = clipboard.getContents(this);
444             if (trans != null) {
445                 PasteType[] pasteTypes = categoryNode.getPasteTypes(trans);
446                 if (pasteTypes != null && pasteTypes.length != 0)
447                     return pasteTypes[0];
448             }
449             return null;
450         }
451
452     }
453     
454     /**
455      * An action to cut a palette item to clipboard.
456      */

457     public static class CutItemAction extends AbstractAction {
458         private Node itemNode;
459         
460         public CutItemAction(Node itemNode) {
461             this.itemNode = itemNode;
462             putValue(Action.NAME, getBundleString("CTL_Cut")); // NOI18N
463
}
464         
465         public void actionPerformed(ActionEvent JavaDoc event) {
466             try {
467                 Transferable trans = itemNode.clipboardCut();
468                 if (trans != null) {
469                     Clipboard clipboard = (Clipboard)
470                         Lookup.getDefault().lookup(ExClipboard.class);
471                     clipboard.setContents(trans, new StringSelection("")); // NOI18N
472
}
473             } catch (java.io.IOException JavaDoc e) {
474                 ERR.log( Level.INFO, e.getLocalizedMessage(), e );
475             }
476         }
477
478         public boolean isEnabled() {
479             return itemNode.canCut();
480         }
481     }
482     
483     /**
484      * An action to copy palette item to clipboard.
485      */

486     public static class CopyItemAction extends AbstractAction {
487         private Node itemNode;
488         
489         public CopyItemAction(Node itemNode) {
490             this.itemNode = itemNode;
491             putValue(Action.NAME, getBundleString("CTL_Copy")); // NOI18N
492
}
493         
494         public void actionPerformed(ActionEvent JavaDoc event) {
495             try {
496                 Transferable trans = itemNode.clipboardCopy();
497                 if (trans != null) {
498                     Clipboard clipboard = (Clipboard)
499                         Lookup.getDefault().lookup(ExClipboard.class);
500                     clipboard.setContents(trans, new StringSelection("")); // NOI18N
501
}
502             } catch (java.io.IOException JavaDoc e) {
503                 ERR.log( Level.INFO, e.getLocalizedMessage(), e );
504             }
505         }
506
507         public boolean isEnabled() {
508             return itemNode.canCopy();
509         }
510     }
511     
512     /**
513      * An action to remove an item from palette.
514      */

515     static class RemoveItemAction extends AbstractAction {
516         private Node itemNode;
517         
518         public RemoveItemAction(Node itemNode) {
519             this.itemNode = itemNode;
520             putValue(Action.NAME, getBundleString("CTL_Delete")); // NOI18N
521
}
522         
523         public void actionPerformed(ActionEvent JavaDoc event) {
524             // first user confirmation...
525
String JavaDoc message = MessageFormat.format(
526                 getBundleString("FMT_ConfirmBeanDelete"), // NOI18N
527
new Object JavaDoc[] { itemNode.getDisplayName() });
528
529             NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message,
530                 getBundleString("CTL_ConfirmBeanTitle"), // NOI18N
531
NotifyDescriptor.YES_NO_OPTION);
532
533             if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) {
534                 try {
535                     itemNode.destroy();
536                 } catch (java.io.IOException JavaDoc e) {
537                     ERR.log( Level.INFO, e.getLocalizedMessage(), e );
538                 }
539             }
540         }
541         
542         public boolean isEnabled() {
543             return itemNode.canDestroy();
544         }
545     }
546     
547     /**
548      * An action to remove an item from palette.
549      */

550     private static class ShowCustomizerAction extends AbstractAction {
551         private PaletteController palette;
552         
553         public ShowCustomizerAction( PaletteController palette ) {
554             this.palette = palette;
555             putValue(Action.NAME, getBundleString("CTL_ShowCustomizer")); // NOI18N
556
}
557         
558         public void actionPerformed(ActionEvent JavaDoc event) {
559             palette.showCustomizer();
560         }
561     }
562 }
563
Popular Tags