📄 explorerpanel.java
字号:
package com.mwq.frame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import com.mwq.dao.Dao;
import com.mwq.frame.explorer.InfoDialog;
import com.mwq.frame.explorer.PersonnelDialog;
import com.mwq.frame.mwing.MButton;
import com.mwq.frame.mwing.MTable;
public class ExplorerPanel extends JPanel {
private MTable infoListTable;
private Vector<String> infoListTableColumnV;
private Vector<Vector> infoListTableValueV;
private DefaultTableModel infoListTableModel;
private JTree infoTree;
private DefaultMutableTreeNode infoTreeRoot;
private DefaultTreeModel infoTreeModel;
private MTable cardListTable;
private Vector<String> cardListTableColumnV;
private Vector<Vector> cardListTableValueV;
private DefaultTableModel cardListTableModel;
private JTree cardTree;
private DefaultMutableTreeNode cardTreeRoot;
private DefaultTreeModel cardTreeModel;
private final Dao dao = Dao.getInstance();
/**
* Create the panel
*/
public ExplorerPanel(final DefaultTableModel sendListTableModel,
final JTabbedPane infoTabbedPane, final JTextArea infoTextArea,
final JTextArea emailTextArea) {
super();
setLayout(new BorderLayout());
final JTabbedPane tabbedPane = new JTabbedPane();
add(tabbedPane, BorderLayout.CENTER);
final JSplitPane cardSplitPane = new JSplitPane();
cardSplitPane.setOneTouchExpandable(true);
cardSplitPane.setDividerSize(12);
cardSplitPane.setDividerLocation(244);
tabbedPane.addTab("名片夹", null, cardSplitPane, null);
final JPanel cardTreePanel = new JPanel();
cardSplitPane.setLeftComponent(cardTreePanel);
cardTreePanel.setLayout(new BorderLayout());
final JScrollPane cardTreeScrollPane = new JScrollPane();// 创建显示名片夹树的滚动面板
cardTreePanel.add(cardTreeScrollPane);// 添加到上级面板中
cardTreeRoot = new DefaultMutableTreeNode("root");// 创建名片夹树的根节点
initTree(cardTreeRoot, "card");// 初始化名片夹树
cardTreeModel = new DefaultTreeModel(cardTreeRoot);// 创建名片夹树模型
cardTree = new JTree(cardTreeModel);// 创建名片夹树
cardTree.setRootVisible(false);// 设置名片夹树的根节点不可见
System.out.println(cardTree.getSelectionModel().getSelectionMode());
cardTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);// 设置名片夹树的选择模式为单选
if (cardTreeRoot.getChildCount() > 0)
cardTree.setSelectionRow(0);// 如果名片夹树存在子节点,则设置选中第一个子节点
cardTree.addTreeSelectionListener(new TreeSelectionListener() {// 为名片夹树添加接点选中事件监听器
public void valueChanged(TreeSelectionEvent e) {
initCardListTable();// 初始化名片夹列表
}
});
cardTreeScrollPane.setViewportView(cardTree);// 将名片夹树添加到滚动面板中
final JPanel cardTreeButtonPanel = new JPanel();
final FlowLayout flowLayout_1 = new FlowLayout();
flowLayout_1.setVgap(0);
flowLayout_1.setHgap(0);
cardTreeButtonPanel.setLayout(flowLayout_1);
cardTreePanel.add(cardTreeButtonPanel, BorderLayout.SOUTH);
final MButton addCardTypeButton = new MButton();
addCardTypeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = addTreeNode(cardTreeRoot, "名片夹");// 获得新名片夹名称
if (name != null) {// 当用户取消新建时名称为空
int childCount = cardTreeRoot.getChildCount();// 获得当前拥有名片夹的数量
cardTreeModel.insertNodeInto(new DefaultMutableTreeNode(
name), cardTreeRoot, childCount);// 在名片夹树的最后创建新的名片夹
cardTreeModel.reload();// 刷新名片夹树模型
cardTree.setSelectionRow(childCount);// 设置新建名片夹为选中状态
dao.iType(name, "card");// 将新建名片夹保存到数据库中
}
}
});
URL creCardTypeUrl = this.getClass().getResource("/img/add_tree.png");
addCardTypeButton.setIcon(new ImageIcon(creCardTypeUrl));
URL creCardTypeOverUrl = this.getClass().getResource(
"/img/add_tree_over.png");
addCardTypeButton.setRolloverIcon(new ImageIcon(creCardTypeOverUrl));
cardTreeButtonPanel.add(addCardTypeButton);
final MButton updCardTypeButton = new MButton();
updCardTypeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath selectionPath = cardTree.getSelectionPath();// 获得选中节点的路径对象
if (selectionPath == null) {// 判断路径是否为空
JOptionPane.showMessageDialog(null, "请选择要修改的名片夹!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 如果为空则弹出提示
} else {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath
.getLastPathComponent();// 获得选中节点对象
String nowName = treeNode.getUserObject().toString();// 获得选中节点的名称
int i = JOptionPane.showConfirmDialog(null, "确定要修改名片夹“"
+ nowName + "”的名称?", "友情提示",
JOptionPane.YES_NO_OPTION);// 弹出确认修改提示框
if (i == 0) {// 如果为0则修改
String newName = updateTreeNode(treeNode);// 获得修改后的名称
if (newName != null) {// 判断修改后的名称是否为空,如果为空则用户取消了修改
treeNode.setUserObject(newName);// 修改节点名称
cardTreeModel.reload();// 刷新树
cardTree.setSelectionPath(selectionPath);// 设置修改的节点为选中节点
dao.uTypeNameByName("card", nowName, newName);// 将修改后的名称保存到数据库
}
}
}
}
});
URL updCardTypeUrl = this.getClass().getResource("/img/upd_tree.png");
updCardTypeButton.setIcon(new ImageIcon(updCardTypeUrl));
URL updCardTypeOverUrl = this.getClass().getResource(
"/img/upd_tree_over.png");
updCardTypeButton.setRolloverIcon(new ImageIcon(updCardTypeOverUrl));
cardTreeButtonPanel.add(updCardTypeButton);
final MButton delCardTypeButton = new MButton();
delCardTypeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) cardTree
.getLastSelectedPathComponent();// 获得选中的名片夹对象
if (treeNode == null) {// 未选择要删除的名片夹
JOptionPane.showMessageDialog(null, "请选择要删除的名片夹!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 弹出提示信息
return;// 并直接返回
}
String name = treeNode.getUserObject().toString();// 获得欲删除名片夹的名称
int i = JOptionPane.showConfirmDialog(null, "确定要删除名片夹“" + name
+ "”?", "友情提示", JOptionPane.YES_NO_OPTION);// 弹出删除的确认提示
if (i != 0)// 用户取消了删除操作
return;// 直接返回
if (dao.sPersonnelVByTypeName(name).size() > 0) {// 该名片夹中包含名片
String options[] = { "取消", "移入其他名片夹", "删除" };// 定义对其包含名片的处理方式
int optionIndex = JOptionPane.showOptionDialog(null,
"请选择对该名片夹下人员的处理方式:", "友情提示",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);// 弹出提示信息,令用户选择提供的处理方式
if (optionIndex == 0)// 用户取消了删除操作
return;// 直接返回
int typeId = dao.sTypeIdByUsedAndName("card", name);// 获得名片夹的主键ID
if (optionIndex == 1) {// 用户选择移入其他名片夹
Vector<Vector> cardV = dao.sTypeByUsedExcept("card",
typeId);// 查询可移入的名片夹
String[] cards = new String[cardV.size() + 1];// 创建一个选择项数组
cards[0] = "请选择";// 添加一个提示选择项
for (int j = 0; j < cardV.size(); j++) {// 初始化选择项数组
cards[j + 1] = cardV.get(j).get(2).toString();// 添加可移入的名片夹
}
Object card = "请选择";// 默认为选中“请选择”
while (card.equals("请选择")) {// 当选中的为“请选择”时执行循环
card = JOptionPane.showInputDialog(null,
"请选择要移入的名片夹:", "友情提示",
JOptionPane.INFORMATION_MESSAGE, null,
cards, cards[0]);// 弹出对话框令用户选择欲移入的名片夹
if (card == null)// 用户取消了删除操作
return;// 直接返回
}
int newTypeId = dao.sTypeIdByUsedAndName("card", card
.toString());// 获得欲移入名片夹的主键ID
dao.uPersonnelTypeIdByTypeId(typeId, newTypeId);// 修改名片的外键
}
if (optionIndex == 2) {// 用户选择删除其包含的名片
dao.dPersonnelByTypeId(typeId);// 从数据库删除其包含的名片
}
}
cardTreeModel.removeNodeFromParent(treeNode);// 从名片夹树中删除名片夹
dao.dTypeByName("card", name);// 从数据库中删除名片夹
}
});
URL delCardTypeUrl = this.getClass().getResource("/img/del_tree.png");
delCardTypeButton.setIcon(new ImageIcon(delCardTypeUrl));
URL delCardTypeOverUrl = this.getClass().getResource(
"/img/del_tree_over.png");
delCardTypeButton.setRolloverIcon(new ImageIcon(delCardTypeOverUrl));
cardTreeButtonPanel.add(delCardTypeButton);
final JPanel cardListPanel = new JPanel();
cardSplitPane.setRightComponent(cardListPanel);
cardListPanel.setLayout(new BorderLayout());
final JScrollPane cardListScrollPane = new JScrollPane();
cardListPanel.add(cardListScrollPane);
cardListTableColumnV = new Vector<String>();
String cardListTableColumns[] = { "序号", "编号", "姓名", "性别", "出生日期", "公司",
"部门", "职务", "移动电话", "E-mail" };
for (int i = 0; i < cardListTableColumns.length; i++) {
cardListTableColumnV.add(cardListTableColumns[i]);
}
cardListTableValueV = new Vector<Vector>();
cardListTableModel = new DefaultTableModel(cardListTableValueV,
cardListTableColumnV);
cardListTable = new MTable(cardListTableModel);
initCardListTable();
cardListScrollPane.setViewportView(cardListTable);
final JPanel cardButtonPanel = new JPanel();
cardButtonPanel.setLayout(new BoxLayout(cardButtonPanel,
BoxLayout.Y_AXIS));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -