/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import org.freehep.graphics2d.VectorGraphics;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import java.util.Vector;

import jhplot.jadraw.JaArcObject;
import jhplot.jadraw.JaBlob;
import jhplot.jadraw.JaBox;
import jhplot.jadraw.JaFillObject;
import jhplot.jadraw.JaGlArc;
import jhplot.jadraw.JaGlLine;
import jhplot.jadraw.JaGlLoop;
import jhplot.jadraw.JaLatexText;
import jhplot.jadraw.JaLineObject;
import jhplot.jadraw.JaLoopObject;
import jhplot.jadraw.JaObject;
import jhplot.jadraw.JaPArc;
import jhplot.jadraw.JaPLine;
import jhplot.jadraw.JaPLoop;
import jhplot.jadraw.JaText;
import jhplot.jadraw.JaVertexT2;
import jhplot.jadraw.JaVertexT4;
import jhplot.jadraw.JaVertexT5;
import jhplot.jadraw.JaZigZagLine;
import japlot.Plot;


/** Groups together a number of JaxoObjects. */
public class JaxoGroup extends JaObject {
    /** The default stroke (line width) of the objects of this group. */
    private float stroke;

    /** The default amplitude of all photon and gluon objects in this group. */
    private int groupAmp;

    /** The default size of the TeX labels of this group. */
    private int groupTeXSize;

    /** The default font of the PS labels of this group. */
    private Font groupTextFont;
    private Vector groupObList;

    //private Vector availableFonts;
    private String[] availableFonts;

    /** Constructor: creates a new empty vector and sets the dimensions
     *  to a default value.
     */
    public JaxoGroup() {
        this.groupObList = new Vector(5, 5);
        setRelw(INIT_SIZE);
        setRelh(INIT_SIZE);
        setStroke(JaxoPrefs.getFloatPref(JaxoPrefs.PREF_LINEWIDTH));
        setAmp(JaxoPrefs.getIntPref(JaxoPrefs.PREF_AMPLITUDE));
        setTeXSize(JaxoPrefs.getIntPref(JaxoPrefs.PREF_TEXSIZE));
        setTextFont(new Font(JaxoPrefs.getPref(JaxoPrefs.PREF_PSFAMILY),
                JaxoPrefs.getIntPref(JaxoPrefs.PREF_PSSTYLE),
                JaxoPrefs.getIntPref(JaxoPrefs.PREF_PSSIZE)));
    }

    /** Returns an exact copy of this JaxoGroup.
     * @return A copy of this JaxoGroup.
     */
    public final JaObject copy() {
        JaxoGroup temp = new JaxoGroup();

        temp.setObList(copyObList(groupObList));
        temp.setX(getX());
        temp.setY(getY());
        temp.setSize(getWidth(), getHeight(), getWidth(), getHeight());
        temp.setBoundingBox(getBoundingBox());
        temp.setStroke(getStroke());
        temp.setAmp(getAmp());
        temp.setTeXSize(getTeXSize());
        temp.setTextFont(getTextFont());

        return temp;
    }

    /** Returns true if all serializable variables of this JaObject
     *  and those of the specified one are equal.
     * @param comp A JaObject to compare with.
     * @return True if the objects are equal, false otherwise.
     */
    public final boolean isCopy(JaObject comp) {
        // Two groups are equal if they contain equal objects
        // at equal positions in the the object list vector.
        boolean isCopy = false;

        if (comp instanceof JaxoGroup) {
            JaxoGroup group = (JaxoGroup) comp;
            Vector v1 = getObList();
            Vector v2 = group.getObList();
            int length = v1.size();
            if (length == v2.size()) {
                boolean temp = true;
                for (int i = 0; i < length; i++) {
                    JaObject ob1 = (JaObject) v1.elementAt(i);
                    JaObject ob2 = (JaObject) v2.elementAt(i);
                    temp = temp && ob1.isCopy(ob2);
                }
                isCopy = temp;
            }
        }

        return isCopy;

    }

