田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
package com.vci.client.workflow.editor;
 
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.InputStream;
 
import javax.swing.ImageIcon;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
 
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxGraphTransferable;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.vci.client.workflow.editor.CustomGraphComponent.CustomGraph;
import com.vci.corba.common.VCIError;
 
public class FlowDesigner extends BasicGraphEditor {
 
    private static final long serialVersionUID = -4716858349938153598L;
 
    public FlowDesigner() throws VCIError {
        this(new CustomGraphComponent());
    }
 
    public FlowDesigner(mxGraphComponent component) {
        super(component);
        ((CustomGraphComponent)graphComponent).setFlowDesigner(this);
        final mxGraph graph = getGraphComponent().getGraph();
        getGraphComponent().getGraphControl().addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                try {
                    updatePanel(graph.getSelectionCell());
                } catch (VCIError e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    }
    
    public void updatePanel(Object cell) throws VCIError{
        if(cell == null){
            cell = getGraphComponent().getGraph().getModel().getRoot();
        }
        getEditor().updatePanel(cell);
    }
 
    public boolean isEdit(boolean flag){
        if ("1".equals(getDeployPanelFlag())) {
            ((FlowDeployPanel)getDeployPanel()).isEdit(flag);
        } else if ("2".equals(getDeployPanelFlag())) {
            ((FlowDeployNewPanel)getDeployPanel()).isEdit(flag);
        }
//        getDeployPanel().isEdit(flag);
        return flag;
    }
    public EditorPalette getEditorPalette() {
        if(palette == null){
            palette = new EditorPalette();
            addTemplate(palette);
            final mxGraph graph = graphComponent.getGraph();
            
            // Sets the edge template to be used for creating new edges if an edge
            // is clicked in the shape palette
            palette.addListener(mxEvent.SELECT, new mxIEventListener() {
                public void invoke(Object sender, mxEventObject evt) {
                    Object tmp = evt.getProperty("transferable");
                    if (tmp instanceof mxGraphTransferable) {
                        mxGraphTransferable t = (mxGraphTransferable) tmp;
                        Object cell = t.getCells()[0];
                        if (graph.getModel().isEdge(cell)) {
                            ((CustomGraph) graph).setEdgeTemplate(cell);
                        }
                    }
                }
            });
        }
        
        return palette;
    }
    
    private void addTemplate(EditorPalette palette){
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ClassLoader cl = FlowDesigner.class.getClassLoader();
            InputStream in = cl.getResourceAsStream("flow-tools.xml");
            Document doc = db.parse(in);
//            Document doc = db.parse(FlowDesigner.class.getResource("/flow-tools.xml").toString());
            NodeList nodelist = doc.getElementsByTagName("tool");
            for (int i = 0; i < nodelist.getLength(); i++) {
                String type = ((Element) nodelist.item(i)).getAttribute("type");
                String label = ((Element) nodelist.item(i)).getAttribute("label");
                String icon = ((Element) nodelist.item(i)).getAttribute("icon");
                int width = Integer.parseInt(((Element) nodelist.item(i)).getAttribute("width"));
                int height = Integer.parseInt(((Element) nodelist.item(i)).getAttribute("height"));
 
                FlowNode tool = new FlowNode(type, label);
                if (type != null && type.startsWith("transition_")) {
                    palette.addEdgeTemplate(label, new ImageIcon(
                            FlowDesigner.class.getResource(icon)), type,
                            width, height, tool);
                } else {
                    palette.addTemplate(label,
                            new ImageIcon(FlowDesigner.class.getResource(icon)),
                            type, width, height, tool);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}