
/*
 * 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;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import rbsla.environment.view.ViewDialog;

/**
 * Main ECA Window.

 */
public class ECAWindow extends JFrame implements WindowListener, ActionListener
{
	
	
	
    ECAPanel contentPanel = null;

    JDialog lastActivatedViewer;
    
    
    //menu bar
    private JMenuBar menu = new JMenuBar();
    
    private JMenu facts;
    private JMenuItem addRules;
    private JMenu remRules;
    
    private JMenu stats;
    private JMenuItem statsAval;
    private JMenuItem statsRTD;
    private JMenuItem statsRepairTime;

    public ECAWindow(ECAPanel panel)
    {

        super();

        //init the menu bar
        facts = new JMenu("Rules");
        addRules = new JMenuItem(ECAConstraints._ADDRULES);
        remRules = new JMenu(ECAConstraints._REMRULES);
        
        stats = new JMenu("Statistics");
        statsAval = new JMenuItem(ECAConstraints._STATIC);
        statsRTD = new JMenuItem(ECAConstraints._ACTUAL);
        statsRepairTime = new JMenuItem(ECAConstraints._QUERY);
        
        //default empty Item
        JMenuItem tmp = new JMenuItem("no files imported");
		tmp.setEnabled(false);
		remRules.add(tmp);
         
        //set the items
        facts.add(addRules);
        facts.add(remRules);
        
        stats.add(statsAval);
        stats.add(statsRTD);
        stats.add(statsRepairTime);
        
        menu.add(facts);
        menu.add(stats);
        
        
        addRules.addActionListener(this);
        statsAval.addActionListener(this);
        statsRTD.addActionListener(this);
        statsRepairTime.addActionListener(this);
        
        this.setJMenuBar(menu);
        
        contentPanel = panel;

        contentPanel.parentWindow = this;
        this.getContentPane().setLayout(new GridBagLayout());
        this.getContentPane().add(
                contentPanel,
                new ECAConstraints(0, 0, 1, 1, 1.0, 1.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));

        addWindowListener(this);

        // show the window and get going
        pack();
    }

    public void setLastActivatedViewer(ViewDialog childDlg)
    {
        this.lastActivatedViewer = childDlg;
    }

    public JDialog getLastActivatedViewer()
    {
        return lastActivatedViewer;
    }

    /**
     * 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().abort();
        return;
    }

    /**
     * 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)
    {
    }

    /**
     * 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)
    {
    }
    
    //send incoming items to the panel
    public void actionPerformed(ActionEvent e)
    {
    	String cmd = e.getActionCommand();
    	contentPanel.menuEvent(cmd);
    }
    
    public void updateMenu(Vector impFiles)
    {
    	remRules.removeAll();
    	for(int i=0; i<impFiles.size();i++)
    	{
    		String s = (String)impFiles.elementAt(i);
    		JMenuItem tmp = new JMenuItem(s);
    		tmp.addActionListener(this);
    		remRules.add(tmp);    		
    	}
    	if(impFiles.size() == 0)
    	{
    		JMenuItem tmp = new JMenuItem("no files imported");
    		tmp.setEnabled(false);
    		remRules.add(tmp);
    	}
    }

}