/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* DadosHistoricos.java
*
* Created on 26/07/2011, 15:14:58
*/
package Interface;
import Dados.Medicao;
import Dados.Sessao;
import Dados.Paciente;
import info.monitorenter.gui.chart.*;
import info.monitorenter.gui.chart.labelformatters.LabelFormatterDate;
import info.monitorenter.gui.chart.rangepolicies.RangePolicyMinimumViewport;
import info.monitorenter.gui.chart.traces.Trace2DSimple;
import info.monitorenter.gui.chart.traces.painters.TracePainterDisc;
import info.monitorenter.gui.chart.traces.painters.TracePainterVerticalBar;
import info.monitorenter.util.Range;
import java.awt.BasicStroke;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.ListModel;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Renan
*/
public class DadosHistoricos extends javax.swing.JPanel implements PacientHolder{
private ListModel modelSessoes;
private Paciente paciente;
private Sessao sessaoAtual;
private static String[] tituloTabela = {"Dado", "Mínimo", "Médio", "Máximo"};
private class SessaoLoaderThread extends SwingWorker<Sessao[], Void> {
@Override
protected Sessao[] doInBackground() throws Exception {
return paciente.getSessoes();
}
@Override
protected void done() {
boolean carregou = false;
try {
final Sessao[] sessoes = get();
if (sessoes != null){
modelSessoes = new javax.swing.AbstractListModel() {
Sessao[] arraySessoes = sessoes;
@Override
public int getSize() { return arraySessoes.length; }
@Override
public Sessao getElementAt(int i) { return arraySessoes[i]; }
};
listSessoes.setModel(modelSessoes);
carregou = true;
}
}
catch (InterruptedException ex) {ex.printStackTrace();}
catch (ExecutionException ex) {ex.printStackTrace();}
if (!carregou){
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("Não foi possível carregar.");
listSessoes.setModel(listModel);
}
}
}
private class MedidasLoaderThread extends SwingWorker<List<Medicao>, Void> {
@Override
protected List<Medicao> doInBackground() throws Exception {
return sessaoAtual.getMedicoes();
}
@Override
protected void done() {
boolean carregou = false;
try {
List<Medicao> medicoes = get();
if (medicoes != null){
int i = 0, j = 0;
boolean adicionou;
Object[][] dados = new Object[NomeMedida.values().length][4];
for (NomeMedida n : NomeMedida.values()){
adicionou = false;
for (Medicao m : medicoes){
if (m.getNome().startsWith(n.toString())){
if (m.getNome().endsWith("min")){
j = 1;
} else if (m.getNome().endsWith("avg")){
j = 2;
} else if (m.getNome().endsWith("max")){
j = 3;
}
dados[i][j] = m.getValor();
adicionou = true;
}
}
if(adicionou){
dados[i][0] = n.toString();
i++;
}
}
tableDadosHistoricos.setModel(new DefaultTableModel(Arrays.copyOf(dados, i), tituloTabela));
}else{
tableDadosHistoricos.setModel(new DefaultTableModel(null, tituloTabela));
}
carregou = true;
}
catch (InterruptedException ex) {ex.printStackTrace();}
catch (ExecutionException ex) {ex.printStackTrace();}
if (!carregou){
tableDadosHistoricos.setModel(new DefaultTableModel(new String[][] {{"Não foi possível carregar."}}, tituloTabela));
}
}
}
/** Creates new form DadosHistoricos */
public DadosHistoricos() {
initComponents();
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("Carregando...");
listSessoes.setModel(listModel);
addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
getParent().setPreferredSize(getPreferredSize());
}
});
}
/** 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">//GEN-BEGIN:initComponents
private void initComponents() {
lblTitle = new javax.swing.JLabel();
lblSessoes = new javax.swing.JLabel();
btnSelTodas = new javax.swing.JButton();
btnSelUltimas = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
listSessoes = new javax.swing.JList();
textfieldSelUltimas = new javax.swing.JTextField();
jSeparator2 = new javax.swing.JSeparator();
lblDados = new javax.swing.JLabel();
jSeparator3 = new javax.swing.JSeparator();
lblGraficos = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
tableDadosHistoricos = new javax.swing.JTable();
chkValorMinimo = new javax.swing.JCheckBox();
chkValorMedio = new javax.swing.JCheckBox();
chkValorMax = new javax.swing.JCheckBox();
btnGerarGrafico = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
listGraficos = new javax.swing.JList();
barraDeStatus = new Interface.BarraDeStatus();
jScrollPane1 = new javax.swing.JScrollPane();
txtObs = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTitle.setText("Dados Históricos");
lblSessoes.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblSessoes.setText("Sessões Anteriores");
btnSelTodas.setText("Selecionar Todas");
btnSelTodas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSelTodasActionPerformed(evt);
}
});
btnSelUltimas.setText("Selecionar Últimas");
btnSelUltimas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSelUltimasActionPerformed(evt);
}
});
listSessoes.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "07/07/2007 10:00", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
listSessoes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
listSessoesValueChanged(evt);
}
});
jScrollPane2.setViewportView(listSessoes);
textfieldSelUltimas.setText("40");
jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
lblDados.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblDados.setText("Dados da Sessão Selecionada");
jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
lblGraficos.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblGraficos.setText("Gerador de Gráficos");
tableDadosHistoricos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null}
},
new String [] {
"Dado", "Mínimo", "Médio", "Máximo"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, true, true, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tableDadosHistoricos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
tableDadosHistoricos.setEnabled(false);
jScrollPane3.setViewportView(tableDadosHistoricos);
chkValorMinimo.setText("Valor Mínimo");
chkValorMedio.setText("Valor Médio");
chkValorMax.setText("Valor Máximo");
btnGerarGrafico.setText("Gerar Gráfico");
btnGerarGrafico.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGerarGraficoActionPerformed(evt);
}
});
listGraficos.setModel(new javax.swing.AbstractListModel() {
public int getSize() { return NomeMedida.values().length; }
public Object getElementAt(int i) { return NomeMedida.values()[i]; }
});
jScrollPane4.setViewportView(listGraficos);
txtObs.setColumns(20);
txtObs.setRows(5);
txtObs.setEnabled(false);
jScrollPane1.setViewportView(txtObs);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Observações");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(btnSelUltimas)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textfieldSelUltimas, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lblSessoes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSelTodas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblDados, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))
.addGap(2, 2, 2)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(chkValorMax, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
.addComponent(chkValorMedio, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
.addComponent(chkValorMinimo, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
.addComponent(btnGerarGrafico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblGraficos, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
.addComponent(barraDeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblTitle)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lblSessoes)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
.addGap(2, 2, 2)
.addComponent(btnSelTodas)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSelUltimas, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textfieldSelUltimas, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(lblDados)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(lblGraficos)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkValorMinimo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkValorMedio)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkValorMax)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnGerarGrafico))
.addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(barraDeStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
private void btnSelTodasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelTodasActionPerformed
listSessoes.setSelectionInterval(0, listSessoes.getModel().getSize() - 1);
}//GEN-LAST:event_btnSelTodasActionPerformed
private void btnSelUltimasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelUltimasActionPerformed
int lowerIndex = listSessoes.getModel().getSize() - Integer.parseInt(textfieldSelUltimas.getText());
lowerIndex = (lowerIndex < 0)?0:lowerIndex;
listSessoes.setSelectionInterval(lowerIndex, listSessoes.getModel().getSize() - 1);
}//GEN-LAST:event_btnSelUltimasActionPerformed
private MedidasLoaderThread medidasLoaderThread = null;
private void listSessoesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listSessoesValueChanged
if (medidasLoaderThread == null || medidasLoaderThread.isDone() || medidasLoaderThread.cancel(true)){
sessaoAtual = (Sessao) listSessoes.getSelectedValue();
txtObs.setText(sessaoAtual.getObservacoes());
sessaoAtual.carregaMedicoes();
medidasLoaderThread = new MedidasLoaderThread();
medidasLoaderThread.execute();
}
}//GEN-LAST:event_listSessoesValueChanged
private void btnGerarGraficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGerarGraficoActionPerformed
// Create a chart:
Chart2D chart = new Chart2D();
IAxis eixoX = chart.getAxisX();
IAxis eixoY = chart.getAxisY();
eixoX.setFormatter(new LabelFormatterDate(new SimpleDateFormat()));
eixoX.setAxisTitle(new IAxis.AxisTitle("Data da Consulta"));
eixoY.setAxisTitle(new IAxis.AxisTitle("Medida"));
// Create an ITrace:
ITrace2D traceMin = new Trace2DSimple("Valores Mínimos");
traceMin.setColor(Color.blue);
ITrace2D traceAvg = new Trace2DSimple("Valores Médios");
traceAvg.setColor(Color.green);
ITrace2D traceMax = new Trace2DSimple("Valores Máximos");
traceMax.setColor(Color.red);
// Add the trace to the chart. This has to be done before adding points (deadlock prevention):
if (chkValorMinimo.isSelected()){
chart.addTrace(traceMin);
traceMin.addTracePainter(new TracePainterDisc());
}
if (chkValorMedio.isSelected()){
chart.addTrace(traceAvg);
traceAvg.addTracePainter(new TracePainterDisc());
}
if (chkValorMax.isSelected()){
chart.addTrace(traceMax);
traceMax.addTracePainter(new TracePainterDisc());
}
// Add all points, as it is static:
Object[] values = listSessoes.getSelectedValues();
String medida = "";
if (! listGraficos.isSelectionEmpty())
medida = listGraficos.getSelectedValue().toString();
double min = 180, max = 0;
for(int i = values.length - 1; i>=0; i-- ){
Sessao s = (Sessao) values[i];
s.carregaMedicoes();
if (s != null && s.getMedicoes() != null){
for (Medicao m : s.getMedicoes()){
if (m.getNome().equals(medida + "min") && chkValorMinimo.isSelected()){
traceMin.addPoint(s.getData().getTime(), m.getValor());
min = (m.getValor() < min)?m.getValor():min;
max = (m.getValor() > max)?m.getValor():max;
}
if (m.getNome().equals(medida + "avg") && chkValorMedio.isSelected()){
traceAvg.addPoint(s.getData().getTime(), m.getValor());
min = (m.getValor() < min)?m.getValor():min;
max = (m.getValor() > max)?m.getValor():max;
}
if (m.getNome().equals(medida + "max") && chkValorMax.isSelected()){
traceMax.addPoint(s.getData().getTime(), m.getValor());
min = (m.getValor() < min)?m.getValor():min;
max = (m.getValor() > max)?m.getValor():max;
}
}
}
}
min -= 1;
eixoY.setRangePolicy(new RangePolicyMinimumViewport(new Range(min,max)));
// Make it visible:
// Create a frame.
JFrame frame = new JFrame("Gráfico");
// add the chart to the frame:
frame.getContentPane().add(chart);
frame.setSize(800,600);
frame.setVisible(true);
}//GEN-LAST:event_btnGerarGraficoActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private Interface.BarraDeStatus barraDeStatus;
private javax.swing.JButton btnGerarGrafico;
private javax.swing.JButton btnSelTodas;
private javax.swing.JButton btnSelUltimas;
private javax.swing.JCheckBox chkValorMax;
private javax.swing.JCheckBox chkValorMedio;
private javax.swing.JCheckBox chkValorMinimo;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JLabel lblDados;
private javax.swing.JLabel lblGraficos;
private javax.swing.JLabel lblSessoes;
private javax.swing.JLabel lblTitle;
private javax.swing.JList listGraficos;
private javax.swing.JList listSessoes;
private javax.swing.JTable tableDadosHistoricos;
private javax.swing.JTextField textfieldSelUltimas;
private javax.swing.JTextArea txtObs;
// End of variables declaration//GEN-END:variables
@Override
public void setPaciente(Paciente paciente) {
this.paciente = paciente;
new SessaoLoaderThread().execute();
barraDeStatus.setNomePaciente(paciente.getNome());
barraDeStatus.setIdade(paciente.getIdade() + "");
}
}