В JGraphx можно рисовать собственную вершину. Я определил пользовательский объект и использовал его как значение вершины. Чтобы сохранить и загрузить график в виде XML-файла, пользовательский объект был зарегистрирован следующим образом:
mxCodecRegistry.addPackage(the path of the custom object);
mxCodecRegistry.register(new mxObjectCodec(new Myobject()));
При сохранении вершины пользовательский объект сохраняется как значение вершины. Когда я загружаю XML-файл, значение вершины возвращается нулевым, хотя вершина отображается правильно. Другими словами, графический дисплей в порядке, но когда я хочу прочитать свойства объекта или даже переместить его, появляется следующая ошибка:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mxgraph.swing.handler.mxVertexHandler.createHandles(Unknown Source)
at com.mxgraph.swing.handler.mxCellHandler.refresh(Unknown Source)
at com.mxgraph.swing.handler.mxCellHandler.<init>(Unknown Source)
at com.mxgraph.swing.handler.mxVertexHandler.<init>(Unknown Source)
at com.mxgraph.swing.mxGraphComponent.createHandler(Unknown Source)
at com.mxgraph.swing.handler.mxSelectionCellsHandler.refresh(Unknown Source)
at com.mxgraph.swing.handler.mxSelectionCellsHandler$1.invoke(Unknown Source)
at com.mxgraph.util.mxEventSource.fireEvent(Unknown Source)
at com.mxgraph.util.mxEventSource.fireEvent(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel$mxSelectionChange.execute(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.changeSelection(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.setCells(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.setCell(Unknown Source)
at com.mxgraph.view.mxGraph.setSelectionCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent.selectCellForEvent(Unknown Source)
at com.mxgraph.swing.handler.mxGraphHandler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Что показывает, что объект нулевой. Я проследил за документацией и зарегистрировал свой объект. Вот графический интерфейс:
package graphicalUserInterface;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.io.mxCodecRegistry;
import com.mxgraph.io.mxObjectCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.shape.mxStencilShape;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import netElements.Valve;
public class GUIBoard extends JPanel {
private mxGraph _graph;
private mxGraphComponent _graphComponent;
private JPanel _panel;
private JFrame _frame;
private ArrayList<Valve> _valves;
public GUIBoard(JFrame frame) {
// TODO Auto-generated constructor stub
_frame=frame;
initialize();
}
private void initialize(){
_panel=new JPanel();
_panel.setLayout(new BoxLayout(_panel, BoxLayout.Y_AXIS));
addToRegistry("Images/valve.shape");
/**
* Define a new object to be saved in the Model
*/
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));
_graph=new mxGraph() {
public boolean isCellFoldable(Object cell, boolean collapse)
{
return false;
}
public String convertValueToString(Object cell)
{
Object value = null ;
if (cell instanceof mxCell)
{
value = ((mxCell) cell).getValue();
if (value instanceof Valve)
{
Valve v=(Valve) value;
return ((Valve) value).getElementName();
}
}
return super.convertValueToString(value);
}
public boolean isCellResizable(Object cell)
{
return !getModel().isVertex(cell);
}
};
_graph.addListener(mxEvent.MOVE_CELLS, new mxIEventListener()
{
@Override
public void invoke(Object sender, mxEventObject mxevt) {
Object[] cells = (Object[]) mxevt.getProperty("cells");
Point x = (Point) mxevt.getProperty("location");
Double dx = (Double) mxevt.getProperty("dx");
Double dy = (Double) mxevt.getProperty("dy");
}
});
setDefaultConnector();
_graphComponent=new mxGraphComponent(_graph);
_graphComponent.setPreferredSize(new Dimension(600, 400));
this.add(_graphComponent);
_graph.setVertexLabelsMovable(true);
_graph.setDisconnectOnMove(false);
_graph.setConnectableEdges(false);
_graph.setCellsEditable(false);
_graph.setResetEdgesOnMove(true);
_valves=new ArrayList<Valve>();
_panel.add(new GUIBoardToolBars(this).addFileToolBar());
_frame.add(new GUIBoardToolBars(this).addPowerElements(), BorderLayout.EAST);
_frame.add(_panel, BorderLayout.NORTH);
}
public mxGraph getGraph(){
return _graph;
}
public mxGraphComponent getGraphComponent(){
return _graphComponent;
}
private void setDefaultConnector() {
Map<String, Object> connector = new HashMap<String, Object>();
connector.put(mxConstants.STYLE_ROUNDED, false);
connector.put(mxConstants.STYLE_ORTHOGONAL, true);
connector.put(mxConstants.STYLE_EDGE, "elbowEdgeStyle");
connector.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
connector.put(mxConstants.STYLE_ENDARROW, mxConstants.NONE);
mxStylesheet connectorStyle = new mxStylesheet();
connectorStyle.setDefaultEdgeStyle(connector);
_graph.setStylesheet(connectorStyle);
}
public void addValves(Valve valve){
_valves.add(valve);
}
public ArrayList<Valve> getValves(){
return _valves;
}
public void addToRegistry(String element){
String nodeXml, objectName;
mxStencilShape newShape;
try {
nodeXml = mxUtils.readFile(element);
newShape = new mxStencilShape(nodeXml);
objectName = newShape.getName();
mxGraphics2DCanvas.putShape(objectName, newShape);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public GUIBoard(LayoutManager arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public GUIBoard(boolean arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public GUIBoard(LayoutManager arg0, boolean arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
}
Действие графического интерфейса:
package graphicalUserInterface;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.AbstractAction;
import org.w3c.dom.Document;
import com.mxgraph.io.mxCodec;
import com.mxgraph.io.mxCodecRegistry;
import com.mxgraph.io.mxObjectCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import netElements.Valve;
public class GUIBoardActions {
public GUIBoardActions() {
// TODO Auto-generated constructor stub
}
public static class NewNetwork extends AbstractAction {
private GUIBoard _newNDB;
public NewNetwork(GUIBoard ndb) {
_newNDB=ndb;
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
public static class OpenNetwork extends AbstractAction {
/**
*
*/
private static final long serialVersionUID = 8166859573134106478L;
private GUIBoard _openNDB;
private Document document;
mxCodec codec ;
public OpenNetwork(GUIBoard ndb) {
_openNDB=ndb;
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));
}
@Override
public void actionPerformed(ActionEvent aevt) {
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));
try {
document = mxXmlUtils.parseXml(mxUtils.readFile("C:\\test graph/file.xml"));
codec = new mxCodec(document);
codec.decode(document.getDocumentElement(), _openNDB.getGraph().getModel());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_openNDB.getGraph().getModel().beginUpdate();
Object[] cells;
try
{
cells= _openNDB.getGraph().getChildCells(_openNDB.getGraph().getDefaultParent(), true, true);
}
finally
{
_openNDB.getGraph().getModel().endUpdate();
}
_openNDB.getGraph().addCells(cells);
for (Object c : cells) {
mxCell cell = (mxCell) c;
if (cell.isVertex()) {
if (cell.getValue() instanceof Valve) {
Valve valve=(Valve) cell.getValue();
_openNDB.addValves(valve);
}
}
}
}
}
public static class SaveNetwork extends AbstractAction {
/**
*
*/
private static final long serialVersionUID = 4757526430199667311L;
private GUIBoard _saveNDB;
public SaveNetwork(GUIBoard ndb) {
_saveNDB=ndb;
}
@Override
public void actionPerformed(ActionEvent aevt) {
mxCodec codec = new mxCodec();
try {
String xml2 = mxUtils.getPrettyXml(codec.encode(_saveNDB.getGraph().getModel()));
mxUtils.writeFile(xml2, "C:\\test graph/file.xml");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static class AddNewValve extends AbstractAction {
/**
*
*/
private static final long serialVersionUID = 4187443683391653845L;
private GUIBoard _valveNDB;
private MouseMotionListener _boardMotionListener;
private MouseListener _boardListener;
public AddNewValve(GUIBoard ndb){
_valveNDB=ndb;
}
@Override
public void actionPerformed(ActionEvent aevt) {
Valve v = new Valve();
v.setElementNumber(_valveNDB.getValves().size()+1);
v.setElementName("V"+v.getElementNumber());
v.setElementStyleName("VALVE"+v.getElementNumber());
v.setElementStyle(_valveNDB.getGraph());
_valveNDB.addValves(v);
mxCell vCell;
_valveNDB.getGraph().getModel().beginUpdate();
try
{
vCell=(mxCell) _valveNDB.getGraph().insertVertex(_valveNDB.getGraph().getDefaultParent(), null, v, MouseInfo.getPointerInfo().getLocation().x,MouseInfo.getPointerInfo().getLocation().y, 80, 30,v.getElementStyleString());
}
finally
{
_valveNDB.getGraph().getModel().endUpdate();
}
vCell.setAttribute("Name", v.getElementName());
vCell.setConnectable(false);
_valveNDB.getGraphComponent().getGraphControl().addMouseMotionListener(_boardMotionListener=new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent mevt) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent mevt) {
vCell.setGeometry(new mxGeometry(mevt.getX(),mevt.getY(),80,30));
_valveNDB.getGraph().refresh();
}
});
_valveNDB.getGraphComponent().getGraphControl().addMouseListener(_boardListener=new MouseListener() {
@Override
public void mouseReleased(MouseEvent mevt) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent mevt) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent mevt) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent mevt) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent mevt) {
_valveNDB.getGraphComponent().getGraphControl().removeMouseListener(_boardListener);
_valveNDB.getGraphComponent().getGraphControl().removeMouseMotionListener(_boardMotionListener);
}
});
}
}
}
Вот код панели инструментов:
package graphicalUserInterface;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import graphicalUserInterface.GUIBoardActions.*;
public class GUIBoardToolBars extends JToolBar {
/**
*
*/
private static final long serialVersionUID = 7241903583392004921L;
GUIBoard _drawingBoard;
//JToolBar _fileToolBar;
public GUIBoardToolBars(GUIBoard ndb) {
_drawingBoard=ndb;
//_fileToolBar=new JToolBar();
}
public JToolBar addFileToolBar(){
JToolBar fileToolBar=new JToolBar();
fileToolBar.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3), getBorder()));
fileToolBar.setFloatable(false);
fileToolBar.setAlignmentX(0);
UIManager.put("ToolTip.background", Color.TRANSLUCENT);
fileToolBar.add(addNewComponent("New", new NewNetwork(_drawingBoard), new ImageIcon("Icon/new.gif"))).setToolTipText("New");
fileToolBar.add(addNewComponent("Open", new OpenNetwork(_drawingBoard), new ImageIcon("Icon/open.gif"))).setToolTipText("Open");
fileToolBar.add(addNewComponent("Save", new SaveNetwork(_drawingBoard), new ImageIcon("Icon/save.gif"))).setToolTipText("Save");
//JButton newFileToolBar=new JButton();
//newFileToolBar.setActionCommand("newFileToolBar");
//newFileToolBar.setIcon(new ImageIcon("Image/new.gif"));
//newFileToolBar.setToolTipText("New");
//fileToolBar.add(newFileToolBar);
return fileToolBar;
}
public JToolBar addPowerElements(){
JToolBar powerElements=new JToolBar(VERTICAL);
powerElements.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3,3,3,3), getBorder()));
powerElements.setFloatable(false);
powerElements.add(addNewComponent("Valve", new AddNewValve(_drawingBoard), new ImageIcon("Icon/valve1.png"))).setToolTipText("Valve");
return powerElements;
}
@SuppressWarnings("serial")
public Action addNewComponent(String name, Action componentAction, ImageIcon icon){
AbstractAction newComponent=new AbstractAction(name,(icon!=null) ? icon : null){
public void actionPerformed(ActionEvent e)
{
componentAction.actionPerformed(new ActionEvent(_drawingBoard.getGraphComponent(),e.getID(),e.getActionCommand()));
}
};
icon.setDescription(name);
return newComponent;
}
public GUIBoardToolBars(int arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public GUIBoardToolBars(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public GUIBoardToolBars(String arg0, int arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
}
И, наконец, класс пользовательских объектов:
package netElements;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
public class Valve implements BasicPropertiesOfElement, Serializable {
/**
*
*/
private static final long serialVersionUID = 6425822395370778513L;
private mxCell _valveCell;
private transient mxStylesheet _valveStyleSheet;
protected int _valveNumber;
private double _valveX, _valveY, _valveRotationalAngle=0;
private String _valveName, _valveStyleName, _valveStyleString="";
public Valve() {
}
public void setElementStyleName(String styleName) {
_valveStyleName=styleName;
}
public String getElementStyleName() {
return _valveStyleName;
}
public void setElementStyle(mxGraph g) {
Map<String, Object> transProperties;
_valveStyleSheet = g.getStylesheet();
transProperties = new HashMap<String, Object>();
transProperties.put(mxConstants.STYLE_SHAPE, "valve");
transProperties.put(mxConstants.STYLE_STROKECOLOR, "black");
transProperties.put(mxConstants.STYLE_ROTATION, _valveRotationalAngle);
transProperties.put(mxConstants.STYLE_VERTICAL_LABEL_POSITION, mxConstants.ALIGN_TOP);
_valveStyleSheet.putCellStyle(_valveStyleName, transProperties);
this.setElementStyleString(transProperties);
}
public mxStylesheet getElementStyle() {
return this._valveStyleSheet;
}
public void setElementStyleString(Map<String, Object> transProperties) {
transProperties.entrySet().stream().forEach(entry ->
_valveStyleString+=entry.getKey()+"="+entry.getValue().toString()+";");
}
public String getElementStyleString() {
return _valveStyleString;
}
@Override
public void setElementRotationalAngle(double angle) {
// TODO Auto-generated method stub
_valveRotationalAngle=angle;
}
@Override
public double getElementRotationalAngle() {
// TODO Auto-generated method stub
return _valveRotationalAngle;
}
@Override
public void setElementNumber(int elementNumber) {
// TODO Auto-generated method stub
_valveNumber=elementNumber;
}
@Override
public int getElementNumber() {
// TODO Auto-generated method stub
return _valveNumber;
}
@Override
public void setElementName(String elementName) {
// TODO Auto-generated method stub
_valveName=elementName;
}
@Override
public String getElementName() {
// TODO Auto-generated method stub
return _valveName;
}
}
Я проверил файл xml и обнаружил, что свойства пользовательского объекта (здесь с именем Valve) не отображаются. Основываясь на примере здесь , пользовательский объект должен быть зарегистрирован в кодировке, а остальное будет обрабатываться JGraph.
Я не знаю, что делать или какой метод следует переопределить, чтобы сохранить свойства пользовательского объекта вместе с ячейкой.
Любая помощь будет очень признательна.