package com.prs.abook.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;
import org.hibernate.annotations.Parent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.jpa.JpaAccessor;
import org.springframework.transaction.jta.JtaAfterCompletionSynchronization;
import com.prs.abook.service.*;
import com.prs.abook.entity.*;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.tree.TreePath;
@SuppressWarnings("serial")
public class View extends JFrame {
public static void main(String[] args) {
// 显示联系人
View tree = new View();
tree.init();
tree.showAddressBook();
tree.validate();
}
public void treeView() {
View tree = new View();
tree.init();
tree.validate();
}
/**
* 初始化界面内容
*/
public void init() {
this.setTitle("个人通讯录");
this.setSize(1200, 600);
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
this.setLayout(fl);
// 将自己创建的树添加到界面上
JTree tree = createJTree();
JPopupMenu popupMenu = createpopupMenu(tree);
tree.setComponentPopupMenu(popupMenu);
this.add(tree);
this.setVisible(true);
}
/**
* 创建树状图
*
* @return
*/
@SuppressWarnings("unchecked")
public JTree createJTree() {
// 树根节点名称
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("通讯录联系人");
// 读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
AddressBookService ser = (AddressBookService) context
.getBean("addressBookService");
// 获取联系人类型
List<Type> tyepList = ser.getAllType();
// 树状显示联系人类型
for (Iterator iterator = tyepList.iterator(); iterator.hasNext();) {
Type type = (Type) iterator.next();
DefaultMutableTreeNode teamNode = new DefaultMutableTreeNode(type
.getTname());
// 获取联系人类型下的人名
List<Abook> Abooklist = ser.getAllTypeById(type.getTid());
for (Iterator iterator2 = Abooklist.iterator(); iterator2.hasNext();) {
Abook abook = (Abook) iterator2.next();
DefaultMutableTreeNode toolNode = new DefaultMutableTreeNode(
abook.getAname());
// 将子节点放入组节点
teamNode.add(toolNode);
// 将组节点放入根节点
rootNode.add(teamNode);
}
}
// 创建model,传入根节点
DefaultTreeModel model = new DefaultTreeModel(rootNode);
// 创建默认树,将模型设给树
JTree tree = new JTree(model);
return tree;
}
/**
* 鼠标图上的鼠标操作显示
*/
public JPopupMenu createpopupMenu(final JTree tree) {
// 创建弹出菜单对象
JPopupMenu popupMenu = new JPopupMenu();
// 创建菜单的菜单选项
JMenuItem menuItemOne = new JMenuItem("添加");
menuItemOne.setActionCommand("ins");
JMenuItem menuItemTwo = new JMenuItem("移除");
menuItemTwo.setActionCommand("del");
JMenuItem menuItemThree = new JMenuItem("修改");
menuItemThree.setActionCommand("upd");
JMenuItem menuItemFour = new JMenuItem("查看");
menuItemFour.setActionCommand("sel");
// 创建菜单事件监听器对象
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 当事件发生时调用方法处理
menuAction(e, tree);
}
};
// 给菜单加上事件监听器
menuItemOne.addActionListener(actionListener);
menuItemTwo.addActionListener(actionListener);
menuItemThree.addActionListener(actionListener);
menuItemFour.addActionListener(actionListener);
// 将菜单选项加到弹出菜单对象上
popupMenu.add(menuItemOne);
popupMenu.add(menuItemTwo);
popupMenu.add(menuItemThree);
popupMenu.add(menuItemFour);
return popupMenu;
}
/**
* 响应树上的弹出菜单事件
*/
public void menuAction(ActionEvent e, JTree tree) {
// 得到菜单选中的命令
String command = e.getActionCommand();
// 得到在树上选中的路径
TreePath treePath = tree.getSelectionPath();
// 如果选上了树上的某个节点
if (null != treePath) {
// 得到选中的节点
DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath
.getLastPathComponent();
// 判断取得根的等级
int i = node.getLevel();
System.out.println(i);
// 判断取得的是何种对象
if (1 == i) {
// 调用处理方法处理事件
HandleType(command, node);
} else if (2 == i) {
// 调用处理方法处理事件
HandleAbook(command, node);
}
} else {
JOptionPane.showMessageDialog(null, "请选中节点");
}
}
/**
* 处理树上选中Type类型事件
*/
private void HandleType(String command, DefaultMutableTreeNode node) {
if (command.equals("del")) {
// 用户确认
int i = JOptionPane.showConfirmDialog(this, "你确定要移除吗");
if (i == 0) {
// 表示确认移除
node.removeFromParent();
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
} else if (command.equals("ins") || command.equals("upd")
|| command.equals("sel")) {
JOptionPane.showMessageDialog(null, "此节点不允许此操作");
}
}
/**
* 处理树上选中的abook类型事件
*/
@SuppressWarnings({ "unchecked", "static-access" })
private void HandleAbook(String command, DefaultMutableTreeNode node) {
// 取得传入节点的对象
String oj = (String) node.getUserObject();
if (command.equals("del")) {
// 用户确认
int i = JOptionPane.showConfirmDialog(this, "你确定要删除吗");
if (i == 0) {
// 读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
AddressBookService deleteService = (AddressBookService) context
.getBean("addressBookService");
// 执行删除
int j = deleteService.deleteAbook(oj);
if (j == 1) {
JOptionPane.showMessageDialog(this, "删除成功");
View tree = new View();
tree.validate();
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
}
} else if (command.equals("ins")) { // 增加
addAbook();
} else if (command.equals("upd")) { // 修改
updateAbook();
} else if (command.equals("sel")) { // 单条查看
JDialog jf = new JDialog();
int count = 0;
// 读取配置文件
ApplicationContext contextShow = new ClassPathXmlApplicationContext(
"applicationContext.xml");
AddressBookService showService = (AddressBookService) contextShow
.getBean("addressBookService");
List<Abook> list = showService.getAbookByName(oj);
Object title[][] = new Object[list.size()][6];
for (int i = 0; i < list.size(); i++) {
Abook p = list.get(i);
title[count][0] = p.getAname();
title[count][1] = p.getAmajorPhone();
title[count][2] = p.getAdate();
title[count][3] = p.getAdress();
title[count][4] = p.getAqq();
title[count][5] = p.getArests();
count++;
}
// 创建表头
String[] message = { "姓名", "电话号码", "存入时间", "地址", "QQ号码", "其他备注" };
JTable table = new JTable(title, message);
table.setSize(6, list.size());
table.setBackground(getBackground().green);
table.setPreferredScrollableViewportSize(new Dimension(550, 30));
JScrollPane scrollPane = new JScrollPane(table);
jf.getContentPane().add(scrollPane, BorderLayout.CENTER);
jf.pack();
jf.setModal(true);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
/**
* 显示数据
*/
public void showAddressBook() {
// 读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
AddressBookService getService = (AddressBookService) context
.getBean("addressBookService");
// 执行获取所有具体信息
int count = 0;
List<Abook> list = getService.getAllAbook();
Object title[][] = new Object[list.size()][6];
for (int i = 0; i < list.size(); i++) {
Abook p = list.get(i);
title[count][0] = p.getAname();
title[count][1] = p.getAmajorPhone();
title[count][2] = p.getAdate();
title[count][3] = p.getAdress();
title[count][4] = p.getAqq();
title[count][5] = p.getArests();
count++;
}
// 创建表头
String[] message = { "姓名", "电话号码", "存入时间", "地址", "QQ号码", "其他备注" };
JTable table = new JTable(title, message);
table.setSize(6, list.size());
table.setBackground(getBackground().green);
table.setPreferredScrollableViewportSize(new Dimension(550, 30));
JScrollPane scrollPane = new JScrollPane(table);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* 增加信息
*/
public void addAbook(){
}
/**
* 修改数据
*/
public void updateAbook(){
JDialog jd = new JDialog();
jd.setTitle("修改信息");
jd.setSize(400, 300);
jd.setBackground(getBackground().BLUE);
JLabel jlabel1=new JLabel("姓名");
JTextField jtext1=new JTextField();
JLabel jlabel2=new JLabel("电话号码");
JTextField jtext2=new JTextField();
JLabel jlabel3=new JLabel("存入时间");
JTextField jtext3=new JTextField();
JLabel jlabel4=new JLabel("地址(可以为空)");
JTextField jtext4=new JTextField();
JLabel jlabel5=new JLabel("QQ号码(可以为空)");
JTextField jtext5=new JTextField();
JLabel jlabel6=new JLabel("其他备注(可以为空)");
JTextField jtext6=new JTextField();
jd.setModal(true);
jd.setVisible(true);
}
}