001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.lib;
042:
043: import java.awt.event.ActionEvent;
044: import javax.swing.Action;
045: import javax.swing.SwingUtilities;
046:
047: import org.openide.ErrorManager;
048: import org.openide.NotifyDescriptor;
049: import org.openide.DialogDisplayer;
050: import org.openide.awt.StatusDisplayer;
051: import org.openide.filesystems.FileObject;
052: import org.openide.nodes.Node;
053: import org.openide.loaders.DataObject;
054: import org.openide.loaders.DataObjectNotFoundException;
055: import org.openide.util.Mutex;
056:
057: /**
058: * @author Libor Kramolis
059: */
060: public final class GuiUtil {
061:
062: private GuiUtil() {
063: }
064:
065: /**
066: * Try to perform default action on specified file object.
067: */
068: public static void performDefaultAction(FileObject fo) {
069: if (fo == null) {
070: if (Util.THIS.isLoggable()) /* then */
071: Util.THIS.debug("FileObject can not be null.",
072: new IllegalArgumentException()); // NOI18N
073: return;
074: }
075: final DataObject obj;
076: try {
077: obj = DataObject.find(fo);
078: } catch (DataObjectNotFoundException e) {
079: if (Util.THIS.isLoggable()) {
080: Util.THIS.debug("DataObject not found", e); // NOI18N
081: }
082: return;
083: }
084: // All else is GUI, do in EQ.
085: Mutex.EVENT.readAccess(new Runnable() {
086: public void run() {
087: Node node = obj.getNodeDelegate();
088: Action a = node.getPreferredAction();
089: if (a != null) {
090: a.actionPerformed(new ActionEvent(node,
091: ActionEvent.ACTION_PERFORMED, "")); // NOI18N
092: }
093: }
094: });
095: }
096:
097: public static boolean confirmAction(String message) {
098: NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
099: message, NotifyDescriptor.YES_NO_OPTION);
100: Object option = DialogDisplayer.getDefault().notify(nd);
101: return (option == NotifyDescriptor.YES_OPTION);
102: }
103:
104: public static void setStatusText(String text) {
105: StatusDisplayer.getDefault().setStatusText(text);
106: }
107:
108: /**
109: * Notify exception to user. Just shortcut to ErrorManager.
110: */
111: public static void notifyException(Throwable exc) {
112: notifyException(null, exc);
113: }
114:
115: /**
116: * Notify annotated exception to user. Just shortcut to ErrorManager.
117: */
118: public static void notifyException(String desc, Throwable ex) {
119: ErrorManager err = ErrorManager.getDefault();
120: if (desc != null) {
121: err.annotate(ex, desc);
122: }
123: err.notify(err.EXCEPTION, ex); // show stack trace to user
124: }
125:
126: /**
127: * Thread safe notify message as WARNING_MESSAGE.
128: */
129: public static void notifyWarning(final String message) {
130: // invokeLater??? there had to be some error with DialogDisplyer
131: SwingUtilities.invokeLater(new Runnable() {
132: public void run() {
133: NotifyDescriptor nd = new NotifyDescriptor.Message(
134: message, NotifyDescriptor.WARNING_MESSAGE);
135: DialogDisplayer.getDefault().notify(nd);
136: }
137: });
138: }
139:
140: public static void notifyError(final String message) {
141: SwingUtilities.invokeLater(new Runnable() {
142: public void run() {
143: DialogDisplayer.getDefault().notify(
144: new NotifyDescriptor.Message(message,
145: NotifyDescriptor.ERROR_MESSAGE));
146: }
147: });
148: }
149:
150: }
|