yuxc
2025-01-15 c09f81131e8b7c83937206d7cf76f34d2020be75
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
package com.vci.client.workflow.editor;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
 
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
 
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.mxEdgeLabelLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.mxOrganicLayout;
import com.mxgraph.layout.mxParallelEdgeLayout;
import com.mxgraph.layout.mxPartitionLayout;
import com.mxgraph.layout.mxStackLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.mxGraphOutline;
import com.mxgraph.swing.handler.mxKeyboardHandler;
import com.mxgraph.swing.handler.mxRubberband;
import com.mxgraph.swing.util.mxMorphing;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxResources;
import com.mxgraph.util.mxUndoManager;
import com.mxgraph.util.mxUndoableEdit;
import com.mxgraph.util.mxUndoableEdit.mxUndoableChange;
import com.mxgraph.view.mxGraph;
import com.vci.client.workflow.editor.shape.mxTaskShape;
import com.vci.client.workflow.editor.ui.FlowEditor;
import com.vci.client.workflow.editor.ui.TaskNewPanel;
import com.vci.client.workflow.template.object.ProcessCategoryObject;
import com.vci.client.workflow.template.object.ProcessDefinitionObject;
 
public abstract class BasicGraphEditor extends JPanel {
 
    private static final long serialVersionUID = -2957233040496509650L;
 
    /**
     * 
     */
    protected mxGraphComponent graphComponent;
 
 
    protected EditorPalette palette;
 
    /**
     * 
     */
    protected mxUndoManager undoManager;
 
    /**
     * 
     */
    protected JLabel statusBar;
 
    /**
     * Flag indicating whether the current graph has been modified 
     */
    protected boolean modified = false;
 
    /**
     * 
     */
    protected mxRubberband rubberband;
 
    /**
     * 
     */
    protected mxKeyboardHandler keyboardHandler;
    
    private FlowEditor editor;
 
    private Object deployPanel;
    private String deployPanelFlag;
    
    /**
     * 
     */
    protected mxIEventListener undoHandler = new mxIEventListener() {
        public void invoke(Object source, mxEventObject evt) {
            undoManager.undoableEditHappened((mxUndoableEdit) evt
                    .getProperty("edit"));
        }
    };
 
    /**
     * 
     */
    protected mxIEventListener changeTracker = new mxIEventListener() {
        public void invoke(Object source, mxEventObject evt) {
            setModified(true);
        }
    };
 
    /**
     * 
     */
    public BasicGraphEditor(mxGraphComponent component)
    {
        mxGraphics2DCanvas.putShape("task", new mxTaskShape());
        // Stores a reference to the graph and creates the command history
        graphComponent = component;
        final mxGraph graph = graphComponent.getGraph();
        undoManager = createUndoManager();
 
        // Do not change the scale and translation after files have been loaded
        graph.setResetViewOnRootChange(false);
 
        // Updates the modified flag if the graph model changes
        graph.getModel().addListener(mxEvent.CHANGE, changeTracker);
 
        // Adds the command history to the model and view
        graph.getModel().addListener(mxEvent.UNDO, undoHandler);
        graph.getView().addListener(mxEvent.UNDO, undoHandler);
 
        // Keeps the selection in sync with the command history
        mxIEventListener undoHandler = new mxIEventListener()
        {
            public void invoke(Object source, mxEventObject evt)
            {
                List<mxUndoableChange> changes = ((mxUndoableEdit) evt
                        .getProperty("edit")).getChanges();
                graph.setSelectionCells(graph
                        .getSelectionCellsForChanges(changes));
            }
        };
        
        undoManager.addListener(mxEvent.UNDO, undoHandler);
        undoManager.addListener(mxEvent.REDO, undoHandler);
 
        // Creates the outer split pane that contains the inner split pane and
        // the graph component on the right side of the window
        palette = getEditorPalette();
        palette.setGraph(graphComponent.getGraph());
        palette.setPreferredWidth(60);
 
        final JScrollPane scrollPane = new JScrollPane(palette);
        editor = new FlowEditor(this);
        JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, editor);
        outer.setOneTouchExpandable(true);
        outer.setDividerLocation(150);//原来是200
        outer.setDividerSize(7);
 
