package org.salamanca.ui;
import java.awt.BorderLayout;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JList;
import java.awt.Dimension;
import org.salamanca.domain.User;
import org.salamanca.broker.BrokerServer;
import java.util.Vector;
import java.util.Enumeration;
import javax.swing.DefaultListModel;
import org.salamanca.domain.Descuento;
import org.salamanca.domain.Persona;
import javax.swing.WindowConstants;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameAdapter;
import java.util.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import org.salamanca.commands.CommandFactory;
import javax.swing.JOptionPane;
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 FrameDescuentosPersona extends JInternalFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
BorderLayout borderLayout3 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabelPersona = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
BorderLayout borderLayout4 = new BorderLayout();
BorderLayout borderLayout5 = new BorderLayout();
DefaultListModel modelDescuentosDisponibles = new DefaultListModel();
JList jListDescuentosDisponibles = new JList(modelDescuentosDisponibles);
DefaultListModel modelDescuentosDelAlumno = new DefaultListModel();
JList jListDescuentosDelAlumno = new JList(modelDescuentosDelAlumno);
MainFrame mainFrame;
Persona persona;
public FrameDescuentosPersona(MainFrame mainFrame) {
this.mainFrame = mainFrame;
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void loadPersona(Persona persona) {
this.persona = persona;
loadDescuentosDisponibles();
loadDescuentosDelAlumno();
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jLabelPersona.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
jLabelPersona.setText("...");
jLabel3.setFont(new java.awt.Font("Arial", Font.BOLD, 11));
jLabel3.setText("Descuentos disponibles");
jLabel4.setFont(new java.awt.Font("Arial", Font.BOLD, 11));
jLabel4.setText("Descuentos del alumno");
jPanel5.setLayout(borderLayout4);
jPanel7.setLayout(borderLayout5);
jPanel7.setMinimumSize(new Dimension(50, 100));
jPanel5.setMaximumSize(new Dimension(32767, 32767));
jPanel5.setMinimumSize(new Dimension(144, 100));
jListDescuentosDelAlumno.setPreferredSize(new Dimension(144, 100));
jListDescuentosDelAlumno.addMouseListener(new
FrameDescuentosPersona_jListDescuentosDelAlumno_mouseAdapter(this));
jPanel6.setMaximumSize(new Dimension(32767, 32767));
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.addInternalFrameListener(new
FrameDescuentosPersona_this_internalFrameAdapter(this));
jListDescuentosDisponibles.addMouseListener(new
FrameDescuentosPersona_jListDescuentosDisponibles_mouseAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
jLabel1.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
jPanel1.add(jLabel1);
jPanel1.add(jLabelPersona);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setLayout(borderLayout2);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setLayout(borderLayout3);
this.getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
jPanel5.setBorder(BorderFactory.createEtchedBorder());
jPanel4.setBorder(null);
jPanel7.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setText("Alumno :");
jPanel2.add(jPanel4, java.awt.BorderLayout.NORTH);
jPanel4.add(jLabel3);
jPanel2.add(jPanel5, java.awt.BorderLayout.CENTER);
jPanel5.add(jListDescuentosDisponibles, java.awt.BorderLayout.CENTER);
jPanel3.add(jPanel7, java.awt.BorderLayout.CENTER);
jPanel7.add(jListDescuentosDelAlumno, java.awt.BorderLayout.NORTH);
jPanel3.add(jPanel6, java.awt.BorderLayout.NORTH);
jPanel6.add(jLabel4);
}
/**
* loadDescuentosDisponibles
*/
private void loadDescuentosDisponibles() {
modelDescuentosDisponibles.clear();
Vector v = BrokerServer.instance().getAll(Descuento.class);
Enumeration enumer = v.elements();
while (enumer.hasMoreElements()) {
Descuento descuento = (Descuento) enumer.nextElement();
modelDescuentosDisponibles.addElement(descuento.getNombre());
}
}
/**
* loadDescuentosDisponibles
*/
private void loadDescuentosDelAlumno() {
this.jLabelPersona.setText(persona.getNombre());
modelDescuentosDelAlumno.clear();
Iterator iter = persona.getSetDescuento().iterator();
while (iter.hasNext()) {
Descuento descuento = (Descuento) iter.next();
modelDescuentosDelAlumno.addElement(descuento.getNombre());
}
}
public void this_internalFrameClosing(InternalFrameEvent e) {
this.mainFrame.personaFrame.setVisible(true);
}
/**
* jListDescuentosDisponibles_mouseClicked
*
* @param e MouseEvent
*/
public void jListDescuentosDisponibles_mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
String nombre = (String) jListDescuentosDisponibles.
getSelectedValue();
Vector v = BrokerServer.instance().query(Descuento.class,
"nombre==\"" + nombre + "\"");
if (v.size() > 0) {
Descuento descuento = (Descuento) v.firstElement();
ICommand agregarDescuentoAPersona = CommandFactory.
createAgregarDescuentoAPersona(
persona, descuento);
try {
agregarDescuentoAPersona.execute();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
//actualizo la persona con las modificaciones
persona = (Persona) BrokerServer.instance().getPMF().
getPersistenceManager().
getObjectById(javax.jdo.JDOHelper.getObjectId(persona), true);
this.loadDescuentosDelAlumno();
}
}
}
/**
* jListDescuentosDelAlumno_mouseClicked
*
* @param e MouseEvent
*/
public void jListDescuentosDelAlumno_mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
String nombre = (String) this.jListDescuentosDelAlumno.
getSelectedValue();
Descuento elDescuento=null;
Iterator iter = persona.getSetDescuento().iterator();
while (iter.hasNext()) {
Descuento descuento = (Descuento) iter.next();
if(descuento.getNombre().equals(nombre) ){
elDescuento=descuento;
}
}
if (elDescuento!=null) {
ICommand desAgregarDescuentoAPersona = CommandFactory.createDesAgregarDescuentoAPersona(
persona, elDescuento);
try {
desAgregarDescuentoAPersona.execute();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
//actualizo la persona con las modificaciones
persona = (Persona) BrokerServer.instance().getPMF().
getPersistenceManager().
getObjectById(javax.jdo.JDOHelper.getObjectId(persona), true);
this.loadDescuentosDelAlumno();
}
}
}
}
class FrameDescuentosPersona_jListDescuentosDisponibles_mouseAdapter extends
MouseAdapter {
private FrameDescuentosPersona adaptee;
FrameDescuentosPersona_jListDescuentosDisponibles_mouseAdapter(
FrameDescuentosPersona adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jListDescuentosDisponibles_mouseClicked(e);
}
}
class FrameDescuentosPersona_jListDescuentosDelAlumno_mouseAdapter extends
MouseAdapter {
private FrameDescuentosPersona adaptee;
FrameDescuentosPersona_jListDescuentosDelAlumno_mouseAdapter(
FrameDescuentosPersona adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jListDescuentosDelAlumno_mouseClicked(e);
}
}
class FrameDescuentosPersona_this_internalFrameAdapter extends
InternalFrameAdapter {
private FrameDescuentosPersona adaptee;
FrameDescuentosPersona_this_internalFrameAdapter(FrameDescuentosPersona
adaptee) {
this.adaptee = adaptee;
}
public void internalFrameClosing(InternalFrameEvent e) {
adaptee.this_internalFrameClosing(e);
}
}