BaseGraphModel.java :  » UML » MetaBoss » com » metaboss » applications » designstudio » Java Open Source

Java Open Source » UML » MetaBoss 
MetaBoss » com » metaboss » applications » designstudio » BaseGraphModel.java
// THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
// OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Copyright 2000-2005  Softaris Pty.Ltd. All Rights Reserved.
package com.metaboss.applications.designstudio;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.jgraph.event.GraphModelEvent;
import org.jgraph.event.GraphModelListener;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Edge;
import org.jgraph.graph.GraphConstants;

import com.metaboss.applications.designstudio.components.DesignGraph;
import com.metaboss.applications.designstudio.components.VertexCell;
import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
import com.metaboss.sdlctools.models.metabossmodel.visualmodel.ColorUtils;
import com.metaboss.sdlctools.models.metabossmodel.visualmodel.Diagram;
import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DiagramModelElement;

/*    Base class for graph models    */

public class BaseGraphModel extends DefaultGraphModel
{
  public final static int    UNDEFINED = -1000;
  
  public static boolean isUndefinedPosition(Point2D lPosition)
  {
    return (lPosition.getX()==UNDEFINED && lPosition.getY()==UNDEFINED);
  }

  protected ModelElement             mObject = null;
  public Diagram                 mDiagram = null;
  protected boolean               mChanged = false;
  protected boolean               mLoading = true;
  protected HashMap                mVertexPorts = new HashMap();
  public Point2D                 mOldEdgePoint = null;
  public DesignGraph               mGraph = null;
    public DesignGraphModelListener    mModelListener = new DesignGraphModelListener();

  /** Constructor. */
  public BaseGraphModel(ModelElement pObject, Diagram pDiagram) throws Exception
  {
    super();
    
    mObject = pObject;
    mDiagram = pDiagram;
    setModelListener();
    loadModel();
  }

  // reload graph model
  public void loadModel() throws Exception
  {
    //???loadVertexesPositions();
  }
  
  // get diagram interface
  /*public Diagram getDiaram() throws Exception
  {
    return null;
  }*/

  // add vertex into the model
  public DefaultGraphCell addVertex(BaseUserObject pUserObject, 
    Point pPosition, Dimension pSize, Map pStyle) throws Exception
  {
    if (pUserObject==null) return null;
    if (pPosition==null) pPosition = new Point(10, 10);

    DefaultGraphCell     lCell = new VertexCell(pUserObject);
    DefaultPort       lPort = new DefaultPort(pUserObject);
    Map           lAttributes = new Hashtable();
    ArrayList         lModelRoots = new ArrayList();
    ConnectionSet       lConnectionSet = new ConnectionSet();
    
    if (pSize!=null)
      GraphConstants.setBounds(pStyle, new Rectangle(pPosition, pSize));
    else    
      GraphConstants.setBounds(pStyle, new Rectangle(pPosition));
    
    Color lColor = getVertexColor(pUserObject.getBOObject());
    if (lColor!=null) GraphConstants.setBackground(pStyle, lColor);
    
    lAttributes.put(lCell, pStyle);
    lCell.add(lPort);
    mVertexPorts.put(pUserObject.getID(), lPort);
    lModelRoots.add(lCell); 

    insert(lModelRoots.toArray(), lAttributes, lConnectionSet, null, null);
    if (mGraph!=null) mGraph.setSelectionCell(lCell);
    return lCell;
  }

