/*

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

import java.util.ArrayList;

import jhplot.jadraw.JaLatexText;
import jhplot.jadraw.JaObject;
import jhplot.jadraw.JaText;


/** A JaxoGraph is an extended version of a JaxoSaveGraph: in addition to the
 * Vector holding the list of JaxoObjects and the description of the graph,
 * it contains some information about how the graph is presented on the screen:
 * a backup list of JaxoObjects (used for undo), a file name (used for saving)
 * the currently selected object, etc.
 */
public class JaxoGraph implements Serializable {
    private JaxoSaveGraph saveGraph;
    private ArrayList backupList; //Hold the back up list for undo
    private String saveFileName;
    private boolean graphIsSaved = true;
    private JaObject selectedObject;
    private JaObject backupSelectedObject;


    /** Creates a new JaxoGraph with empty list of objects, empty description
     * and empty saveFileName.
     */
    public JaxoGraph() {
        this.saveGraph = new JaxoSaveGraph();
        this.backupList = new ArrayList(JaxoSaveGraph.LIST_INIT_SIZE);
        this.saveFileName = "";
    }

    /** Creates a new JaxoGraph with the given list of objects,
     * empty description and empty saveFileName.
     * @param list The new list of objects
     */
    public JaxoGraph(ArrayList list) {
        this.saveGraph = new JaxoSaveGraph(list);
        this.backupList = list;
        this.saveFileName = "";
    }

    /** Creates a new JaxoGraph with the given list of objects and description
     * and empty saveFileName.
     * @param list The new list of objects
     * @param describe String which decsribes this JaxoGraph
     */
    public JaxoGraph(ArrayList list, String describe) {
        this.saveGraph = new JaxoSaveGraph(list, describe);
        this.backupList = list;
        this.saveFileName = "";
    }

    /** Creates a new JaxoGraph with the given list of objects, description
     * and saveFileName.
     * @param list The new list of objects
     * @param describe String which decsribes this JaxoGraph
     * @param saveFile String holding the file name for saving
     */
    public JaxoGraph(ArrayList list, String describe, String saveFile) {
        this.saveGraph = new JaxoSaveGraph(list, describe);
        this.backupList = list;
        this.saveFileName = saveFile;
    }

    // methods to get/set the variables

    /** Returns the JaxoSaveGraph (i.e., the current list of objects and
     * the current description) of the current JaxoGraph.
     * @return The JaxoSaveGraph of the current JaxoGraph.
     */
    public final JaxoSaveGraph getSaveGraph() {
        return saveGraph;
    }

    /** Sets the JaxoSaveGraph (i.e., the current list of objects and
     * the current description) of the current JaxoGraph.
     * @param newSaveGraph The new JaxoSaveGraph.
     */
    public final void setSaveGraph(JaxoSaveGraph newSaveGraph) {
        this.saveGraph = newSaveGraph;
    }

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


    /** Reverse the object list
    */
    public final void reverse() {
      saveGraph.reverse(); 
    }

    /** Sets the list of objects of this JaxoGraph to list and
     * flags the JaxoGraph as not saved.
     * @param list The vector holding the new list of JaxoObjects.
     */
    public final void setObjectList(ArrayList list) {
        saveGraph.setObjectList(list);
        setSaved(false);
    }

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

    /** Sets the list of Latex packages of this JaxoGraph to list and
     * flags the JaxoGraph as not saved.
     * @param list The vector holding the new list of Latex packages.
     */
    public final void setPackageList(ArrayList list) {
        saveGraph.setPackageList(list);
        setSaved(false);
    }

    /** Sets the backup list of the current JaxoGraph to v.
     * @param v The new backup list.
     */
    public final void setBackupList(ArrayList v) {
        this.backupList = v;
    }

    /** Returns the backup list of the current JaxoGraph.
     * @return The backup list of the current JaxoGraph.
     */
    public final ArrayList getBackupList() {
        return backupList;
    }

    /** Sets the description text of this JaxoGraph and
     * flags the JaxoGraph as not saved.
     * @param describe The description of this JaxoGraph.
     */
    public final void setDescription(String describe) {
        saveGraph.setDescription(describe);
        setSaved(false);
    }

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

    /** Indicates whether the current JaxoGraph contains unsaved changes.
     * @return True if the Current JaxoGraph is saved.
     */
    public final boolean isSaved() {
        return graphIsSaved;
    }

    /** Declares the current JaxoGraph as saved or unsaved,
     * according to the boolean variable isSaved.
     * @param isSaved Boolean variable to indicate whether the current
     * JaxoGraph is saved or not.
     */
    public final void setSaved(boolean isSaved) {
        this.graphIsSaved = isSaved;
    }

    /** Sets the default file name for save operations.
     * @param fileName The file name to be used for saving.
     */
    public final void setSaveFileName(String fileName) {
        this.saveFileName = fileName;
    }

    /** Returns the current default save file name.
     * @return The current default save file name.
     */
    public final String getSaveFileName() {
        return saveFileName;
    }

    /** Returns the name of the current file (ie without the absolute path).
     * @return The current file
     */
    public final String getShortName() {
        File sname = new File(this.saveFileName);

        return sname.getName();
    }

    /** Returns the currently selected JaObject of this JaxoGraph.
     * @return The currently selected JaObject of this JaxoGraph.
     */
    public final JaObject getSelectedObject() {
        return selectedObject;
    }

    /** Sets the specified JaObject as currently selected.
     * @param newOb The JaObject to be set selected.
     */
    public final void setSelectedObject(JaObject newOb) {
        this.selectedObject = newOb;
    }

    /** Returns the current backupSelectedObject.
     * @return The current backupSelectedObject.
     */
    public final JaObject getBackupSelectedObject() {
        return backupSelectedObject;
    }

