Package eas.plugins.standard.liveInteraction

Source Code of eas.plugins.standard.liveInteraction.AllroundPluginManager

/*
* File name:        AllroundPluginManager.java (package eas.plugins.standard.liveInteraction)
* Author(s):        Lukas König
* Java version:     8.0 (at generation time)
* Generation date:  25.06.2014 (20:33:51)
*
* (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.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import eas.miscellaneous.system.windowFrames.GeneralDialog;
import eas.plugins.AbstractDefaultPlugin;
import eas.plugins.Plugin;
import eas.plugins.PluginFactory;
import eas.simulation.EASRunnable;
import eas.simulation.Wink;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;

/**
* This plugin allows to register plugins at runtime as well as put them to sleep
* or re-awake them. The plugin manager allows only plugins that safely match the
* main runnable used to be registered. However, for the matching procedure to
* work the managed plugins have to extend AbstractDefaultPlugin; plugins which
* just implement the Plugin interface will not be shown by the manager.
*
* @author Lukas König
*/
public class AllroundPluginManager extends AbstractDefaultPlugin<EASRunnable> implements MouseListener {

    private static final long serialVersionUID = 8996030477117738984L;
    private EASRunnable mainRunnable;
    private JFrame mainWindow;
   
    @Override
    public String id() {
        return AbstractDefaultPlugin.ALLROUND_PLUGIN_PREFIX + "-pluginManager";
    }

    private JButton registerButton = new JButton("====> Register ====>");
    private JButton sleepButton = new JButton("====> Sleep ====>");
    private JButton wakeButton = new JButton("<==== Wake <====");
    private JButton refreshButton = new JButton("Refresh");
    private LinkedList<Plugin<?>> sleepingPlugins = new LinkedList<Plugin<?>>();
    private JList<String> allPluginsList;
    private JList<Plugin<?>> runningPluginsList;
    private JList<Plugin<?>> sleepingPluginsList;
   
    @Override
    public void runBeforeSimulation(EASRunnable env, ParCollection params) {
        this.mainRunnable = env;
       
        this.mainWindow = new JFrame("Plugin Manager (legend: --- | AVAILABLE | RUNNING | SLEEPING | --- plugins)");
        this.mainWindow.getContentPane().setLayout(new GridLayout(1, 5));
        this.mainWindow.setSize(1000, 500);

        this.refreshRequested = true;

        registerButton.addMouseListener(this);
        sleepButton.addMouseListener(this);
        wakeButton.addMouseListener(this);
        refreshButton.addMouseListener(this);
    }

    private Plugin<?>[] getPlugArray(LinkedList<Plugin<?>> list) {
        Plugin<?>[] array = new Plugin[list.size()];
        for (int i = 0; i < array.length; i++) {
            array[i] = list.get(i);
        }
        return array;
    }
   
