?? frameworkpanel.java
字號:
package com.mwq.frame.system;
// Download by http://www.codefans.net
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
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 javax.swing.tree.TreeSelectionModel;
import com.mwq.hibernate.Dao;
import com.mwq.hibernate.HibernateSessionFactory;
import com.mwq.hibernate.mapping.TbDept;
public class FrameworkPanel extends JPanel {
private JTree tree;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode root;
private Dao dao;
private TbDept company;
/**
* Create the panel
*/
public FrameworkPanel() {
super();
dao = Dao.getInstance();
setLayout(new BorderLayout());
final JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
add(buttonPanel, BorderLayout.NORTH);
final JButton updateButton = new JButton();
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath selectionPath = tree.getSelectionPath();
TbDept selected = null;
String newName = null;
if (selectionPath.getPathCount() == 1) {// 修改公司名稱
int i = JOptionPane.showConfirmDialog(null, "確定要修改貴公司的名稱?",
"友情提示", JOptionPane.YES_NO_OPTION);// 彈出提示框
if (i == 0) {// 修改(單擊“是”按鈕)
String infos[] = { "請輸入貴公司的新名稱:", "修改公司名稱",
"請輸入貴公司的新名稱!" };
newName = getName(infos);// 獲得修改后的名稱
if (newName != null)
selected = company;// 修改的為公司名稱
}
} else {// 修改部門名稱
String infos[] = { "請輸入部門的新名稱:", "修改部門名稱", "請輸入部門的新名稱!" };
newName = getName(infos);// 獲得修改后的名稱
if (newName != null) {
selected = company;// 選中部門的所屬部門
Object[] paths = selectionPath.getPath();// 選中部門節(jié)點的路徑對象
for (int i = 1; i < paths.length; i++) {// 遍歷選中節(jié)點路徑
Iterator deptIt = selected.getTbDepts().iterator();
finded: while (deptIt.hasNext()) {// 通過循環(huán)查找選中節(jié)點路徑對應的部門
TbDept dept = (TbDept) deptIt.next();
if (dept.getName().equals(paths[i].toString())) {
selected = dept;// 找到選中節(jié)點路徑對應的部門
break finded;// 跳出到指定位置
}
}
}
}
}
if (selected != null) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath
.getLastPathComponent();// 獲得選中節(jié)點對象
treeNode.setUserObject(newName);// 修改節(jié)點名稱
treeModel.reload();// 刷新樹結(jié)構(gòu)
tree.setSelectionPath(selectionPath);// 設置節(jié)點為選中狀態(tài)
//
selected.setName(newName);// 修改部門對象
dao.updateObject(company);// 將修改持久化到數(shù)據(jù)庫
HibernateSessionFactory.closeSession();// 關(guān)閉數(shù)據(jù)庫連接
}
}
});
updateButton.setText("修改名稱");
buttonPanel.add(updateButton);
final JButton delButton = new JButton();
delButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath selectionPath = tree.getSelectionPath();
int pathCount = selectionPath.getPathCount();// 獲得選中節(jié)點的級別
if (pathCount == 1) {// 選中的為1級節(jié)點,即公司節(jié)點
JOptionPane.showMessageDialog(null, "公司節(jié)點不能刪除!", "友情提示",
JOptionPane.WARNING_MESSAGE);
} else {// 選中的為2級或3級節(jié)點,即部門節(jié)點
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath
.getLastPathComponent();// 獲得選中部門節(jié)點對象
int i = JOptionPane.showConfirmDialog(null, "確定要刪除該部門:"
+ treeNode, "友情提示", JOptionPane.YES_NO_OPTION);
if (i == 0) {// 刪除
treeModel.removeNodeFromParent(treeNode);// 刪除選中節(jié)點
tree.setSelectionRow(0);// 選中根(公司)節(jié)點
TbDept selected = company;// 選中部門的所屬部門
Object[] paths = selectionPath.getPath();// 選中部門節(jié)點的路徑對象
int lastIndex = paths.length - 1;// 獲得最大索引
for (int j = 1; j <= lastIndex; j++) {// 遍歷選中節(jié)點路徑
Iterator deptIt = selected.getTbDepts().iterator();
finded: while (deptIt.hasNext()) {// 通過循環(huán)查找選中節(jié)點路徑對應的部門
TbDept dept = (TbDept) deptIt.next();
if (dept.getName().equals(paths[j].toString())) {
if (j == lastIndex) // 為選中節(jié)點
selected.getTbDepts().remove(dept);// 刪除選中部門
else
// 為所屬節(jié)點
selected = dept;
break finded;// 跳出到指定位置
}
}
}
dao.updateObject(company);// 同步刪除數(shù)據(jù)庫
HibernateSessionFactory.closeSession();// 關(guān)閉數(shù)據(jù)庫連接
}
}
}
});
delButton.setText("刪除該部門");
buttonPanel.add(delButton);
final JButton addButton = new JButton();
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath selectionPath = tree.getSelectionPath();
int pathCount = selectionPath.getPathCount();// 獲得選中節(jié)點的級別
had: if (pathCount == 3) {// 選中的為3級節(jié)點
JOptionPane.showMessageDialog(null, "很抱歉,在該級部門下不能再包含子部門!",
"友情提示", JOptionPane.WARNING_MESSAGE);
} else {// 選中的為1級或2級節(jié)點
String infos[] = { "請輸入部門名稱:", "添加新部門", "請輸入部門名稱!" };
String newName = getName(infos);// 獲得新部門的名稱
if (newName != null) {// 創(chuàng)建新部門
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectionPath
.getLastPathComponent();// 獲得選中部門節(jié)點對象
int childCount = parentNode.getChildCount();// 獲得該部門包含子部門的個數(shù)
for (int i = 0; i < childCount; i++) {// 查看新創(chuàng)建的部門是否已經(jīng)存在
TreeNode childNode = parentNode.getChildAt(i);
if (childNode.toString().equals(newName)) {
JOptionPane.showMessageDialog(null, "該部門已經(jīng)存在!",
"友情提示", JOptionPane.WARNING_MESSAGE);
break had;// 已經(jīng)存在,跳出到指定位置
}
}
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
newName);// 創(chuàng)建新部門節(jié)點對象
treeModel.insertNodeInto(childNode, parentNode,
childCount);// 將新部門節(jié)點插入到選中部門節(jié)點的最后
tree.expandPath(selectionPath);// 展開選中部門節(jié)點
//
TbDept selected = company;// 默認選中的為1級節(jié)點
if (pathCount == 2) {// 選中的為2級節(jié)點
String selectedName = selectionPath.getPath()[1]
.toString();// 獲得選中節(jié)點的名稱
Iterator deptIt = company.getTbDepts().iterator();// 創(chuàng)建公司直屬部門的迭代器對象
finded: while (deptIt.hasNext()) {// 遍歷公司的直屬部門
TbDept dept = (TbDept) deptIt.next();
if (dept.getName().equals(selectedName)) {// 查找與選中節(jié)點對應的部門
selected = dept;// 設置為選中部門
break finded;// 跳出循環(huán)
}
}
}
TbDept sonDept = new TbDept();// 創(chuàng)建新部門對象
sonDept.setName(newName);// 設置部門名稱
sonDept.setTbDept(selected);// 建立從新部門到所屬部門的關(guān)聯(lián)
selected.getTbDepts().add(sonDept);// 建立從所屬部門到新部門的關(guān)聯(lián)
dao.updateObject(company);// 將新部門持久化到數(shù)據(jù)庫
HibernateSessionFactory.closeSession();// 關(guān)閉數(shù)據(jù)庫連接
}
}
}
});
addButton.setText("添加子部門");
buttonPanel.add(addButton);
final JPanel treePanel = new JPanel();
treePanel.setBackground(Color.WHITE);
add(treePanel, BorderLayout.CENTER);
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setVgap(50);
treePanel.setLayout(flowLayout);
company = (TbDept) dao.queryDeptById(1);
root = new DefaultMutableTreeNode(company.getName());
Set depts = company.getTbDepts();
for (Iterator iter = depts.iterator(); iter.hasNext();) {
TbDept dept = (TbDept) iter.next();
DefaultMutableTreeNode deptNode = new DefaultMutableTreeNode(dept
.getName());
root.add(deptNode);
if (dept.getTbDepts().size() != 0) {
Set sonDepts = dept.getTbDepts();
for (Iterator iterator = sonDepts.iterator(); iterator
.hasNext();) {
TbDept sonDept = (TbDept) iterator.next();
deptNode.add(new DefaultMutableTreeNode(sonDept.getName()));
}
}
}
HibernateSessionFactory.closeSession();
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setForeground(new Color(255, 0, 0));
tree.setFont(new Font("", Font.BOLD, 14));
DefaultTreeCellRenderer render = new DefaultTreeCellRenderer();// 設置結(jié)點的圖標、字體和背景色
render.setLeafIcon(new ImageIcon());// 設置葉子結(jié)點的圖標
render.setClosedIcon(new ImageIcon());// 設置結(jié)點折疊時的圖標
render.setOpenIcon(new ImageIcon());// 設置結(jié)點展開時的圖標
tree.setCellRenderer(render);
tree.setSelectionRow(0);// 默認選中根節(jié)點
tree.setRowHeight(26);// 設置節(jié)點的行高
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);// 設置節(jié)點的選取模式為單選
treePanel.add(tree);
//
}
public String getName(String infos[]) {
String newName = "";
while (newName != null && newName.length() == 0) {
newName = JOptionPane.showInputDialog(null, infos[0], infos[1],
JOptionPane.INFORMATION_MESSAGE);
if (newName != null && newName.length() == 0) {
JOptionPane.showMessageDialog(null, infos[2], "友情提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
return newName;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -