/*
 * Copyright (C) 1999-2006 <a href="mailto:adrian.paschke@gmx.de">Adrian Paschke</a>
 * 
 * The RBSLM Environment is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla.environment.view;

import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JDialog;

import org.apache.log4j.Logger;

import rbsla.environment.ECAWindow;
import rbsla.environment.StartViewDaemon;

/**
 * Main Viewer logic for all viewers.
 */
public class ViewDialog extends JDialog implements WindowListener,
        MouseListener, Observer
{
    ViewModell viewModell;

    ECAWindow parentWindow;

    Viewer viewerInterface;
    
    private static Logger logger = Logger.getLogger(ViewDialog.class);
    
    private boolean initSuccessfull = false;


    public ViewDialog(ECAWindow _parentWindow, ViewModell _viewModell,
            Viewer _viewerInterface) throws IOException
    {

        super(_parentWindow);
        parentWindow = _parentWindow;
        viewModell = _viewModell;
        viewerInterface = _viewerInterface;

        setContentPane(viewerInterface.getContentPane(viewModell
                .getViewerProperties()));

        this.setSize(viewModell.getDimension());
        this.setLocation(viewModell.getPosition());

        this.addWindowListener(this);
        //textArea.addMouseListener(this);

        setVisible(true);
        this.requestFocus();

        //add this view in observer textViewModell
        viewModell.addObserver(this);

        initSuccessfull = true;
        
        updateView();
    }

    public void updateView()
    {
        if (initSuccessfull)
        {
            String result = "";
            try
            {
                result = viewModell.getResult();
                if (result == null)
                    result = "";
                viewerInterface.dataChanged(result, viewModell.getDebugText());
            } catch (Throwable ex)
            {
                logger.info("", ex);
            }
            setTitle(viewModell.getViewTitel());

            // unfortunately this is needed in the modell
            writeWindowsSizeToModel();
        }
    }

    /**
     * Called by modell if data is changed.
     */
    public void update(Observable arg0, Object arg1)
    {
        //textArea.
        updateView();
        //textArea.repaint();
        this.repaint();
    }

    private void writeWindowsSizeToModel()
    {
        viewModell.setDimension(getSize());
        viewModell.setPosition(getLocation());
    }

    public void setViewModell(ViewModell textViewModell)
    {
        viewModell = textViewModell;
    }

    public ViewModell getViewModell()
    {
        return viewModell;

    }

    public void setParent(Frame parent)
    {
        this.parentWindow = (ECAWindow) parent;
    }

    public Hashtable getViewerProperties()
    {
        return new Hashtable();
    }

    /**
     * Invoked the first time a window is made visible.
     */
    public void windowOpened(WindowEvent e)
    {
    }

    /**
     * Invoked when the user attempts to close the window from the window's
     * system menu. If the program does not explicitly hide or dispose the
     * window while processing this event, the window close operation will be
     * cancelled.
     */
    public void windowClosing(WindowEvent e)
    {
        StartViewDaemon.getInstance().deleteView(getViewModell());
        this.dispose();
    }

    /**
     * Invoked when a window has been closed as the result of calling dispose on
     * the window.
     */
    public void windowClosed(WindowEvent e)
    {
    }

    /**
     * Invoked when a window is changed from a normal to a minimized state. For
     * many platforms, a minimized window is displayed as the icon specified in
     * the window's iconImage property.
     * 
     * @see java.awt.Frame#setIconImage
     */
    public void windowIconified(WindowEvent e)
    {
    }

    /**
     * Invoked when a window is changed from a minimized to a normal state.
     */
    public void windowDeiconified(WindowEvent e)
    {
    }

    /**
     * Invoked when the Window is set to be the active Window. Only a Frame or a
     * Dialog can be the active Window. The native windowing system may denote
     * the active Window or its children with special decorations, such as a
     * highlighted title bar. The active Window is always either the focused
     * Window, or the first Frame or Dialog that is an owner of the focused
     * Window.
     */
    public void windowActivated(WindowEvent e)
    {
        parentWindow.setLastActivatedViewer(this);
    }

    /**
     * Invoked when a Window is no longer the active Window. Only a Frame or a
     * Dialog can be the active Window. The native windowing system may denote
     * the active Window or its children with special decorations, such as a
     * highlighted title bar. The active Window is always either the focused
     * Window, or the first Frame or Dialog that is an owner of the focused
     * Window.
     */
    public void windowDeactivated(WindowEvent e)
    {
    }

    /**
     * Invoked when the mouse button has been clicked (pressed and released) on
     * a component.
     */
    public void mouseClicked(MouseEvent e)
    {
    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e)
    {
    }

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e)
    {
    }

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e)
    {
    }

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e)
    {
    }

    /*
     * (non-Javadoc)
     * 
     * @see eca.gui.view.Viewer#viewerInit()
     */
    public void viewerInit()
    {

    }

}

