/*

 * 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 java.io.Serializable;

import java.util.ArrayList;

import jhplot.jadraw.JaObject;


/** A JaxoSaveGraph is the skeleton of a JaxoGraph: it contains just a Vector
 * holding the list of JaxoObjects and a description of the graph.
 */
public class JaxoSaveGraph implements Serializable {
    private ArrayList objectList;
    private String description;
    private ArrayList packageList;

    /** The default initial size for array lists. */
    public static final int LIST_INIT_SIZE = 5;

    /** Creates a new JaxoSaveGraph with empty list of objects and
     * empty description.
     */
    public JaxoSaveGraph() {
        this.objectList = new ArrayList(LIST_INIT_SIZE);
        this.description = "";
        this.packageList = new ArrayList(LIST_INIT_SIZE);
        packageList.add("axodraw");
        packageList.add("pstricks");
        packageList.add("color");
    }

    /** Creates a new JaxoSaveGraph with the given list of JaxoObjects.
     * @param list The new list of objects
     */
    public JaxoSaveGraph(ArrayList list) {
        this.objectList = list;
        this.description = "";
        this.packageList = new ArrayList(LIST_INIT_SIZE);
        packageList.add("axodraw");
        packageList.add("pstricks");
        packageList.add("color");
    }

    /** Creates a new JaxoSaveGraph with the given list of objects
     * and description.
     * @param list Vector that holds the JaxoObjects
     * @param describe String which decsribes this JaxoSaveGraph
     */
    public JaxoSaveGraph(ArrayList list, String describe) {
        this.objectList = list;
        this.description = describe;
        this.packageList = new ArrayList(LIST_INIT_SIZE);
        packageList.add("axodraw");
        packageList.add("pstricks");
        packageList.add("color");
    }



    // methods to get/set the variables

    /** Returns the Vector holding the list of JaxoObjects.
     * @return The vector holding the list of JaxoObjects.
     */
    public final ArrayList getObjectList() {
        return objectList;
    }

    /** Sets the list of JaxoObjects of this JaxoSaveGraph to list.
     * @param list The vector holding the new list of JaxoObjects.
     */
    public final void setObjectList(ArrayList list) {
        this.objectList = list;
    }

    /** Returns the Vector holding the list of Latex packages.
    * @return The vector holding the list of Latex packages.
    */
    public final ArrayList getPackageList() {
        return packageList;
    }


   /** Reverse the object list 
    */
    public final void reverse() {
      java.util.Collections.reverse(objectList);        
    }

    /** Sets the list of Latex packages of this JaxoSaveGraph.
     * @param list The vector holding the new list of  Latex packages.
     */
    public final void setPackageList(ArrayList list) {
        this.packageList = list;
    }

    /** Sets the description text of this JaxoSaveGraph.
     * @param describe The description of this JaxoSaveGraph.
     */
    public final void setDescription(String describe) {
        this.description = describe;
    }

    /** Returns the description text of this JaxoSaveGraph.
     * @return The decription of this JaxoSaveGraph.
     */
    public final String getDescription() {
        return description;
    }

    /** Returns the JaObject at position i of the object list.
     * @param i The index of the JaObject to be returned.
     * @return The JaObject at position i of the object list.
     */
    public final JaObject listElementAt(int i) {
        if (i <= listSize()) {
            return (JaObject) objectList.get(i);
        } else {
            return null;
        }
    }

    /** Returns the size of the current object list,
     * i.e., the number of objects it contains.
     * @return The number of objects currently in the object list.
     */
    public final int listSize() {
        return objectList.size();
    }

    // methods that modify the objectList

    /** Puts the specified JaObject into the foreground, i.e.,
     * to the last position in the object list.
     * @param object The JaObject to be put into the foreground.
     * @return True if the object was already in the foreground, i.e.,
     * the JaxoSaveGraph has not been modified.
     */
    public final boolean foreground(JaObject object) {
        boolean isLastElement = true;

        // Remove object from current list and append it
        int i = objectList.indexOf(object);

        if (i != (objectList.size() - 1)) {
            //JaObject goback = (JaObject) objectList.get(i);
            objectList.remove(i);
            objectList.add(object);
            isLastElement = false;
        }

        return isLastElement;
    }




    /** Puts the specified JaObject into the background, i.e.,
     * to the first position in the object list.
     * @param object The JaObject to be put into the background.
     * @return True if the JaObject was already in the background, i.e.,
     * the JaxoSaveGraph has not been modified.
     */
    public final boolean background(JaObject object) {
        boolean isFirstElement = true;

        // Remove object from current list and prepend it
        int i = objectList.indexOf(object);

        if (i != 0) {
            //JaObject goback = (JaObject) objectList.elementAt(i);
            objectList.remove(i);
            objectList.add(0, object);
            isFirstElement = false;
        }

        return isFirstElement;
    }

    /** Removes the specified JaObject from the list of objects.
     * @param object The JaObject to be removed from the list.
     */
    public final void delete(JaObject object) {
        if (objectList.contains(object)) {
            objectList.remove(objectList.indexOf(object));
        }
    }

    /** Adds the specified JaObject to the list of objects by appending it.
     * @param object The JaObject to be added to the list.
     */
    public final void addObject(JaObject object) {
        objectList.add(object);
    }


    /** Adds the specified JaObject to the first position 
     * @param object The JaObject to be added to the list.
     */
    public final void addFirst(JaObject object) {

        if (objectList.size() == 0) objectList.add(object);
        objectList.add(0, object);  
    }

    /** Removes all JaxoObjects from the list of objects.
     * @return True if the list was already empty before, i.e.,
     * the JaxoSaveGraph has not been modified.
     */
    public final boolean clear() {
        boolean listIsEmpty = objectList.isEmpty();

        if (!listIsEmpty) {
            objectList.clear();
        }

        return listIsEmpty;
    }

    /** Removes all JaxoObjects from the list of objects and sets the
     * description to a string to size zero.
     * @return True if the list of objects and the string holding the
     * description were already empty, i.e.,
     * the JaxoSaveGraph has not been modified.
     */
    public final boolean clearAll() {
        boolean graphIsEmpty = false;

        if (objectList.isEmpty() && (description.length() == 0)) {
            graphIsEmpty = true;
        } else {
            this.objectList.clear();
            setDescription("");
            graphIsEmpty = false;
        }

        return graphIsEmpty;
    }

    // other methods

    /** Returns the smallest bounding box that contains all the JaxoObjects
     * of this JaxoSaveGraph.
     * @return An array holding the bounds of the current JaxoSaveGraph:
     * bBox[0] = Xmin, bBox[1] = Ymin, bBox[2] = Xmax and bBox[3] = Ymax.
     */
    public final double[] getBoundingBox() {
        double minX = 0.;
        double maxX = 1.;
        double minY = 0.;
        double maxY = 1.;
        double[] bBox = {minX, minY, maxX, maxY};

        int length = objectList.size();

        for (int i = 0; i < length; i++) {
            JaObject ob = (JaObject) objectList.get(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;
    }
}

 

Ads help maintain this website.