        // Creates the status bar
        statusBar = createStatusBar();
 
        // Display some useful information about repaint events
        installRepaintListener();
 
        // Puts everything together
        setLayout(new BorderLayout());
        add(outer, BorderLayout.CENTER);
        add(statusBar, BorderLayout.SOUTH);
        
        Properties properties = new Properties();
        try {
            ClassLoader cl = TaskNewPanel.class.getClassLoader();
            InputStream in = cl.getResourceAsStream("flow-custom.properties");
            properties.load(in);
            deployPanelFlag = (String) properties.get("workflow.template");
            if ("1".equals(deployPanelFlag)) {
                //获取流程模板上的所有信息(修改流程模板)
                deployPanel = new FlowDeployPanel(this);
            } else if ("2".equals(deployPanelFlag)) {
                //获取流程模板上的所有信息(修改流程模板)
                deployPanel = new FlowDeployNewPanel(this);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        if ("1".equals(deployPanelFlag)) {
            add((FlowDeployPanel)deployPanel, BorderLayout.NORTH);
        } else if ("2".equals(deployPanelFlag)) {
            add((FlowDeployNewPanel)deployPanel, BorderLayout.NORTH);
        }
        
 
        // Installs rubberband selection and handling for some special
        // keystrokes such as F2, Control-C, -V, X, A etc.
        installHandlers();
        installListeners();
    }
    
    protected mxUndoManager createUndoManager() {
        return new mxUndoManager();
    }
 
    /**
     * 
     */
    protected void installHandlers()
    {
        rubberband = new mxRubberband(graphComponent);
        keyboardHandler = new EditorKeyboardHandler(graphComponent);
    }
 
    /**
     * 
     */
    protected JLabel createStatusBar()
    {
        JLabel statusBar = new JLabel(mxResources.get("ready"));
        statusBar.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
 
        return statusBar;
    }
 
    /**
     * 
     */
    protected void installRepaintListener()
    {
        graphComponent.getGraph().addListener(mxEvent.REPAINT,
                new mxIEventListener()
                {
                    public void invoke(Object source, mxEventObject evt)
                    {
                        String buffer = (graphComponent.getTripleBuffer() != null) ? ""
                                : " (unbuffered)";
                        mxRectangle dirty = (mxRectangle) evt
                                .getProperty("region");
 
                        if (dirty == null)
                        {
                            status("Repaint all" + buffer);
                        }
                        else
                        {
                            status("Repaint: x=" + (int) (dirty.getX()) + " y="
                                    + (int) (dirty.getY()) + " w="
                                    + (int) (dirty.getWidth()) + " h="
                                    + (int) (dirty.getHeight()) + buffer);
                        }
                    }
                });
    }
 
    public abstract EditorPalette getEditorPalette();
 
    /**
     * 
     */
    protected void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getWheelRotation() < 0) {
            graphComponent.zoomIn();
        } else {
            graphComponent.zoomOut();
        }
 