    /** Sets the specified JaObject to be the current backupSelectedObject.
     * @param newOb The JaObject to be set as backupSelectedObject.
     */
    public final void setBackupSelectedObject(JaObject newOb) {
        if (newOb != null) {
            this.backupSelectedObject = newOb.copy();
        }
    }

    /** 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) {
        return saveGraph.listElementAt(i);
    }

    /** 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 saveGraph.listSize();
    }

    // methods that modify the objectList

    /** Puts the specified JaObject into the foreground, i.e.,
     * to the last position in the object list. If the JaxoGraph was
     * modified by this operation, flags it as not saved.
     * @param object The JaObject to be put into the foreground.
     */
    public final void foreground(JaObject object) {
        boolean isLastElement = saveGraph.foreground(object);

        if (!isLastElement) {
            setSaved(false);
        }
    }

    /** Puts the specified JaObject into the background, i.e.,
     * to the first position in the object list. If the JaxoGraph was
     * modified by this operation, flags it as not saved.
     * @param object The JaObject to be put into the background.
     */
    public final void background(JaObject object) {
        boolean isFirstElement = saveGraph.background(object);

        if (!isFirstElement) {
            setSaved(false);
        }
    }

    /** Removes the specified JaObject from the list of objects
     * and flags the JaxoGraph as not saved.
     * @param object The JaObject to be removed from the list.
     */
    public final void delete(JaObject object) {
        saveGraph.delete(object);
        setSaved(false);
    }


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

    /** Adds the specified JaObject to the list of objects by appending it
     * and flags the JaxoGraph as not saved..
     * @param object The JaObject to be added to the list.
     */
    public final void addObject(JaObject object) {
        saveGraph.addObject(object);
        setSaved(false);
    }

    /** Removes all JaxoObjects from the list of objects.
     * If the JaxoGraph was modified by this operation, flags it as not saved.
     */
    public final void clear() {
        boolean listIsEmpty = saveGraph.clear();

        if (listIsEmpty) {
            setSaved(true);
        } else {
            setSaved(false);
        }
    }

    /** Removes all JaxoObjects from the list of objects and sets the
     * description to a string to size zero.
     * If the JaxoGraph was modified by this operation, flags it as not saved.
     */
    public final void clearAll() {
        boolean graphIsEmpty = saveGraph.clearAll();

        if (graphIsEmpty && (saveFileName.length() == 0)) {
            setSaved(true);
        } else {
            setSaveFileName("");
            setSaved(false);
        }
    }

    /** Exchanges the current list of objects with the
     * backup list and the current SelectedObject with the BackupSelectedObject
     * and sets the graph as not saved.
     */
    public final void undo() {
        ArrayList temp = copyFrom(backupList);
        setBackupList(copyFrom(getObjectList()));
        setObjectList(temp);

        JaObject ob = getBackupSelectedObject();
        setBackupSelectedObject(selectedObject);
        setSelectedObject(ob);
    }

    // other methods

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

    /** Copies the list of objects to the backup list and the selected
     * object to the backup selected object.
     */
    public final void backup() {
        if (copyFrom(getObjectList()).size() != 0) {
            setBackupList(copyFrom(getObjectList()));
            setBackupSelectedObject(getSelectedObject());
        }
    }

    /** Returns a copy of the specified vector.
     * @return A copy of v.
     * @param v A vector to be duplicated.
     */
    public final ArrayList copyFrom(ArrayList v) {
        ArrayList temp = new ArrayList(v.size());

        for (int i = 0; i < v.size(); i++) {
            JaObject cur = (JaObject) v.get(i);
            temp.add(cur.copy());
        }

        return temp;
    }

    /** Checks if there are PSText objects present in this current graph.
     * @return True, if there are PSTexts, false otherwise.
     */
    public final boolean containsPSText() {
        boolean contains = false;
        ArrayList templist = getObjectList();

        for (int j = 0; j < templist.size(); j++) {
            JaObject ob = (JaObject) templist.get(j);

            if (ob instanceof JaText) {
                contains = true;

                break;
            }

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

                if (contains) {
                    break;
                }
            }
        }

        return contains;
    }

    /** Checks if there are LatexText objects present in this current graph.
     * @return True, if there are LatexTexts, false otherwise.
     */
    public final boolean containsLatexText() {
        boolean contains = false;
        ArrayList templist = getObjectList();

        for (int j = 0; j < templist.size(); j++) {
            JaObject ob = (JaObject) templist.get(j);

            if (ob instanceof JaLatexText) {
                contains = true;

                break;
            }

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

                if (contains) {
                    break;
                }
            }
        }

        return contains;
    }

    /** Draws handles on all objects in this graph.
     * @param g2 The graphics context to draw the handles to.
     */
    public final void drawHandles(VectorGraphics g2) {
        for (int j = 0; j < listSize(); j++) {
            JaObject jaxoOb = (JaObject) listElementAt(j);
            jaxoOb.drawHandles(g2);
        }
    }

    /** Draws handles on all group objects in this graph.
     * @param g2 The graphics context to draw the handles to.
     */
    public final void drawGroupHandles(VectorGraphics g2) {
        for (int j = 0; j < listSize(); j++) {
            JaObject jaxoOb = (JaObject) listElementAt(j);

            if (jaxoOb instanceof JaxoGroup) {
                jaxoOb.drawHandles(g2);
            }
        }
    }

    /** Marks all objects in this graph.
     * @param marked A boolean that indicates whether to mark or not.
     */
    public final void setAsMarked(boolean marked) {
        for (int i = 0; i < listSize(); i++) {
            JaObject ob = (JaObject) listElementAt(i);
            ob.setAsMarked(marked);
        }
    }
}

 

Ads help maintain this website.