Package eas.startSetup.marbBuilder

Source Code of eas.startSetup.marbBuilder.VisTrans

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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.Zwischenablage;
import eas.miscellaneous.system.windowFrames.WindHideAdapt;
import eas.simulation.spatial.sim2D.marbSimulation.translator.Translator;
import eas.startSetup.ParCollection;
import eas.startSetup.marbBuilder.darstellungsModi.ConstantsDarstellung;
import eas.startSetup.marbBuilder.darstellungsModi.DarstModEA;
import eas.startSetup.marbBuilder.graph.Knoten;
import eas.startSetup.marbBuilder.zeichenModi.ArrowMaster;


/**
* Applet zur Visualisierung eines Graphen.
*
* @author Lukas König.
*/
public class VisTrans extends Frame {

    /**
     * Am 14.09.06 generiert.
     */
    private static final long serialVersionUID = -6801708414284535034L;

    /**
     * Der aktuelle Graph, der ausgegeben werden soll.
     */
    private Translator aktTrans;

    /**
     * Der aktuell gewählte Knoten.
     */
    private Knoten gewKnot;

    /**
     * Der aktuell gewählte Knoten.
     */
    private Knoten selKnot;

    /**
     * Objekt, das die Polygone (u. �.) zur Darstellung eines Graphen erzeugt.
     */
    private ArrowMaster zeichenArt;

    /**
     * Objekt, das aktuell die Graphdarstellung erzeugt.
     */
    private DarstModEA darstArt;

    /**
     * Der aktuell anzuzeigende Graph.
     */
    private DargestellterGraph aktGraph;

    /**
     * Der Automat als Sequenz.
     */
    private JTextField sequenz;

    /**
     * Die Größe der Sequenz.
     */
    private JLabel groesse;

    /**
     * Der Name des zu <code>this</code> gehörenden Roboters.
     */
    private String robName;

    /**
     * Gibt an, ob gerade eine Simulation läuft.
     */
    private boolean simLaueft;

    /**
     * Die Parameter.
     */
    private ParCollection pars;

    /**
     * Implementierung einiger Methoden zum benutzen der Windows-Zwischenablage.
     */
    private Zwischenablage zwischenablage;
   
    /**
     * Die aktuelle Reihe des Layouts (dummy).
     */
    int row = 0;

    /**
     * Konstruktor.
     *
     * @param fenstertitel  Der als Titel anzuzeigende String.
     * @param trans         Der initial einzustellende Translator.
     * @param params        Der Parametersatz.
     */
    public VisTrans(
            final String        fenstertitel,
            final Translator    trans,
            final ParCollection params) {

        super(fenstertitel);

        this.aktTrans = trans;
        this.pars = params;
        this.simLaueft = false;

        this.init();
        this.start();
    }

    /**
     * Initialisierungsmethode (Roboter und Darstellung).
     */
    private void init() {
        this.setSize(200, 200);
        this.setLocation(new Point(400, 100));
        this.darstArt = new DarstModEA(
                50,
                ConstantsDarstellung.OBEN,
                this.getWidth() - 50,
                this.getHeight() - 50);

        this.aktGraph = this.darstArt.erzeuge(this.aktTrans, this.gewKnot);

        this.removeAll();
       
        this.addWindowListener(new WindHideAdapt());
       
        this.zwischenablage = new Zwischenablage();

        this.zeichenArt = new ArrowMaster(this.pars);
        this.gewKnot = null;

        this.setLayout(new BorderLayout());
       
        JPanel editPanel = new JPanel();
        editPanel.setLayout(new GridBagLayout());
        Panel masterPanel = new Panel();

        masterPanel.setLayout(new BorderLayout());
       
        editPanel.setPreferredSize(new Dimension(600, 20));
       
        JScrollPane scroll = new JScrollPane(editPanel);
        scroll.setPreferredSize(new Dimension(400, 100));

        this.sequenz = new JTextField("", 40);
        this.groesse = new JLabel("(9999 Bytes)", Label.LEFT);
       
        editPanel.add(this.groesse, this.getGridBagConstraint());
        editPanel.add(this.sequenz, this.getGridBagConstraint3());

        masterPanel.add(scroll);
        this.add(masterPanel, BorderLayout.NORTH);
       
        editPanel.add(new JLabel(), getGridBagConstraintEnd());

        this.setResizable(true);
    }

    /**
     * Dummy.
     *
     * @return Dummy.
     */
    private GridBagConstraints getGridBagConstraint() {
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 1;
        c.gridy = this.row;
        c.anchor = GridBagConstraints.LINE_START;
        return c;
    }

    /**
     * Dummy.
     *
     * @return Dummy.
     */
    private GridBagConstraints getGridBagConstraint3() {
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 3;
        c.gridy = this.row;

        c.anchor = GridBagConstraints.LINE_START;
        return c;
    }

    /**
     * Dummy.
     *
     * @return Dummy.
     */
    private GridBagConstraints getGridBagConstraintEnd() {
        GridBagConstraints x = new GridBagConstraints();
        row++;
        x.gridx = 5;
        x.gridy = 100;
        x.weightx = 1;
        x.weighty = 1;
        x.anchor = GridBagConstraints.LINE_START;
        return x;
    }