    public void refresh() {
        this.mainWindow.getContentPane().removeAll();

        this.sleepingPlugins = new LinkedList<>(this.mainRunnable.getSimTime().getAllSleepingPlugins());
        LinkedList<Plugin<?>> runningWithoutSleeping = new LinkedList<>(this.mainRunnable.getSimTime().getPlugins());
        runningWithoutSleeping.removeAll(this.sleepingPlugins);
       
        allPluginsList = new JList<>(getPlugIDs());
        runningPluginsList = new JList<>(getPlugArray(runningWithoutSleeping));
        sleepingPluginsList = new JList<>(getPlugArray(this.sleepingPlugins));
       
        allPluginsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        runningPluginsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        sleepingPluginsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       
        allPluginsList.setBackground(new Color(222, 222, 200));
        sleepingPluginsList.setBackground(new Color(222, 222, 200));
       
        JScrollPane scrollBar1 = new JScrollPane(
                allPluginsList,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        JPanel panel12 = new JPanel();
        JPanel panel23 = new JPanel();
        panel12.add(registerButton);
        panel23.add(refreshButton);
        panel23.add(sleepButton);
        panel23.add(wakeButton);
       
        JScrollPane scrollBar2 = new JScrollPane(
                runningPluginsList,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
       
        JScrollPane scrollBar3 = new JScrollPane(
                sleepingPluginsList,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        mainWindow.getContentPane().add(scrollBar1);
        mainWindow.getContentPane().add(panel12);
        mainWindow.getContentPane().add(scrollBar2);
        mainWindow.getContentPane().add(panel23);
        mainWindow.getContentPane().add(scrollBar3);

        this.mainWindow.setVisible(true);
    }

    private String[] getPlugIDs() {
        ArrayList<Class<AbstractDefaultPlugin<?>>> plugins = new ArrayList<>(PluginFactory.getMatchingPlugins(this.mainRunnable));
        String[] plugArray = new String[plugins.size()];
        for (int i = 0; i < plugins.size(); i++) {
            try {
                plugArray[i] = plugins.get(i).newInstance().id();
            } catch (Exception e) {
            }
        }
       
        return plugArray;
    }
   
    @Override
    public void runAfterSimulation(EASRunnable env, ParCollection params) {
       
    }

    private boolean refreshRequested = false;
   
    @Override
    public void runDuringSimulation(EASRunnable env, Wink currentSimTime,
            ParCollection params) {
        if (this.refreshRequested) {
            env.getSimTime().requestNotification(this, currentSimTime.getLastTick() + 0.5);
            this.refreshRequested = false;
        }
       
        if (!currentSimTime.isTick()) {
            this.refresh();
        }
    }
   
    @Override
    public void onSimulationResumed(EASRunnable env, Wink resumeTime,
            ParCollection params) {
        super.onSimulationResumed(env, resumeTime, params);
        this.mainWindow.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(this.registerButton)) {
            List<String> selected = this.allPluginsList.getSelectedValuesList();
            try {
                selected.forEach(id -> {
                    if (!this.mainRunnable.getSimTime().registerPlugin(
                                PluginFactory.getKonstPlug(id, GlobalVariables
                                        .getPrematureParameters()))) {
                        GeneralDialog dia = new GeneralDialog(null, "Plugin '" + id + "' not registered, see log for details.",
                                "Plugin not registered", GeneralDialog.OK_BUTT,
                                null);
                        dia.setVisible(true);
                    }
                });
            } catch (Exception e1) {
                GeneralDialog dia = new GeneralDialog(null, null, "Exception occurred", GeneralDialog.OK_BUTT, Arrays.deepToString(e1.getStackTrace()).replace(",", "\n"));
                dia.setVisible(true);
            }
        } else if (e.getSource().equals(this.sleepButton)) {
            List<Plugin<?>> selected = this.runningPluginsList.getSelectedValuesList();
            selected.forEach(plug -> {
                if ((!plug.id().equals(new AllroundPluginManager().id())
                        || GeneralDialog.yesNoAnswer(
                                "Put plugin manager to sleep?",
                                "Do you really want to put the plugin manager to sleep?\n"
                                + "You won't be able to manage plugins anymore\n"
                                + "and you particularly won't be able to wake the manager back up!"))
                        && (plug != this.mainRunnable.getSimTime().getMasterScheduler()
                        || GeneralDialog.yesNoAnswer(
                                "Put master scheduler to sleep?",
                                "Do you really want to put the master scheduler to sleep?\n"
                                + "The main scheduling will just stop working and the main runnable probably "
                                + "won't be notified about progression of time anymore until you wake him up again."))) {
                    this.mainRunnable.getSimTime().neverNotifyAgain(plug);
                }
            });
        } else if (e.getSource().equals(this.wakeButton)) {
            List<Plugin<?>> selected = this.sleepingPluginsList.getSelectedValuesList();
            selected.forEach(plug -> {
                this.mainRunnable.getSimTime().requestAllEvents(plug);
                this.mainRunnable.getSimTime().requestTicks(plug);
            });
        }
       
        this.refreshRequested = true;
    }
   
    @Override public void mousePressed(MouseEvent e) {}
    @Override public void mouseReleased(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
}
TOP

Related Classes of eas.plugins.standard.liveInteraction.AllroundPluginManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.