01: /*
02: * The contents of this file are subject to the terms of the Common Development
03: * and Distribution License (the License). You may not use this file except in
04: * compliance with the License.
05: *
06: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
07: * or http://www.netbeans.org/cddl.txt.
08: *
09: * 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.tcpmon.integration;
21:
22: import java.io.IOException;
23: import javax.swing.SwingUtilities;
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: /**
40: * Handle a user question exception later (in the event thread).
41: * Displays a dialog and invokes the appropriate method on the callback.
42: * The callback will be notified in the event thread.
43: * Use when catching {@link UserQuestionException} during {@link FileObject#lock}.
44: */
45: public static void handle(final UserQuestionException e,
46: final Callback callback) {
47: SwingUtilities.invokeLater(new Runnable() {
48: public void run() {
49: NotifyDescriptor.Confirmation desc = new NotifyDescriptor.Confirmation(
50: e.getLocalizedMessage(), NbBundle.getMessage(
51: UserQuestionHandler.class,
52: "TITLE_CannotWriteFile"),
53: NotifyDescriptor.Confirmation.OK_CANCEL_OPTION);
54: if (DialogDisplayer.getDefault().notify(desc).equals(
55: NotifyDescriptor.OK_OPTION)) {
56: try {
57: e.confirmed();
58: callback.accepted();
59: } catch (IOException x) {
60: callback.error(x);
61: }
62: } else {
63: callback.denied();
64: }
65: }
66: });
67: }
68:
69: /**
70: * Intended behavior.
71: */
72: public interface Callback {
73:
74: /**
75: * Called later if the user accepted the question.
76: */
77: void accepted();
78:
79: /**
80: * Called later if the user denied the question.
81: */
82: void denied();
83:
84: /**
85: * Called later if the user accepted the question but there was in fact a problem.
86: */
87: void error(IOException e);
88:
89: }
90:
91: }
|