dangsn
2024-12-26 4e9ff2ce6a830bb2340d7c8612c72eea0c5a553e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.vci.client.workflow.editor;
 
import java.awt.Color;
import java.awt.Point;
import java.io.InputStream;
import java.io.Serializable;
import java.text.NumberFormat;
 
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
 
import com.mxgraph.io.mxCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxGraphModel.Filter;
import com.mxgraph.model.mxICell;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import com.vci.client.workflow.editor.ui.IProcessProperty;
import com.vci.client.workflow.editor.ui.TaskPanel;
import com.vci.corba.common.VCIError;
 
public class CustomGraphComponent extends mxGraphComponent {
 
    private static final long serialVersionUID = 1141100263201752158L;
    private static FlowDesigner flowDesigner;
    
    public CustomGraphComponent() throws VCIError {
        this(new CustomGraph());
    }
    
    public CustomGraphComponent(final mxGraph graph) throws VCIError {
        super(graph);
 
        // Sets switches typically used in an editor
//        setPageVisible(true);
//        setGridVisible(true);
        setToolTips(true);
//        getConnectionHandler().setCreateTarget(true);
        getConnectionHandler().setHandleEnabled(false);
//        getConnectionHandler().setEnabled(false);
 
        mxCell cell = (mxCell)getGraph().getModel().getRoot();
        cell.setValue(ProcessComponentFactory.getComponent(graph, cell));
 
        // Loads the defalt stylesheet from an external file
        mxCodec codec = new mxCodec();
//        mxCodec codec = new mxCodec();
        Document doc = getDocument();
/*        org.w3c.dom.Document doc = mxUtils.loadDocument(CustomGraphComponent.class.getResource(
                "/default-style.xml").toString());*/
        codec.decode(doc.getDocumentElement(), graph.getStylesheet());
 
        // Sets the background to white
        getViewport().setOpaque(true);
        getViewport().setBackground(Color.WHITE);
    }
 
    private Document getDocument() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ClassLoader cl = CustomGraphComponent.class.getClassLoader();
            InputStream in = cl.getResourceAsStream("default-style.xml");
            return db.parse(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    /**
     * Overrides drop behaviour to set the cell style if the target is not a
     * valid drop target and the cells are of the same type (eg. both
     * vertices or both edges).
     */
    public Object[] importCells(Object[] cells, double dx, double dy,
            Object target, Point location) {
        try{
            if (target == null && cells.length == 1 && location != null) {
                target = getCellAt(location.x, location.y);
    
                if (target instanceof mxICell && cells[0] instanceof mxICell) {
                    mxICell targetCell = (mxICell) target;
                    mxICell dropCell = (mxICell) cells[0];
    
                    if (targetCell.isVertex() == dropCell.isVertex()
                            || targetCell.isEdge() == dropCell.isEdge()) {
                        mxIGraphModel model = graph.getModel();
                        model.setStyle(target, model.getStyle(cells[0]));
                        graph.setSelectionCell(target);
                        flowDesigner.getEditor().updatePanel(target);
                        return null;
                    }
                }
            }
            
            Object[] importCells = super.importCells(cells, dx, dy, target, location);
            return importCells;
        }catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage(), "importCells", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
 
    /**
     * A graph that creates new edges from a given template edge.
     */
    public static class CustomGraph extends mxGraph implements Serializable {
        
        private static final long serialVersionUID = 5968783294124195597L;
        public static final NumberFormat numberFormat = NumberFormat.getInstance();
        
        /**
         * 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;
        }
 
        /**
         * Overrides the method to use the currently selected edge template for
         * new edges.
         */
        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);
        }
 
        @Override
        public void cellLabelChanged(Object cell, Object value, boolean autoSize) {
            model.beginUpdate();
            try {
                mxCell tmpCell = (mxCell) cell;
                Object cellValue = tmpCell.getValue();
                if(cellValue instanceof IProcessProperty){
                    IProcessProperty flowNode = (IProcessProperty) cellValue;
                    flowNode.setValue(value.toString());
                }else{
                    FlowNode flowNode = new FlowNode("transition_horizontal", value.toString());
                    tmpCell.setValue(flowNode);
                    tmpCell.setValue(ProcessComponentFactory.getComponent(this, tmpCell));
                }
                if (autoSize) {
                    cellSizeUpdated(cell, false);
                }
            } catch (VCIError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, e.messages, "cellLabelChanged", JOptionPane.ERROR_MESSAGE);
 
            } finally {
                model.endUpdate();
                refresh();
            }
        }
        
        public Object[] getCloneableCells(Object[] cells) {
            return mxGraphModel.filterCells(cells, new Filter() {
                public boolean filter(Object cell) {
                    Object cellValue = ((mxCell)cell).getValue();
                    boolean isStart = false;
                    if(cellValue != null && cellValue instanceof FlowNode){
                        //禁止start被克隆
                        if(((FlowNode)cellValue).getType().equals("start")){
                            isStart = true;
                        }
                    }
                    return isCellCloneable(cell) && !isStart;
                }
            });
        }
        
        @Override
        public Object[] cloneCells(Object[] cells, boolean allowInvalidEdges) {
            try{
                Object[] clones = super.cloneCells(cells, allowInvalidEdges);
                if(clones != null && clones.length > 0){
                    for (int i = 0; i < clones.length; i++) {
                        mxCell mxCell = (mxCell)clones[i];
                        Object value = mxCell.getValue();
                        if(value instanceof FlowNode){
                            JComponent component = ProcessComponentFactory.getComponent(this, mxCell);
                            if(component instanceof TaskPanel) {
    //                            PhaseObject[] phaseObjects = flowDesigner.getDeployPanel().getPhaseObjects();
    //                            ((TaskPanel)component).setPhaseObjects(phaseObjects);
                            }
                            mxCell.setValue(component);
                        }
                    }
                }
    
                return clones;
            }catch(Exception e){
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, e.getMessage(), "cloneCells", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        }
    }
 
    public void setFlowDesigner(final FlowDesigner flowDesigner) {
        this.flowDesigner = flowDesigner;
    }
    
}