wangting
2024-12-30 306a9c4fde94c54d91aee5a69d59b993c7c707a1
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
package com.vci.client.workflow.editor;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;
import javax.swing.border.LineBorder;
 
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.util.mxGraphTransferable;
import com.mxgraph.swing.util.mxSwingConstants;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.vci.client.workflow.editor.ui.IProcessProperty;
 
public class EditorPalette extends JPanel {
    
    private static final long serialVersionUID = 8470805668097198382L;
 
    protected JLabel selectedEntry = null;
    private mxGraph graph;
 
    protected mxEventSource eventSource = new mxEventSource(this);
 
    @SuppressWarnings("serial")
    public EditorPalette() {
        setBackground(Color.WHITE);
        setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
 
        // Clears the current selection when the background is clicked
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                clearSelection();
            }
        });
 
        // Shows a nice icon for drag and drop but doesn't import anything
        setTransferHandler(new TransferHandler() {
            public boolean canImport(JComponent comp, DataFlavor[] flavors) {
                return true;
            }
        });
    }
 
    public void clearSelection() {
        setSelectionEntry(null, null);
    }
 
    public void setSelectionEntry(JLabel entry, mxGraphTransferable t) {
        JLabel previous = selectedEntry;
        selectedEntry = entry;
 
        if (previous != null) {
            previous.setBorder(null);
            previous.setOpaque(false);
        }
 
        if (selectedEntry != null) {
            selectedEntry.setBorder(new LineBorder(Color.BLUE));
            selectedEntry.setOpaque(true);
            selectedEntry.setBackground(new Color(117, 195, 173));
        }
 
        eventSource.fireEvent(new mxEventObject(mxEvent.SELECT, "entry",
                selectedEntry, "transferable", t, "previous", previous));
    }
 
    /**
     * 
     */
    public void setPreferredWidth(int width) {
        int cols = Math.max(1, width / 55);
        setPreferredSize(new Dimension(width,
                (getComponentCount() * 55 / cols) + 30));
        revalidate();
    }
 
    /**
     * 
     * @param name
     * @param icon
     * @param style
     * @param width
     * @param height
     * @param value
     */
    public void addEdgeTemplate(final String name, ImageIcon icon,
            String style, int width, int height, Object value)
    {
        mxGeometry geometry = new mxGeometry(0, 0, width, height);
        geometry.setTerminalPoint(new mxPoint(0, height), true);
        geometry.setTerminalPoint(new mxPoint(width, 0), false);
        geometry.setRelative(true);
 
        mxCell cell = new mxCell(value, geometry, style);
        cell.setEdge(true);
 
        addTemplate(name, icon, cell);
    }
 
    /**
     * 
     * @param name
     * @param icon
     * @param style
     * @param width
     * @param height
     * @param value
     */
    public void addTemplate(final String name, ImageIcon icon, String style,
            int width, int height, Object value)
    {
        mxCell cell = new mxCell(value, new mxGeometry(0, 0, width, height),
                style);
        cell.setVertex(true);
 
        addTemplate(name, icon, cell);
    }
 
    /**
     * 
     * @param name
     * @param icon
     * @param cell
     */
    public void addTemplate(final String name, ImageIcon icon, mxCell cell)
    {
        mxRectangle bounds = (mxGeometry) cell.getGeometry().clone();
        final mxGraphTransferable t = new mxGraphTransferable(
                new Object[] { cell }, bounds);
 
        // Scales the image if it's too large for the library
        if (icon != null)
        {
            if (icon.getIconWidth() > 32 || icon.getIconHeight() > 32)
            {
                icon = new ImageIcon(icon.getImage().getScaledInstance(32, 32,
                        0));
            }
        }
 
        final JLabel entry = new JLabel(icon);
        entry.setPreferredSize(new Dimension(60, 60));
        entry.setBackground(getBackground().brighter());
        entry.setFont(new Font(entry.getFont().getFamily(), 0, 12));
 
        entry.setVerticalTextPosition(JLabel.BOTTOM);
        entry.setHorizontalTextPosition(JLabel.CENTER);
        entry.setIconTextGap(0);
 
        entry.setToolTipText(name);
        entry.setText(name);
 
        entry.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                setSelectionEntry(entry, t);
            }
        });
 
        // Install the handler for dragging nodes into a graph
        DragGestureListener dragGestureListener = new DragGestureListener() {
            public void dragGestureRecognized(DragGestureEvent e) {
                JLabel label=(JLabel)e.getComponent();
                if (label.getText().equals("开始")) {
                    Object parent = graph.getDefaultParent();
                    Object[] cells = graph.getChildCells(parent);
                    boolean hasStart=false;
                    for (Object cell : cells) {
                        if (!graph.getModel().isEdge(cell)){
                            IProcessProperty tool = (IProcessProperty)((mxCell)cell).getValue();
                            if (tool.getNodeType().equals("start")) {
                                hasStart=true;
                                break;
                            }
                        }
                    }
                    if (!hasStart) {
                        e.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(),
                                t, null);
                    }
                }else {
                    e.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(),
                            t, null);
                }
//                e.startDrag(null, mxConstants.EMPTY_IMAGE, new Point(), t, null);
            }
        };
 
        DragSource dragSource = new DragSource();
        dragSource.createDefaultDragGestureRecognizer(entry,
                DnDConstants.ACTION_COPY, dragGestureListener);
 
        add(entry);
    }
 
    /**
     * @param eventName
     * @param listener
     * @see com.mxgraph.util.mxEventSource#addListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
     */
    public void addListener(String eventName, mxIEventListener listener)
    {
        eventSource.addListener(eventName, listener);
    }
 
    /**
     * @return whether or not event are enabled for this palette
     * @see com.mxgraph.util.mxEventSource#isEventsEnabled()
     */
    public boolean isEventsEnabled()
    {
        return eventSource.isEventsEnabled();
    }
 
    /**
     * @param listener
     * @see com.mxgraph.util.mxEventSource#removeListener(com.mxgraph.util.mxEventSource.mxIEventListener)
     */
    public void removeListener(mxIEventListener listener)
    {
        eventSource.removeListener(listener);
    }
 
    /**
     * @param eventName
     * @param listener
     * @see com.mxgraph.util.mxEventSource#removeListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
     */
    public void removeListener(mxIEventListener listener, String eventName)
    {
        eventSource.removeListener(listener, eventName);
    }
 
    /**
     * @param eventsEnabled
     * @see com.mxgraph.util.mxEventSource#setEventsEnabled(boolean)
     */
    public void setEventsEnabled(boolean eventsEnabled)
    {
        eventSource.setEventsEnabled(eventsEnabled);
    }
 
    public void setGraph(mxGraph graph) {
        this.graph = graph;
    }
 
}