KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > lib > GuiUtil


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 package org.netbeans.modules.xml.core.lib;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import javax.swing.Action JavaDoc;
23 import javax.swing.SwingUtilities JavaDoc;
24
25 import org.openide.ErrorManager;
26 import org.openide.NotifyDescriptor;
27 import org.openide.DialogDisplayer;
28 import org.openide.awt.StatusDisplayer;
29 import org.openide.filesystems.FileObject;
30 import org.openide.nodes.Node;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.DataObjectNotFoundException;
33 import org.openide.util.Mutex;
34
35 /**
36  * @author Libor Kramolis
37  */

38 public final class GuiUtil {
39     
40     private GuiUtil() {}
41
42     /**
43      * Try to perform default action on specified file object.
44      */

45     public static void performDefaultAction (FileObject fo) {
46         if (fo == null) {
47             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("FileObject can not be null.", new IllegalArgumentException JavaDoc()); // NOI18N
48
return;
49         }
50         final DataObject obj;
51         try {
52             obj = DataObject.find(fo);
53         } catch (DataObjectNotFoundException e) {
54             if (Util.THIS.isLoggable()) {
55                 Util.THIS.debug("DataObject not found", e); // NOI18N
56
}
57             return;
58         }
59         // All else is GUI, do in EQ.
60
Mutex.EVENT.readAccess(new Runnable JavaDoc() {
61             public void run() {
62                 Node node = obj.getNodeDelegate();
63                 Action JavaDoc a = node.getPreferredAction();
64                 if (a != null) {
65                     a.actionPerformed(new ActionEvent JavaDoc(node, ActionEvent.ACTION_PERFORMED, "")); // NOI18N
66
}
67             }
68         });
69     }
70
71     public static boolean confirmAction (String JavaDoc message) {
72         NotifyDescriptor nd = new NotifyDescriptor.Confirmation (message, NotifyDescriptor.YES_NO_OPTION);
73         Object JavaDoc option = DialogDisplayer.getDefault().notify (nd);
74         return ( option == NotifyDescriptor.YES_OPTION );
75     }
76
77
78     public static void setStatusText (String JavaDoc text) {
79         StatusDisplayer.getDefault().setStatusText (text);
80     }
81
82
83
84     /**
85      * Notify exception to user. Just shortcut to ErrorManager.
86      */

87     public static void notifyException (Throwable JavaDoc exc) {
88         notifyException (null, exc);
89     }
90
91
92     /**
93      * Notify annotated exception to user. Just shortcut to ErrorManager.
94      */

95     public static void notifyException (String JavaDoc desc, Throwable JavaDoc ex) {
96         ErrorManager err = ErrorManager.getDefault();
97         if (desc != null) {
98             err.annotate (ex, desc);
99         }
100         err.notify (err.EXCEPTION, ex); // show stack trace to user
101
}
102
103     /**
104      * Thread safe notify message as WARNING_MESSAGE.
105      */

106     public static void notifyWarning (final String JavaDoc message) {
107         // invokeLater??? there had to be some error with DialogDisplyer
108
SwingUtilities.invokeLater (new Runnable JavaDoc () {
109                 public void run () {
110                     NotifyDescriptor nd = new NotifyDescriptor.Message
111                         (message, NotifyDescriptor.WARNING_MESSAGE);
112                     DialogDisplayer.getDefault ().notify (nd);
113                 }
114             });
115     }
116
117     public static void notifyError (final String JavaDoc message) {
118         SwingUtilities.invokeLater (new Runnable JavaDoc () {
119                 public void run () {
120                     DialogDisplayer.getDefault().notify
121                         (new NotifyDescriptor.Message (message, NotifyDescriptor.ERROR_MESSAGE)
122                          );
123                 }
124             });
125     }
126
127 }
128
Popular Tags