ManageSortDialog.java
资源名称:MapModule.rar [点击查看]
上传用户:njlgjx
上传日期:2022-08-07
资源大小:9105k
文件大小:13k
源码类别:
图形图象
开发平台:
Java
- /*
- * ManageSortDialog.java
- *
- * Created on 2008年7月9日, 上午10:22
- */
- package com.mwq.map.frame;
- import com.mwq.map.dao.Dao;
- import com.mwq.map.mwing.MTreeNode;
- import com.mwq.map.tool.ScreenSize;
- import java.util.Iterator;
- import java.util.Vector;
- import javax.swing.JList;
- import javax.swing.JOptionPane;
- import javax.swing.JSlider;
- import javax.swing.tree.DefaultTreeModel;
- import javax.swing.tree.TreePath;
- /**
- *
- * @author Administrator
- */
- public class ManageSortDialog extends javax.swing.JDialog {
- private Dao dao = Dao.getInstance();
- private DefaultTreeModel treeModel;
- private JSlider slider;
- private JList list;
- /** Creates new form ManageSortDialog2 */
- public ManageSortDialog(java.awt.Frame parent, boolean modal, JSlider slider, JList list) {
- super(parent, modal);
- this.slider = slider;
- this.list = list;
- initComponents();
- initTree();
- setBounds((ScreenSize.getWidth() - getWidth()) / 2, (ScreenSize.getHeight() - getHeight()) / 2, getWidth(), getHeight());
- }
- private void initTree() {
- MTreeNode root = new MTreeNode("Sort");
- loadChildNode(root);
- treeModel = new DefaultTreeModel(root, true);
- sortTree.setModel(treeModel);
- }
- public void loadChildNode(MTreeNode node) {
- Vector sorts = dao.selectChildSortByFatherId(node.getId());
- for (Iterator it = sorts.iterator(); it.hasNext();) {
- Vector sort = (Vector) it.next();
- node.add(new MTreeNode((Integer) sort.get(0), sort.get(2)));
- }
- node.setLoad(true);
- }
- private String getAlbumName(int fatherId, String title, String message) {
- String name = null;
- while (name == null) {
- name = JOptionPane.showInputDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
- if (name == null) {
- break;
- } else {
- if (name.length() == 0) {
- JOptionPane.showMessageDialog(this, "请输入类别名称!", "友情提示", JOptionPane.INFORMATION_MESSAGE);
- name = null;
- } else {
- Object node = dao.selectOnlyValue("select id from tb_sort where father_id=" + fatherId + " and name='" + name + "'");
- if (node != null) {
- JOptionPane.showMessageDialog(this, "该类别名称已经存在!", "友情提示", JOptionPane.INFORMATION_MESSAGE);
- name = null;
- }
- }
- }
- }
- return name;
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jToolBar1 = new javax.swing.JToolBar();
- addButton = new javax.swing.JButton();
- updButton = new javax.swing.JButton();
- delButton = new javax.swing.JButton();
- cancelChooseButton = new javax.swing.JButton();
- scrollPane = new javax.swing.JScrollPane();
- sortTree = new javax.swing.JTree();
- setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
- setTitle("维护类别");
- jToolBar1.setFloatable(false);
- jToolBar1.setRollover(true);
- addButton.setText("添加类别");
- addButton.setFocusable(false);
- addButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- addButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
- addButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- addButtonActionPerformed(evt);
- }
- });
- jToolBar1.add(addButton);
- updButton.setText("修改类别");
- updButton.setFocusable(false);
- updButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- updButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
- updButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- updButtonActionPerformed(evt);
- }
- });
- jToolBar1.add(updButton);
- delButton.setText("删除类别");
- delButton.setFocusable(false);
- delButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- delButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
- delButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- delButtonActionPerformed(evt);
- }
- });
- jToolBar1.add(delButton);
- cancelChooseButton.setText("取消选择");
- cancelChooseButton.setFocusable(false);
- cancelChooseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- cancelChooseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
- cancelChooseButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- cancelChooseButtonActionPerformed(evt);
- }
- });
- jToolBar1.add(cancelChooseButton);
- getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);
- scrollPane.setPreferredSize(new java.awt.Dimension(290, 220));
- sortTree.setRootVisible(false);
- sortTree.setShowsRootHandles(true);
- sortTree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
- public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
- }
- public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
- sortTreeTreeWillExpand(evt);
- }
- });
- scrollPane.setViewportView(sortTree);
- getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
- // TODO add your handling code here:
- MTreeNode lastSelectedNode =
- (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
- int fatherId = (lastSelectedNode == null ? 0 : lastSelectedNode.getId());// 定义父类别ID
- String albumName = getAlbumName(fatherId, "添加类别", "请输入类别名称:");// 获得添加类别名称
- if (albumName != null) {// 输入了类别名称
- int id = dao.insertSort(fatherId, albumName);// 添加到数据库
- if (lastSelectedNode == null) {// 未选中任何节点
- MTreeNode root = (MTreeNode) treeModel.getRoot();// 获得树的根节点
- root.add(new MTreeNode(id, albumName));// 添加子节点
- treeModel.reload();// 重新加载树模型
- } else {// 存在选中的节点
- TreePath selectionPath = sortTree.getSelectionPath();// 获得选中节点路径对象
- if (!sortTree.isExpanded(selectionPath)) {// 该节点尚未展开
- sortTree.expandPath(selectionPath);// 展开节点
- } else {// 该节点已经展开
- lastSelectedNode.add(new MTreeNode(id, albumName));// 添加子节点
- treeModel.reload(lastSelectedNode);// 重新加载指定节点
- }
- }
- }
- }//GEN-LAST:event_addButtonActionPerformed
- private void updButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updButtonActionPerformed
- // TODO add your handling code here:
- MTreeNode lastSelectedNode =
- (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
- if (lastSelectedNode == null) {// 未选中任何节点
- JOptionPane.showMessageDialog(this, "请选择要修改的类别!", "友情提示",
- JOptionPane.INFORMATION_MESSAGE);// 弹出提示框
- } else {// 存在选中的节点
- String albumName = getAlbumName(lastSelectedNode.getId(), "修改类别", "请输入类别“" +
- lastSelectedNode.getUserObject() + "”的新名称:");// 获得修改后的名称
- if (albumName != null) {// 输入了类别名称
- dao.updateSortNameById(lastSelectedNode.getId(), albumName);// 保存到数据库
- lastSelectedNode.setUserObject(albumName);// 修改节点的用户标签
- treeModel.reload(lastSelectedNode);// 重新加载该节点
- }
- }
- }//GEN-LAST:event_updButtonActionPerformed
- private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
- // TODO add your handling code here:
- MTreeNode lastSelectedNode =
- (MTreeNode) sortTree.getLastSelectedPathComponent();// 获得选中节点对象
- if (lastSelectedNode == null) {// 未选中任何节点
- JOptionPane.showMessageDialog(this, "请选择要删除的类别!", "友情提示",
- JOptionPane.INFORMATION_MESSAGE);// 弹出提示框
- } else {// 存在选中的节点
- String[] infos = {"如果删除该类别,将同时删除其包含的", "所有类别,以及属于这些类别的标记!", "", "确定要删除类别“" + lastSelectedNode.getUserObject() + "”?"};// 定义确认删除提示信息
- int i = JOptionPane.showConfirmDialog(this, infos, "友情提示",
- JOptionPane.YES_NO_OPTION);// 弹出提示框
- if (i == 0) {// 确认删除
- dao.deleteSortById(lastSelectedNode.getId());// 从数据库中删除
- list.setModel(new javax.swing.AbstractListModel() {
- public int getSize() {
- return 0;
- }
- public Object getElementAt(int i) {
- return null;
- }
- });// 清空查询结果列表
- slider.setValue(slider.getValue() + 1);// 触发滑块事件
- MTreeNode parentNode = (MTreeNode) lastSelectedNode.getParent();// 获得删除节点的父节点
- parentNode.remove(lastSelectedNode);// 移除该节点
- treeModel.reload(parentNode);// 重新加载父节点
- }
- }
- }//GEN-LAST:event_delButtonActionPerformed
- private void cancelChooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelChooseButtonActionPerformed
- // TODO add your handling code here:
- sortTree.clearSelection();
- }//GEN-LAST:event_cancelChooseButtonActionPerformed
- private void sortTreeTreeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_sortTreeTreeWillExpand
- // TODO add your handling code here:
- MTreeNode lastNode = (MTreeNode) evt.getPath().getLastPathComponent();
- if (!lastNode.isLoad()) {
- loadChildNode(lastNode);
- treeModel.reload(lastNode);
- }
- }//GEN-LAST:event_sortTreeTreeWillExpand
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new
- Runnable() {
- public void run() {
- ManageSortDialog dialog = new ManageSortDialog(new javax.swing.JFrame(), true, null, null);
- dialog.addWindowListener(new java
- .awt.event.WindowAdapter
- () {
- @Override
- public void windowClosing(java.awt.event.WindowEvent e) {
- System.exit(0);
- }
- });
- dialog.setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JButton addButton;
- private javax.swing.JButton cancelChooseButton;
- private javax.swing.JButton delButton;
- private javax.swing.JToolBar jToolBar1;
- private javax.swing.JScrollPane scrollPane;
- private javax.swing.JTree sortTree;
- private javax.swing.JButton updButton;
- // End of variables declaration//GEN-END:variables
- }