package org.salamanca.ui;
import java.awt.BorderLayout;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import org.salamanca.commands.user.*;
import org.salamanca.domain.*;
import org.salamanca.broker.BrokerServer;
import java.util.*;
import javax.swing.DefaultListModel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JScrollPane;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.WindowConstants;
import org.salamanca.commands.MessageException;
import org.salamanca.commands.CommandFactory;
import org.salamanca.commands.ICommand;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class UserFrame extends JInternalFrame {
public UserFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
load();
}
/**
* getRole
*
* @return int
*/
private int getRole() {
if (this.jRadioButton1.isSelected()) {
return User.ADMINISTRADOR;
}
if (this.jRadioButton2.isSelected()) {
return User.ADMINISTRATIVO;
}
if (this.jRadioButton3.isSelected()) {
return User.PROFESOR;
}
return User.PROFESOR;
}
/**
* jbInit
*
* @throws Exception
*/
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jList1.setBorder(BorderFactory.createEtchedBorder());
jList1.addMouseListener(new UserFrame_jList1_mouseAdapter(this));
jLabel1.setFont(new java.awt.Font("Arial", Font.PLAIN, 20));
jButton1.setBounds(new Rectangle(9, 81, 82, 20));
jButton1.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jButton1.setText("Insertar");
jButton1.addActionListener(new UserFrame_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(9, 118, 82, 20));
jButton2.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jButton2.setText("Eliminar");
jButton2.addActionListener(new UserFrame_jButton2_actionAdapter(this));
jRadioButton1.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jRadioButton1.setText("Administrador");
jRadioButton1.setBounds(new Rectangle(14, 171, 111, 24));
jRadioButton2.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jRadioButton2.setText("Administrativo");
jRadioButton2.setBounds(new Rectangle(132, 171, 111, 24));
jRadioButton2.addActionListener(new
UserFrame_jRadioButton2_actionAdapter(this));
jRadioButton3.setFont(new java.awt.Font("Arial", Font.PLAIN, 11));
jRadioButton3.setSelected(true);
jRadioButton3.setText("Profesor");
jRadioButton3.setBounds(new Rectangle(246, 171, 111, 24));
jScrollPane1.setBorder(BorderFactory.createEtchedBorder());
jScrollPane1.setBounds(new Rectangle(101, 50, 217, 111));
this.addInternalFrameListener(new UserFrame_this_internalFrameAdapter(this));
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.setTitle("Administraci�n de Accesos");
this.getContentPane().add(jLabel1);
this.getContentPane().add(jRadioButton3);
this.getContentPane().add(jRadioButton2);
this.getContentPane().add(jRadioButton1);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jScrollPane1);
jScrollPane1.getViewport().add(jList1);
jLabel1.setText("Usuarios");
jLabel1.setBounds(new Rectangle(111, 13, 107, 30));
buttonGroup1.add(jRadioButton3);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton1);
}
DefaultListModel model = new DefaultListModel();
JList jList1 = new JList(model);
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
ButtonGroup buttonGroup1 = new ButtonGroup();
JScrollPane jScrollPane1 = new JScrollPane();
public void jRadioButton2_actionPerformed(ActionEvent e) {
}
private void load() {
model.clear();
Vector v = BrokerServer.instance().getAll(User.class);
Enumeration enumer = v.elements();
while (enumer.hasMoreElements()) {
User user = (User) enumer.nextElement();
model.addElement(user.getName());
}
}
/**
* jButton1_actionPerformed
*
* @param e ActionEvent
*/
public void jButton1_actionPerformed(ActionEvent e) {
JOptionPane pane = new JOptionPane();
String name = pane.showInputDialog(
"Intruduzca el nombre del nuevo usuario");
if (name != null && name.length() > 0) {
User user = new User();
user.setName(name);
user.setRole(getRole());
ICommand insertar = CommandFactory.createInsertarUsuario(user);
try {
insertar.execute();
} catch (MessageException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Entrada no valida");
}
load();
}
}
public void jList1_mouseClicked(MouseEvent e) {
String nombre = (String) jList1.getSelectedValue();
Vector v = BrokerServer.instance().query(User.class,
"name==\"" + nombre + "\"");
if (v.size() > 0) {
User user = (User) v.firstElement();
if (user.getRole() == User.ADMINISTRADOR) {
this.jRadioButton1.setSelected(true);
}
if (user.getRole() == User.ADMINISTRATIVO) {
this.jRadioButton2.setSelected(true);
}
if (user.getRole() == User.PROFESOR) {
this.jRadioButton3.setSelected(true);
}
}
}
public void this_internalFrameClosing(InternalFrameEvent e) {
}
public void jButton2_actionPerformed(ActionEvent e) {
String nombre = (String) jList1.getSelectedValue();
Vector v = BrokerServer.instance().query(User.class,
"name==\"" + nombre + "\"");
if (v.size() > 0) {
User user = (User) v.firstElement();
ICommand eliminarUsuario = CommandFactory.createEliminarUsuario(user);
try {
eliminarUsuario.execute();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
load();
}
}
}
class UserFrame_jButton2_actionAdapter implements ActionListener {
private UserFrame adaptee;
UserFrame_jButton2_actionAdapter(UserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class UserFrame_this_internalFrameAdapter extends InternalFrameAdapter {
private UserFrame adaptee;
UserFrame_this_internalFrameAdapter(UserFrame adaptee) {
this.adaptee = adaptee;
}
public void internalFrameClosing(InternalFrameEvent e) {
adaptee.this_internalFrameClosing(e);
}
}
class UserFrame_jList1_mouseAdapter extends MouseAdapter {
private UserFrame adaptee;
UserFrame_jList1_mouseAdapter(UserFrame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jList1_mouseClicked(e);
}
}
class UserFrame_jButton1_actionAdapter implements ActionListener {
private UserFrame adaptee;
UserFrame_jButton1_actionAdapter(UserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class UserFrame_jRadioButton2_actionAdapter implements ActionListener {
private UserFrame adaptee;
UserFrame_jRadioButton2_actionAdapter(UserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}