KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > tree > ConfirmDialog


1 package com.ca.directory.jxplorer.tree;
2
3 import com.ca.commons.cbutil.*;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 /**
13  * A small GUI utility dialog window, that prompts the user for an
14  * ok/cancel answer, while also allowing them to set the value of
15  * a boolean property that determines whether the dialog is ever
16  * to be displayed.
17  */

18 public class ConfirmDialog extends JDialog implements ActionListener JavaDoc
19 {
20
21     CBPanel display;
22     JCheckBox showAgain;
23     Properties JavaDoc myProperties;
24     String JavaDoc binaryProperty = "";
25     CBButton OK, Cancel;
26     boolean userResponse = false;
27
28     private static Logger JavaDoc log = Logger.getLogger(ConfirmDialog.class.getName());
29     /**
30      * This should work similarly to an OptionDialog -
31      * ask the user something, and return a value. As
32      * an added extra, it will take a properties list, and
33      * the name of a properties object (that should be storing
34      * 'true' and 'false' values), and use it for a 'show this
35      * dialog again' message. If the user selects no, the
36      * property will be modified to 'false' and the dialog
37      * will not operate until it has been set to 'true' by
38      * some external means.
39      * @param Msg the message to display to the user.
40      * @param binaryProperty the property name (key) that sets whether to show the
41      * dialog. (may be null.)
42      * @param parent the gui parent to use when displaying the dialog
43      */

44
45     public ConfirmDialog(String JavaDoc Msg, String JavaDoc binaryProperty, Frame parent)
46     {
47         super(parent);
48         this.myProperties = com.ca.directory.jxplorer.JXplorer.myProperties;
49         this.binaryProperty = binaryProperty;
50         //XXX should really check Msg string length...
51
setSize(new Dimension(300,120));
52         getContentPane().add(display = new CBPanel());
53         
54         display.addln(new JLabel(Msg));
55         JPanel buttons = new JPanel();
56         buttons.add(OK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to confirm.")));
57         buttons.add(Cancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to cancel.")));
58         display.addln(buttons);
59         
60         OK.addActionListener(this);
61         Cancel.addActionListener(this);
62         
63         if ((myProperties != null) && (binaryProperty != null) && (myProperties.getProperty(binaryProperty) != null))
64         {
65             showAgain = new JCheckBox(CBIntText.get("show this dialog every time"), true);
66             display.addln(showAgain);
67             showAgain.addActionListener(this);
68         }
69         else
70             showAgain = null;
71             
72         setVisible(false);
73     }
74
75     /**
76      * the main method to call; this shows the dialog, makes it modal,
77      * gets a response, unmodalifies the window, hides the dialog, and returns
78      * the value to the user.
79      */

80     public boolean getUserResponse()
81     {
82 System.out.println("binary prop " + binaryProperty);
83 System.out.println("myProperties " + !(myProperties==null));
84
85         if ("false".equals(myProperties.getProperty(binaryProperty)))
86             return true; // property says not to show this dialog.
87

88         if (showAgain == null) return true; // confirm not set up.
89

90         showAgain.setSelected(true);
91         
92         setVisible(true);
93         showAgain.setSelected(true);
94         
95         startModal(); // this blocks until the user has done stuff...
96
showAgain.setSelected(true);
97
98         //setVisible(false); redundant
99
return userResponse;
100     }
101     
102     /**
103      * Traditional action loop. Either of the buttons will cause the
104      * Modality to stop and the getValue() method to return.
105      */

106     public void actionPerformed(ActionEvent JavaDoc ev)
107     {
108         
109         if (ev.getSource() == showAgain)
110         {
111             //System.out.println("setting button to: " + showAgain.isSelected());
112
myProperties.setProperty(binaryProperty, String.valueOf(showAgain.isSelected()));
113             showAgain.repaint();
114         }
115         else if (ev.getSource() == OK)
116         {
117             //System.out.println("Ok selected");
118
userResponse = true;
119             stopModal();
120             setVisible(false);
121         }
122         else if (ev.getSource() == Cancel)
123         {
124             //System.out.println("Cancel selected");
125
userResponse = false;
126             stopModal();
127             setVisible(false);
128         }
129         else
130         {
131             //System.out.println("huh? \n" + ev.toString());
132
}
133     }
134     
135     
136     /*
137      * Creates a new EventDispatchThread to dispatch events from. This
138      * method returns when stopModal is invoked.
139      * (Code stolen verbatim from JInternalFrame)
140      */

141     synchronized void startModal() {
142     /* Since all input will be blocked until this dialog is dismissed,
143      * make sure its parent containers are visible first (this component
144      * is tested below). This is necessary for JApplets, because
145      * because an applet normally isn't made visible until after its
146      * start() method returns -- if this method is called from start(),
147      * the applet will appear to hang while an invisible modal frame
148      * waits for input.
149      */

150     if (isVisible() && !isShowing()) {
151         Container parent = this.getParent();
152         while (parent != null) {
153         if (parent.isVisible() == false) {
154             parent.setVisible(true);
155         }
156         parent = parent.getParent();
157         }
158     }
159
160         try {
161             if (SwingUtilities.isEventDispatchThread()) {
162                 EventQueue theQueue = getToolkit().getSystemEventQueue();
163                 while (isVisible()) {
164                     // This is essentially the body of EventDispatchThread
165
AWTEvent event = theQueue.getNextEvent();
166                     Object JavaDoc src = event.getSource();
167                     if (src instanceof Component) {
168                           ((Component) src).dispatchEvent(event);
169                       } else if (src instanceof MenuComponent) {
170                           ((MenuComponent) src).dispatchEvent(event);
171                       } else {
172                           log.warning("unable to dispatch event: " + event);
173                       }
174                 }
175             } else
176                 while (isVisible())
177                     wait();
178         } catch(InterruptedException JavaDoc e){}
179     }
180   
181     /*
182      * Stops the event dispatching loop created by a previous call to
183      * <code>startModal</code>.
184      */

185     synchronized void stopModal() {
186         notifyAll();
187     }
188   
189
190 }
Popular Tags