/*
* File name: IntelligentInput.java (package eas.statistics)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 11.11.2010 (16:34:54)
*
* (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.startSetup;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.HashSet;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.windowFrames.StaticWindow;
import eas.miscellaneous.useful.autoComplete.JSuggestField;
import eas.plugins.Plugin;
import eas.plugins.PluginFactory;
import eas.plugins.masterScheduler.MasterScheduler;
import eas.simulation.EASRunnable;
/**
* @author Lukas König
*/
public class IntelligentInput extends JDialog
implements ActionListener, ListSelectionListener, ComponentListener, ChangeListener {
private static final long serialVersionUID = 1L;
private String[] objects;
@SuppressWarnings("rawtypes")
private JList list;
private JSuggestField suggester;
private ParCollection pars;
private JScrollPane jScrollPane1 = new JScrollPane(list);
private JTextArea additionalText1 = new JTextArea();
private JTextArea additionalText2 = new JTextArea();
private JCheckBox additionalCheck1 = new JCheckBox();
private JCheckBox additionalCheck2 = new JCheckBox();
private JSlider slider = null;
private JLabel sliderVal = null;
private String pluginsToAddReq = "";
private String pluginsToAddSugg = "";
/**
* @return The plugins that are to be added automatically to the plugins
* list (String).
*/
public String getPluginsToAdd() {
String s = "";
if (this.additionalCheck1.isSelected()) {
s += this.pluginsToAddReq;
}
if (this.additionalCheck2.isSelected()) {
s += this.pluginsToAddSugg;
}
return s;
}
public IntelligentInput(
JFrame owner,
String title,
int min,
int max,
int selected,
ParCollection params,
SingleParameter myParameter) throws HeadlessException {
this(owner, title, new String[0], new int[0], params, new sliderConf(min, max, selected), myParameter, false);
}
private static class sliderConf {
private int min;
private int max;
private int selected;
public sliderConf(int min, int max, int selected) {
super();
this.min = min;
this.max = max;
this.selected = selected;
}
}
public IntelligentInput(
JFrame owner,
String title,
String[] objects,
int[] selected,
ParCollection params,
SingleParameter myParameter,
boolean singleSelectionOnly) throws HeadlessException {
this(owner, title, objects, selected, params, null, myParameter, singleSelectionOnly);
}
// @SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public IntelligentInput(
JFrame owner,
String title,
String[] objects,
int[] selected,
ParCollection params,
sliderConf conf,
SingleParameter myParameter,
boolean singleSelectionOnly) throws HeadlessException {
super(owner, "Choose a parameter value [" + title + "]", true);
this.singleSelectionOnly = singleSelectionOnly;
this.objects = objects;
this.pars = params;
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JPanel p = new JPanel(new GridLayout());
list = new JList(objects);
list.setSelectedIndices(selected);
list.addListSelectionListener(this);
JPanel panel0 = new JPanel(new GridLayout(2, 1));
jScrollPane1 = new JScrollPane(list);
jScrollPane1.getViewport().add(list);
panel0.add(jScrollPane1);
if (this.singleSelectionOnly) {
this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
if (myParameter != null && myParameter.getDescription() != null) {
JTextArea area = new JTextArea(5, 20);
area.setBackground(this.getBackground());
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.setEditable(false);
area.setText(myParameter.getDescription());
panel0.add(area);
}
this.addComponentListener(this);
JPanel panel2 = new JPanel(new GridLayout());
JPanel panel3 = new JPanel(new GridLayout(6, 1));
panel2.add(ok);
panel2.add(cancel);
if (conf == null) {
panel3.add(additionalText1);
panel3.add(additionalCheck1);
panel3.add(additionalText2);
panel3.add(additionalCheck2);
JPanel littlePanel = new JPanel(new GridLayout(1, 2));
this.suggester = new JSuggestField(this, objects);
this.suggester.addSelectionListener(this);
this.suggester.requestFocus();
// this.suggester.setText(objects[selected[0]]);
// this.suggester.setSelectionStart(0);
// this.suggester.setSelectionEnd(this.suggester.getText().length());;
littlePanel.add(new JLabel("Search list:"));
littlePanel.add(this.suggester);
panel3.add(littlePanel);
} else {
panel3.add(additionalText1);
panel3.add(additionalCheck1);
this.sliderVal = new JLabel(" " + conf.selected);
this.sliderVal.setOpaque(true);
this.slider = new JSlider(conf.min, conf.max, conf.selected);
panel3.add(this.sliderVal);
panel3.add(this.slider);
this.slider.addChangeListener(this);
this.list.setVisible(false);
}
panel3.add(panel2);
p.add(panel0);
p.add(panel3);
ok.addActionListener(this);
cancel.addActionListener(this);
this.add(p);
this.setSize(600, 300);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.additionalCheck2.setVisible(false);
this.additionalCheck1.setVisible(false);
this.additionalText1.setVisible(false);
this.additionalText2.setVisible(false);
this.additionalText1.setFont(new Font("", Font.BOLD, 12));
this.additionalText2.setFont(new Font("", Font.BOLD, 12));
this.additionalText1.setForeground(Color.blue);
this.additionalText2.setForeground(Color.blue);
this.additionalText1.setLineWrap(true);
this.additionalText2.setLineWrap(true);
this.additionalText1.setBackground(new Color(230, 230, 230));
this.additionalText2.setBackground(new Color(230, 230, 230));
this.additionalCheck1.setSelected(true);
this.additionalCheck2.setSelected(false);
this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
this.valueChanged(new ListSelectionEvent(
this.list,
this.list.getSelectedIndex(),
this.list.getSelectedIndex(),
true));
}
public String getResult() {
if (this.slider != null) {
return "" + this.slider.getValue();
}
if (this.objects == null) {
return null;
}
String s = "";
for (int i : list.getSelectedIndices()) {
s += "," + objects[i];
}
s = s.substring(1);
return s;
}
private boolean singleSelectionOnly;
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource().equals(this.suggester)) {
// if (this.singleSelectionOnly) {
// this.list.setSelectedValue(((JSuggestField) arg0.getSource()).getText(), true);
// } else {
for (int i = 0; i < this.list.getModel().getSize(); i++) {
if (this.list.getModel().getElementAt(i).toString().equals(((JSuggestField) arg0.getSource()).getText())) {
this.list.addSelectionInterval(i, i);
list.ensureIndexIsVisible(i);
break;
}
}
// }
this.suggester.setText("");
} else {
if (arg0.getActionCommand().equals("Cancel")) {
this.objects = null;
this.slider = null;
}
if (this.liveWindow != null) {
this.liveWindow.setVisible(false);
this.liveWindow.dispose();
}
this.setVisible(false);
this.dispose();
}
}
private StaticWindow liveWindow = null;
@Override
public void valueChanged(ListSelectionEvent e) {
if (e != null && e.getSource().equals(this.list)) {
try {
if (this.getTitle().contains("masterScheduler") || this.getTitle().contains("Plugins")) {
this.pluginsToAddReq = "";
this.pluginsToAddSugg = "";
this.additionalText1.setVisible(true);
this.additionalText2.setVisible(true);
// if (this.getTitle().contains("masterScheduler")) {
// this.additionalCheck1.setVisible(true);
// this.additionalCheck2.setVisible(true);
// this.additionalCheck1.setText("Add all required plugins");
// this.additionalCheck2.setText("Add all suggested plugins");
// } else {
// this.additionalCheck1.setSelected(false);
// this.additionalCheck2.setSelected(false);
// }
List<?> selected = this.list.getSelectedValuesList();
Plugin<?> selectedPlug;
HashSet<String> reqPlugs = new HashSet<String>();
HashSet<String> suppPlugs = new HashSet<String>();
for (Object plugString : selected) {
selectedPlug = PluginFactory.getKonstPlug((String) plugString, this.pars);
if (selectedPlug.getRequiredPlugins() != null) {
reqPlugs.addAll(selectedPlug.getRequiredPlugins());
}
if (selectedPlug.getSupportedPlugins() != null) {
suppPlugs.addAll(selectedPlug.getSupportedPlugins());
}
}
this.pluginsToAddReq += reqPlugs.toString().replace("]", "").replace("[", "").replace(" ", "");
this.pluginsToAddSugg += suppPlugs.toString().replace("]", "").replace("[", "").replace(" ", "");
if (!this.pluginsToAddReq.equals("")) {
this.pluginsToAddReq = "," + this.pluginsToAddReq;
}
if (!this.pluginsToAddSugg.equals("")) {
this.pluginsToAddSugg = "," + this.pluginsToAddSugg;
}
this.additionalText1.setText("Required Plugins:\n" + reqPlugs.toString().replace(" ", ""));
this.additionalText2.setText("Suggested Plugins:\n" + suppPlugs.toString().replace(" ", ""));
if (this.getTitle().contains("masterScheduler")) {
this.additionalCheck1.setVisible(true);
this.additionalCheck2.setVisible(true);
this.additionalCheck1.setText("Add all required plugins");
this.additionalCheck2.setText("Add all suggested plugins");
MasterScheduler<?> p = (MasterScheduler<?>) PluginFactory
.getKonstPlug(this.list.getSelectedValue().toString(),
this.pars);
EASRunnable env = p.generateRunnables(this.pars)[0];
int i = 0;
// for (EASRunnable env : p.generateRunnables(this.pars)) {
try {
BufferedImage img = env.getOutsideView();
if (liveWindow != null) {
liveWindow.setVisible(false);
liveWindow.dispose();
}
liveWindow = StaticMethods.showImage(img, "Preview of " + p.id() + " - Runnable_" + env.id());
liveWindow.setLocation(this.getX() + this.getWidth(), 0 + i * 100);
liveWindow.setAlwaysOnTop(true);
} catch (Exception e1) {
StaticMethods.logWarning(
"Could not generate image from environment '"
+ "Runnable_" + env.id()
+ "'",
this.pars);
if (liveWindow != null) {
liveWindow.setVisible(false);
liveWindow.dispose();
}
}
// i++;
// }
} else {
this.additionalCheck1.setSelected(false);
this.additionalCheck2.setSelected(false);
}
}
} catch (Exception e1) {
if (liveWindow != null) {
liveWindow.setVisible(false);
liveWindow.dispose();
}
}
}
}
static int i = 0;
@Override
public void componentResized(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void stateChanged(ChangeEvent arg0) {
this.sliderVal.setText(" " + this.slider.getValue());
}
}