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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import rbsla.eca.Daemon;
import rbsla.environment.ECAConstraints;

public class ECARuleWindow extends JDialog
{
	private JPanel rulePanel = new JPanel(new BorderLayout());
	private JList ruleList = new JList();
	private JScrollPane rulePane = new JScrollPane(ruleList);
	
	private Vector rules = new Vector();
	private int lastFiredRule = 0;
	
	public ECARuleWindow(JFrame parent)
	{
		super(parent, "ECA Rules");
		//put the dialog into the frame
        Point parentLocation = parent.getLocationOnScreen();
        this.setLocation(parentLocation.x +9 , parentLocation.y + 74);
		
		//init the dialog
		ruleList.setEnabled(false);
		rulePanel.add(rulePane, BorderLayout.CENTER);
		this.add(rulePanel);
		this.setSize(new Dimension(410,210));
		setVisible(true);
		
		Daemon.setRuleWindow(this);
	}
	
	public void ruleFired(int rule)
	{	
		String test = (String)rules.elementAt(rule);
    	test = test.replaceAll(ECAConstraints._ACITVE_RULE, ECAConstraints._FIRED_RULE);
    	rules.setElementAt(test, rule);
    	ruleList.setListData(rules);
    	lastFiredRule = rule;
	}
	
	public void resetRule()
	{
		String test = (String)rules.elementAt(lastFiredRule);
    	test = test.replaceAll(ECAConstraints._FIRED_RULE, ECAConstraints._ACITVE_RULE);
    	rules.setElementAt(test, lastFiredRule);
    	ruleList.setListData(rules);
	}
	
	public void setListData(Vector newRules)
	{
		rules = newRules;
		ruleList.setListData(newRules);
	}
	
}
