DiagramEdge.java :  » Web-Framework » argun » biz » hammurapi » diagram » Java Open Source

Java Open Source » Web Framework » argun 
argun » biz » hammurapi » diagram » DiagramEdge.java
/*
  * argun 1.0
 * Web 2.0 delivery framework 
 * Copyright (C) 2007  Hammurapi Group
 *
 * 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 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * URL: http://www.hammurapi.biz
 * e-Mail: support@hammurapi.biz 
 */
package biz.hammurapi.diagram;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;

import biz.hammurapi.diagram.data.Edge;
import biz.hammurapi.diagram.data.Property;

public class DiagramEdge extends DefaultEdge implements DiagramElement {
  
  private DiagramModel owner;
  protected Edge data;

  public DiagramEdge(DiagramModel owner, biz.hammurapi.diagram.data.Edge data) {
    this.owner=owner;    
    if (data==null) {
      id = owner.nextElementId();
    } else {
      BigInteger elementId = data.getElementId();
      if (elementId!=null) {
        id = elementId.intValue();
      }
      setName(data.getName());
      setDescription(data.getDescription());
      
      
      Property[] pa = data.getPropertyArray();
      for (int i=0; pa!=null && i<pa.length; ++i) {
        setProperty(pa[i].getName(), pa[i].getStringValue());
      }
      
      pa = data.getViewPropertyArray();
      for (int i=0; pa!=null && i<pa.length; ++i) {
        setViewProperty(pa[i].getName(), pa[i].getStringValue());
      }
      
      this.data = data;
    }
  }  
  
  private String description;
  private String name;
  private int id;
    
  public void setUserObject(Object userObject) {
    setName(userObject.toString());
  }
  
  public Object getUserObject() {
    return getName();
  }
  
  public String getDescription() {
    return description;
  }

  public String getName() {
    return name;
  }

  public DiagramModel getModel() {
    return owner;
  }


  public void setDescription(String description) {
    this.description = description;
  }

  public void setName(String name) {
    this.name = name;
    super.setUserObject(name);
  }
  
  public boolean acceptsSource(Object port) {
    return true;
  }

  public boolean acceptsTarget(Object port) {
    return true;
  }
    
  public void setAttributes(Map attributes) {
    GraphConstants.setBendable(attributes, true);
    //GraphConstants.setDisconnectable(attributes, true);
  }
  
  public int getId() {
    return id;
  }
  
  public void store(JGraph graph, biz.hammurapi.diagram.data.Edge data, boolean skipSourceInfo) {
    data.setName(getName());
    data.setDescription(getDescription());
    data.setElementId(new BigInteger(String.valueOf(id)));
    data.setType(getClass().getName());
    
    Enumeration pne = getPropertyNames();
    while (pne.hasMoreElements()) {
      String name = (String) pne.nextElement();
      Property property = data.addNewProperty();
      property.setName(name);
      property.setStringValue(properties.getProperty(name));
    }
    
    Enumeration vpne = getViewPropertyNames();
    while (vpne.hasMoreElements()) {
      String name = (String) vpne.nextElement();
      Property property = data.addNewViewProperty();
      property.setName(name);
      property.setStringValue(viewProperties.getProperty(name));
    }
    
    EdgeView edgeView = (EdgeView) graph.getGraphLayoutCache().getMapping(this, false);
    List points = edgeView.getPoints();
    if (points!=null) {
      Iterator pit=points.iterator();
      while (pit.hasNext()) {
        Object point = pit.next();
        if ((point instanceof Point2D)) {
          Point2D thePoint = (Point2D) point;
          biz.hammurapi.diagram.data.Point pointData =  data.addNewPoint();
          pointData.setX(thePoint.getX());
          pointData.setY(thePoint.getY());
        }
      }
    }
    
    Point2D labelPosition = edgeView.getLabelPosition();
    if (labelPosition!=null) {
      biz.hammurapi.diagram.data.Point pointData =  data.addNewLabelPosition();      
      pointData.setX(labelPosition.getX());
      pointData.setY(labelPosition.getY());
    }
    
    if (!skipSourceInfo) {
      Object source = getSource();
      if (source instanceof DefaultPort) {        
        DefaultPort sdp = (DefaultPort) source;
        if (sdp.getParent() instanceof DiagramElement) {
          Object uo = sdp.getUserObject();
          if (!DiagramCell.isBlank(uo)) {
            data.setSourcePort(uo.toString());
          }
          data.setSource(new BigInteger(String.valueOf(((DiagramElement) sdp.getParent()).getId())));
        }
      }
    }
    
    Object target = getTarget();
    if (target instanceof DefaultPort) {        
      DefaultPort tdp = (DefaultPort) target;
      if (tdp.getParent() instanceof DiagramElement) {
        Object uo = tdp.getUserObject();
        if (!DiagramCell.isBlank(uo)) {
          data.setTargetPort(uo.toString());
        }
        data.setTarget(new BigInteger(String.valueOf(((DiagramElement) tdp.getParent()).getId())));
      }
    }
    
  }
  
  protected Properties properties = new Properties();
  
  public void setProperty(String name, String value) {
    properties.setProperty(name, value);
  }
  
  public String getProperty(String name) {
    return properties.getProperty(name);
  }
  
  public Enumeration getPropertyNames() {
    return properties.propertyNames();
  }
  
  protected Properties viewProperties = new Properties();
  
  public void setViewProperty(String name, String value) {
    viewProperties.setProperty(name, value);
  }
  
  public String getViewProperty(String name) {
    return viewProperties.getProperty(name);
  }
  
  public Enumeration getViewPropertyNames() {
    return viewProperties.propertyNames();
  }
  
  public void populatePopupMenu(final DiagramApplet applet, final java.awt.Point pt, final JPopupMenu menu) {
    // Edit
    menu.add(new AbstractAction("Edit") {
      public void actionPerformed(ActionEvent e) {
        Frame frame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, applet.getGraph());          
        DiagramPropertiesDialog pd = new DiagramPropertiesDialog(frame, DiagramEdge.this);
        if (pd.edit()) {
          applet.getGraph().repaint();
        }
      }
    });
  }
  
}
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.