package siprp.higiene.relatorio; import static com.evolute.utils.strings.UnicodeLatin1Map.atilde; import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil; import static com.evolute.utils.strings.UnicodeLatin1Map.oacute; import info.clearthought.layout.TableLayout; import info.clearthought.layout.TableLayoutConstraints; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.List; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import siprp.database.cayenne.objects.Empresas; import siprp.database.cayenne.objects.Estabelecimentos; import siprp.database.cayenne.objects.HsArea; import siprp.database.cayenne.objects.HsMedida; import siprp.database.cayenne.objects.HsPosto; import siprp.database.cayenne.objects.HsPostoEstabelecimento; import siprp.database.cayenne.objects.HsPostoMedida; import siprp.database.cayenne.objects.HsPostoRisco; import siprp.database.cayenne.objects.HsRelatorio; import siprp.database.cayenne.objects.HsRelatorioRiscoValorQualitativo; import siprp.database.cayenne.objects.HsRisco; import siprp.database.cayenne.objects.HsRiscoMedida; import siprp.database.cayenne.objects.MarcacoesEstabelecimento; import siprp.database.cayenne.providers.MedicinaDAO; import siprp.database.cayenne.providers.PlanoActuacaoDAO; import com.evolute.utils.tables.VectorTableModel; import com.evolute.utils.ui.calendar.JCalendarPanel; public class RelatorioWindow extends JFrame { private static final long serialVersionUID = 1L; public static final String TITLE = "Relat" + oacute + "rios de Higiene e Seguran" + ccedil + "a no Trabalho"; public static final Dimension SIZE = new Dimension( 800, 600 ); private final JComboBox comboEmpresas = new JComboBox(); private final JComboBox comboEstabelecimentos = new JComboBox(); private final VectorTableModel modelVisitas = new VectorTableModel( new String[]{ "Data" } ); private final JTable tableVisitas = new JTable( modelVisitas ); private final JScrollPane scrollVisitas = new JScrollPane( tableVisitas ); private final JButton buttonCriarRelatorio = new JButton("Criar Relat"+oacute+"rio"); private final JPanel panelRelatorio = new JPanel(); private final JCalendarPanel dateRelatorio = new JCalendarPanel( this ); private final JComboBox comboArea = new JComboBox(); private final JComboBox comboPosto = new JComboBox(); private final JPanel panelRiscos = new JPanel(); private final VectorTableModel modelRiscos = new VectorTableModel( new String[]{ "Riscos" } ); private final JTable tableRiscos = new JTable( modelRiscos ); private final JScrollPane scrollRiscos = new JScrollPane( tableRiscos ); private final JPanel panelRiscoValores = new JPanel(); private final JRadioButton radioValorQuantitativo = new JRadioButton(); private final JRadioButton radioValorQualitativo = new JRadioButton(); private final JTextField textSeveridade = new JTextField(); private final JTextField textProbabilidade = new JTextField(); private final JComboBox comboValorQualitativo = new JComboBox(); private final JCheckBox checkPlanoActuacao = new JCheckBox("Plano de actua" + ccedil + atilde + "o" ); private final JPanel panelMedidas = new JPanel(); private final VectorTableModel modelMedidas = new VectorTableModel( new String[]{ "Medidas" } ); private final JTable tableMedidas = new JTable( modelMedidas ); private final JScrollPane scrollMedidas = new JScrollPane( tableMedidas ); private final JTextArea textMedida = new JTextArea(); private final JTextArea textRequisitosLegais = new JTextArea(); private final JButton buttonMedidaAdd = new JButton("Adicionar"); private final JButton buttonMedidaRemove = new JButton("Remover"); private final JButton buttonSubmeterRelatorio = new JButton( "Submeter Plano de Actua" + ccedil + atilde + "o" ); private final ItemListener listenerComboEmpresas = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedEmpresa(); } }; private final ItemListener listenerComboEstabelecimentos = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedEstabelecimento(); } }; private final ItemListener listenerComboArea = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedArea(); } }; private final ItemListener listenerComboPosto = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedPosto(); } }; private final ItemListener listenerComboValorQualitativo = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedValorQualitativo(); } }; private final PlanoActuacaoDAO provider = new PlanoActuacaoDAO(); public RelatorioWindow() { startupComponents(); startupLayout(); placeComponents(); setupListeners(); setTitle( TITLE ); setSize( SIZE ); loadEmpresas(); loadValoresQualitativos(); } private void startupComponents() { scrollMedidas.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); scrollMedidas.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); scrollMedidas.setPreferredSize( new Dimension( 150, 0 ) ); scrollRiscos.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); scrollRiscos.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); scrollRiscos.setPreferredSize( new Dimension( 300, 0 ) ); scrollVisitas.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); scrollVisitas.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); scrollVisitas.setPreferredSize( new Dimension( 200, 0 ) ); panelRelatorio.setBorder( BorderFactory.createTitledBorder( "Relat" + oacute + "rio" ) ); panelRiscoValores.setBorder( BorderFactory.createTitledBorder( "Valores" ) ); panelRiscos.setBorder( BorderFactory.createEtchedBorder() ); panelMedidas.setBorder( BorderFactory.createEtchedBorder() ); dateRelatorio.setPreferredSize( new Dimension( 150, 0 ) ); textSeveridade.setPreferredSize( new Dimension( 30, 0 ) ); textProbabilidade.setPreferredSize( new Dimension( 30, 0 ) ); tableVisitas.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); tableRiscos.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); tableMedidas.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); ButtonGroup bg = new ButtonGroup(); bg.add( radioValorQualitativo ); bg.add( radioValorQuantitativo ); } private void disableAndClearRelatorio( ) { dateRelatorio.setEnabled( false ); dateRelatorio.clear(); comboArea.setEnabled( false ); comboPosto.setEnabled( false ); buttonSubmeterRelatorio.setEnabled( false ); disableAndClearRisco(); disableAndClearMedida(); } private void disableAndClearRisco() { modelRiscos.clearAll(); radioValorQualitativo.setEnabled( false ); radioValorQualitativo.setSelected( false ); radioValorQuantitativo.setEnabled( false ); radioValorQuantitativo.setSelected( false ); textSeveridade.setEnabled( false ); textSeveridade.setText( "" ); textProbabilidade.setEnabled( false ); textProbabilidade.setText( "" ); comboValorQualitativo.setEnabled( false ); checkPlanoActuacao.setEnabled( false ); } private void disableAndClearMedida() { modelMedidas.clearAll(); textMedida.setEnabled( false ); textMedida.setText( "" ); textRequisitosLegais.setEnabled( false ); textRequisitosLegais.setText( "" ); buttonMedidaAdd.setEnabled( false ); buttonMedidaRemove.setEnabled( false ); } private void startupLayout() { TableLayout layout = new TableLayout( new double[]{ TableLayout.PREFERRED, TableLayout.FILL }, new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM } ); layout.setVGap( 5 ); layout.setHGap( 5 ); getContentPane().setLayout( layout ); layout = new TableLayout( new double[]{ TableLayout.MINIMUM, TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED }, new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL } ); layout.setVGap( 5 ); layout.setHGap( 5 ); panelRelatorio.setLayout( layout ); layout = new TableLayout( new double[]{ TableLayout.PREFERRED, TableLayout.FILL }, new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL } ); layout.setVGap( 5 ); layout.setHGap( 5 ); panelRiscos.setLayout( layout ); layout = new TableLayout( new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.PREFERRED, TableLayout.MINIMUM, TableLayout.PREFERRED }, new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM } ); layout.setVGap( 5 ); layout.setHGap( 5 ); panelRiscoValores.setLayout( layout ); layout = new TableLayout( new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.PREFERRED, TableLayout.FILL }, new double[]{ TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM } ); layout.setVGap( 5 ); layout.setHGap( 5 ); panelMedidas.setLayout( layout ); } private void placeComponents() { panelRiscoValores.add( radioValorQuantitativo, new TableLayoutConstraints( 0, 0 ) ); panelRiscoValores.add( new JLabel("Severidade"), new TableLayoutConstraints( 1, 0 ) ); panelRiscoValores.add( textSeveridade, new TableLayoutConstraints( 2, 0 ) ); panelRiscoValores.add( new JLabel("Probabilidade"), new TableLayoutConstraints( 3, 0 ) ); panelRiscoValores.add( textProbabilidade, new TableLayoutConstraints( 4, 0 ) ); panelRiscoValores.add( radioValorQualitativo, new TableLayoutConstraints( 0, 1 ) ); panelRiscoValores.add( comboValorQualitativo, new TableLayoutConstraints( 1, 1, 4, 1 ) ); panelRiscos.add( panelRiscoValores, new TableLayoutConstraints( 1, 0 ) ); panelRiscos.add( checkPlanoActuacao, new TableLayoutConstraints( 1, 1 ) ); panelRiscos.add( scrollRiscos, new TableLayoutConstraints( 0, 0, 0, 2 ) ); panelMedidas.add( scrollMedidas, new TableLayoutConstraints( 0, 0, 2, 3 ) ); panelMedidas.add( buttonMedidaAdd, new TableLayoutConstraints( 0, 4 ) ); panelMedidas.add( buttonMedidaRemove, new TableLayoutConstraints( 1, 4 ) ); panelMedidas.add( new JLabel("Medidas"), new TableLayoutConstraints( 3, 0 ) ); panelMedidas.add( textMedida, new TableLayoutConstraints( 3, 1 ) ); panelMedidas.add( new JLabel("Requisitos Legais"), new TableLayoutConstraints( 3, 2 ) ); panelMedidas.add( textRequisitosLegais, new TableLayoutConstraints( 3, 3 ) ); panelRelatorio.add( new JLabel("Data") , new TableLayoutConstraints( 0, 0 ) ); panelRelatorio.add( dateRelatorio , new TableLayoutConstraints( 1, 0 ) ); panelRelatorio.add( new JLabel("Areas") , new TableLayoutConstraints( 0, 1 ) ); panelRelatorio.add( comboArea, new TableLayoutConstraints( 1, 1, 2, 1 ) ); panelRelatorio.add( new JLabel("Postos de Trabalho") , new TableLayoutConstraints( 0, 2 ) ); panelRelatorio.add( comboPosto, new TableLayoutConstraints( 1, 2, 2, 2 ) ); panelRelatorio.add( buttonSubmeterRelatorio, new TableLayoutConstraints( 3, 0, 3, 2 ) ); panelRelatorio.add( panelRiscos , new TableLayoutConstraints( 0, 3, 3, 3 ) ); panelRelatorio.add( panelMedidas , new TableLayoutConstraints( 0, 4, 3, 4 ) ); getContentPane().add( new JLabel("Empresa"), new TableLayoutConstraints( 0, 0, 1, 0 ) ); getContentPane().add( comboEmpresas, new TableLayoutConstraints( 0, 1, 1, 1 ) ); getContentPane().add( new JLabel("Estabelecimento"), new TableLayoutConstraints( 0, 2, 1, 2 ) ); getContentPane().add( comboEstabelecimentos, new TableLayoutConstraints( 0, 3, 1, 3 ) ); getContentPane().add( scrollVisitas, new TableLayoutConstraints( 0, 4 ) ); getContentPane().add( buttonCriarRelatorio, new TableLayoutConstraints( 0, 5 ) ); getContentPane().add( panelRelatorio, new TableLayoutConstraints( 1, 4, 1, 5 ) ); } private void setupListeners() { comboEmpresas.addItemListener( listenerComboEmpresas ); comboEstabelecimentos.addItemListener( listenerComboEstabelecimentos ); comboArea.addItemListener( listenerComboArea ); comboPosto.addItemListener( listenerComboPosto ); comboValorQualitativo.addItemListener( listenerComboValorQualitativo ); tableVisitas.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged( ListSelectionEvent e ) { if( e.getValueIsAdjusting() && tableVisitas.getSelectedRowCount() > 0 ) { selectedVisita(); } } } ); tableRiscos.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged( ListSelectionEvent e ) { if( e.getValueIsAdjusting() ) { selectedRisco(); } } } ); tableMedidas.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged( ListSelectionEvent e ) { if( e.getValueIsAdjusting() ) { selectedMedida(); } } } ); buttonCriarRelatorio.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { criarRelatorio(); } } ); radioValorQuantitativo.addItemListener( new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedRadioQuantitativo( ); } } ); radioValorQualitativo.addItemListener( new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedRadioQualitativo( ); } } ); checkPlanoActuacao.addItemListener( new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { selectedPlanoActuacao( ); } } ); } private void criarRelatorio() { MarcacoesEstabelecimento visita = getSelectedVisita(); HsRelatorio relatorio = provider.criarRelatorioForVisita( visita ); if( relatorio != null ) { fillRelatorio( relatorio ); } } private HsRisco getSelectedRisco() { int selected = tableRiscos.getSelectedRow(); return selected > -1 ? modelRiscos.getRowAt( selected ) : null; } private HsPosto getSelectedPosto() { return (HsPosto) comboPosto.getSelectedItem(); } private void selectedPlanoActuacao() { HsRisco risco = getSelectedRisco(); HsPosto posto = getSelectedPosto(); if( risco != null && posto != null ) { HsPostoRisco valores = getValores( risco, posto ); if( valores == null ) { valores = provider.createValoresFor( risco, posto ); } valores.setIsPlanoActuacao( checkPlanoActuacao.isSelected() ? "y" : "n" ); provider.commit(); } } private void selectedEmpresa() { disableAndClearRelatorio( ); buttonCriarRelatorio.setEnabled( false ); tableVisitas.setEnabled( false ); modelVisitas.clearAll(); Empresas empresa = (Empresas) comboEmpresas.getSelectedItem(); loadEstabelecimentos( empresa ); if( empresa == null ) { comboEstabelecimentos.setEnabled( false ); } } private void selectedEstabelecimento() { disableAndClearRelatorio( ); modelVisitas.clearAll(); buttonCriarRelatorio.setEnabled( false ); Estabelecimentos estabelecimento = (Estabelecimentos) comboEstabelecimentos.getSelectedItem(); if( estabelecimento != null ) { List marcacoes = estabelecimento.getMarcacoesEstabelecimentoArray(); Vector filteredMarcacoes = new Vector(); for( MarcacoesEstabelecimento marcacao : marcacoes ) { if( "y".equals( marcacao.getRealizada() ) ) { filteredMarcacoes.add( marcacao ); } } modelVisitas.setValues( filteredMarcacoes ); tableVisitas.setEnabled( true ); } } private void selectedArea() { disableAndClearRisco(); disableAndClearMedida(); HsArea area = (HsArea) comboArea.getSelectedItem(); fillPostos( (Estabelecimentos) comboEstabelecimentos.getSelectedItem(), area ); comboPosto.setEnabled( area != null ); } private void selectedPosto() { modelRiscos.clearAll(); HsPosto posto = (HsPosto) comboPosto.getSelectedItem(); if( posto != null ) { Vector riscos = new Vector(); for( HsPostoMedida rel : posto.getHsPostoMedidaArray() ) { for( HsRiscoMedida medidaRelRisco : rel.getToHsMedida().getHsRiscoMedidaArray() ) { riscos.add( medidaRelRisco.getToHsRisco() ); } } modelRiscos.setValues( riscos ); } } private void selectedRadioQuantitativo() { textProbabilidade.setEnabled( true ) ; textSeveridade.setEnabled( true ); comboValorQualitativo.setEnabled( false ); } private void selectedRadioQualitativo() { textProbabilidade.setEnabled( false ); textSeveridade.setEnabled( false ); comboValorQualitativo.setEnabled( true ); } private void selectedValorQualitativo() { } private void selectedRisco() { disableAndClearMedida(); int selected = tableRiscos.getSelectedRow(); HsRisco risco = selected > -1 ? modelRiscos.getRowAt( selected ) : null; checkPlanoActuacao.setEnabled( risco != null ); radioValorQualitativo.setEnabled( risco != null ); radioValorQuantitativo.setEnabled( risco != null ); fillRisco( risco, (HsPosto) comboPosto.getSelectedItem() ); } private void selectedMedida() { int selected = tableMedidas.getSelectedRow(); HsMedida medida = selected > -1 ? modelMedidas.getRowAt( selected ) : null; textMedida.setEnabled( medida != null ); textRequisitosLegais.setEnabled( medida != null ); fillMedida( medida ); } private MarcacoesEstabelecimento getSelectedVisita() { MarcacoesEstabelecimento result = null; int row = tableVisitas.getSelectedRow(); if( row > -1 ) { result = modelVisitas.getRowAt( row ); } return result; } private void selectedVisita() { disableAndClearRelatorio( ); MarcacoesEstabelecimento visita = getSelectedVisita(); List relatorios = visita.getHsRelatorioArray(); HsRelatorio relatorio = null; for( HsRelatorio current : relatorios ) { if( current.getDeletedDate() == null) { relatorio = current; break; } } if( relatorio == null ) { buttonCriarRelatorio.setEnabled( true ); } else { fillRelatorio( relatorio ); buttonCriarRelatorio.setEnabled( false ); } } private void fillRelatorio( HsRelatorio relatorio ) { boolean submetido = relatorio.getIsSubmetido() != null; dateRelatorio.setEnabled( !submetido ); dateRelatorio.setDate( relatorio.getData() ); buttonSubmeterRelatorio.setEnabled( !submetido ); comboArea.setEnabled( true ); fillAreas( relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos() ); } private void fillAreas( Estabelecimentos estabelecimento ) { comboArea.removeAllItems(); if( estabelecimento != null ) { for( HsArea area : new Vector( estabelecimento.getToEmpresas().getHsAreaArray() ) ) { comboArea.addItem( area ); } } } private void fillPostos( Estabelecimentos estabelecimento, HsArea area ) { comboPosto.removeAllItems(); if( estabelecimento != null && area != null ) { for( HsPostoEstabelecimento rel : estabelecimento.getHsPostoEstabelecimentoArray() ) { if( area.getHsPostoArray().contains( rel.getToHsPosto() ) ) { comboPosto.addItem( rel.getToHsPosto() ); } } } } private HsPostoRisco getValores( HsRisco risco, HsPosto posto ) { HsPostoRisco result = null; if( risco != null && posto != null ) { for( HsPostoRisco valores : risco.getHsPostoRiscoArray() ) { if( valores.getToHsPosto().equals( posto ) ) { result = valores; break; } } } return result; } private void fillRisco( HsRisco risco, HsPosto posto ) { modelMedidas.clearAll(); textProbabilidade.setText( "" ); textSeveridade.setText( "" ); checkPlanoActuacao.setSelected( false ); radioValorQuantitativo.setSelected( true ); if( risco != null && posto != null ) { HsPostoRisco valores = getValores( risco, posto ); if( valores != null ) { if( valores.getToHsRelatorioRiscoValorQualitativo() != null ) { radioValorQualitativo.setSelected( true ); } else { radioValorQuantitativo.setSelected( true ); textProbabilidade.setText( valores.getProbabilidade() != null ? valores.getProbabilidade() + "" : "" ); textSeveridade.setText( valores.getSeveridade() != null ? valores.getSeveridade() + "" : "" ); checkPlanoActuacao.setSelected( "y".equals( valores.getIsPlanoActuacao() ) ); } } Vector medidas = new Vector(); for( HsPostoMedida relPostoMedida : posto.getHsPostoMedidaArray() ) { for( HsRiscoMedida relRiscoMedida : risco.getHsRiscoMedidaArray() ) { if( relRiscoMedida.getToHsMedida().equals( relPostoMedida.getToHsMedida() ) ) { medidas.add( relPostoMedida.getToHsMedida() ); } } } modelMedidas.setValues( medidas ); } } private void fillMedida( HsMedida medida ) { textMedida.setText( "" ); textRequisitosLegais.setText( "" ); if( medida != null ) { textMedida.setText( medida.getDescription() ); textRequisitosLegais.setText( medida.getRequesitosLegais() ); } } private void loadEmpresas() { comboEmpresas.removeAllItems(); for( Empresas empresa : new MedicinaDAO().getAllEmpresas() ) { comboEmpresas.addItem( empresa ); } } private void loadValoresQualitativos() { comboValorQualitativo.removeAllItems(); for( HsRelatorioRiscoValorQualitativo valor : provider.getAllValoresQualitativos() ) { comboValorQualitativo.addItem( valor ); } } private void loadEstabelecimentos( Empresas empresa ) { comboEstabelecimentos.removeAllItems(); for( Estabelecimentos estabelecimento : empresa.getEstabelecimentosArray() ) { comboEstabelecimentos.addItem( estabelecimento ); } } public static void main( String[] args ) { RelatorioWindow window = new RelatorioWindow(); window.setVisible( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }