Package simtools.diagram.test

Source Code of simtools.diagram.test.TestShapesContainer

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*
* (C) Copyright 2001-2007, by :
*     Corporate:
*         EADS Astrium
*     Individual:
*         Claude Cazenave
*
* $Id: TestShapesContainer.java,v 1.1 2009/01/08 16:43:05 ogor Exp $
*
* Changes
* -------
* 2 oct. 2008  : Initial public release
*
*/
package simtools.diagram.test;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.CompoundEdit;

import simtools.diagram.DiagramClipboard;
import simtools.diagram.DiagramComponent;
import simtools.diagram.Element;
import simtools.diagram.ElementsContainer;
import simtools.diagram.DiagramParameters;
import simtools.diagram.DiagramSelection;
import simtools.diagram.ElementSelection;
import simtools.diagram.gate.Connection;
import simtools.diagram.gate.Gate;
import simtools.diagram.gate.GatedComponent;
import simtools.diagram.undo.GateConnectEdit;

public class TestShapesContainer extends ArrayList implements ElementsContainer {
   
    protected transient TestDiagramComponent _comp;
    protected transient TestSelection _selection;

    public TestShapesContainer(){
        super();
       
        // Create the selection
        _selection = new TestSelection();
       
        // Create a component
        _comp = new TestDiagramComponent();

        // A random generation of rectangles
        Random z = new Random();
        for (int i = 0; i < 10; i++) {
            TestGatedRectangle trs = new TestGatedRectangle(
                    z.nextInt() & 0x3ff,
                    z.nextInt() & 0x3ff,
                    z.nextInt() & 0xff,
                    z.nextInt() & 0xff);

            add(trs);
        }
        for (int i = 0; i < 5; i++) {
            TestConnection trs = new TestConnection(
                    z.nextInt() & 0x3ff,
                    z.nextInt() & 0x3ff,
                    z.nextInt() & 0xff,
                    z.nextInt() & 0xff);

            add(trs);
        }
    }

   
    /* (non-Javadoc)
     * @see simtools.diagram.DiagramComponentElements#getDiagramComponent()
     */
    public DiagramComponent getComponent() {
        return _comp;
    }
   

    /* (non-Javadoc)
     * @see simtools.diagram.ElementsContainer#getSelection()
     */
    public DiagramSelection getSelection() {
        return _selection;
    }


    /* (non-Javadoc)
     * @see simtools.diagram.ElementsContainer#setComponent(simtools.diagram.DiagramComponent)
     */
    public void setComponent(DiagramComponent dc) {
        _comp = (TestDiagramComponent)dc;
    }


    /* (non-Javadoc)
     * @see simtools.diagram.ElementsContainer#setSelection(simtools.diagram.DiagramSelection)
     */
    public void setSelection(DiagramSelection ds) {
        _selection = (TestSelection)ds;
    }

    /**
     * A model selection enables to select and edit several parts of a model
     * diagram at a time
     *
     * @see DiagramSelection
     */
    public class TestSelection extends DiagramSelection {

        public TestSelection(){
            super(TestShapesContainer.this);
        }

        /* (non-Javadoc)
         * @see simtools.diagram.DiagramSelection#cloneElement(simtools.diagram.Element)
         */
        public Element cloneElement(Element e){
            Element res;
            if(e instanceof TestGatedRectangle){
                res = ((TestGatedRectangle)e).cloneTestGatedRectangle();

            } else  if(e instanceof TestConnection){
                res = ((TestConnection)e).cloneTestConnection();

            } else{
                res = super.cloneElement(e);
            }
            return res;
        }
    }
   
    public class TestDiagramComponent extends DiagramComponent {

        public TestDiagramComponent(){
            super(new DiagramParameters(), TestShapesContainer.this);
        }


        // Implementation of the abstract methods
        protected void translate(int x, int y) {
            for (int i = 0; i < TestShapesContainer.this.size(); i++) {
                Element s = (Element) TestShapesContainer.this.get(i);
                s.translate(x, y);
            }
        }


        // Overridden to store undo info
        protected void translationEnd() {
            // _compoundEdit has been
            _compoundEdit.end();
            fireUndoableEditUpdate( new UndoableEditEvent(this, _compoundEdit));
            super.translationEnd();
        }

        /* (non-Javadoc)
         * @see simtools.diagram.DiagramComponent#getMin(java.awt.Point)
         */
        protected void getMin(Point p) {
            for(int i=0;i< TestShapesContainer.this.size();i++){
                Element s = (Element)TestShapesContainer.this.get(i);
               
                Rectangle rect = s.getBounds();
                if (rect.x < p.x){
                    p.x = rect.x;
                }
               
                if (rect.y < p.y){
                    p.y = rect.y;
                }
            }
        }