  // add graph edge
  public DefaultEdge addEdge(BaseUserObject pUserObject, Point pLabelPosition,  
    java.util.List pPoints, String pIDFrom, String pIDTo, Map pStyle)
  {
    ArrayList         lModelRoots = new ArrayList();
    ConnectionSet       lConnectionSet = new ConnectionSet();
    Map           lAttributes = new Hashtable();
    DefaultEdge       lEdge = new DefaultEdge(pUserObject);
    
    Object          lFromPort = getPort(pIDFrom);
    Object          lToPort = getPort(pIDTo);

    if (lFromPort==null)
    {
      lFromPort = addTempVertex(mOldEdgePoint);
      Point lPoint = (mOldEdgePoint!=null) ? new Point((int)mOldEdgePoint.getX(), (int)mOldEdgePoint.getY()) : new Point(10, 10);
      if (pPoints==null) pPoints = new ArrayList();
      pPoints.add(lPoint);
    }
    if (lToPort==null)
    {
      lToPort = addTempVertex(mOldEdgePoint);
      Point lPoint = (mOldEdgePoint!=null) ? new Point((int)mOldEdgePoint.getX(), (int)mOldEdgePoint.getY()) : new Point(10, 10);
      if (pPoints==null) pPoints = new ArrayList();
      pPoints.add(lPoint);
    }

    lConnectionSet.connect(lEdge, lFromPort, lToPort);
    lAttributes.put(lEdge, pStyle);
    lModelRoots.add(lEdge);
    specifyEdgePoints(lEdge, pPoints);
    insert(lModelRoots.toArray(), lAttributes, lConnectionSet, null, null);
    
    if (pLabelPosition!=null)
      GraphConstants.setLabelPosition(lEdge.getAttributes(), pLabelPosition);
      
    if (mGraph!=null) mGraph.setSelectionCell(lEdge);
        
        return lEdge;
  }

  // add temporary vertex
  public DefaultPort addTempVertex(Point2D pPoint)
  {
    Point          lPoint = (pPoint!=null) ? new Point((int)pPoint.getX(), (int)pPoint.getY()) : new Point(10, 10);
    Map           lPortAttributes = new Hashtable();
    DefaultGraphCell     lPortCell = new VertexCell("test");
    DefaultPort       lPort = new DefaultPort("test");
    ArrayList         lPortModelRoots = new ArrayList();
    Map            lPortStyle = GraphConstants.createMap();
  
    GraphConstants.setBounds(lPortStyle, new Rectangle(lPoint));
    lPortAttributes.put(lPortCell, lPortStyle);
    lPortModelRoots.add(lPortCell); 
    lPortCell.add(lPort);
    insert(lPortModelRoots.toArray(), lPortAttributes, null, null, null);

    return lPort;
  }
  
  // remove temporary vertexes
  public void removeTempVertexes()
  {
    DefaultGraphCell lCell = findElementCell("test");
    if (lCell!=null)
    {
      ArrayList lList = new ArrayList();
      lList.add(lCell); 
      Set lEdgesSet = BaseGraphModel.getEdges(this, lList.toArray());
      if (lEdgesSet==null || lEdgesSet.size()==0)
        remove(lList.toArray());
    }
  }

  // save graph objects positions
  public void savePositions()
  {
    if (!getChanged() || mDiagram==null) return;

    try
    {    
      removeTempVertexes();
      Application.beginTransaction();
      try
      {
        for (int i=0; i<getRootCount(); i++)
        {
          DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
          saveCellPosition(lObject);
          saveCellColor(lObject);
        }        
        Application.commit();
      }
      finally
      {
        if (Application.isInTransaction()) Application.rollback();
      }
    }
    catch(Exception e)
    {
      e.printStackTrace(); 
    }    
    setChanged(false);
  }
      
  public void setLoading(boolean pValue)
  {
    mLoading = pValue;
  }
    
