/* * 此类主要是实现生命周期模板的创建功能,以及对已经创建好的生命周期模板的各种操作,主要用的了jgraph的相关知识。 * */ package com.vci.client.uif.engine.client; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Point; import java.awt.Toolkit; import java.awt.geom.Rectangle2D; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; import org.dom4j.DocumentException; import org.jgraph.JGraph; import org.jgraph.event.GraphModelEvent; import org.jgraph.event.GraphModelListener; import org.jgraph.graph.DefaultEdge; import org.jgraph.graph.DefaultGraphCell; import org.jgraph.graph.DefaultPort; import org.jgraph.graph.GraphConstants; import org.jgraph.graph.GraphLayoutCache; import com.vci.client.bof.ClientBusinessObject; import com.vci.client.omd.lifecycle.ui.WestJpanel; import com.vci.client.uif.actions.client.UIFUtils; import com.vci.corba.omd.lcm.LifeCycle; import com.vci.corba.omd.lcm.TransitionVO; import com.vci.corba.omd.lcm.TransitionVOEvent; public class LifeCyclePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private static ImageIcon ii = null; private String imageName = ""; static boolean isNoPressSaveTransitionEvents = false; static boolean door = false; /** * 跃迁事件全局变量 */ private static List>> transitionevents_maps = get_transitionevents_maps_instance(); private static JLabel startStateLabel; private static JPanel eastJpanel = new JPanel(); private static LifeCyclePanel lifecycleUI = null; private JPanel mainPanel = new JPanel(); private JPanel jp; private static JPanel eastJPanel1 = new JPanel(); private static DefaultGraphCell[] cellsBounds = null; private static DefaultGraphCell[] cellsRoutes = null; private static DefaultGraphCell[] cellsLifecyles = null; ArrayList cells = new ArrayList(); ArrayList cells1 = new ArrayList(); Map map = new HashMap(); private JScrollPane jScrollPane = new JScrollPane(mj); private JScrollPane eastsScrollPane = new JScrollPane(); Point point = new Point(0, 0); Point newPoint = new Point(0, 0); static MainJpanel mj = (MainJpanel) new MainJpanel().lifecycleui(); static Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); private static LifeCycle lc; private static ClientBusinessObject cbo; // 构造器,用于初始化首先要执行的操作 public LifeCyclePanel(LifeCycle lc,ClientBusinessObject cbo) { initComponent(); try { mj.treeUI(lc,cbo); } catch (DocumentException e) { e.printStackTrace(); } } public static LifeCyclePanel getInstance() { if (lifecycleUI == null) { lifecycleUI = new LifeCyclePanel(lc,cbo); return lifecycleUI; } JScrollPane newScrollPane = new JScrollPane(new WestJpanel()); newScrollPane .setPreferredSize(new Dimension(d.width / 6, d.height / 2)); eastJPanel1.removeAll(); eastJPanel1.updateUI(); return lifecycleUI; } public static List>> get_transitionevents_maps_instance() { if (transitionevents_maps == null) { transitionevents_maps = new ArrayList>>(); } return transitionevents_maps; } // 页面布局,首次进入的时候所看到的页面 public void initComponent() { door = false; setLayout(new BorderLayout()); mainPanel.setBorder(BorderFactory.createTitledBorder("生命周期模板")); mainPanel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTH; gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 3.0; gbc.weighty = 3.0; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; gbc.gridy = 1; gbc.weighty = 1.0; gbc.weightx = 0.5; gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 8.0; gbc.gridheight = 2; jScrollPane .setPreferredSize(new Dimension(d.width * (5 / 6), d.height)); mainPanel.add(jScrollPane, gbc); mainPanel.setPreferredSize(new Dimension(d.width * (5 / 6), d.height)); gbc.gridx = 2; gbc.gridy = 0; gbc.weightx = 2.0; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; eastJPanel1.setPreferredSize(new Dimension(d.width / 3, d.height / 4)); gbc.gridy = 1; gbc.fill = GridBagConstraints.BOTH; eastJpanel.setPreferredSize(new Dimension(d.width / 3, d.height)); eastsScrollPane.setPreferredSize(new Dimension(d.width / 3, d.height)); eastsScrollPane.setViewportView(eastJpanel); eastsScrollPane .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); eastsScrollPane .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); this.add(mainPanel, BorderLayout.CENTER); } // 点击添加页面的时候执行的方法,用于显示要添加属性的页面 // 定义jgraph页面的相关属性 static class MainJpanel extends JGraph { /** * */ private static final long serialVersionUID = 1L; ArrayList cells; DefaultPort port00, port01; DefaultGraphCell cell1; DefaultGraphCell cell2; DefaultEdge edges; JGraph j = this; // 设置一个静态的数组用于存放当前jgraph页面得到的组件 private static Object[] oo; public JGraph lifecycleui() { this.getModel().remove(this.getRoots()); this.getModel().remove(this.getRoots()); this.updateUI(); { this.setGridEnabled(true); this.setGridVisible(true); } this.setDropEnabled(true); j.getModel().addGraphModelListener(new GraphModelListener() { public void graphChanged(GraphModelEvent arg0) { setOo(j.getRoots()); mj.getCells(); Object[] ll = arg0.getChange().getInserted(); if (arg0.getChange().getInserted() != null) { if (ll.length == 1) { DefaultGraphCell defau = (DefaultGraphCell) ll[0]; List cells1 = cells; k: for (int i = 0; i < cells1.size(); i++) { if (defau.equals(cells1.get(i))) { cells1.remove(i); break k; } } for (int k1 = 0; k1 < cells1.size(); k1++) { DefaultGraphCell dfdf = cells1.get(k1); if (defau .getUserObject() .toString() .equals(dfdf.getUserObject().toString())) { JOptionPane.showMessageDialog(null, "不能选择重复的状态!"); Object[] objcet = new Object[1]; objcet[0] = defau; j.getModel().remove(objcet); } } } } mj.getCells(); } }); j.refresh(); j.setMoveBelowZero(false); return j; } public static Object[] getOo() { return oo; } public static void setOo(Object[] oo) { MainJpanel.oo = oo; } private UIFUtils cbalo = new UIFUtils(); public List> getCells(LifeCycle lc,ClientBusinessObject cbo) { try { cellsBounds = new DefaultGraphCell[lc.bounds.length]; double x, y, w, h; String cellicon = null, name = null; String currentStatus = cbo.getLcStatus(); Map map = null; List> list = null; list = new ArrayList>(); for (int i = 0; i < lc.bounds.length; i++) { cellsBounds[i] = new DefaultGraphCell(lc.bounds[i].name); cellsBounds[i].setUserObject(cbalo.transferLifecycle(lc.bounds[i].name)); x = Double.parseDouble(lc.bounds[i].cellx); y = Double.parseDouble(lc.bounds[i].celly); w = Double.parseDouble(lc.bounds[i].cellw); h = Double.parseDouble(lc.bounds[i].cellh); cellicon = lc.bounds[i].cellicon; name = lc.bounds[i].name; GraphConstants.setBounds(cellsBounds[i].getAttributes(), new Rectangle2D.Double(x, y, w, h)); if(currentStatus.equals(lc.bounds[i].name)){ GraphConstants.setBorderColor( cellsBounds[i].getAttributes(), Color.RED); }else{ GraphConstants.setBorderColor( cellsBounds[i].getAttributes(), Color.BLACK); } GraphConstants.setOpaque(cellsBounds[i].getAttributes(), true); GraphConstants.setEditable(cellsBounds[i].getAttributes(), false); // GraphConstants.setAutoSize(cellsBounds[i].getAttributes(), // true); if (!cellicon.startsWith("http://")) { ii = new ImageIcon(cellicon); } else { URL url = null; try { url = new URL(cellicon); } catch (MalformedURLException e) { e.printStackTrace(); } if (url == null) { continue; } ii = new ImageIcon(url); } GraphConstants.setIcon(cellsBounds[i].getAttributes(), ii); DefaultPort fp = new DefaultPort(); cellsBounds[i].add(fp); map = new HashMap(); map.put(name, cellsBounds[i]); list.add(map); } return list; } catch (NumberFormatException e) { e.printStackTrace(); } return null; } public String getKey(Map map) { String key = null; Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); key = (String) entry.getKey(); // System.out.println(entry.getKey()); } return key; } public DefaultGraphCell[] arraycat(DefaultGraphCell[] dgc1, DefaultGraphCell[] dgc2) { try { int len1 = 0; int len2 = 0; if (dgc1 != null) len1 = dgc1.length; if (dgc2 != null) len2 = dgc2.length; if (len1 + len2 > 0) cellsLifecyles = new DefaultGraphCell[len1 + len2]; if (len1 > 0) System.arraycopy(dgc1, 0, cellsLifecyles, 0, len1); if (len2 > 0) System.arraycopy(dgc2, 0, cellsLifecyles, len1, len2); return cellsLifecyles; } catch (Exception e) { e.printStackTrace(); } return null; } // 点击左边树的时候的显示页面 public void treeUI(LifeCycle lc,ClientBusinessObject cbo) throws DocumentException { try { this.j.getModel().remove(getRoots()); door = true; List> list = getCells(lc,cbo); DefaultGraphCell cell = null; TransitionVO[] routes = lc.routes; if (routes != null && routes.length > 0) { cellsRoutes = new DefaultGraphCell[routes.length]; for (int i = 0; i < routes.length; i++) { DefaultEdge edge = new DefaultEdge(); edge.setUserObject(routes[i].connect); String source = routes[i].source; String destination = routes[i].destination; for (Map map : list) { if (source.equals(getKey(map))) { cell = map.get(source); edge.setSource(cell.getChildAt(0)); } if (destination.equals(getKey(map))) { cell = map.get(destination); edge.setTarget(cell.getChildAt(0)); } } cellsRoutes[i] = edge; int arrow = GraphConstants.ARROW_CLASSIC; GraphConstants.setLineEnd(edge.getAttributes(), arrow); GraphConstants.setEndFill(edge.getAttributes(), true); GraphConstants.setDisconnectable(edge.getAttributes(), false); } } cellsLifecyles = arraycat(cellsBounds, cellsRoutes); if (cellsLifecyles != null && cellsLifecyles.length > 0 && cellsRoutes.length > 0 && cellsBounds.length > 0) { for (DefaultGraphCell cellLifecyle : cellsLifecyles) { GraphLayoutCache glc = this.j.getGraphLayoutCache(); glc.insert(cellLifecyle); } } // 清内存 clearDefaultGraphCells(cellsBounds); clearDefaultGraphCells(cellsRoutes); clearDefaultGraphCells(cellsLifecyles); setOo(this.j.getRoots()); this.j.setMoveable(false); lifecycleUI.mj.setEdgeLabelsMovable(false); lifecycleUI.mj.setEditable(false); lifecycleUI.mj.setMoveable(false); lifecycleUI.mj.setEditable(false); lifecycleUI.mj.setEnabled(false); eastJpanel.removeAll(); eastJpanel.updateUI(); eastJpanel.add(eastJPanel1, BorderLayout.CENTER); } catch (Exception e) { e.printStackTrace(); } } public void clearDefaultGraphCells(DefaultGraphCell[] defaultGraphCells) { if (defaultGraphCells != null) { for (int i = 0; i < defaultGraphCells.length; i++) { DefaultGraphCell defaultGraphCell = defaultGraphCells[i]; defaultGraphCell = null; } defaultGraphCells = null; } } // 得到当前页面的所有cell,当页面有所改变的时候将这些cell更新进去 public void getCells() { cells = new ArrayList(); Object[] o2 = j.getRoots(); for (int i = 0; i < o2.length; i++) { if (!(o2[i] instanceof DefaultPort)) { if ((o2[i] instanceof DefaultGraphCell) && !(o2[i] instanceof DefaultEdge)) { DefaultGraphCell dgf1 = (DefaultGraphCell) o2[i]; cells.add(dgf1); } } } } } /*** * * @param map * @return */ public static String getListImageKey(Map map) { String key = null; Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); key = (String) entry.getKey(); // System.out.println(entry.getKey()); } return key; } }