        status(mxResources.get("scale") + ": "
                + (int) (100 * graphComponent.getGraph().getView().getScale())
                + "%");
    }
 
    protected void mouseLocationChanged(MouseEvent e) {
        status(e.getX() + ", " + e.getY());
    }
 
    /**
     * 
     */
    protected void installListeners()
    {
        // Installs mouse wheel listener for zooming
        MouseWheelListener wheelTracker = new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (e.getSource() instanceof mxGraphOutline
                        || e.isControlDown()) {
                    BasicGraphEditor.this.mouseWheelMoved(e);
                }
            }
 
        };
 
        // Handles mouse wheel events in the outline and graph component
        graphComponent.addMouseWheelListener(wheelTracker);
 
        // Installs a mouse motion listener to display the mouse location
        graphComponent.getGraphControl().addMouseMotionListener(
                new MouseMotionListener()
                {
 
                    /*
                     * (non-Javadoc)
                     * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
                     */
                    public void mouseDragged(MouseEvent e)
                    {
                        mouseLocationChanged(e);
                    }
 
                    /*
                     * (non-Javadoc)
                     * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
                     */
                    public void mouseMoved(MouseEvent e)
                    {
                        mouseDragged(e);
                    }
 
                });
    }
 
    /**
     * 
     * @param modified
     */
    public void setModified(boolean modified) {
        boolean oldValue = this.modified;
        this.modified = modified;
 
        firePropertyChange("modified", oldValue, modified);
 
    }
 
    /**
     * 
     * @return whether or not the current graph has been modified
     */
    public boolean isModified()
    {
        return modified;
    }
 
    /**
     * 
     */
    public mxGraphComponent getGraphComponent()
    {
        return graphComponent;
    }
 
    /**
     * 
     */
    public mxUndoManager getUndoManager()
    {
        return undoManager;
    }
 
    /**
     * 
     * @param name
     * @param action
     * @return a new Action bound to the specified string name
     */
    public Action bind(String name, final Action action)
    {
        return bind(name, action, null);
    }
 
    /**
     * 
     * @param name
     * @param action
     * @return a new Action bound to the specified string name and icon
     */
    @SuppressWarnings("serial")
    public Action bind(String name, final Action action, String iconUrl)
    {
        ImageIcon imageIcon = null;
        if(iconUrl != null)
        {
            imageIcon = new ImageIcon(
                    BasicGraphEditor.class.getResource(iconUrl));
        }
 
        
        AbstractAction abstractAction = new AbstractAction(name, imageIcon)
        {
            public void actionPerformed(ActionEvent e)
            {
                action.actionPerformed(new ActionEvent(getGraphComponent(), e
                        .getID(), e.getActionCommand()));
            }
        };
        return abstractAction;
    }
 
    /**
     * 
     * @param msg
     */
    public void status(String msg)
    {
        statusBar.setText(msg);
    }
 
    /**
     * 
     */
    public void exit()
    {
        JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
 
        if (frame != null)
        {
            frame.dispose();
        }
    }
 
    /**
     * 
     */
    public void setLookAndFeel(String clazz)
    {
        JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
 
        if (frame != null)
        {
            try
            {
                UIManager.setLookAndFeel(clazz);
                SwingUtilities.updateComponentTreeUI(frame);
 
                // Needs to assign the key bindings again
                keyboardHandler = new EditorKeyboardHandler(graphComponent);
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
            }
        }
    }
 
    /**
     * Creates an action that executes the specified layout.
     * 
     * @param key Key to be used for getting the label from mxResources and also
     * to create the layout instance for the commercial graph editor example.
     * @return an action that executes the specified layout
     */
    @SuppressWarnings("serial")
    public Action graphLayout(final String key, boolean animate)
    {
        final mxIGraphLayout layout = createLayout(key, animate);
 
        if (layout != null)
        {
            return new AbstractAction(mxResources.get(key))
            {
                public void actionPerformed(ActionEvent e)
                {
                    final mxGraph graph = graphComponent.getGraph();
                    Object cell = graph.getSelectionCell();
 
                    if (cell == null
                            || graph.getModel().getChildCount(cell) == 0)
                    {
                        cell = graph.getDefaultParent();
                    }
 
                    graph.getModel().beginUpdate();
                    try
                    {
                        long t0 = System.currentTimeMillis();
                        layout.execute(cell);
                        status("Layout: " + (System.currentTimeMillis() - t0)
                                + " ms");
                    }
                    finally
                    {
                        mxMorphing morph = new mxMorphing(graphComponent, 20,
                                1.2, 20);
 
                        morph.addListener(mxEvent.DONE, new mxIEventListener()
                        {
 
                            public void invoke(Object sender, mxEventObject evt)
                            {
                                graph.getModel().endUpdate();
                            }
 
                        });
 
                        morph.startAnimation();
                    }
 
                }
 
            };
        }
        else
        {
            return new AbstractAction(mxResources.get(key))
            {
 
                public void actionPerformed(ActionEvent e)
                {
                    JOptionPane.showMessageDialog(graphComponent,
                            mxResources.get("noLayout"));
                }
 
            };
        }
    }
 
    /**
     * Creates a layout instance for the given identifier.
     */
    protected mxIGraphLayout createLayout(String ident, boolean animate)
    {
        mxIGraphLayout layout = null;
 
        if (ident != null)
        {
            mxGraph graph = graphComponent.getGraph();
 
            if (ident.equals("verticalHierarchical"))
            {
                layout = new mxHierarchicalLayout(graph);
            }
            else if (ident.equals("horizontalHierarchical"))
            {
                layout = new mxHierarchicalLayout(graph, JLabel.WEST);
            }
            else if (ident.equals("verticalTree"))
            {
                layout = new mxCompactTreeLayout(graph, false);
            }
            else if (ident.equals("horizontalTree"))
            {
                layout = new mxCompactTreeLayout(graph, true);
            }
            else if (ident.equals("parallelEdges"))
            {
                layout = new mxParallelEdgeLayout(graph);
            }
            else if (ident.equals("placeEdgeLabels"))
            {
                layout = new mxEdgeLabelLayout(graph);
            }
            else if (ident.equals("organicLayout"))
            {
                layout = new mxOrganicLayout(graph);
            }
            if (ident.equals("verticalPartition"))
            {
                layout = new mxPartitionLayout(graph, false)
                {
                    /**
                     * Overrides the empty implementation to return the size of the
                     * graph control.
                     */
                    public mxRectangle getContainerSize()
                    {
                        return graphComponent.getLayoutAreaSize();
                    }
                };
            }
            else if (ident.equals("horizontalPartition"))
            {
                layout = new mxPartitionLayout(graph, true)
                {
                    /**
                     * Overrides the empty implementation to return the size of the
                     * graph control.
                     */
                    public mxRectangle getContainerSize()
                    {
                        return graphComponent.getLayoutAreaSize();
                    }
                };
            }
            else if (ident.equals("verticalStack"))
            {
                layout = new mxStackLayout(graph, false)
                {
                    /**
                     * Overrides the empty implementation to return the size of the
                     * graph control.
                     */
                    public mxRectangle getContainerSize()
                    {
                        return graphComponent.getLayoutAreaSize();
                    }
                };
            }
            else if (ident.equals("horizontalStack"))
            {
                layout = new mxStackLayout(graph, true)
                {
                    /**
                     * Overrides the empty implementation to return the size of the
                     * graph control.
                     */
                    public mxRectangle getContainerSize()
                    {
                        return graphComponent.getLayoutAreaSize();
                    }
                };
            }
            else if (ident.equals("circleLayout"))
            {
                layout = new mxCircleLayout(graph);
            }
        }
 
        return layout;
    }
 
    public FlowEditor getEditor() {
        return editor;
    }
    
    public Object getDeployPanel() {
        return deployPanel;
    }
    
    public String getDeployPanelFlag() {
        return deployPanelFlag;
    }
 
    public void setFlowCategory(ProcessCategoryObject flowCategory) {
        if ("1".equals(deployPanelFlag)) {
            ((FlowDeployPanel)deployPanel).setFlowCategory(flowCategory);
        } else if ("2".equals(deployPanelFlag)) {
            ((FlowDeployNewPanel)deployPanel).setFlowCategory(flowCategory);
        }
    }
 
    public void setProcessDefinition(ProcessDefinitionObject selectedObj) throws Exception {
        if ("1".equals(deployPanelFlag)) {
            ((FlowDeployPanel)deployPanel).setProcessDefinition(selectedObj);
        } else if ("2".equals(deployPanelFlag)) {
            ((FlowDeployNewPanel)deployPanel).setProcessDefinition(selectedObj);
        }
    }
    
    /**
     * 模板导入时调用
     * @param graphXml
     * @param processXml
     * @throws Exception 
     */
    public void setProcessDefinition(byte[] graphXml, byte[] processXml,ProcessDefinitionObject pd,byte[] objectXml ) throws Exception {
        if ("1".equals(deployPanelFlag)) {
            ((FlowDeployPanel)deployPanel).setProcessDefinition(graphXml,processXml,pd,objectXml);
        } else if ("2".equals(deployPanelFlag)) {
            ((FlowDeployNewPanel)deployPanel).setProcessDefinition(graphXml,processXml,pd,objectXml);
        }
    }
    
}