Package eas.plugins.standard.visualization

Source Code of eas.plugins.standard.visualization.LiveWindow

/*
* Datei:          LiveWindow.java
* Autor(en):      Lukas König
* Java-Version:   6.0
* Erstellt (vor): -
*
* (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.visualization;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.miscellaneous.StaticMethods;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.spatial.sim2D.gridSimulation.standardGridObjects.GridObject;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.ParCollection;

/**
* Panel class.
*
* @author Lukas König
*/
public class LiveWindow extends JFrame
        implements MouseListener, MouseMotionListener, ComponentListener,
                   MouseWheelListener, WindowListener {
   
    protected boolean ticks = true;
    protected ParCollection pars;
    public MetaInfVisualizer metaInfVis;
    protected AllroundVideoPlugin vidParent;
    boolean isHand = false;
    protected Rectangle2D zoomRechteck = null;
    private Vector2D handStartingPoint;
    protected AbstractEnvironment<?> environment;
    protected String windowName;
    private boolean zoom;
    protected ControlPanel controlPanel;
    private static final long serialVersionUID = 1L;

    private JTextArea toolText = new JTextArea();
    private JPanel jPanel = new JPanel();
    private JDialog tooltip = new JDialog();

    public void setPauseInterval(int pauseIntervalMs) {
        this.pauseInterval = -pauseIntervalMs;
        this.controlPanel.speedSlider.setValue(this.pauseInterval);
    }
   
    protected int pauseInterval = 0;
   
    public void disposeAll() {
        this.controlPanel.dispose();
        this.dispose();
    }

    public void setZoom(boolean zoom) {
        this.zoom = zoom;
        if (this.pars != null) {
            StaticMethods.logStage1("Zoom set to " + zoom + ".", this.pars);
        }
        if (this.zoom) {
            this.setCursor(new Cursor(1));
        } else {
            this.setCursor(new Cursor(12));
        }
    }

    public void resetZoom() {
        this.zoomRechteck = null;
        this.vidParent.setZoom(null);
    }

    public LiveWindow(final String windowTitle, final AbstractEnvironment<?> env, final ParCollection params, boolean showTT) {
        this(windowTitle, env, null, params, showTT);
    }
   
    public LiveWindow(final String windowTitle, final AbstractEnvironment<?> env, final AllroundVideoPlugin videoParent, final ParCollection params, boolean showTT) {
        this(windowTitle, env, videoParent, params, 500, 500, showTT);
//        this.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    public LiveWindow(final String windowTitle, final AbstractEnvironment<?> env, final ParCollection params, int i, int j, boolean showTT) {
        this(windowTitle, env, null, params, i, j, showTT);
    }

    Vector2D centerChosenByClick = new Vector2D(0, 0);

    private boolean showTooltipp;
   
    public void setShowTooltipp(boolean showTooltipp) {
        this.showTooltipp = showTooltipp;
    }

    public LiveWindow(
            final String windowTitle,
            final AbstractEnvironment<?> env,
            final AllroundVideoPlugin videoParent,
            final ParCollection params,
            int i,
            int j,
            boolean showTooltipp) {
        super();
        this.showTooltipp = showTooltipp;
        this.environment = env;
        this.windowName = windowTitle;
       
        int width = 300, height = 100;

        this.tooltip.setUndecorated(true);
        this.tooltip.setBackground(Color.white);
        this.toolText.setBackground(Color.white);
        this.jPanel.setBackground(Color.white);
//        toolText.setLeftRightMargin(lrMargin);
//        toolText.setTopBottomMargin(tbMargin);
        toolText.setSize(width, height);
        this.jPanel.add(toolText);
        this.tooltip.getContentPane().add(this.jPanel);
        this.tooltip.setSize(width, height);
       
        this.setCursor(new Cursor(12));
        this.isHand = true;
        this.pars = params;
        this.vidParent = videoParent;
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setSize(i, j);
        this.metaInfVis = new MetaInfVisualizer(this);
        this.getContentPane().add(this.metaInfVis);
       
        this.controlPanel = new ControlPanel(this, this.vidParent, "Controls");
       
        if (videoParent != null) {
            this.controlPanel.addComponents();
           
            try {
                String[] dimensions = StaticMethods.liesTextAusDatei(
                        new File(params.getStdDirectory() + "/liveWindowLocation.dat"), params).split(",");
                this.setSize(Integer.parseInt(dimensions[0]), Integer.parseInt(dimensions[1]));
                this.setLocation(Integer.parseInt(dimensions[2]), Integer.parseInt(dimensions[3]));
                StaticMethods.loadWindowFramePosition(LiveWindow.this.controlPanel, ControlPanel.ID);
            } catch (Exception e) {}
           
            this.addMouseWheelListener(this);
            this.addWindowListener(this);
            this.addComponentListener(this);
            this.addMouseMotionListener(this);
            this.addMouseListener(this);
        }
       
        this.setVisible(true);
    }

    private Point p = null;
    private Point screenPos = null;
   
    public void showTooltipp(MouseEvent e) {
        if (!showTooltipp) {
            return;
        }
       
        if (e != null) {
            p = e.getPoint();
            screenPos = e.getLocationOnScreen();
            return;
        }
        if (p == null || screenPos == null) {
            return;
        }
       
        List<GridObject> gos = LiveWindow.this.vidParent.getGridObjectsUnderMouse(
                p.x - LiveWindow.this.getInsets().left,
                p.y - LiveWindow.this.getInsets().top);
   
        if (gos != null) {
            LiveWindow.this.tooltip.setVisible(true);
            LiveWindow.this.tooltip.setLocation(screenPos.x + 10, screenPos.y + 10);
            LiveWindow.this.tooltip.setAlwaysOnTop(true);
           
            String gosString = "";
            String id;
            int i = 0;
            int j = 0;
            String s;
           
            for (GridObject go : gos) {
                try {
                    id = " (" + ((AbstractAgent<?>) go).id() + ")";
                } catch (Exception e2) {
                    id = "";
                }
               
                String simpleName = go.getClass().getSimpleName();
               
                if (simpleName.length() > 0) {
                    simpleName += id + ": ";
                }
               
                s = simpleName + go.toString();
               
                if (s.length() > j) {
                    j = s.length();
                }
               
                gosString += s;
                if (i < gos.size() - 1) {
                    gosString += "\n";
                }
               
                i++;
            }
           
            toolText.setText(gosString);
            tooltip.setSize(j * 7 + 10, i * 16 + 12);
        } else {
            LiveWindow.this.tooltip.setVisible(false);
        }
    }

    private String getWindowDimensions() {
        String s = "";
       
        s += LiveWindow.this.getWidth() + ",";
        s += LiveWindow.this.getHeight() + ",";
        s += LiveWindow.this.getLocation().x + ",";
        s += LiveWindow.this.getLocation().y;
       
        return s;
    }

    // Listeners.
   
    @Override public void windowOpened(WindowEvent e) {}
    @Override public void windowIconified(WindowEvent e) {}
    @Override public void windowDeiconified(WindowEvent e) {}
    @Override public void windowDeactivated(WindowEvent e) {}
    @Override public void windowClosing(WindowEvent e) {}
    @Override public void windowActivated(WindowEvent e) {}
   
    @Override public void windowClosed(WindowEvent e) {
        LiveWindow.this.vidParent.setNeglectTicks(true);
    }

    @Override
    public void componentShown(ComponentEvent e) {
    }
   
    @Override
    public void componentResized(ComponentEvent e) {
        StaticMethods.speichereTextDatei(
                this.pars.getStdDirectory(),
                "liveWindowLocation.dat",
                this.getWindowDimensions(),
                this.pars);
    }
   
    @Override
    public void componentMoved(ComponentEvent e) {
        StaticMethods.speichereTextDatei(
                this.pars.getStdDirectory(),
                "liveWindowLocation.dat",
                this.getWindowDimensions(),
                this.pars);
    }
   
    @Override
    public void componentHidden(ComponentEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (LiveWindow.this.zoom) {
            try {
                LiveWindow.this.zoomRechteck.setRight(e.getX()
                        - LiveWindow.this.getInsets().left);
                LiveWindow.this.zoomRechteck.setBottom(e.getY()
                        - LiveWindow.this.getInsets().top);
                if (LiveWindow.this.zoomRechteck.upperLeftCorner().distance(
                                LiveWindow.this.zoomRechteck.lowerRightCorner()) > 0.0001) {
                    LiveWindow.this.vidParent.setZoom(LiveWindow.this.zoomRechteck);
                    LiveWindow.this.setZoom(false);
                }
                LiveWindow.this.zoomRechteck = null;
                LiveWindow.this.isHand = true;
            } catch (Exception e1) {
            }
        } else if (isHand) {
            handStartingPoint = null;
        } else {
            LiveWindow.this.vidParent.setClickPosition(
                    e.getPoint().x - LiveWindow.this.getInsets().left,
                    e.getPoint().y - LiveWindow.this.getInsets().top);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (LiveWindow.this.zoom) {
            try {
                if (LiveWindow.this.zoomRechteck == null) {
                    LiveWindow.this.zoomRechteck = new Rectangle2D(
                            new Vector2D(
                                    e.getX() - LiveWindow.this .getInsets().left,
                                    e.getY() - LiveWindow.this .getInsets().top),
                            new Vector2D(
                                    e.getX() - LiveWindow.this.getInsets().left,
                                    e.getY() - LiveWindow.this.getInsets().top));
                }
               
                LiveWindow.this.zoomRechteck.setLeft(e.getX() - LiveWindow.this.getInsets().left);
                LiveWindow.this.zoomRechteck.setTop(e.getY() - LiveWindow.this.getInsets().top);
                LiveWindow.this.zoomRechteck.setRight(e.getX() - LiveWindow.this.getInsets().left);
                LiveWindow.this.zoomRechteck.setBottom(e.getY() - LiveWindow.this.getInsets().top);
            } catch (Exception e1) {}
        } else if (isHand) {
            handStartingPoint = new Vector2D(e.getPoint());
        } else {
            // LiveWindow.this.vidParent.setClickPosition(
            // e.getPoint().x - LiveWindow.this.getInsets().left,
            // e.getPoint().y - LiveWindow.this.getInsets().top);
        }
    }

    @Override public void mouseExited(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == 2) {
            try {
                AbstractEnvironment2D<?> env2D = (AbstractEnvironment2D<?>) LiveWindow.this.vidParent.getEnvironment();
                centerChosenByClick = new Vector2D(env2D.getRealPosFromScreenPos(new Vector2D(
                    env2D.getScreenWidth() / 2, env2D.getScreenHeight() / 2)));
            } catch (Exception e2) {}
        }
       
        if (LiveWindow.this.zoom) {

        } else {
            if (e.getButton() == 3) {
                if (LiveWindow.this.ticks) {
                    LiveWindow.this.vidParent.neglectTicks();
                } else {
                    LiveWindow.this.vidParent.requestTicks();
                }
                LiveWindow.this.ticks = !LiveWindow.this.ticks;
                LiveWindow.this.metaInfVis.repaint();
            } else {

                if (e.getClickCount() > 1) {
                    LiveWindow.this.vidParent.unmarkSelektion();
                } else {
                    LiveWindow.this.vidParent.setClickPosition(
                            e.getPoint().x - LiveWindow.this.getInsets().left,
                            e.getPoint().y - LiveWindow.this.getInsets().top);
                }
            }
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        showTooltipp(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        LiveWindow.this.vidParent.setFollowAgent(false);
        if (isHand && handStartingPoint != null) {
            Vector2D midVector = new Vector2D(handStartingPoint);
            midVector.sub(new Vector2D(e.getPoint()));
            vidParent.adjustZoomBoxMiddle(midVector);
            handStartingPoint = new Vector2D(e.getPoint());
        } else {
            try {
                LiveWindow.this.zoomRechteck.setRight(e.getX() - LiveWindow.this.getInsets().left);
                LiveWindow.this.zoomRechteck.setBottom(e.getY() - LiveWindow.this.getInsets().top);
            } catch (Exception e1) {
            }
        }
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getWheelRotation() < 0) {
            LiveWindow.this.vidParent.setZoom2DPlus();
        } else {
            LiveWindow.this.vidParent.setZoom2DMinus();
        }
    }
}
TOP

Related Classes of eas.plugins.standard.visualization.LiveWindow

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.