    /**
     * Die Startmethode des (ursprünglichen) Applets.
     */
    private void start() {

        this.addComponentListener(new ComponentListener() {
            @Override
            public void componentHidden(final ComponentEvent e) {

            }

            @Override
            public void componentMoved(final ComponentEvent e) {

            }

            @Override
            public void componentResized(final ComponentEvent e) {
                VisTrans.this.darstArt.setAusd(
                        50,
                        ConstantsDarstellung.OBEN,
                        VisTrans.this.getWidth() - 50,
                        VisTrans.this.getHeight() - 50);
                VisTrans.this.darstArt.loescheKnotenKoord();
                VisTrans.this.neuZeichnen();
            }

            @Override
            public void componentShown(final ComponentEvent e) {
               
            }
        }
        );

        this.addKeyListener(new KeyListener() {

            /**
             * Die Key-Typed-Methode.
             */
            @Override
            public void keyTyped(final KeyEvent e) {
   
            }
   
            /**
             * Die Key-Released-Methode.
             */
            @Override
            public void keyReleased(final KeyEvent e) {
   
            }

            /**
             * Die Key-Pressed-Methode.
             */
            @Override
            public void keyPressed(final KeyEvent e) {
                if (e.getKeyCode() == java.awt.event.KeyEvent.VK_C) {
                    if (e.isControlDown()) {
                        String copy = VisTrans.this.getSeq();

                        StaticMethods.log(StaticMethods.LOG_DEBUG,
                                "In Zwischenablage (" + copy + ")",
                                VisTrans.this.pars);
                        VisTrans.this.zwischenablage.copyToClipboard(copy);
                    }
                }

                if (e.getKeyCode() == java.awt.event.KeyEvent.VK_V) {
                    if (e.isControlDown()) {
                        String copy = VisTrans.this.zwischenablage.getClipbB();

                        StaticMethods.log(1, "Aus Zwischenablage (" + copy + ")",
                                VisTrans.this.pars);
                        VisTrans.this.erzeugeAusSeq(copy, null, false);
                        VisTrans.this.repaint();
                    }
                }
            }
        });

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent event) {
                if (!VisTrans.this.simLaueft) {
                    final int x = event.getX();
                    final int y = event.getY();

                    final int radius = (int) (
                       eas.startSetup.marbBuilder.zeichenModi.ConstantsZeichenModi.KN_DURCHM) / 2;
                    ArrayList<Knoten> naKn;

                    naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                    if (naKn.size() > 0) {
                        VisTrans.this.gewKnot = naKn.get(0);
                        VisTrans.this.selKnot = naKn.get(0);
                    } else {
                        VisTrans.this.selKnot = null;
                    }
                    VisTrans.this.neuZeichnen();
                }
            }

            @Override
            public void mouseClicked(final MouseEvent event) {
                if (!VisTrans.this.simLaueft) {
                    if (event.getButton() == 1 && event.getClickCount() == 2) {
                        final int x = event.getX();
                        final int y = event.getY();

                        final int radius = (int) (
                            eas.startSetup.marbBuilder.zeichenModi.ConstantsZeichenModi.KN_DURCHM)
                            / 2;
                        ArrayList<Knoten> naKn;

                        naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                        if (naKn.size() > 0) {
                            VisTrans.this.aktTrans.setStart(
                                    naKn.get(0));
                        } else {
                            VisTrans.this.aktTrans.entferneStartZ();
                        }

                        VisTrans.this.neuZeichnen();
                    } else if (event.getButton() == 3
                            && event.getClickCount() == 2) {
                        final int x = event.getX();
                        final int y = event.getY();

                        final int radius = (int) (
                            eas.startSetup.marbBuilder.zeichenModi.ConstantsZeichenModi.KN_DURCHM)
                            / 2;
                        ArrayList<Knoten> naKn;

                        naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                        if (naKn.size() > 0) {
                           VisTrans.this.aktTrans.einfStern(
                            naKn.get(0).holeName(),
                            new ArrayList<Integer>(
                                    VisTrans.this.aktTrans.holAdj().keySet()));
                            VisTrans.this.neuZeichnen();
                        }
                    } else if (event.getButton() == 3
                            && event.getClickCount() == 3) {
                        final int x = event.getX();
                        final int y = event.getY();
                        final int radius = (int) (
                            eas.startSetup.marbBuilder.zeichenModi.ConstantsZeichenModi.KN_DURCHM)
                            / 2;
                        ArrayList<Knoten> naKn;

                        naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                        if (naKn.size() == 0) {
                            VisTrans.this.aktTrans.einfClique(
                                new ArrayList<Integer>(
                                    VisTrans.this.aktTrans.holAdj().keySet()));
                            VisTrans.this.neuZeichnen();
                        }
                    }
                }
            }