  // get UserObject Cells
  public Object[] getUserObjectCells(BaseUserObject pObject)
  {
    ArrayList pList = new ArrayList();
    if (pObject!=null)
    {
      try
      {
        for (int i=0; i<getRootCount(); i++)
        {
          DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
          Object lCellUserObject = lObject.getUserObject();

          if (lCellUserObject instanceof BaseUserObject)
            if (pObject.getClass().equals(lCellUserObject.getClass()) &&
              ((BaseUserObject)lCellUserObject).getID().equals(pObject.getID())) 
              pList.add(lObject); 
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return pList.toArray();
  }
  public Object[] getUserObjectCells(String pID)
  {
    ArrayList pList = new ArrayList();
    try
    {
      for (int i=0; i<getRootCount(); i++)
      {
        DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
        Object lCellUserObject = lObject.getUserObject();

        if (lCellUserObject instanceof BaseUserObject)
          if (((BaseUserObject)lCellUserObject).getID().equals(pID)) 
            pList.add(lObject); 
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return pList.toArray();
  }
    
  // find user object by id
  public BaseUserObject findUserObjectByID(String pID)
  {
    for (int i=0; i<getRootCount(); i++)
    {
      DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
      Object lCellUserObject = lObject.getUserObject();

      if (lCellUserObject instanceof BaseUserObject)
        if (((BaseUserObject)lCellUserObject).getID().equals(pID)) 
          return (BaseUserObject)lCellUserObject; 
    }
    return null;
  }
    
  // get element package
  public MetaBossModelPackage getPackage()
  {
    return BaseUserObject.getObjectPackage(mObject);
  }
    
  // clear edge points
  protected void clearPoints(ModelElement pEdge)
  {
    DiagramModelElement lDiagramElement = getModelElement(pEdge, false);
    if (lDiagramElement!=null)
    {
      Object[] lPoints = lDiagramElement.getGraphicFeatures().toArray();
      for (int i=0; i<lPoints.length; i++)
      {
        ModelElement lElement = (ModelElement)lPoints[i];
        if (lElement.getDescription()!=null && lElement.getDescription().equals("EdgePoint"))
        {
          lDiagramElement.getGraphicFeatures().remove(lElement);
          lElement.refDelete();
        }
      }
    }
  }
  protected void clearPoints(ModelElement pEdge, String pName)
  {
    DiagramModelElement lDiagramElement = getModelElement(pEdge, pName, false);
    if (lDiagramElement!=null)
    {
      Object[] lPoints = lDiagramElement.getGraphicFeatures().toArray();
      for (int i=0; i<lPoints.length; i++)
      {
        ModelElement lElement = (ModelElement)lPoints[i];
        if (lElement.getDescription()!=null && lElement.getDescription().equals("EdgePoint"))
        {
          lDiagramElement.getGraphicFeatures().remove(lElement);
          lElement.refDelete();
        }
      }
    }
  }

  // save edge points
  protected void saveEdgePoints(ModelElement pEdge, java.util.List pPoints) 
  {
    clearPoints(pEdge);
    DiagramModelElement lDiagramElement = getModelElement(pEdge, true);
    if (lDiagramElement!=null && pPoints!=null && pPoints.size()>2)
    {
      for (int z=1; z<pPoints.size()-1; z++)
      {
        Point2D lPoint = (Point2D)pPoints.get(z);
        com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage().getVisualModel().getPoint().createPoint();
        lPosition.setDescription("EdgePoint");
        lPosition.setX((int)lPoint.getX());
        lPosition.setY((int)lPoint.getY());
        lDiagramElement.getGraphicFeatures().add(lPosition);  
      }
    }
  }
  protected void saveEdgePoints(ModelElement pEdge, String pName, java.util.List pPoints) 
  {
    clearPoints(pEdge, pName);
    DiagramModelElement lDiagramElement = getModelElement(pEdge, pName, true);
    if (lDiagramElement!=null && pPoints!=null && pPoints.size()>2)
    {
      for (int z=1; z<pPoints.size()-1; z++)
      {
        Point2D lPoint = (Point2D)pPoints.get(z);
        com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage().getVisualModel().getPoint().createPoint();
        lPosition.setDescription("EdgePoint");
        lPosition.setX((int)lPoint.getX());
        lPosition.setY((int)lPoint.getY());
        lDiagramElement.getGraphicFeatures().add(lPosition);  
      }
    }
  }

  // specify edge points
  protected void specifyEdgePoints(DefaultEdge pEdge, java.util.List pPoints)
  {
    if (pPoints!=null && pPoints.size()>0)
    {
      java.util.List lList = GraphConstants.getPoints(pEdge.getAttributes());  
      for (int i=0; i<pPoints.size(); i++)
      {
        Point2D lPosition = (Point2D)pPoints.get(i);
        if (lPosition!=null && !isUndefinedPosition(lPosition))
          lList.add(lList.size()-1, pPoints.get(i));
      }  
      GraphConstants.setPoints(pEdge.getAttributes(), lList);
    }
  }

  // load points for edge
  protected java.util.List loadEdgePoints(ModelElement pEdge)
  {
    java.util.List   lResult = new ArrayList();
    DiagramModelElement lDiagramElement = getModelElement(pEdge, false);
    if (lDiagramElement!=null)
    {
      Object[] lPoints = lDiagramElement.getGraphicFeatures().toArray();
      for (int i=0; i<lPoints.length; i++)
      {
        ModelElement lElement = (ModelElement)lPoints[i];
        if (lElement.getDescription()!=null && lElement.getDescription().equals("EdgePoint"))
        {
          com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition =
            (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point)lElement;   
          Point lPoint = new Point(lPosition.getX(), lPosition.getY());
          lResult.add(lPoint);
        }
      }
    }
    return lResult;
  }
  protected java.util.List loadEdgePoints(ModelElement pEdge, String pName)
  {
    java.util.List   lResult = new ArrayList();
    DiagramModelElement lDiagramElement = getModelElement(pEdge, pName, false);
    if (lDiagramElement!=null)
    {
      Object[] lPoints = lDiagramElement.getGraphicFeatures().toArray();
      for (int i=0; i<lPoints.length; i++)
      {
        ModelElement lElement = (ModelElement)lPoints[i];
        if (lElement.getDescription()!=null && lElement.getDescription().equals("EdgePoint"))
        {
          com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition =
            (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point)lElement;   
          Point lPoint = new Point(lPosition.getX(), lPosition.getY());
          lResult.add(lPoint);
        }
      }
    }
    return lResult;
  }
  
  // create circled points
  protected void createCircledPoints(Point pStartPoint, java.util.List pPoints)
  {
    if (pStartPoint==null || pPoints==null) return;
  
    Point lPoint00 = new Point(pStartPoint);
    Point lPoint01 = new Point(pStartPoint);
    Point lPoint11 = new Point(pStartPoint);
          
    int lStep = 20;
          
    lPoint00.x -= lStep; lPoint00.y -= lStep*2;
    lPoint01.x -= lStep*2; lPoint01.y -= lStep*2;
    lPoint11.x -= lStep*2; //lPoint11.y += 40;
          
    pPoints.add(lPoint00);
    pPoints.add(lPoint01);
    pPoints.add(lPoint11);
  }
  
  public Map getVertexStyle()
  {
    Map lVertexStyle = GraphConstants.createMap(); 

    GraphConstants.setSizeable(lVertexStyle, false);
    GraphConstants.setForeground(lVertexStyle, Color.black);
    GraphConstants.setFont(lVertexStyle, Application.VERTEX_FONT);
    GraphConstants.setOpaque(lVertexStyle, false);
    GraphConstants.setBounds(lVertexStyle, new Rectangle(1, 1));
    GraphConstants.setAutoSize(lVertexStyle, true);

    return lVertexStyle;
  }

  public Map getAssociationStyle()
  {
    Map lAssociationStyle = GraphConstants.createMap(); 

    GraphConstants.setBendable(lAssociationStyle, true);
    GraphConstants.setOpaque(lAssociationStyle, false);
    GraphConstants.setFont(lAssociationStyle, Application.DEFAULT_FONT);
    GraphConstants.setLineEnd(lAssociationStyle, GraphConstants.ARROW_SIMPLE);
    GraphConstants.setLineBegin(lAssociationStyle, GraphConstants.ARROW_NONE);
    
    return lAssociationStyle;
  }

  public Map getAggregationStyle()
  {
    Map lAggregationStyle = GraphConstants.createMap(); 

    GraphConstants.setBendable(lAggregationStyle, true);
    GraphConstants.setLineBegin(lAggregationStyle, GraphConstants.ARROW_DIAMOND);
    GraphConstants.setBeginFill(lAggregationStyle, false);
    GraphConstants.setBeginSize(lAggregationStyle, 15);
    GraphConstants.setLabelPosition(lAggregationStyle, new Point(500, 1200));
    GraphConstants.setFont(lAggregationStyle, Application.DEFAULT_FONT);
    GraphConstants.setOpaque(lAggregationStyle, false);
    GraphConstants.setLineEnd(lAggregationStyle, GraphConstants.ARROW_NONE);
    
    //???GraphConstants.setFont(mAggregationStyle, GraphConstants.defaultFont.deriveFont(10));
  
    return lAggregationStyle;
  }

  public Map getCompositionStyle()
  {
    Map lCompositionStyle = GraphConstants.createMap(); 

    GraphConstants.setBendable(lCompositionStyle, true);
    GraphConstants.setLineBegin(lCompositionStyle, GraphConstants.ARROW_DIAMOND);
    GraphConstants.setBeginFill(lCompositionStyle, true);
    GraphConstants.setBeginSize(lCompositionStyle, 15);
    GraphConstants.setLabelPosition(lCompositionStyle, new Point(500, 1200));
    GraphConstants.setFont(lCompositionStyle, Application.DEFAULT_FONT);
    GraphConstants.setOpaque(lCompositionStyle, false);
    GraphConstants.setLineEnd(lCompositionStyle, GraphConstants.ARROW_NONE);

    //???GraphConstants.setFont(mCompositionStyle, GraphConstants.defaultFont.deriveFont(10));
    //???GraphConstants.setBorderColor(lCompositionStyle, Color.black);
  
    return lCompositionStyle;
  }
  
  public Map getGeneralizationStyle()
  {
    Map lGeneralizationStyle = GraphConstants.createMap(); 
  
    GraphConstants.setBendable(lGeneralizationStyle, true);
    //GraphConstants.setLineBegin(mGeneralizationStyle, GraphConstants.ARROW_TECHNICAL);
    //GraphConstants.setBeginFill(mGeneralizationStyle, false);
    //GraphConstants.setBeginSize(mGeneralizationStyle, 15);
    GraphConstants.setLineEnd(lGeneralizationStyle, GraphConstants.ARROW_TECHNICAL);
    GraphConstants.setEndFill(lGeneralizationStyle, false);
    GraphConstants.setEndSize(lGeneralizationStyle, 15);
    GraphConstants.setLabelPosition(lGeneralizationStyle, new Point(500, 1200));
    GraphConstants.setOpaque(lGeneralizationStyle, false);
  
    return lGeneralizationStyle;
  }

  public Map getDependencyStyle()
  {
    Map lAssociationStyle = GraphConstants.createMap(); 

    GraphConstants.setBendable(lAssociationStyle, true);
    GraphConstants.setOpaque(lAssociationStyle, false);
    GraphConstants.setFont(lAssociationStyle, Application.DEFAULT_FONT);
    GraphConstants.setLineEnd(lAssociationStyle, GraphConstants.ARROW_SIMPLE);
    GraphConstants.setLineBegin(lAssociationStyle, GraphConstants.ARROW_NONE);
    //GraphConstants.setLineStyle(lAssociationStyle, GraphConstants.STYLE_QUADRATIC);
    GraphConstants.setDashPattern(lAssociationStyle, new float[] {6, 2}); 
    
    return lAssociationStyle;
  }

  // find ModelElement graph cell
  public DefaultGraphCell findElementCell(ModelElement pElement)
  {
    try
    {
      for (int i=0; i<getRootCount(); i++)
      {
        DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
        Object lCellUserObject = lObject.getUserObject();

        if (lCellUserObject instanceof BaseUserObject)
                {
            String lID = ((BaseUserObject)lCellUserObject).getID();
                    if (lID!=null && lID.equals(pElement.refMofId()))
                        return lObject; 
                }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }

  // find ModelElement graph cell by User Object
  public DefaultGraphCell findElementCell(Object pObject)
  {
    try
    {
      for (int i=0; i<getRootCount(); i++)
      {
        DefaultGraphCell lObject = (DefaultGraphCell)getRootAt(i);
        Object lCellUserObject = lObject.getUserObject();

        if (lCellUserObject!=null && lCellUserObject.equals(pObject))
          return lObject; 
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }

  // process cell change  
  protected void processCellChange(DefaultGraphCell pCell) 
  {
    //???
  }
  
  // save cell position
  protected void saveCellPosition(DefaultGraphCell pCell) throws Exception
  {
    if (pCell==null) return;
    Object lCellUserObject = pCell.getUserObject();
    if (lCellUserObject instanceof BaseUserObject)
    {
      ModelElement lElement = ((BaseUserObject)lCellUserObject).getBOObject();
      if (pCell instanceof VertexCell)
      {
        Rectangle2D lBounds = (Rectangle2D)GraphConstants.getBounds(pCell.getAttributes());
        setPosition(lElement, new Point((int)lBounds.getX(), (int)lBounds.getY()));
      }   else
      if (pCell instanceof Edge)
      {
        Point lPosition = GraphConstants.getLabelPosition(pCell.getAttributes());
        setPosition(lElement, lPosition);              
        saveEdgePoints(lElement, GraphConstants.getPoints(pCell.getAttributes()));
      }
    }
  }

  // save vertex color
  protected void saveCellColor(DefaultGraphCell pCell) throws Exception
  {
    if (pCell!=null && pCell instanceof VertexCell && 
      pCell.getUserObject() instanceof BaseUserObject)
    {
      BaseUserObject lUserObject = (BaseUserObject)pCell.getUserObject();
      if (lUserObject!=null)
        setVertexColor(lUserObject.getBOObject(), GraphConstants.getBackground(pCell.getAttributes()));
    }
  }

  // get model element
  protected DiagramModelElement getModelElement(ModelElement pElement, boolean pCreate)
  {
    // find
    if (mDiagram!=null)
    {
      Object[] lElements = mDiagram.getElements().toArray();
      for (int i=0; i<lElements.length; i++)
      {
        DiagramModelElement lElement = (DiagramModelElement)lElements[i];
        if (lElement.getModelElement()!=null && lElement.getModelElement().equals(pElement))
          return lElement;
      }
    }
    // create
    if (pCreate)
    {
      DiagramModelElement lDiagramElement = getPackage().getVisualModel().getDiagramModelElement().createDiagramModelElement();
      lDiagramElement.setModelElement(pElement);
      mDiagram.getElements().add(lDiagramElement);
      return lDiagramElement;
    }
    return null;
  }
  protected DiagramModelElement getModelElement(ModelElement pElement, String pName, boolean pCreate)
  {
    // find
    if (mDiagram!=null)
    {
      Object[] lElements = mDiagram.getElements().toArray();
      for (int i=0; i<lElements.length; i++)
      {
        DiagramModelElement lElement = (DiagramModelElement)lElements[i];
        if (lElement.getDescription()!=null && lElement.getDescription().equals(pName))
          return lElement;
      }
    }
    // create
    if (pCreate)
    {
      DiagramModelElement lDiagramElement = getPackage().getVisualModel().getDiagramModelElement().createDiagramModelElement();
      lDiagramElement.setDescription(pName);
      lDiagramElement.setModelElement(pElement);
      mDiagram.getElements().add(lDiagramElement);
      return lDiagramElement;
    }
    return null;
  }
  
  // get Model Element position
  protected com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point getElementPosition(DiagramModelElement pElement, boolean pCreate)
  {
    Object[] lPoints = pElement.getGraphicFeatures().toArray();
    for (int i=0; i<lPoints.length; i++)
    {
      ModelElement lElement = (ModelElement)lPoints[i];
      if (lElement.getName().equals("Anchor"))
        return (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point)lElement;
    }
    if (pCreate)
    {
      com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getPackage().getVisualModel().getPoint().createPoint();
      lPosition.setName("Anchor");
      pElement.getGraphicFeatures().add(lPosition);  
      return lPosition;
    }
    return null;
  }

  // get Color 
  protected com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color getElementColor(DiagramModelElement pElement, boolean pCreate) 
  {
    Object[] lPoints = pElement.getGraphicFeatures().toArray();
    for (int i=0; i<lPoints.length; i++)
    {
      ModelElement lElement = (ModelElement)lPoints[i];
      if (lElement.getName().equals("Color"))
        return (com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color)lElement;
    }
    if (pCreate)
    {
      com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getPackage().getVisualModel().getColor().createColor();
      lColor.setName("Color");
      pElement.getGraphicFeatures().add(lColor);  
      return lColor;
    }
    return null;
  }

  // set position
  protected void setPosition(ModelElement pElement, Point pPosition) 
  {
    DiagramModelElement lDiagramElement = getModelElement(pElement, true);
    if (lDiagramElement!=null)
    {
      com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getElementPosition(lDiagramElement, true);
      lPosition.setX(pPosition.x);
      lPosition.setY(pPosition.y);
    }
  }
    
  // get position
  protected Point getPosition(ModelElement pElement)
  {
    DiagramModelElement lDiagramElement = getModelElement(pElement, false);
    if (lDiagramElement!=null)
    {
      com.metaboss.sdlctools.models.metabossmodel.visualmodel.Point lPosition = getElementPosition(lDiagramElement, false);
      if (lPosition!=null)
        return new Point(lPosition.getX(), lPosition.getY());
    }
    return null;
  }

  // set vertex color
  protected void setVertexColor(ModelElement pElement, Color pColor)
  {
    if (pElement!=null && pColor!=null)
    {
      DiagramModelElement lDiagramElement = getModelElement(pElement, true);
      if (lDiagramElement!=null)
      {
        com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getElementColor(lDiagramElement, true);
        ColorUtils.fromAwtColor(lColor, pColor);
      }
    }
  }
  
  // get vertex color
  protected Color getVertexColor(ModelElement pElement)
  {
    DiagramModelElement lDiagramElement = getModelElement(pElement, false);
    if (lDiagramElement!=null)
    {
      com.metaboss.sdlctools.models.metabossmodel.visualmodel.Color lColor = getElementColor(lDiagramElement, false);
      if (lColor!=null)
        return ColorUtils.toAwtColor(lColor); 
    }
    return null;
  }

  // get port
  protected Object getPort(String pID)
  {
    if (mVertexPorts!=null && pID!=null)
      return mVertexPorts.get(pID);
    else
      return null;
  }

    // set graph model listener
    public void setModelListener()
    {
        addGraphModelListener(mModelListener);
    }
  
    // remove graph model listener
    public void removeModelListener()
    {
        removeGraphModelListener(mModelListener);
    }
    
  // load vertexeses positins
  /*protected void loadVertexesPositions() throws Exception
  {
    if (mDiagram==null) return;
    
    Object[] lObjects = getVertexesObjects();
    if (lObjects!=null && lObjects.length>0)
    {
      int     lMaxY = 0;
      boolean   lHasUnplacedEntities = false;
    
      for (int i=0; i<lObjects.length; i++)
      {
        Point lPosition = getPosition((ModelElement)lObjects[i]);
        if (lPosition!=null)
        {
          if (lMaxY<((int)lPosition.getY()))
            lMaxY = (int)lPosition.getY();
        }
        else
          lHasUnplacedEntities = true;
      }
      // Calculate missing cell positions if necessary
      if (lHasUnplacedEntities == true)
      {
        int lNextY = lMaxY + 100;
        int lNextX = 10;
        Application.beginTransaction();
        try
        {
          for (int i=0; i<lObjects.length; i++)
          {
            Point lPosition = getPosition((ModelElement)lObjects[i]);
            if (lPosition == null)
            {
              lPosition = new Point(lNextX, lNextY);
              lNextX += 70;
              setPosition((ModelElement)lObjects[i], lPosition);
            }
          }
          Application.commit();
        }
        finally
        {
          if (Application.isInTransaction()) Application.rollback();
        }
      }
    }
  }*/
    
  /*protected Object[] getVertexesObjects()
  {
    return null;
  }*/
  
  /*    properties    */
  
  public boolean getChanged()
  {
    return mChanged;
  }
  public void setChanged(boolean pValue)
  {
    mChanged = pValue;
  }

  public ModelElement getBOObject()
  {
    return mObject;
  }
  
  /*    Auxilary classes    */
  
  public class DesignGraphModelListener implements GraphModelListener
  {
        public void graphChanged(GraphModelEvent pEvent)
        {
      Object[] pChanged = pEvent.getChange().getChanged();
      if (!mLoading)
      {
        setChanged(true);
        for (int i = 0; i < pChanged.length; i++)
        {
          DefaultGraphCell lChangedObject = (DefaultGraphCell)pChanged[i];
          processCellChange(lChangedObject);
        }  
      }
              
      /*try
      {
        javax.naming.Context lContext = new javax.naming.InitialContext();
        BOTransaction lTx = null;
        try
        {
          lTx = (BOTransaction)lContext.lookup(BOTransaction.COMPONENT_URL);
          mLayout.beginEdit(lTx);

          Object[] pChanged = pEvent.getChange().getChanged();
          for (int i = 0; i < pChanged.length; i++)
          {
            DefaultGraphCell lChangedObject = (DefaultGraphCell)pChanged[i];
            Object lChangedCellUserObject = lChangedObject.getUserObject();
            // Funny thing is that the new attributes stored in what is called previous ones
            Map lNewAttributes = (Map)pEvent.getChange().getPreviousAttributes().get(lChangedObject);
            Map lOldAttributes = (Map)pEvent.getChange().getAttributes().get(lChangedObject);
            //if (lChangedCellUserObject instanceof DomainCellUserObject)
            //{
              // Domain object can only change position
            //    Rectangle lNewBounds = GraphConstants.getBounds(lNewAttributes);
            //    Rectangle lOldBounds = GraphConstants.getBounds(lOldAttributes);
            //    if (lNewBounds != null && (lOldBounds == null || (!lNewBounds.getLocation().equals(lOldBounds.getLocation()))))
            //       mLayout.setPosition("Home", ((DomainCellUserObject)lChangedCellUserObject).getDomain().getRef(),lNewBounds.getLocation());
            //}
            //else
            if (lChangedCellUserObject instanceof EntityNodeUserObject)
            {
              // Entity object can change position
              Rectangle lNewBounds = GraphConstants.getBounds(lNewAttributes);
              Rectangle lOldBounds = GraphConstants.getBounds(lOldAttributes);
              if (lNewBounds!=null && (lOldBounds==null || (!lNewBounds.getLocation().equals(lOldBounds.getLocation()))))
                mLayout.setPosition("Entity", ((EntityNodeUserObject)lChangedCellUserObject).getEntity().getRef(),lNewBounds.getLocation());
            } else
            if (lChangedCellUserObject instanceof AssociationNodeUserObject)
            {
              Point lNewPosition = GraphConstants.getLabelPosition(lNewAttributes);              
              Point lOldPosition = GraphConstants.getLabelPosition(lOldAttributes);
              if (lNewPosition!=null && (lOldPosition==null || (!lNewPosition.equals(lOldPosition))))
                mLayout.setPosition("Association", ((AssociationNodeUserObject)lChangedCellUserObject).getAssociation().getRef(), lNewPosition);
            }
          }
          // Commit what we have got
          lTx.doCommit();
          lTx = null;
        }
        finally
        {
          if (lTx != null)
            lTx. 
        }
      }
      catch(Exception e)
      {
        e.printStackTrace(); 
      }*/
      /*{
        //Object[] pChanged = pEvent.getChange().getChanged();
        for (int i = 0; i < pChanged.length; i++)
        {
          Object lChangedObject = pChanged[i];
          if (lChangedObject!=null)
          {
            System.out.println("    Object " + i + ":");
            System.out.println("        Class  : " + lChangedObject.getClass().getName());
            System.out.println("        User   : " + ((DefaultGraphCell)lChangedObject).getUserObject().getClass().getName());
            System.out.println("        ==== Previous attributes");
            {
              Map lNewAttributes = null;
              try
              {
                lNewAttributes = (Map)pEvent.getChange().getPreviousAttributes().get(lChangedObject);
              }
              catch (Exception e)
              {
                lNewAttributes = null;
              }
              if (lNewAttributes == null)
              {
                System.out.println("        No attributes map found for the object");
                continue;
              }
              Rectangle lBounds = GraphConstants.getBounds(lNewAttributes);
              if (lBounds == null)
                System.out.println("        No bounds stored for the object");
              else
                System.out.println("        Bounds : X(" + (int)lBounds.getX() + ") Y(" + (int)lBounds.getY() + ")");
            }
            System.out.println("        ==== Attributes");
            {
              Map lNewAttributes = (Map)pEvent.getChange().getAttributes().get(lChangedObject);
              if (lNewAttributes == null)
              {
                System.out.println("        No attributes map found for the object");
                continue;
              }
              Rectangle lBounds = GraphConstants.getBounds(lNewAttributes);
              if (lBounds == null)
                System.out.println("        No bounds stored for the object");
              else
                System.out.println("        Bounds : X(" + (int)lBounds.getX() + ") Y(" + (int)lBounds.getY() + ")");
            }
          }
        }
      }*/
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.