KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ant > UserQuestionHandler


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.modules.project.ant;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.SwingUtilities JavaDoc;
24 import org.openide.DialogDisplayer;
25 import org.openide.NotifyDescriptor;
26 import org.openide.util.NbBundle;
27 import org.openide.util.UserQuestionException;
28
29 /**
30  * Deals with {@link UserQuestionException}s.
31  * @see "#46089"
32  * @author Jesse Glick
33  */

34 public final class UserQuestionHandler {
35
36     private UserQuestionHandler() {}
37
38     /**
39      * Handle a user question exception later (in the event thread).
40      * Displays a dialog and invokes the appropriate method on the callback.
41      * The callback will be notified in the event thread.
42      * Use when catching {@link UserQuestionException} during {@link FileObject#lock}.
43      */

44     public static void handle(final UserQuestionException e, final Callback callback) {
45         SwingUtilities.invokeLater(new Runnable JavaDoc() {
46             public void run() {
47                 NotifyDescriptor.Confirmation desc = new NotifyDescriptor.Confirmation(
48                     e.getLocalizedMessage(),
49                     NbBundle.getMessage(UserQuestionHandler.class, "TITLE_CannotWriteFile"),
50                     NotifyDescriptor.Confirmation.OK_CANCEL_OPTION);
51                 if (DialogDisplayer.getDefault().notify(desc).equals(NotifyDescriptor.OK_OPTION)) {
52                     try {
53                         e.confirmed();
54                         callback.accepted();
55                     } catch (IOException JavaDoc x) {
56                         callback.error(x);
57                     }
58                 } else {
59                     callback.denied();
60                 }
61             }
62         });
63     }
64     
65     /**
66      * Intended behavior.
67      */

68     public interface Callback {
69         
70         /**
71          * Called later if the user accepted the question.
72          */

73         void accepted();
74         
75         /**
76          * Called later if the user denied the question.
77          */

78         void denied();
79         
80         /**
81          * Called later if the user accepted the question but there was in fact a problem.
82          */

83         void error(IOException JavaDoc e);
84         
85     }
86     
87 }
88
Popular Tags