I have defined a set of shapes in an xml file (intial-node, final-node, activity-node, fork-node...). (I am using

 <shape name="initial-node" aspect="fixed" w="40" h="40" strokewidth="0.5">
  <background>
   <ellipse x="0" y="0" w="40" h="40"/>
  </background>
  <foreground>
   <fillcolor color="#000000" /> 
   <fillstroke/>
  </foreground>
 </shape>

To create a cell with a style defined in the file, I shall proceed as described in the Example Stencils.java but I want to save these styles in mxConstant and I can create my custom cells with the putting their style like this:

Example: Create initial node ---> graph.insertVertex(level1, null, "Initial-Node", 10, 30, 100, 80, mxConstant.INITIAL_NODE); Thank you for helping me.

asked 29 Aug '12, 13:50

rahma's gravatar image

rahma
116
accept rate: 0%

edited 30 Aug '12, 03:11

David's gravatar image

David
4.9k21831


I wouldn't advise to change the source code. Instead use your own stylesheet constants, e. g. something like this:

package com.mxgraph.examples.swing;

import java.util.Hashtable;

import javax.swing.JFrame;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

public class StyleExample extends JFrame {

    private static final long serialVersionUID = 672772281200016954L;

    public static final String MY_CUSTOM_VERTEX_STYLE = "MY_CUSTOM_VERTEX_STYLE";
    public static final String MY_CUSTOM_EDGE_STYLE = "MY_CUSTOM_EDGE_STYLE";

    private static void setStyleSheet(mxGraph graph) {

        Hashtable<String, Object> style;
        mxStylesheet stylesheet = graph.getStylesheet();

        // base style
        Hashtable<String, Object> baseStyle = new Hashtable<String, Object>();
        baseStyle.put(mxConstants.STYLE_STROKECOLOR, "#FF0000");

        // custom vertex style
        style = new Hashtable<String, Object>(baseStyle);
        style.put(mxConstants.STYLE_FILLCOLOR, "#FFFF00");
        stylesheet.putCellStyle(MY_CUSTOM_VERTEX_STYLE, style);

        // custom edge style
        style = new Hashtable<String, Object>(baseStyle);
        style.put(mxConstants.STYLE_STROKEWIDTH, 3);
        stylesheet.putCellStyle(MY_CUSTOM_EDGE_STYLE, style);

    }

    public StyleExample() {
        super("Hello, World!");

        final mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        // create styles
        setStyleSheet(graph);

        graph.getModel().beginUpdate();
        try {

            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30, MY_CUSTOM_VERTEX_STYLE);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30, MY_CUSTOM_VERTEX_STYLE);

            graph.insertEdge(parent, null, "Edge", v1, v2, MY_CUSTOM_EDGE_STYLE);

        } finally {
            graph.getModel().endUpdate();
        }

        final mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);

    }

    public static void main(String[] args) {
        StyleExample frame = new StyleExample();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }

}

You can do the same with the shapes. Just register them, create a custom style with a unique stylename and reference that stylename.

link

answered 30 Aug '12, 00:33

Lofi's gravatar image

Lofi
1.0k1221
accept rate: 37%

edited 30 Aug '12, 00:37

Thank you very much for your answer, I will try this now :)

(30 Aug '12, 06:11) rahma
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×194
×50
×16

Asked: 29 Aug '12, 13:50

Seen: 2,984 times

Last updated: 30 Aug '12, 06:11