package edu.ujs.actionListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import org.apache.ibatis.session.SqlSession;
import edu.ujs.Main.ErrorDialog;
import edu.ujs.beans.DBS_Role;
import edu.ujs.beans.DBS_User;
import edu.ujs.beans.Role;
import edu.ujs.beans.User;
public class RoleActionListener implements ActionListener {
JFrame jFrame;
JDialog jDialog;
SqlSession sqlSession;
List<Role> list=new ArrayList<Role>();
public RoleActionListener(JFrame jFrame,SqlSession sqlSession) {
this.jFrame=jFrame;
this.sqlSession=sqlSession;
}
@Override
public void actionPerformed(ActionEvent e) {
jDialog=new JDialog(jFrame, "Role", true);
jDialog.setSize(500,500);
final JScrollPane scrollPane=new JScrollPane();
scrollPane.setBorder(new BevelBorder(BevelBorder.RAISED));
jDialog.add(scrollPane,BorderLayout.CENTER);
JTextArea jArea=new JTextArea();
scrollPane.setViewportView(jArea);
JLabel jLabel=new JLabel("角色id:", JLabel.CENTER);
JTextField jTextField=new JTextField();
JLabel jLabe2=new JLabel("角色名:", JLabel.CENTER);
JTextField jTextField2=new JTextField(20);
if (e.getActionCommand().equals("Role")){
while(DBS_Role.dbs.size()!=0)
{
DBS_Role.dbs.remove(DBS_Role.i);
DBS_Role.i=DBS_Role.i-1>=0?DBS_Role.i-1:0;
}
DBS_Role.dbs=sqlSession.selectList("edu.ujs.mapper.selectRoles");
DBS_Role.i=DBS_Role.dbs.size()-1;
if(DBS_Role.dbs.size()!=0)
{
jTextField.setText(String.valueOf(DBS_Role.dbs.get(0).getId_role()));
jTextField2.setText(DBS_Role.dbs.get(0).getName_role());
JOptionPane.showMessageDialog(null, "Role数据库打开成功...");
}
else {
JOptionPane.showMessageDialog(null, "Role数据库为空!");
}
}
else
JOptionPane.showMessageDialog(null, "无法打开Role数据库...");
JButton jButton=new JButton("select");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Role role=new Role();
try {
role=sqlSession.selectOne("edu.ujs.mapper.selectRole",Integer.valueOf(jTextField.getText()));
jArea.append(role.toString()+"\n");
jArea.append("----------------------------\n");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "无该用户!");
}
}
});
JButton jButton2=new JButton("selectAll");
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<Role> list=new ArrayList<Role>();
try {
list=sqlSession.selectList("edu.ujs.mapper.selectRoles");
jArea.append("----------所有用户----------\n");
for(Role role: list)
jArea.append(role.toString()+"\n");
jArea.append("----------总计:"+list.size()+" 个用户----------\n");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "无该用户!");
}
}
});
JButton pre=new JButton("pre");
pre.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DBS_Role.i>-1)
{
DBS_Role.i=DBS_Role.i-1>=0?DBS_Role.i-1:0;
jTextField.setText(String.valueOf(DBS_Role.dbs.get(DBS_Role.i).getId_role()));
jTextField2.setText(DBS_Role.dbs.get(DBS_Role.i).getName_role());
}
if(DBS_Role.i==-1)
new ErrorDialog(jFrame,"内容为空!").setVisible(true);
}
});
JButton next=new JButton("next");
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if(DBS_Role.i>-1)
{
DBS_Role.i=DBS_Role.i+1<DBS_Role.dbs.size()?DBS_Role.i+1:DBS_Role.dbs.size()-1;
}
jTextField.setText(String.valueOf(DBS_Role.dbs.get(DBS_Role.i).getId_role()));
jTextField2.setText(DBS_Role.dbs.get(DBS_Role.i).getName_role());
} catch (Exception e2) {
new ErrorDialog(jFrame,"内容为空!").setVisible(true);
}
}
});
JButton insert=new JButton("insert");
insert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Role role=new Role();
role.setId_role(Integer.valueOf((jTextField.getText())));
role.setName_role(jTextField2.getText());
try {
sqlSession.insert("edu.ujs.mapper.insertRole",role);
sqlSession.commit();
DBS_Role.dbs.add(role);
DBS_Role.i=DBS_Role.dbs.size()-1;
JOptionPane.showMessageDialog(null, "success");
jTextField.setText(String.valueOf(DBS_Role.dbs.get(DBS_Role.i).getId_role()));
jTextField2.setText(DBS_Role.dbs.get(DBS_Role.i).getName_role());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "用户名重复!");
}
} catch (Exception e2) {
new edu.ujs.Main.ErrorDialog(jFrame,"内容为空!").setVisible(true);
}
}
});
JButton update=new JButton("update");
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if(DBS_Role.i>-1)
{
DBS_Role.dbs.get(DBS_Role.i).setId_role(Integer.valueOf(jTextField.getText()));
DBS_Role.dbs.get(DBS_Role.i).setName_role(jTextField2.getText());
sqlSession.update("edu.ujs.mapper.updateRole",DBS_Role.dbs.get(DBS_Role.i));
sqlSession.commit();
JOptionPane.showMessageDialog(null, "Success");
}
} catch (Exception e2) {
new edu.ujs.Main.ErrorDialog(jFrame,"内容为空!").setVisible(true);
}
}
});
JButton delete=new JButton("delete");
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DBS_Role.i>-1&&DBS_Role.dbs.size()>1)
{
DBS_Role.dbs.remove(DBS_Role.i);
DBS_Role.i=DBS_Role.i-1>=0?DBS_Role.i-1:0;
sqlSession.delete("edu.ujs.mapper.deleteRole", Integer.valueOf(jTextField.getText()));
sqlSession.commit();
jTextField.setText(String.valueOf(DBS_Role.dbs.get(DBS_Role.i).getId_role()));
jTextField2.setText(DBS_Role.dbs.get(DBS_Role.i).getName_role());
JOptionPane.showMessageDialog(null, "删除成功...");
}
else
{
if(DBS_Role.i==0&&DBS_Role.dbs.size()==1)
{
DBS_Role.i=-1;
jTextField.setText("");
jTextField2.setText("");
}
else {
new ErrorDialog(jFrame,"User数据库为空!").setVisible(true);
}
}
}
});
JPanel jPanel=new JPanel(new GridLayout(6, 2));
jPanel.add(jLabel,"Center");
jPanel.add(jTextField);
jPanel.add(jLabe2);
jPanel.add(jTextField2);
jPanel.add(pre);
jPanel.add(next);
jPanel.add(insert);
jPanel.add(update);
jPanel.add(delete);
jPanel.add(jButton);
jPanel.add(jButton2);
jPanel.add(jButton2);
jDialog.add(jPanel, "South");
jDialog.setVisible(true);
}
}