        /* (non-Javadoc)
         * @see simtools.diagram.DiagramComponent#copyAt(java.util.Vector, int, int)
         */
        protected void copyAt(Vector v, int x, int y) {
            CompoundEdit compoundEdit = new CompoundEdit();

            List selectedConnectors = new ArrayList();
            List selectedGatedComponent = new ArrayList();

            Hashtable clonedGates = new Hashtable();

            for(int i=0;i<v.size();i++){
                Object o=v.elementAt(i);

                if (o instanceof Connection){
                    selectedConnectors.add(o);

                } else {
                    Element as= null;
                    if (o instanceof TestGatedRectangle){
                        as = ((TestGatedRectangle)o).cloneTestGatedRectangle();
                   
                    } else if (o instanceof TestConnection){
                        as = ((TestConnection)o).cloneTestConnection();
                    }

                    if (o instanceof GatedComponent){
                        selectedGatedComponent.add(o);

                        // Kept the relation between the existing and the new gate
                        List oldGates = new ArrayList(((GatedComponent)o).getGates());
                        List newGates = new ArrayList(((GatedComponent)as).getGates());

                        for(int j=0;j<oldGates.size(); j++){
                            clonedGates.put(oldGates.get(j), newGates.get(j));
                        }
                    }

                    as.translate(x,y);
                    TestShapesContainer.this.add(as);
                }
            }

            // Now Copy the connectors
            for (int i = 0; i < selectedConnectors.size(); i++) {
                Connection connection = (Connection) selectedConnectors.get(i);

                if (connection instanceof TestConnection){
                    Gate fisrtGate = (Gate) connection.getFirstEndGate();
                    Gate lastGate = (Gate) connection.getLastEndGate();

                    Gate newFg = null;
                    Gate newLg = null;

                    if ((fisrtGate!=null) && selectedGatedComponent.contains(fisrtGate.getOwner())){
                        newFg = (Gate) clonedGates.get(fisrtGate);
                    }
                    if ((lastGate!=null) && selectedGatedComponent.contains(lastGate.getOwner())){
                        newLg = (Gate) clonedGates.get(lastGate);
                    }

                    TestConnection newConnector = (TestConnection)((TestConnection)connection).cloneTestConnection();
                    newConnector.translate(x,y);

                    if (newFg != null){
                        ((Connection)newConnector).connect(newFg, true);
                        compoundEdit.addEdit(new GateConnectEdit( (Connection)newConnector, newFg, true));
                    }

                    if (newLg != null){
                        ((Connection)newConnector).connect(newLg, false);
                        compoundEdit.addEdit(new GateConnectEdit( (Connection)newConnector, newLg, false));
                    }

                    // Add it ton container
                    TestShapesContainer.this.add(newConnector);  
                }
            }

            compoundEdit.end();
            fireUndoableEditUpdate(new UndoableEditEvent(this, compoundEdit));
        }

        protected void drawDiagramElements(Graphics2D g2, Point pMax) {

            // loop on objects to be displayed
            //--------------------------------
            for (int i = 0; i < TestShapesContainer.this.size(); i++) {
                Element r = (Element) TestShapesContainer.this.get(i);
                boolean bsel = _selection.isSelected(r);
                if (bsel) {
                    g2.setColor(Color.red);
                } else {
                    g2.setColor(Color.blue);
                }

                ElementSelection dss=_selection.getSelectedShape(r);
                if(dss!=null){
                    dss.drawBounds(g2);
                }    
               
                g2.draw(r);

                // UPdate the bottom right point
                Rectangle rect = r.getBounds();
                if (rect.x + rect.width > pMax.x){
                    pMax.x = rect.x;
                }

                if (rect.y + rect.height > pMax.y){
                    pMax.y = rect.y;
                }
            }
        }

        protected void printDiagramElements(Graphics2D g2, Point pMax) {
            drawDiagramElements(g2, pMax);
        }

        protected void drawClipboardElements(Graphics2D g2, Point pMax) {
            int dx=_dragPoint.x;
            int dy=_dragPoint.y;

            g2.translate(dx,dy);

            DiagramClipboard dc=DiagramClipboard.get();

            // loop on objects to be displayed
            //--------------------------------
            g2.setColor(Color.green);
            for(int i=0;i<dc.size();i++){
                // connectors are not connected to connections
                // can't call getConnections() on
                // connections are connected to connectors
                // and have been copied to clipboard
                Element r= (Element)dc.get(i);


                g2.draw(r);

                // UPdate the bottom right point
                Rectangle rect = r.getBounds();
                if (rect.x + rect.width > pMax.x){
                    pMax.x = rect.x;
                }

                if (rect.y + rect.height > pMax.y){
                    pMax.y = rect.y;
                }

            }
            g2.translate(-dx,-dy);
        }
       

    }
}
TOP

Related Classes of simtools.diagram.test.TestShapesContainer

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.