//$Id: Message.java,v 1.3 2004/04/29 21:06:55 m31 Exp $
//
//This file is part of the program suite "Principia Mathematica II"
//which is a working prototype for the main project:
//"Hilbert II" - http://www.qedeq.org
//
//Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
//"Principia Mathematica II" is free software; you can redistribute
//it and/or modify it under the terms of the GNU General Public
//License as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.


/*
 * Created on 14.04.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.meyling.principia.gui;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.meyling.principia.log.Logger;


/**
 * Response message for the user.
 *
 * @version $Revision: 1.3 $
 * @author	Michael Meyling
 */
public class Message {
    
    private static boolean directResponse = false;

    private static Message instance;

    private static JFrame main = null;

    private JTextField statusField = null;
    
    private Color fg = null;


    private final static MessageInterface directResponseState = new MessageInterface() {

        public final void success(final String text) {
            JOptionPane.showMessageDialog(
                Message.main,
                text,
                "Success",
                JOptionPane.INFORMATION_MESSAGE,
                null
            );
        }

        public final void failure(final String text) {
            JOptionPane.showMessageDialog(
                Message.main,
                text,
                "Failure",
                JOptionPane.ERROR_MESSAGE,
                null
            );
        }
        
    };

    private final static MessageInterface eventWindowState = new MessageInterface() {

        public final void success(final String text) {
        }

        public final void failure(final String text) {
        }
        
    };


    private MessageInterface state;
    

    public final static void init(final JFrame main) {
        Message.main = main;
        if (instance != null) {
            throw new IllegalStateException(Message.class.getName() 
                + " is already initialized!");
        }
        synchronized (Message.class) {
            if (instance == null) {
                instance = new Message();
            } else {
                throw new IllegalStateException(Message.class.getName() 
                    + " is already initialized!");
            }
        }
        
        
    }
    

    public final static Message getInstance() {
        if (Message.instance == null) {
            throw new IllegalStateException(Message.class.getName() 
                + " was not initialized");
        }
        return Message.instance;
    }


    private Message() {
        this.state = Message.directResponseState;
    }


    public final void setDirectResponse(final boolean directResponse) {
        if (directResponse) {
            this.state = Message.directResponseState;
        } else {
            this.state = Message.eventWindowState;
        }
    }


    public final void setStatusField(final JTextField  statusField) {
        this.statusField = statusField;
        this.fg = statusField.getForeground();
    }


    public final void success(final String text) {
        Logger.getInstance().logSuccessfulReply(text);
        if (statusField != null) {
            statusField.setText(text);
            statusField.setForeground(fg);
        }
        this.state.success(text); 
    }


    public final void failure(final String text, final String description) {
        Logger.getInstance().logFailureReply(text, description);
        if (statusField != null) {
            statusField.setText(text);
            statusField.setForeground(Color.RED);
        }
        this.state.failure(text); 
    } 


    private interface MessageInterface {

        public void success(String text); 

        public void failure(String text);
    }
    

}
