package br.com.gyncom.view;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ClienteView2 extends JPanel {
public ClienteView2() {
initComponents();
if (!Beans.isDesignTime()) {
entityManager.getTransaction().begin();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("siscomjavaPU").createEntityManager();
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Cliente c");
list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
jPanel1 = new javax.swing.JPanel();
masterScrollPane = new javax.swing.JScrollPane();
masterTable = new javax.swing.JTable();
newButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
detailScrollPane = new javax.swing.JScrollPane();
detailTable = new javax.swing.JTable();
saveButton = new javax.swing.JButton();
refreshButton = new javax.swing.JButton();
deleteDetailButton = new javax.swing.JButton();
newDetailButton = new javax.swing.JButton();
FormListener formListener = new FormListener();
masterTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nomeCliente}"));
columnBinding.setColumnName("Nome Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cpfCliente}"));
columnBinding.setColumnName("Cpf Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${rgCliente}"));
columnBinding.setColumnName("Rg Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${orgaoRg}"));
columnBinding.setColumnName("Orgao Rg");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nascimentoCliente}"));
columnBinding.setColumnName("Nascimento");
columnBinding.setColumnClass(java.util.Date.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${desdeCliente}"));
columnBinding.setColumnName("Desde Cliente");
columnBinding.setColumnClass(java.util.Date.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${profissaoCliente}"));
columnBinding.setColumnName("Profissao Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${empresaCliente}"));
columnBinding.setColumnName("Empresa Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${foneEmpresa}"));
columnBinding.setColumnName("Fone Empresa");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${rendaCliente}"));
columnBinding.setColumnName("Renda Cliente");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipoCliente}"));
columnBinding.setColumnName("Tipo Cliente");
columnBinding.setColumnClass(Character.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cnpjCliente}"));
columnBinding.setColumnName("Cnpj Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${referenciaCliente}"));
columnBinding.setColumnName("Referencia Cliente");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${email}"));
columnBinding.setColumnName("Email");
columnBinding.setColumnClass(String.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
masterScrollPane.setViewportView(masterTable);
masterTable.getColumnModel().getColumn(0).setPreferredWidth(180);
masterTable.getColumnModel().getColumn(1).setPreferredWidth(130);
masterTable.getColumnModel().getColumn(4).setPreferredWidth(120);
masterTable.getColumnModel().getColumn(6).setPreferredWidth(120);
masterTable.getColumnModel().getColumn(7).setPreferredWidth(100);
masterTable.getColumnModel().getColumn(8).setPreferredWidth(120);
masterTable.getColumnModel().getColumn(11).setPreferredWidth(130);
masterTable.getColumnModel().getColumn(12).setPreferredWidth(120);
masterTable.getColumnModel().getColumn(13).setPreferredWidth(150);
newButton.setText("Inserir");
newButton.addActionListener(formListener);
deleteButton.setText("Excluir");
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
deleteButton.addActionListener(formListener);
jButton1.setText("Alterar");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), jButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
jButton1.addActionListener(formListener);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(newButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(deleteButton)))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteButton, newButton});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(deleteButton)
.addComponent(newButton)
.addComponent(jButton1))
.addContainerGap())
);
org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.enderecoClienteCollection}");
jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, eLProperty, detailTable);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${logradouroEndereco}"));
columnBinding.setColumnName("Logradouro Endereco");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${complementoEndereco}"));
columnBinding.setColumnName("Complemento Endereco");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${bairroEndereco}"));
columnBinding.setColumnName("Bairro Endereco");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cepEndereco}"));
columnBinding.setColumnName("Cep Endereco");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cidadeEndereco}"));
columnBinding.setColumnName("Cidade Endereco");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ufEnredeco}"));
columnBinding.setColumnName("Uf Enredeco");
columnBinding.setColumnClass(String.class);
jTableBinding.setSourceUnreadableValue(null);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
detailScrollPane.setViewportView(detailTable);
saveButton.setText("Salvar");
saveButton.addActionListener(formListener);
refreshButton.setText("Cancelar");
refreshButton.addActionListener(formListener);
deleteDetailButton.setText("Excluir");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteDetailButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
deleteDetailButton.addActionListener(formListener);
newDetailButton.setText("Inserir");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), newDetailButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
newDetailButton.addActionListener(formListener);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(newDetailButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteDetailButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(refreshButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(saveButton))
.addComponent(detailScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteDetailButton, newDetailButton, refreshButton, saveButton});
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(detailScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(saveButton)
.addComponent(refreshButton)
.addComponent(deleteDetailButton)
.addComponent(newDetailButton))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(8, 8, 8)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
bindingGroup.bind();
}
// Code for dispatching events from components to event handlers.
private class FormListener implements java.awt.event.ActionListener {
FormListener() {}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == newButton) {
ClienteView2.this.newButtonActionPerformed(evt);
}
else if (evt.getSource() == deleteButton) {
ClienteView2.this.deleteButtonActionPerformed(evt);
}
else if (evt.getSource() == jButton1) {
ClienteView2.this.jButton1ActionPerformed(evt);
}
else if (evt.getSource() == saveButton) {
ClienteView2.this.saveButtonActionPerformed(evt);
}
else if (evt.getSource() == refreshButton) {
ClienteView2.this.refreshButtonActionPerformed(evt);
}
else if (evt.getSource() == deleteDetailButton) {
ClienteView2.this.deleteDetailButtonActionPerformed(evt);
}
else if (evt.getSource() == newDetailButton) {
ClienteView2.this.newDetailButtonActionPerformed(evt);
}
}
}// </editor-fold>
private void deleteDetailButtonActionPerformed(java.awt.event.ActionEvent evt) {
int index = masterTable.getSelectedRow();
br.com.gyncom.bean.Cliente c = list.get(masterTable.convertRowIndexToModel(index));
Collection<br.com.gyncom.bean.EnderecoCliente> es = c.getEnderecoClienteCollection();
int[] selected = detailTable.getSelectedRows();
List<br.com.gyncom.bean.EnderecoCliente> toRemove = new ArrayList<br.com.gyncom.bean.EnderecoCliente>(selected.length);
for (int idx=0; idx<selected.length; idx++) {
selected[idx] = detailTable.convertRowIndexToModel(selected[idx]);
int count = 0;
Iterator<br.com.gyncom.bean.EnderecoCliente> iter = es.iterator();
while (count++ < selected[idx]) iter.next();
br.com.gyncom.bean.EnderecoCliente e = iter.next();
toRemove.add(e);
entityManager.remove(e);
}
es.removeAll(toRemove);
masterTable.clearSelection();
masterTable.setRowSelectionInterval(index, index);
}
private void newDetailButtonActionPerformed(java.awt.event.ActionEvent evt) {
int index = masterTable.getSelectedRow();
br.com.gyncom.bean.Cliente c = list.get(masterTable.convertRowIndexToModel(index));
Collection<br.com.gyncom.bean.EnderecoCliente> es = c.getEnderecoClienteCollection();
if (es == null) {
es = new LinkedList<br.com.gyncom.bean.EnderecoCliente>();
c.setEnderecoClienteCollection(es);
}
br.com.gyncom.bean.EnderecoCliente e = new br.com.gyncom.bean.EnderecoCliente();
entityManager.persist(e);
e.setCodCliente(c);
es.add(e);
masterTable.clearSelection();
masterTable.setRowSelectionInterval(index, index);
int row = es.size()-1;
detailTable.setRowSelectionInterval(row, row);
detailTable.scrollRectToVisible(detailTable.getCellRect(row, 0, true));
}
@SuppressWarnings("unchecked")
private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
java.util.Collection data = query.getResultList();
for (Object entity : data) {
entityManager.refresh(entity);
}
list.clear();
list.addAll(data);
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
int[] selected = masterTable.getSelectedRows();
List<br.com.gyncom.bean.Cliente> toRemove = new ArrayList<br.com.gyncom.bean.Cliente>(selected.length);
for (int idx=0; idx<selected.length; idx++) {
br.com.gyncom.bean.Cliente c = list.get(masterTable.convertRowIndexToModel(selected[idx]));
toRemove.add(c);
entityManager.remove(c);
}
list.removeAll(toRemove);
}
private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {
br.com.gyncom.bean.Cliente c = new br.com.gyncom.bean.Cliente();
entityManager.persist(c);
list.add(c);
int row = list.size()-1;
masterTable.setRowSelectionInterval(row, row);
masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
//JFrame frame = new JFrame();
ClienteEditView2 ce = new ClienteEditView2(null, true);
ce.setRegistroAtual(c);
ce.setVisible(true);
if (ce.isConfirmaCliente()){
saveButton.doClick();
} else {
refreshButton.doClick();
}
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
} catch (RollbackException rex) {
rex.printStackTrace();
entityManager.getTransaction().begin();
List<br.com.gyncom.bean.Cliente> merged = new ArrayList<br.com.gyncom.bean.Cliente>(list.size());
for (br.com.gyncom.bean.Cliente c : list) {
merged.add(entityManager.merge(c));
}
list.clear();
list.addAll(merged);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ClienteEditView2 ce = new ClienteEditView2(null, false);
ce.setRegistroAtual(list.get(masterTable.getSelectedRow()));
ce.setVisible(true);
if (ce.isConfirmaCliente()){
saveButton.doClick();
} else {
refreshButton.doClick();
}
}
// Variables declaration - do not modify
private javax.swing.JButton deleteButton;
private javax.swing.JButton deleteDetailButton;
private javax.swing.JScrollPane detailScrollPane;
private javax.swing.JTable detailTable;
private javax.persistence.EntityManager entityManager;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private java.util.List<br.com.gyncom.bean.Cliente> list;
private javax.swing.JScrollPane masterScrollPane;
private javax.swing.JTable masterTable;
private javax.swing.JButton newButton;
private javax.swing.JButton newDetailButton;
private javax.persistence.Query query;
private javax.swing.JButton refreshButton;
private javax.swing.JButton saveButton;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setContentPane(new ClienteView2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setTitle(args[0]);
}
});
}
}