001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing;
017:
018: import javax.swing.JFrame;
019: import javax.swing.JOptionPane;
020: import javax.swing.SwingUtilities;
021:
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.renderer.MessageRenderer;
024:
025: /**
026: *
027: * <p>Render User messages in Popup windows </p>
028: * <p> </p>
029: * <p>Copyright: Copyright (c) 2004</p>
030: * <p>Company: tp23</p>
031: * @author Paul Hinds
032: * @version $Id: SwingMessageRenderer.java,v 1.4 2006/12/21 00:02:59 teknopaul Exp $
033: */
034: public class SwingMessageRenderer implements MessageRenderer {
035:
036: private InstallerContext ctx = null;
037: private JFrame owner = null;
038:
039: public SwingMessageRenderer() {
040: }
041:
042: public SwingMessageRenderer(InstallerContext ctx) {
043: this .ctx = ctx;
044: }
045:
046: public void setInstallerContext(InstallerContext ctx) {
047: this .ctx = ctx;
048: }
049:
050: public void printMessage(String message) {
051: MessageRunnable messageRunnable = new MessageRunnable();
052: messageRunnable.message = message;
053: if (SwingUtilities.isEventDispatchThread()) {
054: messageRunnable.run();
055: } else {
056: try {
057: SwingUtilities.invokeAndWait(messageRunnable);
058: } catch (Exception e) { // Interrupted or InvocationTarget
059: ctx.log(e);
060: }
061: }
062: }
063:
064: public boolean prompt(String message) {
065: OptionRunnable optionRunnable = new OptionRunnable();
066: optionRunnable.message = message;
067: if (SwingUtilities.isEventDispatchThread()) {
068: optionRunnable.run();
069: } else {
070: try {
071: SwingUtilities.invokeAndWait(optionRunnable);
072: } catch (Exception e) { // Interrupted or InvocationTarget
073: ctx.log(e);
074: }
075: }
076: return optionRunnable.ok;
077: }
078:
079: /**
080: * @param owner The owner to set.
081: */
082: public void setOwner(JFrame owner) {
083: this .owner = owner;
084: }
085:
086: private class MessageRunnable implements Runnable {
087: String message;
088:
089: public void run() {
090: JOptionPane.showMessageDialog(
091: SwingMessageRenderer.this .owner, message,
092: "Message", JOptionPane.INFORMATION_MESSAGE);
093: }
094:
095: }
096:
097: private class OptionRunnable implements Runnable {
098: String message;
099: boolean ok;
100:
101: public void run() {
102: int ret = JOptionPane.showConfirmDialog(
103: SwingMessageRenderer.this .owner, message,
104: "Question", JOptionPane.YES_NO_OPTION);
105: if (ret == JOptionPane.YES_OPTION) {
106: ok = true;
107: } else {
108: ok = false;
109: }
110: }
111:
112: }
113: }
|