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; } }