Package fmg.fmg8.graphVis

Source Code of fmg.fmg8.graphVis.VisTrans

/*
* Datei: VisTrans.java
* Autor(en):        Lukas K�nig
* Java-Version:     6.0
* Erstellt:         10.03.2009
*
* (c) Lukas K�nig, die Datei unterliegt der LGPL
* -> http://www.gnu.de/lgpl-ger.html
*/

package fmg.fmg8.graphVis;

import fmg.fmg8.endlAutomat.translator.Translator;
import fmg.fmg8.graphVis.darstellungsModi.DarstModEA;
import fmg.fmg8.graphVis.darstellungsModi.Konstanten;
import fmg.fmg8.graphVis.fensterDialoge.WindHideAdapt;
import fmg.fmg8.graphVis.graph.Knoten;
import fmg.fmg8.graphVis.zeichenModi.PfeilMaster;
import fmg.fmg8.sonstiges.SonstMeth;
import fmg.fmg8.sonstiges.Zwischenablage;
import fmg.fmg8.statistik.Parametersatz;

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;


/**
* 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 PfeilMaster 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 Parametersatz 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 Parametersatz 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,
                Konstanten.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 PfeilMaster(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,
                        Konstanten.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.
             */
            public void keyTyped(final KeyEvent e) {
   
            }
   
            /**
             * Die Key-Released-Methode.
             */
            public void keyReleased(final KeyEvent e) {
   
            }

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

                        SonstMeth.log(SonstMeth.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();

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

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

                    final int radius = (int) (
                       fmg.fmg8.graphVis.zeichenModi.Konstanten.KN_DURCHM) / 2;
                    ArrayList<Knoten> naKn;

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

            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) (
                            fmg.fmg8.graphVis.zeichenModi.Konstanten.KN_DURCHM)
                            / 2;
                        ArrayList<Knoten> naKn;

                        naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                        if (naKn.size() > 0) {
                            VisTrans.this.aktTrans.setStart(
                                    (Knoten) 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) (
                            fmg.fmg8.graphVis.zeichenModi.Konstanten.KN_DURCHM)
                            / 2;
                        ArrayList<Knoten> naKn;

                        naKn = VisTrans.this.aktGraph.holeUmObj(x, y, radius);
                        if (naKn.size() > 0) {
                           VisTrans.this.aktTrans.einfStern(
                            ((Knoten) 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) (
                            fmg.fmg8.graphVis.zeichenModi.Konstanten.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();
                        }
                    }
                }
            }

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

        this.addMouseMotionListener(new MouseMotionAdapter() {
            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 < Konstanten.OBEN) {
                        y = Konstanten.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.
     */
    public void paint(final Graphics g) {
        this.groesse.setText("("
                + (this.sequenz.getText().length() + 2) / 5
                + " Bytes)");

        fmg.fmg8.graphVis.zeichenModi.Geometrie2D.maleObjListe(
                g,
                this.zeichenArt.graph(this.aktGraph),
                this.pars);

        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.erzeugeAusSequenz(seq, trans, pruefen);
        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>.
     */
    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 savePNG(final String datNam) {
        fmg.fmg8.graphVis.zeichenModi.Geometrie2D.saveObjectsToFile(
                this.zeichenArt.graph(this.aktGraph),
                datNam,
                "png",
                this.pars);
    }
}
TOP

Related Classes of fmg.fmg8.graphVis.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.