            @Override
            public void mouseReleased(final MouseEvent event) {
                if (!VisTrans.this.simLaueft) {
                    VisTrans.this.gewKnot = null;
                }
            }
        });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(final MouseEvent event) {
                int x = event.getX();
                int y = event.getY();

                int un = VisTrans.this.getSize().height - 50;
                int re = VisTrans.this.getSize().width - 50;
                int li = 50;
               
                if (VisTrans.this.gewKnot != null) {
                    if (x < li) {
                        x = li;
                    }
                    if (x > re) {
                        x = re;
                    }
                    if (y < ConstantsDarstellung.OBEN) {
                        y = ConstantsDarstellung.OBEN;
                    }
                    if (y > un) {
                        y = un;
                    }

                    VisTrans.this.darstArt.neueKoord(
                            VisTrans.this.gewKnot,
                            x,
                            y);

                    VisTrans.this.aktGraph
                        = VisTrans.this.darstArt.erzeuge(
                                VisTrans.this.aktTrans,
                                VisTrans.this.selKnot);
                    VisTrans.super.update(VisTrans.this.getGraphics());
                    VisTrans.this.repaint();
                }
            }
        });
    }

    /**
     * Setzt die Knotenpositionen des Automaten zurück.
     */
    public void zuruecksetzen() {
        this.darstArt.loescheKnotenKoord();
        this.aktGraph = this.darstArt.erzeuge(this.aktTrans, this.gewKnot);
        this.repaint();
    }
   
    /**
     * Zeichnet den Graphen neu mit Löschen des Bildschirms.
     */
    public void neuZeichnen() {
        this.setSeq(this.aktTrans.erzeugeStringSeq());
        this.aktGraph = this.darstArt.erzeuge(this.aktTrans, this.gewKnot);
        this.repaint();
    }

    /**
     * Zeichenmethode.
     *
     * @param g
     *            Das Grafikobjekt.
     */
    @Override
    public void paint(final Graphics g) {
        this.groesse.setText("("
                + (this.sequenz.getText().length() + 2) / 5
                + " Bytes)");

        eas.math.geometry.Geometry2D.maleObjListe(g, this.zeichenArt.graph(this.aktGraph));

        this.requestFocus();
    }

    /**
     * Erzeugt den Automaten des Translators aus der übergebenen Sequenz
     * und richtet die entsprechenden Bildschirmausgaben ein.
     *
     * @param seq     Die Automatensequenz.
     * @param trans    Der Translator, mit dem die Sequenz übersetzt werden
     *                 soll.
     * @param pruefen  Ob die Sequenzen auf grammatische Fehler geprüft werden
     *                 soll.
     */
    public void erzeugeAusSeq(
            final String seq,
            final Translator trans,
            final boolean pruefen) {
        this.aktTrans.generateFromSequence(seq, trans, pruefen, this.pars);
        this.neuZeichnen();
    }

    /**
     * @return Returns the translator.
     */
    public Translator getTrans() {
        return this.aktTrans;
    }

    /**
     * Erzeugt eine Textausgabe von <code>this</code>.
     *
     * @return Textausgabe von <code>this</code>.
     */
    @Override
    public String toString() {
        return this.robName;
    }

    /**
     * @return Returns the selKnot of the active automaton.
     */
    public Knoten getAktSelKnot() {
        return this.gewKnot;
    }

    /**
     * @return Returns the graphName.
     */
    public String getGraphName() {
        return this.robName;
    }
   
    /**
     * @param trans  The ea to set.
     */
    public void setRob(final Translator trans) {
        if (this.simLaueft) {
            throw new RuntimeException("Darf nicht aufgerufen werden, solange"
                    + "eine Simulation läuft.");
        }
        this.aktTrans = trans;
    }

    /**
     * @param simLf
     *            The simLaueft to set.
     */
    public void setSimLaueft(final boolean simLf) {
        this.simLaueft = simLf;
    }

    /**
     * @return Returns the darstellArt.
     */
    public DarstModEA getDarstellArt() {
        return this.darstArt;
    }

    /**
     * @param darArt  The darstellArt to set.
     */
    public void setDarstellArt(final DarstModEA darArt) {
        this.darstArt = darArt;
    }

    /**
     * Gibt die Sequenzen als Strings zurück.
     *
     * @return  Die Sequenzen als Strings.
     */
    public String getSeq() {
        return this.sequenz.getText();
    }

    /**
     * Setzt eine Sequenz NUR in die Darstellung.
     *
     * @param s       Die zu setzende Sequenz.
     */
    public void setSeq(final String s) {
        this.sequenz.setText(s);
    }
   
    /**
     * Speichert die Darstellung des aktuellen Automaten in eine PNG-Datei.
     *
     * @param datNam  Der Name der Datei (ohne Erweiterung).
     */
    public void savePNGandPDF(final String datNam) {
        eas.math.geometry.Geometry2D.saveObjectsToFile(
                this.zeichenArt.graph(this.aktGraph),
                datNam,
                "png",
                this.pars);

        Vis.extractPDF(datNam, this.aktTrans, this.pars);
    }
}
TOP

Related Classes of eas.startSetup.marbBuilder.VisTrans

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.