/*
* File name: LiveParameterSetterPlugin.java (package eas.plugins.standard)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 30.10.2011 (15:24:13)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.plugins.standard.liveInteraction;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.datatypes.JGridBagPanel;
import eas.plugins.AbstractDefaultPlugin;
import eas.simulation.Wink;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.IntelligentInput;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.ArrayListString;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*/
public class AllroundLiveParameterSetterPlugin
extends AbstractDefaultPlugin<AbstractEnvironment<?>>
implements ActionListener, MouseListener, ComponentListener {
private static final long serialVersionUID = 6469898702525439411L;
private LinkedList<String> changedNames = new LinkedList<String>();
private LinkedList<String> changedValues = new LinkedList<String>();
private JFrame frame;
private JGridBagPanel panel;
private JTextField[] fields;
private JTextField[] labels;
private JButton[] buttons;
private ParCollection pars;
public static final String SHOW_THESE_PAR_NAME = "showThese";
private static ArrayListString showThese;
private static boolean resetRequested = false;
public static void setShowThese(ArrayListString showThese) {
AllroundLiveParameterSetterPlugin.showThese = showThese;
resetRequested = true;
}
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> list = new ArrayList<SingleParameter>(1);
list.add(new SingleParameter(
SHOW_THESE_PAR_NAME,
Datatypes.STRING_ARR,
new ArrayListString(new String[] {"all"}),
AllroundLiveParameterSetterPlugin.class
));
return list;
}
@Override
public String id() {
return AbstractDefaultPlugin.ALLROUND_PLUGIN_PREFIX + "-liveParameterSetter";
}
@Override
public void runBeforeSimulation(AbstractEnvironment<?> env, ParCollection params) {
frame = new JFrame("Set parameters (" + params.getParValueArrayListString(SHOW_THESE_PAR_NAME) + ")");
this.frame.addComponentListener(this);
pars = params;
StaticMethods.loadWindowFramePosition(this.frame, ID);
// resetRequested = true;
// ArrayList<SingleParameter> allParams = params.getAllPars();
// String[] values = new String[allParams.size() + 1];
//
// for (int z = 0; z < allParams.size(); z++) {
// values[z + 1] = allParams.get(z).getParameterName();
// }
// values[0] = "all";
// params.getSinglePar(SHOW_THESE_PAR_NAME).setParDatatype(Datatypes.fixedStringSet(values));
}
public void reset(ParCollection params) {
resetRequested = false;
panel = new JGridBagPanel();
frame.getContentPane().removeAll();
List<SingleParameter> parameters = params.getAllPars();
JScrollPane scrollBar = new JScrollPane(
panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setAlignmentX(Component.LEFT_ALIGNMENT);
frame.getContentPane().add(scrollBar);
frame.setVisible(true);
this.buttons = new JButton[parameters.size()];
this.fields = new JTextField[parameters.size()];
this.labels = new JTextField[parameters.size()];
int i = 0;
ArrayListString list = showThese;
for (SingleParameter p : parameters) {
if (list.contains("all") || list.contains(p.getParameterName()) || p.getParameterName().equals(SHOW_THESE_PAR_NAME)) {
this.buttons[i] = new JButton("OK_" + StaticMethods.normZahl(i));
this.fields[i] = new JTextField(p.getParValue().toString(), 5);
this.labels[i] = new JTextField(p.getParameterName());
this.panel.addGB(buttons[i], 0, i);
this.panel.addGB(labels[i], 1, i);
this.panel.addGB(fields[i], 2, i);
this.buttons[i].addActionListener(this);
this.buttons[i].setSize(100, buttons[i].getSize().width);
this.fields[i].setSize(100, fields[i].getSize().width);
this.labels[i].setSize(100, labels[i].getSize().width);
this.labels[i].setEditable(false);
if (p.isPluginParameter() && p.getIntelligentInput(null, params) != null) {
this.fields[i].setBackground(Color.yellow);
}
if (p.getParameterName().equals(SHOW_THESE_PAR_NAME)) {
this.fields[i].setBackground(Color.green);
this.fields[i].setColumns(20);
}
this.fields[i].addMouseListener(this);
if (!p.isPluginParameter()) {
this.buttons[i].setEnabled(false);
this.fields[i].setEditable(false);
this.labels[i].setEnabled(false);
}
}
i++;
}
this.frame.setVisible(true);
}
@Override
public void runAfterSimulation(AbstractEnvironment<?> env, ParCollection params) {
}
@Override
public void runDuringSimulation(AbstractEnvironment<?> env, Wink currentSimTime,
ParCollection params) {
if (resetRequested) {
this.reset(params);
}
for (int i = 0; i < this.changedNames.size(); i++) {
params.setParValue(
this.changedNames.get(i),
this.changedValues.get(i));
}
this.changedNames.clear();
this.changedValues.clear();
}
@Override
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(((JButton) e.getSource()).getActionCommand().split("_")[1]);
this.changedNames.add("" + this.labels[i].getText());
this.changedValues.add("" + this.fields[i].getText());
}
@Override
public void mouseClicked(MouseEvent arg0) {
JTextField source = (JTextField) arg0.getSource();
int num = 0;
for (int i = 0; i < this.fields.length; i++) {
if (source == this.fields[i]) {
num = i;
}
}
String parName = this.labels[num].getText();
SingleParameter sp = this.pars.getSinglePar(parName);
IntelligentInput intInp = null;
if (sp.isPluginParameter()) {
intInp = sp.getIntelligentInput(frame, this.pars);
}
if (sp.isPluginParameter() && intInp != null) {
intInp.setVisible(true);
if (intInp.getResult() != null) {
this.fields[num].setText(intInp.getResult());
this.actionPerformed(new ActionEvent(this.buttons[num], 100, ""));
}
}
}
@Override public void mouseEntered(MouseEvent arg0) {}
@Override public void mouseExited(MouseEvent arg0) {}
@Override public void mousePressed(MouseEvent arg0) {}
@Override public void mouseReleased(MouseEvent arg0) {}
@Override
public void componentResized(ComponentEvent e) {
StaticMethods.storeWindowFramePosition(this.frame, ID);
}
@Override
public void componentMoved(ComponentEvent e) {
StaticMethods.storeWindowFramePosition(this.frame, ID);
}
@Override public void componentShown(ComponentEvent e) {}
@Override public void componentHidden(ComponentEvent e) {}
public static final String ID = "liveWindowCentralFrame";
@Override
public void onSimulationResumed(AbstractEnvironment<?> env,
Wink resumeTime, ParCollection params) {
this.frame.setVisible(true);
}
}