KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > diagnostics > Reminder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Reminder.java
26  *
27  * Created on December 30, 2000, 11:33 AM
28  */

29
30 package com.sun.enterprise.util.diagnostics;
31
32 import javax.swing.JOptionPane JavaDoc;
33
34 /**
35  *
36  * This class makes it easy to add a message to be displayed at runtime,
37  * and/or a message requiring a Yes/No answer.
38  * The message appears in a MODAL dialog so it will always get your full attention.
39  * It is designed for development-time only. In fact every message box screams
40  * out a message to not allow it into the release.
41  *The class name, file name, method name and line number of the caller is automatically added to the message window.
42  *<p><b>Examples:</b></p>
43  *
44  <p> 1) You just temporarily did something -- like commented-out some code --
45  * and you don't want to forget to comment it back in later:
46  * <br><code>Reminder.message("Don't forget to uncomment the code here!");</code>
47  * <p>2) You want to add a branch in some code, temporarily. Instead of
48  * changing the source and recompiling again and again do the following and
49  * determine the branching at runtime:
50 <br><code>
51 if(Reminder.message("Use the new improved code?"))
52 <br> new_code();
53 <br>else
54 <br> old_code();
55  </code>
56  *<p> 3) <b>Poor Man's Debugger</b><br>Add watch statements, pause execution so you can check something in the filesystem, etc.
57  * @author bnevins
58  * @version $Revision: 1.3 $
59  */

60 public class Reminder
61 {
62     /**
63      * Displays a Message Box that reminds you to do something. You can't ignore it --
64      * it is a modal dialog box.
65      *
66      * @param msg The message to display in the Message Box
67      */

68     public static void message(String JavaDoc msg)
69     {
70         String JavaDoc s = createMessage(msg);
71         JOptionPane.showMessageDialog(null, s, title, JOptionPane.ERROR_MESSAGE);
72     }
73     
74     //////////////////////////////////////////////////////////
75

76     /**
77      * Displays a Message Box with Yes and No buttons. Handy for temporarily adding a
78      * runtime-chosen decision point in your code. Use this instead of commenting out
79      * code that you have to remember to uncomment later.
80      * @param msg The message to display in the Message Box
81      * @return returns true if <i>yes</i> was chosen, false if <i>no</i> was chosen.
82      */

83     public static boolean yesno(String JavaDoc msg)
84     {
85         String JavaDoc s = createMessage(msg);
86         //return true for yes, false for no...
87
int reply = JOptionPane.showConfirmDialog(null, s, title, JOptionPane.YES_NO_OPTION);
88         
89         if(reply == JOptionPane.YES_OPTION)
90             return true;
91         
92         return false;
93     }
94     
95     //////////////////////////////////////////////////////////
96

97     private Reminder()
98     {
99     }
100     
101     //////////////////////////////////////////////////////////
102

103     private static String JavaDoc createMessage(String JavaDoc s)
104     {
105         String JavaDoc location;
106         
107         try
108         {
109             location = "\n\nCode Location: " + new CallerInfo(me).toString();
110         }
111         catch(CallerInfoException e)
112         {
113             location = "\n\nUnknown code location";
114         }
115         
116         return preMessage + s + location;
117     }
118     
119     //////////////////////////////////////////////////////////
120

121     private final static String JavaDoc title = "Temporary Code Reminder";
122     private final static String JavaDoc preMessage = "***** DO NOT SHIP WITH THIS MESSAGE IN PLACE!!!! ******\n\n";
123     private final static Object JavaDoc[] me = { new Reminder() };
124     
125     //////////////////////////////////////////////////////////
126

127     /**
128      * Simple test code to exercise the class.
129      */

130     public static void main(String JavaDoc[] notUsed)
131     {
132         ReminderTester rt = new ReminderTester();
133         rt.test();
134         System.exit(0);
135     }
136     
137 }
138
139 class ReminderTester
140 {
141     public void test()
142     {
143         Reminder.message("Here is Reminder.message()");
144         boolean ret = Reminder.yesno("Here is Reminder.yesno(). Do you like it?");
145         Reminder.message("You replied: " + (ret ? "yes" : "no"));
146     }
147 }
148
Popular Tags