/* * exec1frame.java * * Created on 6 de Novembro de 2006, 18:35 */ package empresa; import empresa.modelo.Departamento; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import org.apache.log4j.Logger; import org.hibernate.Session; import org.hibernate.HibernateException; import org.hibernate.Transaction; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; /** * * @author 01051010910 */ public class JCargaHorarias extends javax.swing.JFrame { Logger logger = Logger.getLogger(JCargaHorarias.class.getName()); JFreeChart chart; /** Creates new form exec1frame */ public JCargaHorarias() { initComponents(); try { Iterator i = HibernateUtil.currentSession().createQuery( "from Departamento") .list() .iterator(); while (i.hasNext()){ Departamento d = (Departamento) i.next(); cboDepartamento.addItem(d.getNome()); } } catch (HibernateException e) { throw new RuntimeException(e.getMessage()); } finally { HibernateUtil.closeSession(); } } /** 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. */ // //GEN-BEGIN:initComponents private void initComponents() { cboDepartamento = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); lblChart = new javax.swing.JLabel(); btnSalvarGrafico = new javax.swing.JButton(); getContentPane().setLayout(null); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); setFocusTraversalPolicyProvider(true); cboDepartamento.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cboDepartamentoActionPerformed(evt); } }); getContentPane().add(cboDepartamento); cboDepartamento.setBounds(150, 40, 210, 22); jLabel1.setFont(new java.awt.Font("Arial Narrow", 1, 18)); jLabel1.setText("Departamentos:"); getContentPane().add(jLabel1); jLabel1.setBounds(30, 40, 130, 20); getContentPane().add(lblChart); lblChart.setBounds(0, 80, 510, 330); btnSalvarGrafico.setText("Salvar gr\u00e1fico"); btnSalvarGrafico.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSalvarGraficoActionPerformed(evt); } }); getContentPane().add(btnSalvarGrafico); btnSalvarGrafico.setBounds(190, 420, 130, 23); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-517)/2, (screenSize.height-478)/2, 517, 478); }// //GEN-END:initComponents private void btnSalvarGraficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarGraficoActionPerformed String filename = File.separator+"tmp"; JFileChooser fc = new JFileChooser(new File(filename)); fc.showSaveDialog(this); File selFile = fc.getSelectedFile(); try { ChartUtilities.saveChartAsJPEG(selFile, chart, 500, 300); } catch (IOException ex) { logger.error("Ao salvar gráfico", ex); } }//GEN-LAST:event_btnSalvarGraficoActionPerformed private void cboDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboDepartamentoActionPerformed DefaultPieDataset dataSet = new DefaultPieDataset(); String nomeDepartamento=(String) cboDepartamento.getSelectedItem(); Iterator i = HibernateUtil.currentSession().createQuery( "select e.nome, e.quantidadeHorasTrabalhadas from Empregado e where e.departamento.nome= :nome") .setString("nome", nomeDepartamento) .list() .iterator(); if(i.hasNext()) { while (i.hasNext()){ Object[] row = (Object[]) i.next(); String nomeEmpregado = (String)row[0]; Integer totalHoras = (Integer)row[1]; logger.info("Adicionando dados para empregado " + nomeEmpregado); dataSet.setValue(nomeEmpregado, totalHoras); } chart = ChartFactory.createPieChart("Cargas horárias no departamento " + nomeDepartamento, dataSet, true, true, false); ChartFrame frame = new ChartFrame("Grafico de Torta", chart); BufferedImage image = chart.createBufferedImage(500,300); lblChart.setIcon(new ImageIcon(image)); } else { System.out.println("Não há dados para exibição!"); } // frame.pack(); // frame.setVisible(true); }//GEN-LAST:event_cboDepartamentoActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JCargaHorarias().setVisible(true); } }); } // Declaração de variáveis - não modifique//GEN-BEGIN:variables private javax.swing.JButton btnSalvarGrafico; private javax.swing.JComboBox cboDepartamento; private javax.swing.JLabel jLabel1; private javax.swing.JLabel lblChart; // Fim da declaração de variáveis//GEN-END:variables }