package de.dwslab.risk.gui;

import java.util.Iterator;
import java.util.List;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;

/**
 * A graph that creates new edges from a given template edge.
 */
public class CustomGraph extends mxGraph {

    /**
     * Holds the edge to be used as a template for inserting new edges.
     */
    protected Object edgeTemplate;

    /**
     * Custom graph that defines the alternate edge style to be used when
     * the middle control point of edges is double clicked (flipped).
     */
    public CustomGraph() {
        setAlternateEdgeStyle("edgeStyle=mxEdgeStyle.ElbowConnector;elbow=vertical");
    }

    /**
     * Sets the edge template to be used to inserting edges.
     */
    public void setEdgeTemplate(Object template) {
        edgeTemplate = template;
    }

    /**
     * Prints out some useful information about the cell in the tooltip.
     */
    @Override
    public String getToolTipForCell(Object cell) {
        String tip = "<html>";
        mxGeometry geo = getModel().getGeometry(cell);
        mxCellState state = getView().getState(cell);

        if (getModel().isEdge(cell)) {
            tip += "points={";

            if (geo != null) {
                List<mxPoint> points = geo.getPoints();

                if (points != null) {
                    Iterator<mxPoint> it = points.iterator();

                    while (it.hasNext()) {
                        mxPoint point = it.next();
                        tip += "[x=" + RoCA.numberFormat.format(point.getX()) + ",y="
                                + RoCA.numberFormat.format(point.getY()) + "],";
                    }

                    tip = tip.substring(0, tip.length() - 1);
                }
            }

            tip += "}<br>";
            tip += "absPoints={";

            if (state != null) {

                for (int i = 0; i < state.getAbsolutePointCount(); i++) {
                    mxPoint point = state.getAbsolutePoint(i);
                    tip += "[x=" + RoCA.numberFormat.format(point.getX()) + ",y="
                            + RoCA.numberFormat.format(point.getY()) + "],";
                }

                tip = tip.substring(0, tip.length() - 1);
            }

            tip += "}";
        } else {
            tip += "geo=[";

            if (geo != null) {
                tip += "x=" + RoCA.numberFormat.format(geo.getX()) + ",y="
                        + RoCA.numberFormat.format(geo.getY()) + ",width="
                        + RoCA.numberFormat.format(geo.getWidth()) + ",height="
                        + RoCA.numberFormat.format(geo.getHeight());
            }

            tip += "]<br>";
            tip += "state=[";

            if (state != null) {
                tip += "x=" + RoCA.numberFormat.format(state.getX()) + ",y="
                        + RoCA.numberFormat.format(state.getY()) + ",width="
                        + RoCA.numberFormat.format(state.getWidth()) + ",height="
                        + RoCA.numberFormat.format(state.getHeight());
            }

            tip += "]";
        }

        mxPoint trans = getView().getTranslate();

        tip += "<br>scale=" + RoCA.numberFormat.format(getView().getScale()) + ", translate=[x="
                + RoCA.numberFormat.format(trans.getX()) + ",y=" + RoCA.numberFormat.format(trans.getY())
                + "]";
        tip += "</html>";

        return tip;
    }

    /**
     * Overrides the method to use the currently selected edge template for
     * new edges.
     *
     * @param graph
     * @param parent
     * @param id
     * @param value
     * @param source
     * @param target
     * @param style
     * @return
     */
    @Override
    public Object createEdge(Object parent, String id, Object value, Object source,
            Object target, String style) {
        if (edgeTemplate != null) {
            mxCell edge = (mxCell) cloneCells(new Object[] { edgeTemplate })[0];
            edge.setId(id);

            return edge;
        }

        return super.createEdge(parent, id, value, source, target, style);
    }

}
@@ -258,16 +258,103 @@
 aph;%0D%0A%0D%0A
+import de.dwslab.risk.gui.model.Entity;%0D%0Aimport de.dwslab.risk.gui.model.Grounding;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -3531,24 +3531,42 @@
 getScale()) 
+%0D%0A                
 + %22, transla
@@ -3631,23 +3631,58 @@
 getX()) 
-+ %22,y=%22
+%0D%0A                + %22,y=%22%0D%0A               
  + RoCA.
@@ -4182,29 +4182,16 @@
  source,
-%0D%0A           
  Object 
@@ -4197,16 +4197,29 @@
  target,
+%0D%0A           
  String 
@@ -4407,34 +4407,32 @@
 ge;%0D%0A        %7D%0D%0A
-%0D%0A
         return s
@@ -4500,9 +4500,912 @@
    %7D%0D%0A%0D%0A
+    @Override%0D%0A    public String convertValueToString(Object cell) %7B%0D%0A        if (((mxCell) cell).getValue() instanceof Entity) %7B%0D%0A            Entity entity = (Entity) ((mxCell) cell).getValue();%0D%0A            return entity.getName();%0D%0A        %7D else if (((mxCell) cell).getValue() instanceof Grounding) %7B%0D%0A            Grounding grounding = (Grounding) ((mxCell) cell).getValue();%0D%0A            StringBuilder builder = new StringBuilder();%0D%0A            builder.append(grounding.getPredicate().getName());%0D%0A            builder.append('(');%0D%0A            for (String value : grounding.getValues()) %7B%0D%0A                builder.append(value);%0D%0A                builder.append(%22, %22);%0D%0A            %7D%0D%0A            builder.delete(builder.length() - 2, builder.length());%0D%0A            builder.append(')');%0D%0A            return builder.toString();%0D%0A        %7D%0D%0A        return super.convertValueToString(cell);%0D%0A    %7D%0D%0A%0D%0A
 %7D

Implement custom convertValueToString