ManageSortDialog.java
上传用户:njlgjx
上传日期:2022-08-07
资源大小:9105k
文件大小:13k
源码类别:

图形图象

开发平台:

Java

  1. /*
  2.  * ManageSortDialog.java
  3.  *
  4.  * Created on 2008年7月9日, 上午10:22
  5.  */
  6. package com.mwq.map.frame;
  7. import com.mwq.map.dao.Dao;
  8. import com.mwq.map.mwing.MTreeNode;
  9. import com.mwq.map.tool.ScreenSize;
  10. import java.util.Iterator;
  11. import java.util.Vector;
  12. import javax.swing.JList;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JSlider;
  15. import javax.swing.tree.DefaultTreeModel;
  16. import javax.swing.tree.TreePath;
  17. /**
  18.  *
  19.  * @author  Administrator
  20.  */
  21. public class ManageSortDialog extends javax.swing.JDialog {
  22.     private Dao dao = Dao.getInstance();
  23.     private DefaultTreeModel treeModel;
  24.     private JSlider slider;
  25.     private JList list;
  26.     /** Creates new form ManageSortDialog2 */
  27.     public ManageSortDialog(java.awt.Frame parent, boolean modal, JSlider slider, JList list) {
  28.         super(parent, modal);
  29.         this.slider = slider;
  30.         this.list = list;
  31.         initComponents();
  32.         initTree();
  33.         setBounds((ScreenSize.getWidth() - getWidth()) / 2, (ScreenSize.getHeight() - getHeight()) / 2, getWidth(), getHeight());
  34.     }
  35.     private void initTree() {
  36.         MTreeNode root = new MTreeNode("Sort");
  37.         loadChildNode(root);
  38.         treeModel = new DefaultTreeModel(root, true);
  39.         sortTree.setModel(treeModel);
  40.     }
  41.     public void loadChildNode(MTreeNode node) {
  42.         Vector sorts = dao.selectChildSortByFatherId(node.getId());
  43.         for (Iterator it = sorts.iterator(); it.hasNext();) {
  44.             Vector sort = (Vector) it.next();
  45.             node.add(new MTreeNode((Integer) sort.get(0), sort.get(2)));
  46.         }
  47.         node.setLoad(true);
  48.     }
  49.     private String getAlbumName(int fatherId, String title, String message) {
  50.         String name = null;
  51.         while (name == null) {
  52.             name = JOptionPane.showInputDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
  53.             if (name == null) {
  54.                 break;
  55.             } else {
  56.                 if (name.length() == 0) {
  57.                     JOptionPane.showMessageDialog(this, "请输入类别名称!", "友情提示", JOptionPane.INFORMATION_MESSAGE);
  58.                     name = null;
  59.                 } else {
  60.                     Object node = dao.selectOnlyValue("select id from tb_sort where father_id=" + fatherId + " and name='" + name + "'");
  61.                     if (node != null) {
  62.                         JOptionPane.showMessageDialog(this, "该类别名称已经存在!", "友情提示", JOptionPane.INFORMATION_MESSAGE);
  63.                         name = null;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         return name;
  69.     }
  70.     /** This method is called from within the constructor to
  71.      * initialize the form.
  72.      * WARNING: Do NOT modify this code. The content of this method is
  73.      * always regenerated by the Form Editor.
  74.      */
  75.     @SuppressWarnings("unchecked")
  76.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  77.     private void initComponents() {
  78.         jToolBar1 = new javax.swing.JToolBar();
  79.         addButton = new javax.swing.JButton();
  80.         updButton = new javax.swing.JButton();
  81.         delButton = new javax.swing.JButton();
  82.         cancelChooseButton = new javax.swing.JButton();
  83.         scrollPane = new javax.swing.JScrollPane();
  84.         sortTree = new javax.swing.JTree();
  85.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  86.         setTitle("维护类别");
  87.         jToolBar1.setFloatable(false);
  88.         jToolBar1.setRollover(true);
  89.         addButton.setText("添加类别");
  90.         addButton.setFocusable(false);
  91.         addButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  92.         addButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  93.         addButton.addActionListener(new java.awt.event.ActionListener() {
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.                 addButtonActionPerformed(evt);
  96.             }
  97.         });
  98.         jToolBar1.add(addButton);
  99.         updButton.setText("修改类别");
  100.         updButton.setFocusable(false);
  101.         updButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  102.         updButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  103.         updButton.addActionListener(new java.awt.event.ActionListener() {
  104.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  105.                 updButtonActionPerformed(evt);
  106.             }
  107.         });
  108.         jToolBar1.add(updButton);
  109.         delButton.setText("删除类别");
  110.         delButton.setFocusable(false);
  111.         delButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  112.         delButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  113.         delButton.addActionListener(new java.awt.event.ActionListener() {
  114.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  115.                 delButtonActionPerformed(evt);
  116.             }
  117.         });
  118.         jToolBar1.add(delButton);
  119.         cancelChooseButton.setText("取消选择");
  120.         cancelChooseButton.setFocusable(false);
  121.         cancelChooseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  122.         cancelChooseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  123.         cancelChooseButton.addActionListener(new java.awt.event.ActionListener() {
  124.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  125.                 cancelChooseButtonActionPerformed(evt);
  126.             }
  127.         });
  128.         jToolBar1.add(cancelChooseButton);
  129.         getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);
  130.         scrollPane.setPreferredSize(new java.awt.Dimension(290, 220));
  131.         sortTree.setRootVisible(false);
  132.         sortTree.setShowsRootHandles(true);
  133.         sortTree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
  134.             public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
  135.             }
  136.             public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
  137.                 sortTreeTreeWillExpand(evt);
  138.             }
  139.         });
  140.         scrollPane.setViewportView(sortTree);
  141.         getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
  142.         pack();
  143.     }// </editor-fold>//GEN-END:initComponents
  144.     private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
  145. // TODO add your handling code here:
  146.         MTreeNode lastSelectedNode =
  147.                 (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
  148.         int fatherId = (lastSelectedNode == null ? 0 : lastSelectedNode.getId());// 定义父类别ID
  149.         String albumName = getAlbumName(fatherId, "添加类别", "请输入类别名称:");// 获得添加类别名称
  150.         if (albumName != null) {// 输入了类别名称
  151.             int id = dao.insertSort(fatherId, albumName);// 添加到数据库
  152.             if (lastSelectedNode == null) {// 未选中任何节点
  153.                 MTreeNode root = (MTreeNode) treeModel.getRoot();// 获得树的根节点
  154.                 root.add(new MTreeNode(id, albumName));// 添加子节点
  155.                 treeModel.reload();// 重新加载树模型
  156.             } else {// 存在选中的节点
  157.                 TreePath selectionPath = sortTree.getSelectionPath();// 获得选中节点路径对象
  158.                 if (!sortTree.isExpanded(selectionPath)) {// 该节点尚未展开
  159.                     sortTree.expandPath(selectionPath);// 展开节点
  160.                 } else {// 该节点已经展开
  161.                     lastSelectedNode.add(new MTreeNode(id, albumName));// 添加子节点
  162.                     treeModel.reload(lastSelectedNode);// 重新加载指定节点
  163.                 }
  164.             }
  165.         }
  166.     }//GEN-LAST:event_addButtonActionPerformed
  167.     private void updButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updButtonActionPerformed
  168. // TODO add your handling code here:
  169.         MTreeNode lastSelectedNode =
  170.                 (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
  171.         if (lastSelectedNode == null) {// 未选中任何节点
  172.             JOptionPane.showMessageDialog(this, "请选择要修改的类别!", "友情提示",
  173.                     JOptionPane.INFORMATION_MESSAGE);// 弹出提示框
  174.         } else {// 存在选中的节点
  175.             String albumName = getAlbumName(lastSelectedNode.getId(), "修改类别", "请输入类别“" +
  176.                     lastSelectedNode.getUserObject() + "”的新名称:");// 获得修改后的名称
  177.             if (albumName != null) {// 输入了类别名称
  178.                 dao.updateSortNameById(lastSelectedNode.getId(), albumName);// 保存到数据库
  179.                 lastSelectedNode.setUserObject(albumName);// 修改节点的用户标签
  180.                 treeModel.reload(lastSelectedNode);// 重新加载该节点
  181.             }
  182.         }
  183.     }//GEN-LAST:event_updButtonActionPerformed
  184.     private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
  185. // TODO add your handling code here:
  186.         MTreeNode lastSelectedNode =
  187.                 (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
  188.         if (lastSelectedNode == null) {// 未选中任何节点
  189.             JOptionPane.showMessageDialog(this, "请选择要删除的类别!", "友情提示",
  190.                     JOptionPane.INFORMATION_MESSAGE);// 弹出提示框
  191.         } else {// 存在选中的节点
  192.             String[] infos = {"如果删除该类别,将同时删除其包含的", "所有类别,以及属于这些类别的标记!", "", "确定要删除类别“" + lastSelectedNode.getUserObject() + "”?"};// 定义确认删除提示信息
  193.             int i = JOptionPane.showConfirmDialog(this, infos, "友情提示",
  194.                     JOptionPane.YES_NO_OPTION);// 弹出提示框
  195.             if (i == 0) {// 确认删除
  196.                 dao.deleteSortById(lastSelectedNode.getId());// 从数据库中删除
  197.                 list.setModel(new javax.swing.AbstractListModel() {
  198.                     public int getSize() {
  199.                         return 0;
  200.                     }
  201.                     public Object getElementAt(int i) {
  202.                         return null;
  203.                     }
  204.                 });// 清空查询结果列表
  205.                 slider.setValue(slider.getValue() + 1);// 触发滑块事件
  206.                 MTreeNode parentNode = (MTreeNode) lastSelectedNode.getParent();// 获得删除节点的父节点
  207.                 parentNode.remove(lastSelectedNode);// 移除该节点
  208.                 treeModel.reload(parentNode);// 重新加载父节点
  209.             }
  210.         }
  211.     }//GEN-LAST:event_delButtonActionPerformed
  212.     private void cancelChooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelChooseButtonActionPerformed
  213. // TODO add your handling code here:
  214.         sortTree.clearSelection();
  215.     }//GEN-LAST:event_cancelChooseButtonActionPerformed
  216.     private void sortTreeTreeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_sortTreeTreeWillExpand
  217. // TODO add your handling code here:
  218.         MTreeNode lastNode = (MTreeNode) evt.getPath().getLastPathComponent();
  219.         if (!lastNode.isLoad()) {
  220.             loadChildNode(lastNode);
  221.             treeModel.reload(lastNode);
  222.         }
  223.     }//GEN-LAST:event_sortTreeTreeWillExpand
  224.     /**
  225.      * @param args the command line arguments
  226.      */
  227.     public static void main(String args[]) {
  228.         java.awt.EventQueue.invokeLater(new  
  229.               Runnable() {
  230.                       public void run() {
  231.                 ManageSortDialog dialog = new ManageSortDialog(new javax.swing.JFrame(), true, null, null);
  232.                 dialog.addWindowListener(new java 
  233.                       .awt.event.WindowAdapter  
  234.                         () {
  235.                     @Override
  236.                     public void windowClosing(java.awt.event.WindowEvent e) {
  237.                         System.exit(0);
  238.                     }
  239.                 });
  240.                 dialog.setVisible(true);
  241.             }
  242.         });
  243.     }
  244.     // Variables declaration - do not modify//GEN-BEGIN:variables
  245.     private javax.swing.JButton addButton;
  246.     private javax.swing.JButton cancelChooseButton;
  247.     private javax.swing.JButton delButton;
  248.     private javax.swing.JToolBar jToolBar1;
  249.     private javax.swing.JScrollPane scrollPane;
  250.     private javax.swing.JTree sortTree;
  251.     private javax.swing.JButton updButton;
  252.     // End of variables declaration//GEN-END:variables
  253. }