    /** Determines where on this JaObject a mouse click has ocurred.
     * @param clickX The x position of the point where the mouse click ocurred.
     * @param clickY The y position of the point where the mouse click ocurred.
     * @param editmode The current edit mode as defined in JaxoMainPanel.
     * @return An integer specifying whether the click ocurred on one of
     * the handles and if yes, on which.
     */
    public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
        int gx;
        int gy;
        int gwidth;
        int gheight;
        double[] gbox = getBoundingBox();

        gx = (int) gbox[JaObject.BB_MIN_X];
        gy = (int) gbox[JaObject.BB_MIN_Y];
        gwidth = (int) Math.round((gbox[JaObject.BB_MAX_X]
            - gbox[JaObject.BB_MIN_X]));
        gheight = (int) Math.round((gbox[JaObject.BB_MAX_Y]
            - gbox[JaObject.BB_MIN_Y]));

        if ((editmode == MOVE) || (editmode == COPY)) {
            if ((clickX >= (gx + gwidth))
                    && (clickX <= (gx + gwidth + LENGTH))
                    && (clickY >= (gy + gheight))
                    && (clickY <= (gy + gheight + LENGTH))) {
                return SELECT_BODY;
            }

            if ((clickX >= (gx - LENGTH)) && (clickX <= gx)
                    && (clickY >= (gy + gheight))
                    && (clickY <= (gy + gheight + LENGTH))) {
                return SELECT_BODY;
            }

            if ((clickX >= (gx + gwidth))
                    && (clickX <= (gx + gwidth + LENGTH))
                    && (clickY >= (getY() - LENGTH)) && (clickY <= getY())) {
                return SELECT_BODY;
            }

            if ((clickX >= (gx - LENGTH)) && (clickX <= gx)
                    && (clickY >= (gy - LENGTH)) && (clickY <= gy)) {
                return SELECT_BODY;
            }

            return SELECT_NONE;
        } else {
            return SELECT_NONE;
        }
    }

    /** Draws the handles of this group.
     * @param g2 The graphics context to draw to
     */
    public final void drawHandles(VectorGraphics g2) {
        int gx;
        int gy;
        int gwidth;
        int gheight;
        double[] gbox = getBoundingBox();

        gx = (int) gbox[JaObject.BB_MIN_X];
        gy = (int) gbox[JaObject.BB_MIN_Y];
        gwidth = (int) Math.round((gbox[JaObject.BB_MAX_X]
            - gbox[JaObject.BB_MIN_X]));
        gheight = (int) Math.round((gbox[JaObject.BB_MAX_Y]
            - gbox[JaObject.BB_MIN_Y]));

        g2.setColor(JaxoColor.RED);
        g2.setStroke(new BasicStroke(1.f));

        g2.drawRect(gx - LENGTH, gy - LENGTH, LENGTH, LENGTH);
        g2.drawRect(gx - LENGTH, gy + gheight, LENGTH, LENGTH);
        g2.drawRect(gx + gwidth, gy - LENGTH, LENGTH, LENGTH);
        g2.drawRect(gx + gwidth, gy + gheight, LENGTH, LENGTH);

        if (this.isMarked()) {
            g2.setColor(JaxoColor.GRAYSCALE150);
            g2.fillRect(gx - LENGTH + 1, gy - LENGTH + 1, LENGTH - 1,
                LENGTH - 1);
            g2.fillRect(gx - LENGTH + 1, gy + gheight + 1, LENGTH - 1,
                LENGTH - 1);
            g2.fillRect(gx + gwidth + 1, gy - LENGTH + 1, LENGTH - 1,
                LENGTH - 1);
            g2.fillRect(gx + gwidth + 1, gy + gheight + 1, LENGTH - 1,
                LENGTH - 1);
        }
    }

    /** Draws all the objects in this group.
     * @param g2 The graphics context to draw to
     * @param drawToScreen Determines whether the drawing is done
     * on the screen or to a printer
     */
    public final void jaxoDraw(VectorGraphics g2, boolean drawToScreen) {
        int length = groupObList.size();


           for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) groupObList.elementAt(i); 
                if (Plot.drawFirst(ob)) ob.jaxoDraw(g2, drawToScreen);
            }

            // this is second
            for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) groupObList.elementAt(i); 
                Plot.drawRest(ob,g2,drawToScreen);
            }



    }

    /** Displaces the group.
     * @param deltaX The displacement in x direction
     * @param deltaY The displacement in y direction
     */
    public final void moveBy(int deltaX, int deltaY) {
        int length = groupObList.size();

        super.moveBy(deltaX, deltaY);

        for (int i = 0; i < length; i++) {
            JaObject ob = (JaObject) groupObList.elementAt(i);
            ob.moveBy(deltaX, deltaY);
        }

        double[] bbox = getBoundingBox();
        setBoundingBox(bbox);
        setX((int) Math.round(bbox[JaObject.BB_MIN_X]));
        setY((int) Math.round(bbox[JaObject.BB_MIN_Y]));
        setRelw((int) Math.round(bbox[JaObject.BB_MAX_X]
            - bbox[JaObject.BB_MIN_X]));
        setRelh((int) Math.round(bbox[JaObject.BB_MAX_Y]
            - bbox[JaObject.BB_MIN_Y]));
    }

    /** The latex command of this group (obsolete because every JaObject
     * has its own latex command).
     * @param scale A scale factor to translate Java coordinates to
     * LaTeX coordinates.
     * @param canvasDim The current dimension of the canvas.
     * @return The string "%"
     */
    public final String latexCommand(float scale, Dimension canvasDim) {
        String command = "%";

        return command;
    }

    /** The latex command setting the width of this group
     * (obsolete because every JaObject has its own latexWidth).
     * @return The string "%"
     */
    public final String latexWidth() {
        String lineWidth = "%";

        return lineWidth;
    }

    /** Gets the bounding box of this group.
     * @return The bounding box of the group, i.e. the one envelopping
     * all the objects in this group.
     */
    public final double[] getBoundingBox() {
        double minX = 0.;
        double maxX = 1.;
        double minY = 0.;
        double maxY = 1.;
        double[] bBox = {minX, minY, maxX, maxY};

        int length = groupObList.size();

        for (int i = 0; i < length; i++) {
            JaObject ob = (JaObject) groupObList.elementAt(i);
            bBox = ob.getBoundingBox();

            if (i == 0) {
                minX = bBox[JaObject.BB_MIN_X];
                minY = bBox[JaObject.BB_MIN_Y];
                maxX = bBox[JaObject.BB_MAX_X];
                maxY = bBox[JaObject.BB_MAX_Y];
            } else {
                if (bBox[JaObject.BB_MIN_X] < minX) {
                    minX = bBox[JaObject.BB_MIN_X];
                }

                if (bBox[JaObject.BB_MIN_Y] < minY) {
                    minY = bBox[JaObject.BB_MIN_Y];
                }

                if (bBox[JaObject.BB_MAX_X] > maxX) {
                    maxX = bBox[JaObject.BB_MAX_X];
                }

                if (bBox[JaObject.BB_MAX_Y] > maxY) {
                    maxY = bBox[JaObject.BB_MAX_Y];
                }
            }
        }

        bBox[JaObject.BB_MIN_X] = minX;
        bBox[JaObject.BB_MIN_Y] = minY;
        bBox[JaObject.BB_MAX_X] = maxX;
        bBox[JaObject.BB_MAX_Y] = maxY;

        return bBox;
    }

    /** Removes all JaxoObjects from the Group. */
    public final void clearGroup() {
        groupObList.removeAllElements();
    }

    /** Returns the JaxoObjects in this group.
     * @return A vector containing all the JaxoObjects of this group
     */
    public final Vector getObList() {
        return groupObList;
    }

    /** Sets the JaxoObjects of this group.
     * @param obList A vector containing all the JaxoObjects to be set
     * for this group
     */
    public final void setObList(Vector obList) {
        this.groupObList = obList;
    }

    /** Returns the number of JaxoObjects in this group.
     * @return The size of the vector of JaxoObjects in this group
     */
    public final int groupObListSize() {
        return groupObList.size();
    }

    private Vector copyObList(Vector obList) {
        Vector newoblist = new Vector(5, 5);

        for (int i = 0; i < obList.size(); i++) {
            JaObject ob = (JaObject) obList.elementAt(i);
            JaObject newob = ob.copy();
            newoblist.add(newob);
        }

        return newoblist;
    }

    /** Rescales this JaxoGroup by the scale factor scale,
     * keeping the point (orx, ory) fixed.
     * @param orx The x-coordinate of the fixed point
     * @param ory The y-coordinate of the fixed point
     * @param scale The scale parameter
     */
    public final void rescaleObject(int orx, int ory, float scale) {
        double[] boundbox = this.getBoundingBox();

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);
            ob.rescaleObject(orx, ory, scale);
        }

        double[] newbb =
        {
            boundbox[JaObject.BB_MIN_X], boundbox[JaObject.BB_MIN_Y],
            ((boundbox[JaObject.BB_MAX_X] - boundbox[JaObject.BB_MIN_X])
                * scale) + boundbox[JaObject.BB_MIN_X],
            ((boundbox[JaObject.BB_MAX_Y] - boundbox[JaObject.BB_MIN_Y])
                * scale) + boundbox[JaObject.BB_MIN_Y]
        };
        this.setBoundingBox(newbb);
        this.setX((int) Math.round(newbb[0]));
        this.setY((int) Math.round(newbb[1]));
        this.setRelw((int) Math.round(newbb[2] - newbb[0]));
        this.setRelh((int) Math.round(newbb[3] - newbb[1]));
    }

    /** Returns the stroke property of this group object.
     * @return The stroke property of this group object.
     */
    public final float getStroke() {
        return stroke;
    }

    /** Sets the stroke property of this group object.
     * @param newStroke The stroke property of this group object.
     */
    public final void setStroke(float newStroke) {
        this.stroke = newStroke;
    }

    /** Returns the groupAmp property of this group object.
     * @return The groupAmp property of this group object.
     */
    public final int getAmp() {
        return groupAmp;
    }

    /** Sets the groupAmp property of this group object.
     * @param amp The groupAmp property of this group object.
     */
    public final void setAmp(int amp) {
        this.groupAmp = amp;
    }

    /** Returns the groupTeXSize property of this group object.
     * @return The groupTeXSize property of this group object.
     */
    public final int getTeXSize() {
        return groupTeXSize;
    }

    /** Sets the groupTeXSize property of this group object.
     * @param teXSize The groupTeXSize property of this group object.
     */
    public final void setTeXSize(int teXSize) {
        this.groupTeXSize = teXSize;
    }

    /** Returns the groupTextFont property of this text object.
     * @return The groupTextFont property of this text object.
     */
    public final Font getTextFont() {
        return groupTextFont;
    }

    /** Sets the groupTeXSize property of this group object.
     * @param textFont The groupTeXSize property of this group object.
     */
    public final void setTextFont(Font textFont) {
        this.groupTextFont = textFont;
    }

    /** Sets the color for all objects in this group.
     * @param newColor The color to be set.
     */
    public final void setGroupColor(Color newColor) {
        this.setColor(newColor);

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (!(ob instanceof JaLatexText)) {
                if (ob instanceof JaxoGroup) {
                    ob.setColor(newColor);
                    ((JaxoGroup) ob).setGroupColor(newColor);
                } else if (ob instanceof JaBlob || ob instanceof JaBox
                        || ob instanceof JaVertexT5) {
                    if (!(
                                JaxoColor.isGrayScale(
                                    ((JaFillObject) ob).getFillColor())
                            )) {
                        ob.setColor(newColor);
                    }
                } else {
                    ob.setColor(newColor);
                }
            }
        }
    }

    /** Sets the stroke property (line width) for all the objects
     * in this group.
     * @param newStroke The stroke to be set
     */
    public final void setGroupStroke(float newStroke) {
        this.setStroke(newStroke);

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaLineObject) {
                ((JaLineObject) ob).setStroke(newStroke);
            } else if (ob instanceof JaLoopObject) {
                ((JaLoopObject) ob).setStroke(newStroke);
            } else if (ob instanceof JaArcObject) {
                ((JaArcObject) ob).setStroke(newStroke);
            } else if (ob instanceof JaBox) {
                ((JaBox) ob).setStroke(newStroke);
            } else if (ob instanceof JaBlob) {
                ((JaBlob) ob).setStroke(newStroke);
            } else if (ob instanceof JaVertexT2) {
                ((JaVertexT2) ob).setStroke(newStroke);
            } else if (ob instanceof JaVertexT4) {
                ((JaVertexT4) ob).setStroke(newStroke);
            } else if (ob instanceof JaVertexT5) {
                ((JaVertexT5) ob).setStroke(newStroke);
            } else if (ob instanceof JaxoGroup) {
                ((JaxoGroup) ob).setGroupStroke(newStroke);
            }
        }
    }

    /** Determines whether there are photon or gluon objects
     * present in this group.
     * @return A boolean parameter
     */
    public final boolean areAmpObject() {
        boolean thereare = false;

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaxoGroup) {
                thereare = ((JaxoGroup) ob).areAmpObject();
            } else if (ob instanceof JaGlLine || ob instanceof JaPLine
                    || ob instanceof JaZigZagLine || ob instanceof JaPLoop
                    || ob instanceof JaGlLoop || ob instanceof JaGlArc
                    || ob instanceof JaPArc) {
                thereare = true;

                break;
            }
        }

        return thereare;
    }

    /** Sets the amplitude for all the objects in this group.
     * @param newAmp The amplitude to be set.
     */
    public final void setGroupAmp(int newAmp) {
        this.setAmp(newAmp);

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaLineObject) {
                ((JaLineObject) ob).setAmp(newAmp);
            } else if (ob instanceof JaLoopObject) {
                ((JaLoopObject) ob).setAmp(newAmp);
            } else if (ob instanceof JaArcObject) {
                ((JaArcObject) ob).setAmp(newAmp);
            } else if (ob instanceof JaxoGroup) {
                ((JaxoGroup) ob).setGroupAmp(newAmp);
            }
        }
    }

    /** Determines whether there are LaTeX labels present in this group.
      * @return A boolean parameter.
      */
    public final boolean areTeXLabels() {
        boolean thereare = false;

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaxoGroup) {
                thereare = ((JaxoGroup) ob).areTeXLabels();

                if (thereare) {
                    break;
                }
            } else if (ob instanceof JaLatexText) {
                thereare = true;

                break;
            }
        }

        return thereare;
    }

    /** Determines whether there are PS labels present in this group.
      * @return A boolean parameter.
      */
    public final boolean arePSLabels() {
        boolean thereare = false;

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaxoGroup) {
                thereare = ((JaxoGroup) ob).arePSLabels();

                if (thereare) {
                    break;
                }
            } else if (ob instanceof JaText) {
                thereare = true;

                break;
            }
        }

        return thereare;
    }

    /** Sets the font size for all the LaTeX labels in this group.
     * @param newSize The size to be set.
     */
    public final void setGroupTeXSize(int newSize) {
        this.setTeXSize(newSize);

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaLatexText) {
                ((JaLatexText) ob).setLatexFS(newSize);
            } else if (ob instanceof JaxoGroup) {
                ((JaxoGroup) ob).setGroupTeXSize(newSize);
            }
        }
    }

    /** Sets the font for all the PS labels in this group.
     * @param newFont The size to be set.
     */
    public final void setGroupTextFont(Font newFont) {
        this.setTextFont(newFont);

        for (int i = 0; i < this.groupObListSize(); i++) {
            JaObject ob = (JaObject) this.getObList().elementAt(i);

            if (ob instanceof JaText) {
                ((JaText) ob).setFont(newFont);
            } else if (ob instanceof JaxoGroup) {
                ((JaxoGroup) ob).setGroupTextFont(newFont);
            }
        }
    }

    /** Brings up the edit panel that allows to change the parameters of
     * this object.
     * @return True if the editing actually changed the object, false if
     * the object has not been changed.
     */
    public final boolean editPanel() {
        if (availableFonts == null) {
            availableFonts = JaxoStaticCanvasMethods.getSystemFonts();
        }
        JaxoGroupOptionsPanel groupop =
            new JaxoGroupOptionsPanel(this, availableFonts);

        return groupop.hasChanged();
    }

    /** Sets the vector of available fonts.
     * @param fonts The vector of available fonts.
     */
    public final void setAvailableFonts(String[] fonts) {
        this.availableFonts = fonts;
    }
}

 

Ads help maintain this website.