/*
* Demoiselle Framework
* Copyright (C) 2013 SERPRO
* ----------------------------------------------------------------------------
* This file is part of Demoiselle Framework.
*
* Demoiselle Framework is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License version 3
* along with this program; if not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA 02110-1301, USA.
* ----------------------------------------------------------------------------
* Este arquivo é parte do Framework Demoiselle.
*
* O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
* modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
* do Software Livre (FSF).
*
* Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
* GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
* APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
* para maiores detalhes.
*
* Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
* "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
* ou escreva para a Fundação do Software Livre (FSF) Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
*/
package demoisellebehave.serpro.treino.ui;
import demoisellebehave.serpro.treino.dao.Obra;
import demoisellebehave.serpro.treino.dao.ObraDAO;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/**
*
* @author SERPRO
*/
public class JFrameListaObras extends javax.swing.JFrame {
private static JFrameListaObras instance = new JFrameListaObras();
private static final long serialVersionUID = 1L;
private DecimalFormat formatDecimal = new DecimalFormat("###.0");
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
static JFrameListaObras getInstance() {
return instance;
}
/** Creates new form JFrameListaObras */
private JFrameListaObras() {
initComponents();
UICommons.centraliza(this);
this.frameListaObras.setVisible(false);
initDB();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
frameListaObras = new javax.swing.JInternalFrame();
jScrollPane1 = new javax.swing.JScrollPane();
tabelaObras = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
btNovoLance = new javax.swing.JButton();
botaoAdicionar = new javax.swing.JButton();
botaoAlterar = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
menu = new javax.swing.JMenu();
menuListarObras = new javax.swing.JMenuItem();
menuSair = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Painel Principal");
frameListaObras.setMaximizable(true);
frameListaObras.setResizable(true);
frameListaObras.setTitle("Lista de Obras");
frameListaObras.setVisible(true);
frameListaObras.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
frameListaObrasFocusGained(evt);
}
});
jScrollPane1.setViewportView(tabelaObras);
jPanel1.setBackground(new java.awt.Color(245, 210, 147));
jPanel1.setForeground(new java.awt.Color(210, 194, 131));
btNovoLance.setText("Enviar Lance");
btNovoLance.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btNovoLanceActionPerformed(evt);
}
});
botaoAdicionar.setText("Adicionar");
botaoAdicionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoAdicionarActionPerformed(evt);
}
});
botaoAlterar.setText("Alterar");
botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botaoAlterarActionPerformed(evt);
}
});
jButton1.setText("Excluir");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(botaoAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
.addComponent(btNovoLance, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(botaoAdicionar)
.addComponent(botaoAlterar)
.addComponent(jButton1)
.addComponent(btNovoLance))
.addContainerGap())
);
javax.swing.GroupLayout frameListaObrasLayout = new javax.swing.GroupLayout(frameListaObras.getContentPane());
frameListaObras.getContentPane().setLayout(frameListaObrasLayout);
frameListaObrasLayout.setHorizontalGroup(
frameListaObrasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
frameListaObrasLayout.setVerticalGroup(
frameListaObrasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(frameListaObrasLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
menu.setText("Obras");
menuListarObras.setText("Todas as Obras");
menuListarObras.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuListarObrasActionPerformed(evt);
}
});
menu.add(menuListarObras);
jMenuBar1.add(menu);
menuSair.setText("Sair");
menuSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuSairActionPerformed(evt);
}
});
jMenuItem2.setText("Sair do Sistema");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
menuSair.add(jMenuItem2);
jMenuBar1.add(menuSair);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(frameListaObras)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(frameListaObras, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void menuSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSairActionPerformed
this.setVisible(false);
System.exit(0); // TODO add your handling code here:
}//GEN-LAST:event_menuSairActionPerformed
private void menuListarObrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuListarObrasActionPerformed
atualizarLista();
this.frameListaObras.setVisible(true);
}//GEN-LAST:event_menuListarObrasActionPerformed
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
System.exit(0);
}//GEN-LAST:event_jMenuItem2ActionPerformed
private void botaoAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAdicionarActionPerformed
JFrameObra.getIntance().setObra(null);
JFrameObra.getIntance().setVisible(true);
}//GEN-LAST:event_botaoAdicionarActionPerformed
private void frameListaObrasFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_frameListaObrasFocusGained
}//GEN-LAST:event_frameListaObrasFocusGained
private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
int[] rows = tabelaObras.getSelectedRows();
if (rows == null || rows.length == 0) {
JOptionPane.showMessageDialog(this, "Selecione uma obra", "Erro", JOptionPane.ERROR_MESSAGE);
} else {
String id = (String) tabelaObras.getModel().getValueAt(rows[0], 0);
ObraDAO obraDao = new ObraDAO();
Obra obra = obraDao.load(Long.parseLong(id));
JFrameObra.getIntance().setObra(obra);
JFrameObra.getIntance().setVisible(true);
}
}//GEN-LAST:event_botaoAlterarActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int[] rows = tabelaObras.getSelectedRows();
if (rows == null || rows.length == 0) {
JOptionPane.showMessageDialog(this, "Selecione uma obra", "Erro", JOptionPane.ERROR_MESSAGE);
} else {
String id = (String) tabelaObras.getModel().getValueAt(rows[0], 0);
ObraDAO obraDao = new ObraDAO();
Obra obra = obraDao.load(Long.parseLong(id));
int n = JOptionPane.showConfirmDialog(null, "Deseja remover a obra [" + obra.getNome() + "] ?", "Confirmação", JOptionPane.YES_NO_OPTION);
if (n == 0) {
obraDao.remove(obra);
JOptionPane.showMessageDialog(this, "Obra [" + obra.getNome() + "] removida!", "Confirmação", JOptionPane.INFORMATION_MESSAGE);
JFrameListaObras.getInstance().atualizarLista();
}
}
}//GEN-LAST:event_jButton1ActionPerformed
private void btNovoLanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoLanceActionPerformed
int[] rows = tabelaObras.getSelectedRows();
if (rows == null || rows.length == 0) {
JOptionPane.showMessageDialog(this, "Selecione uma obra", "Erro", JOptionPane.ERROR_MESSAGE);
} else {
String id = (String) tabelaObras.getModel().getValueAt(rows[0], 0);
ObraDAO obraDao = new ObraDAO();
Obra obra = obraDao.load(Long.parseLong(id));
JFrameLance.getInstance().setObra(obra);
JFrameLance.getInstance().setVisible(true);
JFrameLance.getInstance().setVisible(true);
}
}//GEN-LAST:event_btNovoLanceActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameListaObras().setVisible(true);
}
});
}
public void atualizarLista() {
if (tabelaObras.getModel().getRowCount() == 0) {
String[] col = new String[]{"ID", "Obra", "Valor", "Prazo", "Vencedor"};
DefaultTableModel dtm = new DefaultTableModel(col, 0);
tabelaObras.setModel(dtm);
TableColumn colId = tabelaObras.getColumn("ID");
colId.setPreferredWidth(0);
colId.setMinWidth(0);
colId.setWidth(0);
colId.setMaxWidth(0);
}
DefaultTableModel dtm = (DefaultTableModel) tabelaObras.getModel();
dtm.setRowCount(0);
ObraDAO dao = new ObraDAO();
List<Obra> listaObras = dao.list();
for (Obra obra : listaObras) {
String[] registro = {obra.getId() + "", obra.getNome(), formatDecimal.format(obra.getValor()), dateFormat.format(obra.getPrazo()), obra.getVencedor()};
dtm.insertRow(dtm.getRowCount(), registro);
}
}
private static void initDB() {
try {
ObraDAO dao = new ObraDAO();
dao.save(new Obra("Arena da Baixada", 100000.00, "30/07/2014"));
dao.save(new Obra("Arena das Dunas", 100000.00, "30/07/2014"));
dao.save(new Obra("Arena Recife", 100000.00, "30/07/2014"));
dao.save(new Obra("Beira Rio", 100000.00, "30/07/2014"));
dao.save(new Obra("Castelão", 100000.00, "30/07/2014"));
dao.save(new Obra("Estádio Corinthians", 100000.00, "30/07/2014"));
dao.save(new Obra("Fonte Nova", 100000.00, "30/07/2014"));
dao.save(new Obra("Mané Garrincha", 100000.00, "30/07/2014"));
dao.save(new Obra("Maracanã", 100000.00, "30/07/2014"));
dao.save(new Obra("Mineirão", 100000.00, "30/07/2014"));
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton botaoAdicionar;
private javax.swing.JButton botaoAlterar;
private javax.swing.JButton btNovoLance;
private javax.swing.JInternalFrame frameListaObras;
private javax.swing.JButton jButton1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JMenu menu;
private javax.swing.JMenuItem menuListarObras;
private javax.swing.JMenu menuSair;
private javax.swing.JTable tabelaObras;
// End of variables declaration//GEN-END:variables
}