1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.renderer.swing;
17
18import javax.swing.JFrame;
19import javax.swing.JOptionPane;
20import javax.swing.SwingUtilities;
21
22import org.tp23.antinstaller.InstallerContext;
23import org.tp23.antinstaller.renderer.MessageRenderer;
24/**
25 *
26 * <p>Render User messages in Popup windows </p>
27 * <p> </p>
28 * <p>Copyright: Copyright (c) 2004</p>
29 * <p>Company: tp23</p>
30 * @author Paul Hinds
31 * @version $Id: SwingMessageRenderer.java,v 1.4 2006/12/21 00:02:59 teknopaul Exp $
32 */
33public class SwingMessageRenderer
34    implements MessageRenderer {
35
36    private InstallerContext ctx = null;
37    private JFrame owner = null;
38
39
40    public SwingMessageRenderer() {
41    }
42    public SwingMessageRenderer(InstallerContext ctx) {
43        this.ctx = ctx;
44    }
45
46    public void setInstallerContext(InstallerContext ctx){
47        this.ctx = ctx;
48    }
49    public void printMessage(String message){
50        MessageRunnable messageRunnable = new MessageRunnable();
51        messageRunnable.message = message;
52        if(SwingUtilities.isEventDispatchThread()){
53            messageRunnable.run();
54        }
55        else{
56            try {
57                SwingUtilities.invokeAndWait(messageRunnable);
58            } catch (Exception e) { // Interrupted or InvocationTarget
59                ctx.log(e);
60            }
61        }
62    }
63
64    public boolean prompt(String message){
65        OptionRunnable optionRunnable = new OptionRunnable();
66        optionRunnable.message = message;
67        if(SwingUtilities.isEventDispatchThread()){
68            optionRunnable.run();
69        }
70        else{
71            try {
72                SwingUtilities.invokeAndWait(optionRunnable);
73            } catch (Exception e) { // Interrupted or InvocationTarget
74                ctx.log(e);
75            }
76        }
77        return optionRunnable.ok;
78    }
79    /**
80     * @param owner The owner to set.
81     */
82    public void setOwner(JFrame owner) {
83        this.owner = owner;
84    }
85    
86    private class MessageRunnable implements Runnable{
87        String message;
88
89        public void run() {
90            JOptionPane.showMessageDialog(SwingMessageRenderer.this.owner, 
91                    message, "Message", JOptionPane.INFORMATION_MESSAGE );
92        }
93        
94    }
95    
96    private class OptionRunnable implements Runnable{
97        String message;
98        boolean ok;
99        public void run() {
00            int ret = JOptionPane.showConfirmDialog(SwingMessageRenderer.this.owner,
01                    message,
02                    "Question",
03                    JOptionPane.YES_NO_OPTION);
04            if (ret == JOptionPane.YES_OPTION) {
05                ok = true;
06            }
07            else {
08                ok = false;
09            }
10        }
11        
12    }
13}
14