package com.vci.client.omd.statepool.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; 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.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; import javax.swing.ScrollPaneConstants; import javax.swing.border.EmptyBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import org.dom4j.Document; import com.vci.client.LogonApplication; import com.vci.client.omd.statepool.StatePoolStart; import com.vci.client.omd.statepool.StatePoolWrapper; import com.vci.client.omd.statepool.itf.InterState; import com.vci.client.omd.statepool.pubimpl.InterStateManager; import com.vci.client.omd.ui.OmdTreeCellRenderer; import com.vci.client.ui.image.BundleImage; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.util.PostorderEnumeration; import com.vci.corba.common.VCIError; import com.vci.corba.omd.stm.StatePool; import com.vci.mw.InvocationUtility; @SuppressWarnings("all") public class StatePoolUI extends JPanel { /** * */ private ImageIcon icon = null; private String imageURL = ""; private boolean modifyFlag = false; private static final long serialVersionUID = -3925091496139447035L; String realName = ""; public static StatePoolUI statePoolUI = null; private StatePool statePool = null; // 定义文本框,用于用户输入数据 private JTextField jtf1 = new JTextField(60), jtf2 = new JTextField(60), jtf3 = new JTextField(60); // 定义标签,显示文本框的含义 private JLabel jl1 = new JLabel("名称"), jl2 = new JLabel("标签"), jl3 = new JLabel("图片"), jl4 = new JLabel("描述"), jl11 = new JLabel("名称(不能为空)"), jl22 = new JLabel("标签(对名称的解释)"), jl33 = new JLabel("图片(路径)"), jl44 = new JLabel("描述(可以为空)"); // 定义文本域,用于书写描述信息 private JTextArea jta = new JTextArea(3, 60); // 定义容器,用于接收需要布局的容器 private JPanel jp; // test private ImageIcon image = new BundleImage().createImageIcon("zoom.png"); // 定义map集合用于存放从XML文件中获取到的数据 Map map = new HashMap(); private JButton addButton, modifyButton, deleteButton, checkButton, saveButton, cancelButton, selectButton = new JButton(image), selectButton1; // 定义小容器用于界面分区的时候使用 private JPanel northJPanel, westJPanel = new JPanel(), mainJPanel, centerJPanel, leftJPanel = new JPanel(), textJPanel = new JPanel(); // 定义滚动条用于存放tree private JScrollPane scrollPane = new JScrollPane(); // 定义一棵树 private JTree tree; private String nodeName = null; private JButton btnExp; private JButton btnImp; private JTextField textField; private JButton sel; // 构造器初始化要优先执行的方法 public StatePoolUI() { createPoolTree(); initUI(); saveButton.setEnabled(false); cancelButton.setEnabled(false); addListeneradd(); addListenermodify(); addListenerdelete(); addListenersave(); addListenercancel(); selectListeneradd(); addListenner(); btnExp.addActionListener(new ExpActionListener()); btnImp.addActionListener(new ImpActionListener()); } // 新建按钮的监听器 private void addListeneradd() { addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (mainJPanel == null) { return; } // selectButton.setIcon(image); selectButton.setEnabled(true); jtf1.setEditable(true); jtf2.setEditable(true); jtf3.setEditable(false); jta.setEditable(true); modifyFlag = false; addButton.setEnabled(false); modifyButton.setEnabled(false); deleteButton.setEnabled(false); checkButton.setEnabled(false); tree.setEnabled(false); mainJPanel.removeAll(); mainJPanel.add(addVerpanel(), BorderLayout.CENTER); mainJPanel.updateUI(); saveButton.setEnabled(true); cancelButton.setEnabled(true); } }); } // 新定义一个JFrame用于列举状态图标供用户选择 // public JDialog listImage() { // List urls = null; // List imagePaths_after = null; // final JDialog mainWin = new JDialog(); // mainWin.setBounds(300, 0, 60, 300); // String basePath = LogonApplication.urlBasePath; // // imagePaths_after = new ArrayList(); // urls = new ArrayList(); // // String[] imagePaths = StatePoolStart.getService().getImagePaths(); // for(String imagePath:imagePaths){ // String imagePaths_middle = basePath+imagePath; // imagePaths_after.add(imagePaths_middle); // } // // String[] friends = imagePaths_after.toArray(new String[0]); // // for(String friend:friends){ // URL url = null; // try { // url = new URL(friend); // urls.add(url); // // imagePaths.add(url.toString()); // } catch (MalformedURLException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // if(url == null){ // continue; // } // // } // //// JList jList = new JList(urls.toArray(new URL[0])); // // 定义一个JList对象 // final JList friendsList = new JList((URL[])urls.toArray(new URL[0])); // // // 设置该JList使用ImageCellRenderer作为单元格绘制器 // friendsList.setCellRenderer(new ImageCellRenderer()); // friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置列表只能单选 // mainWin.add(new JScrollPane(friendsList)); // friendsList.addListSelectionListener(new ListSelectionListener() { // @Override // public void valueChanged(ListSelectionEvent e) { // jtf3.setText((String) friendsList.getSelectedValue()); // mainWin.dispose(); // selectButton.setIcon(new ImageIcon((URL) friendsList // .getSelectedValue())); // } // // }); // mainWin.setModal(true); // mainWin.pack(); // mainWin.addWindowListener(new WindowAdapter() { // public void windowClosing(WindowEvent e) { // mainWin.dispose(); // } // }); // mainWin.setVisible(true); // return mainWin; // } // 此类只在内部使用,因此定义为内部类,实现画图功能 // class ImageCellRenderer extends JPanel implements ListCellRenderer { // private ImageIcon icon; // private String name; // // 定义绘制单元格时的背景色 // private Color background; // // 定义绘制单元格时的前景色 // private Color foreground; // // public Component getListCellRendererComponent(JList list, Object value, // int index, boolean isSelected, boolean cellHasFocus) { // icon = new ImageIcon((URL) value); // name = value.toString(); // background = isSelected ? list.getSelectionBackground() : list // .getBackground(); // foreground = isSelected ? list.getSelectionForeground() : list // .getForeground(); // // 返回该JPanel对象作为单元格绘制器 // return this; // } // // // 重写paintComponent方法,改变JPanel的外观 // public void paintComponent(Graphics g) { // int imageWidth = icon.getImage().getWidth(null); // int imageHeight = icon.getImage().getHeight(null); // g.setColor(background); // g.fillRect(0, 0, getWidth(), getHeight()); // g.setColor(foreground); // // 绘制好友图标 // g.drawImage(icon.getImage(), getWidth() / 2 - imageWidth / 2, 10, // null); // g.setFont(new Font("SansSerif", Font.BOLD, 10)); // // // 绘制好友用户名 // g.drawString(name, getWidth() / 2 - name.length() * 3, // imageHeight + 30); // } // // // 通过该方法来设置该ImageCellRenderer的最佳大小 // public Dimension getPreferredSize() { // return new Dimension(60, 80); // } // } // 此方法是选中按钮的监听器 private void selectListeneradd() { selectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (mainJPanel == null) { return; } // listImage(); ImageSelectDialog dialog = ImageSelectDialog.getInstance(); dialog.setVisible(true); imageURL = dialog.getImageURL(); if (imageURL.startsWith(".")) { imageURL = dialog.getImageURLLocal(); // System.out.println("本地URL================"+imageURL); updateImageUILocal(); } else { imageURL = dialog.getImageURL(); updateImageUI(); } } }); } public void updateImageUILocal() { jtf3.setText(imageURL); // ImageIcon icon = new ImageIcon(imageURL); // if(imageURL == null || imageURL == ""){ // icon = image; // } // selectButton.setIcon(icon); } public void updateImageUI() { jtf3.setText(imageURL); // URL url = null; // try { //// url = new URL(imageURL); // } catch (MalformedURLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // ImageIcon icon = new ImageIcon(url); // ImageIcon icon = null; // if(imageURL.contains("-")){ // icon = new ImageBundle().createImageIcon(imageURL); // } else { // icon = new BundleImage().createImageIcon(imageURL); // } // selectButton.setIcon(icon); } // 新建状态的时候要显示的界面 public JPanel addVerpanel() { jtf1.setText(""); jtf1.setEnabled(true); jtf2.setText(""); jtf3.setText(""); jta.setText(""); // selectButton.setIcon(image); JPanel jp = new JPanel(); jp.setBackground(Color.WHITE); jp.setLayout(new BorderLayout(0, 0)); jp.setBorder(BorderFactory.createTitledBorder("创建状态:")); jp.setLayout(new GridBagLayout()); GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.insets = new Insets(10, 5, 0, 5); g.gridx = 0; g.gridy = 0; jp.add(jl11, g); GridBagConstraints g1 = new GridBagConstraints(); g1.insets = new Insets(10, 5, 0, 5); g1.gridx = 1; g1.gridy = 0; jp.add(jtf1, g1); /* * jtf1.addFocusListener(new FocusListener() { String regex = "[a-z A-Z]*"; * * public void focusLost(FocusEvent e) { if (jtf1.getText().equals("") || * (!jtf1.getText().matches(regex))) { JOptionPane.showMessageDialog(null, * jl11.getText() .substring(0, 2) + "不能为空且只能为英文"); jtf1.setText(""); * jtf1.requestDefaultFocus(); } } * * @Override public void focusGained(FocusEvent e) { * * } }); */ // 限定文本框的有效输入长度为50 jtf1.setDocument(new PlainDocument() { public void insertString(int offs, String str, javax.swing.text.AttributeSet a) throws BadLocationException { String text = jtf1.getText(); if (text.length() + str.length() > 50) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "名称不能超过50个字符"); return; } super.insertString(offs, str, a); } }); g.gridx = 0; g.gridy = 1; jp.add(jl22, g); g.gridx = 1; g.gridy = 1; jp.add(jtf2, g); g.gridx = 0; g.gridy = 2; jp.add(jl33, g); g.gridx = 1; g.gridy = 2; jp.add(jtf3, g); g.gridx = 2; g.gridy = 2; selectButton.setToolTipText("选择"); selectButton.setText("选择"); jp.add(selectButton, g); g.gridx = 0; g.gridy = 3; jp.add(jl44, g); jta.setLineWrap(true); g1.gridx = 1; g1.gridy = 3; jp.add(new JScrollPane(jta), g1); jta.setDocument(new PlainDocument() { public void insertString(int offs, String str, javax.swing.text.AttributeSet a) throws BadLocationException { String text = jtf1.getText(); if (text.length() + str.length() > 255) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "名称不能超过255个字符"); return; } super.insertString(offs, str, a); } }); return jp; } // 主界面的可视化方法,主要是定义界面的布局,不提供具体的功能 private void initUI() { setBackground(Color.WHITE); setLayout(new BorderLayout(0, 0)); // northPanel northJPanel = new JPanel(); FlowLayout fl = (FlowLayout) northJPanel.getLayout(); fl.setAlignment(FlowLayout.CENTER); this.add(northJPanel, BorderLayout.NORTH); addButton = new JButton("创建"); modifyButton = new JButton("修改"); deleteButton = new JButton("删除"); checkButton = new JButton("查看使用范围"); saveButton = new JButton("保存"); cancelButton = new JButton("取消"); btnExp = new JButton("导出"); btnImp = new JButton("导入"); northJPanel.add(addButton); northJPanel.add(modifyButton); northJPanel.add(deleteButton); northJPanel.add(checkButton); northJPanel.add(saveButton); northJPanel.add(cancelButton); northJPanel.add(btnExp); northJPanel.add(btnImp); // leftPanel centerJPanel = new JPanel(); FlowLayout fl1 = (FlowLayout) centerJPanel.getLayout(); fl1.setAlignment(FlowLayout.LEFT); JScrollPane scrollPane_center = new JScrollPane(); scrollPane_center.setViewportView(centerJPanel); scrollPane_center.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane_center.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.add(scrollPane_center, BorderLayout.CENTER); centerJPanel.setLayout(new GridBagLayout()); westJPanel.setBorder(new EmptyBorder(5, 5, 0, 5)); westJPanel.setAutoscrolls(true); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; // add by caill start centerJPanel.add(leftJPanel, gbc); westJPanel.setLayout(new BorderLayout(5, 5)); // Dimension(centerJPanel.getSize().width/4,centerJPanel.getSize().height)); textField = new JTextField(); sel = new JButton("定位"); // 搜索框的设置 GridBagConstraints gb = new GridBagConstraints(); gb.gridx = 0; gb.gridy = 0; gb.weightx = 0.7; gb.weighty = 0; gb.fill = GridBagConstraints.HORIZONTAL; textJPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); textJPanel.setLayout(new GridBagLayout()); textJPanel.add(textField, gb); textJPanel.add(sel); // treeJPanel.add(scrollPane, BorderLayout.CENTER); // 将westJPanel和textJPanel容器放入到leftJPanel,并上下分隔 leftJPanel.setLayout(new BorderLayout(5, 5)); westJPanel.add(scrollPane, BorderLayout.CENTER); leftJPanel.add(textJPanel, BorderLayout.NORTH); leftJPanel.add(westJPanel, BorderLayout.CENTER); // 为定位按钮增加点击事件 sel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // e.getSource(); String test = textField.getText().trim(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.reload(); TreeNode root = (TreeNode) model.getRoot(); PostorderEnumeration enumeration = new PostorderEnumeration(root); while (enumeration.hasMoreElements()) { DefaultMutableTreeNode element = (DefaultMutableTreeNode) enumeration.nextElement(); if (element.getUserObject() instanceof StatePoolWrapper) { StatePoolWrapper wrapper = (StatePoolWrapper) element.getUserObject(); if (wrapper != null && wrapper.getSP() != null && wrapper.getSP().name.equals(test)) { TreeNode[] path = element.getPath(); TreePath treePath = new TreePath(path); tree.setSelectionPath(treePath); return; } ; } } if (test.equals("")) { JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "请输入状态", "提示", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "{ " + test + " }状态不存在", "提示", JOptionPane.INFORMATION_MESSAGE); } } }); // 为定位框添加事件 textField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JTextField text = (JTextField) e.getSource(); String test = text.getText().trim(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.reload(); TreeNode root = (TreeNode) model.getRoot(); PostorderEnumeration enumeration = new PostorderEnumeration(root); while (enumeration.hasMoreElements()) { DefaultMutableTreeNode element = (DefaultMutableTreeNode) enumeration.nextElement(); if (element.getUserObject() instanceof StatePoolWrapper) { StatePoolWrapper wrapper = (StatePoolWrapper) element.getUserObject(); if (wrapper != null && wrapper.getSP() != null && wrapper.getSP().name.equals(test)) { TreeNode[] path = element.getPath(); TreePath treePath = new TreePath(path); tree.setSelectionPath(treePath); return; } ; } } if (test.equals("")) { JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "请输入状态", "提示", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "{ " + test + " }状态不存在", "提示", JOptionPane.INFORMATION_MESSAGE); } } }); // add by caill end // mainJPanel位于右侧 mainJPanel = new JPanel(); // mainJPanel.setPreferredSize(new // Dimension(centerJPanel.getSize().width/5*4,centerJPanel.getSize().height)); gbc.gridx = 1; gbc.weightx = 4.0; centerJPanel.add(mainJPanel, gbc); mainJPanel.setLayout(new BorderLayout(5, 5)); } // 当树的子节点被选中的时候调用此方法显示选中节点的信息 public StatePool poolTreeSelection() { try { selectButton.setEnabled(false); jtf1.setEditable(false); jtf2.setEditable(false); jtf3.setEditable(false); jta.setEditable(false); Document doc = null; DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node != null && node.getUserObject() instanceof StatePoolWrapper) { return ((StatePoolWrapper) node.getUserObject()).getSP(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // 新建一棵树用于显示所有的状态名称 public void createPoolTree() { DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("状态列表"); try { StatePool[] statePools = StatePoolStart.getService().getStatePools(); for (StatePool sp : statePools) { DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new StatePoolWrapper(sp)); node1.add(node2); } } catch (VCIError e) { e.printStackTrace(); } tree = new JTree(node1); scrollPane.setViewportView(tree); tree.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent mouseevent) { StatePool statePool = poolTreeSelection(); westJPanel.removeAll(); westJPanel.add(scrollPane); westJPanel.updateUI(); mainJPanel.removeAll(); if (statePool != null) { mainJPanel.add(addStatePloolPanel(statePool), BorderLayout.CENTER); } mainJPanel.updateUI(); } }); tree.setCellRenderer(new OmdTreeCellRenderer()); } // 添加监听器这个监听器是取消按钮的,点击此按钮后界面将会被重置 private void addListenercancel() { cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showConfirmDialog(null, "您确定要执行取消操作吗?"); buttonManager(); buttonManager1(); mainJPanel.removeAll(); mainJPanel.updateUI(); } }); } // 保存按钮的监听器,主要任务是讲数据存入XML文件,并且重置界面 private void addListenersave() { saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ((!jtf1.getText().equals(""))) { StatePool statePool_ = null; try { statePool_ = StatePoolStart.getService().getStatePool(jtf1.getText()); } catch (VCIError e2) { e2.printStackTrace(); } if (!statePool_.name.equals("") && !modifyFlag) { JOptionPane.showMessageDialog(null, "名称重复请更换名称!"); return; } // add by caill start 2016.1.12状态池名称只能为英文字母 String regex = "[a-z A-Z]*"; if ((!jtf1.getText().matches(regex))) { JOptionPane.showMessageDialog(null, "名称只能为英文!"); return; } // add by caill end buttonManager(); try { statePool = new StatePool(); statePool.name = jtf1.getText(); statePool.tag = jtf2.getText(); statePool.imagePath = jtf3.getText(); statePool.description = jta.getText(); String userName = InvocationUtility.getInvocation().userName; statePool.modifier = userName; if (!modifyFlag) { statePool.creator = userName; StatePoolStart.getService().addStatePool(statePool); } else { StatePool selectedSP = poolTreeSelection(); selectedSP.tag = jtf2.getText(); selectedSP.imagePath = jtf3.getText(); selectedSP.description = jta.getText(); selectedSP.modifier = userName; nodeName = selectedSP.name; StatePoolStart.getService().modifyStatePool(selectedSP); } } catch (Exception e1) { e1.printStackTrace(); } createPoolTree(); westJPanel.add(scrollPane, BorderLayout.CENTER); westJPanel.updateUI(); buttonManager1(); mainJPanel.removeAll(); mainJPanel.updateUI(); } else { if ((jtf1.getText().equals(""))) { JOptionPane.showMessageDialog(null, "请检查输入的数据,将数据补充完整!"); return; } } } }); } // 修改按钮的监听器,主要任务是提示操作人员是否执行修改操作并且将修改后的数据存入XML文件和重置界面 private void addListenermodify() { modifyButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { selectButton.setEnabled(true); jtf1.setEditable(false); jtf2.setEditable(true); jtf3.setEditable(false); jta.setEditable(true); modifyFlag = true; StatePool selectedSP = poolTreeSelection(); if (selectedSP != null) { JOptionPane.showConfirmDialog(null, "您确定要执行修改操作吗?"); // nodeName = selectedNode.toString(); selectButton.setToolTipText("选择"); selectButton.setEnabled(true); jtf1.setEditable(true); jtf2.setEditable(true); jtf3.setEditable(false); jta.setEditable(true); buttonManager2(); // try { // statePool = new StatePool(); // statePool.name = jtf1.getText(); // statePool.tag = jtf2.getText(); // statePool.imagePath = jtf3.getText(); // statePool.describe = jta.getText(); // statePool.id = nodeName; // StatePoolStart.getService().modifyStatePool(statePool); // createPoolTree(); // scrollPane.setViewportView(tree); // westJPanel.add(scrollPane, BorderLayout.CENTER); // westJPanel.updateUI(); // buttonManager(); // buttonManager1(); // mainJPanel.removeAll(); // mainJPanel.updateUI(); // } catch (Exception e) { // e.printStackTrace(); // } } else { buttonManager(); buttonManager1(); JOptionPane.showMessageDialog(null, "请先选中要修改的项"); mainJPanel.removeAll(); mainJPanel.updateUI(); } } }); } // 删除按钮的监听器,主要任务是将要删除的项在XML文件中删除并且重置界面 private void addListenerdelete() { deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { StatePool selectedSP = poolTreeSelection(); if (selectedSP != null) { Object[] options = { "Yes", "No" }; int n = JOptionPane.showOptionDialog(getInstance(), "确认是否删除状态\"" + selectedSP.name + "\"", "生命周期状态删除", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); // int n = JOptionPane.showConfirmDialog(null, "您确定要执行删除操作吗?"); if (n != JOptionPane.YES_OPTION) { return; } nodeName = selectedSP.name; try { ArrayList interVerList = InterStateManager.getInstance().getInterVerList(); for (Iterator i = interVerList.iterator(); i.hasNext();) { InterState interState = (InterState) i.next(); ArrayList usedNameList = interState.getUsedNameList(nodeName); if (usedNameList != null && usedNameList.size() > 0) { JOptionPane.showMessageDialog(null, "该状态已经被使用"); return; } } StatePool statePool_ = StatePoolStart.getService().getStatePool(jtf1.getText()); StatePoolStart.getService().deleteStatePool(statePool_); } catch (Exception e) { e.printStackTrace(); } createPoolTree(); scrollPane.setViewportView(tree); westJPanel.add(scrollPane, BorderLayout.CENTER); westJPanel.updateUI(); buttonManager(); buttonManager1(); mainJPanel.removeAll(); mainJPanel.updateUI(); createPoolTree(); westJPanel.updateUI(); centerJPanel.updateUI(); } else { JOptionPane.showMessageDialog(null, "请先选中要删除的项"); } } }); } public static void cleanInstance() { statePoolUI = null; } // 此方法是一个静态方法,初始化的时候判断主容器是否存在,存在则继续执行,不存在则新建容器 public static StatePoolUI getInstance() { if (statePoolUI == null) { statePoolUI = new StatePoolUI(); } return statePoolUI; } // 此方法是点击树节点的时候要显示的界面与新建界面略微有所不同因此不能用同一个界面 public JPanel addStatePloolPanel(StatePool statePool) { StatePool selectedSP = poolTreeSelection(); if (selectedSP != null) { nodeName = selectedSP.name; } jp = new JPanel(); jp.setBackground(Color.WHITE); jp.setLayout(new BorderLayout(0, 0)); jp.setBorder(BorderFactory.createTitledBorder(nodeName)); setBackground(Color.WHITE); setLayout(new BorderLayout(0, 0)); jp.setLayout(new GridBagLayout()); GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.insets = new Insets(10, 5, 0, 5); g.gridx = 0; g.gridy = 0; jp.add(jl1, g); jtf1.setText(statePool.name); GridBagConstraints g1 = new GridBagConstraints(); g1.anchor = GridBagConstraints.WEST; g1.insets = new Insets(10, 5, 0, 5); g1.gridx = 1; g1.gridy = 0; jtf1.setEnabled(false); jtf1.updateUI(); jp.add(jtf1, g1); g.gridx = 0; g.gridy = 1; jp.add(jl2, g); g.gridx = 1; g.gridy = 1; jtf2.setText(statePool.tag); jp.add(jtf2, g); g.gridx = 0; g.gridy = 2; jp.add(jl3, g); g.gridx = 1; g.gridy = 2; jtf3.setText(statePool.imagePath); jtf3.setEnabled(false); jtf3.updateUI(); jp.add(jtf3, g); g.gridx = 2; g.gridy = 2; // if(!statePool.imagePath.startsWith("http://")){ //// icon = new ImageIcon(statePool.imagePath); // String imagePath = statePool.imagePath; // if(imagePath.contains("-")){ // icon = new ImageBundle().createImageIcon(imagePath); // } else { // icon = new BundleImage().createImageIcon(imagePath); // } // if(statePool.imagePath == "" || statePool.imagePath == null){ // icon = image; // } // }else { // URL url = null; // try { // url = new URL(statePool.imagePath); // icon = new ImageIcon(url); // } catch (MalformedURLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // selectButton.setIcon(icon); jp.add(selectButton, g); g.gridx = 0; g.gridy = 3; jp.add(jl4, g); jta.setText(statePool.description); jta.setLineWrap(true); g1.gridx = 1; g1.gridy = 3; jp.add(new JScrollPane(jta), g1); return jp; } // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置 public void buttonManager() { addButton.setEnabled(true); modifyButton.setEnabled(true); deleteButton.setEnabled(true); checkButton.setEnabled(true); tree.setEnabled(true); } // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置 public void buttonManager1() { saveButton.setEnabled(false); cancelButton.setEnabled(false); } // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置 public void buttonManager2() { addButton.setEnabled(false); modifyButton.setEnabled(false); deleteButton.setEnabled(false); checkButton.setEnabled(false); tree.setEnabled(false); saveButton.setEnabled(true); cancelButton.setEnabled(true); } private void addListenner() { /** * 查看使用范围 */ checkButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { StatePool selectedSP = poolTreeSelection(); if (selectedSP == null) { JOptionPane.showMessageDialog(null, "请选中要查看的状态", "无状态", JOptionPane.WARNING_MESSAGE); return; } String name = selectedSP.name; if (name == null || name.equals("") || name.equals("状态池")) { JOptionPane.showMessageDialog(null, "请选中要查看的状态", "无状态", JOptionPane.WARNING_MESSAGE); return; } UsedToDialog usedToDialog = new UsedToDialog(name); usedToDialog.setVisible(true); } }); } public List getSelectedSPs() { TreePath[] selectionPaths = tree.getSelectionPaths(); // 未选中, 返回 if (selectionPaths == null || selectionPaths.length == 0) { return null; } // 选中根节点, 返回 if (selectionPaths.length == 1 && !(((DefaultMutableTreeNode) selectionPaths[0].getLastPathComponent()) .getUserObject() instanceof StatePoolWrapper)) { return null; } List list = new ArrayList(); for (TreePath path : selectionPaths) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); Object obj = node.getUserObject(); if (obj instanceof StatePoolWrapper) { list.add(((StatePoolWrapper) obj).getSP()); } } return list; } }