forked from Coded/SIPRP
git-svn-id: https://svn.coded.pt/svn/SIPRP@752 bb69d46d-e84e-40c8-a05a-06db0d633741
parent
6a112b9eca
commit
ac5bed0a3f
@ -0,0 +1,499 @@
|
||||
package siprp.higiene.riscos;
|
||||
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.aacute;
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.*;
|
||||
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.util.Vector;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.event.CaretEvent;
|
||||
import javax.swing.event.CaretListener;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
import siprp.database.cayenne.objects.Medida;
|
||||
import siprp.database.cayenne.objects.Risco;
|
||||
import siprp.database.cayenne.objects.RiscoMedida;
|
||||
import siprp.database.cayenne.providers.PlanoActuacaoDAO;
|
||||
|
||||
import com.evolute.utils.tables.VectorTableModel;
|
||||
|
||||
public class GestaoRiscosWindow extends JFrame
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final String TITLE = "Gest" + atilde + "o de Riscos";
|
||||
|
||||
private static final Dimension SIZE = new Dimension( 640, 480 );
|
||||
|
||||
private final VectorTableModel<Risco> modelRiscos = new VectorTableModel<Risco>( new String[]{ "Riscos" } );
|
||||
|
||||
private final VectorTableModel<RiscoMedida> modelMedidas = new VectorTableModel<RiscoMedida>( new String[]{ "Medidas" } );
|
||||
|
||||
private final JTable tableRiscos = new JTable( modelRiscos );
|
||||
|
||||
private final JTable tableMedidas = new JTable( modelMedidas );
|
||||
|
||||
private final JScrollPane scrollRiscos = new JScrollPane(tableRiscos, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
|
||||
|
||||
private final JScrollPane scrollMedidas = new JScrollPane(tableMedidas, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
|
||||
|
||||
private final JButton buttonRiscoCriar = new JButton("Criar");
|
||||
|
||||
private final JButton buttonRiscoRemover = new JButton("Remover");
|
||||
|
||||
private final JButton buttonValorGuardar = new JButton("Guardar");
|
||||
|
||||
private final JButton buttonMedidaCriar = new JButton("Criar");
|
||||
|
||||
private final JButton buttonMedidaAdicionar = new JButton("Adicionar");
|
||||
|
||||
private final JButton buttonMedidaRemover = new JButton("Remover");
|
||||
|
||||
private final JLabel labelValor = new JLabel("Valor");
|
||||
|
||||
private final JTextField fieldValor = new JTextField();
|
||||
|
||||
private final JPanel panelRiscos = new JPanel();
|
||||
|
||||
private final JPanel panelMedidas = new JPanel();
|
||||
|
||||
private final JPanel panelValor = new JPanel();
|
||||
|
||||
private final PlanoActuacaoDAO provider = new PlanoActuacaoDAO();
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
GestaoRiscosWindow window = new GestaoRiscosWindow();
|
||||
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||
window.setVisible( true );
|
||||
}
|
||||
|
||||
public GestaoRiscosWindow()
|
||||
{
|
||||
this.setTitle( TITLE );
|
||||
this.setSize( SIZE );
|
||||
startupComponents();
|
||||
startupLayout();
|
||||
startupListeners();
|
||||
placeComponents();
|
||||
refreshRiscos();
|
||||
}
|
||||
|
||||
private void startupComponents()
|
||||
{
|
||||
panelMedidas.setBorder( BorderFactory.createEtchedBorder() );
|
||||
buttonMedidaRemover.setEnabled( false );
|
||||
buttonMedidaAdicionar.setEnabled( false );
|
||||
buttonMedidaCriar.setEnabled( false );
|
||||
buttonRiscoRemover.setEnabled( false );
|
||||
buttonValorGuardar.setEnabled( false );
|
||||
fieldValor.setEnabled( false );
|
||||
}
|
||||
|
||||
private void startupLayout()
|
||||
{
|
||||
startupPanelRiscosLayout();
|
||||
startupPanelMedidasLayout();
|
||||
startupPanelValorLayout();
|
||||
TableLayout layout = new TableLayout(
|
||||
new double[]{ TableLayout.FILL, TableLayout.FILL },
|
||||
new double[]{ TableLayout.MINIMUM, TableLayout.FILL }
|
||||
);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
this.getContentPane().setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupPanelRiscosLayout()
|
||||
{
|
||||
TableLayout layout = new TableLayout(
|
||||
new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL },
|
||||
new double[]{ TableLayout.FILL, TableLayout.MINIMUM }
|
||||
);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelRiscos.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupPanelMedidasLayout()
|
||||
{
|
||||
TableLayout layout = new TableLayout(
|
||||
new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL },
|
||||
new double[]{ TableLayout.FILL, TableLayout.MINIMUM }
|
||||
);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelMedidas.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupPanelValorLayout()
|
||||
{
|
||||
TableLayout layout = new TableLayout(
|
||||
new double[]{ TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM },
|
||||
new double[]{ TableLayout.MINIMUM }
|
||||
);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelValor.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupListeners()
|
||||
{
|
||||
buttonMedidaCriar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
Risco risco = getSelectedRisco();
|
||||
if( risco != null )
|
||||
{
|
||||
criarMedida( risco );
|
||||
}
|
||||
}
|
||||
} );
|
||||
buttonMedidaAdicionar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
Risco risco = getSelectedRisco();
|
||||
if( risco != null )
|
||||
{
|
||||
adicionarMedida( risco );
|
||||
}
|
||||
}
|
||||
} );
|
||||
buttonMedidaRemover.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
removerMedidas();
|
||||
}
|
||||
} );
|
||||
buttonRiscoCriar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
criarRisco();
|
||||
}
|
||||
} );
|
||||
buttonRiscoRemover.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
removerRiscos();
|
||||
}
|
||||
} );
|
||||
buttonValorGuardar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
alterarValor();
|
||||
}
|
||||
} );
|
||||
tableMedidas.getSelectionModel().addListSelectionListener( new ListSelectionListener()
|
||||
{
|
||||
@Override
|
||||
public void valueChanged( ListSelectionEvent e )
|
||||
{
|
||||
if( !e.getValueIsAdjusting() )
|
||||
{
|
||||
int [] indexes = tableMedidas.getSelectedRows();
|
||||
if( indexes != null && indexes.length > 0 )
|
||||
{
|
||||
buttonMedidaCriar.setEnabled( true );
|
||||
}
|
||||
else
|
||||
{
|
||||
buttonMedidaRemover.setEnabled( false );
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
tableRiscos.getSelectionModel().addListSelectionListener( new ListSelectionListener()
|
||||
{
|
||||
@Override
|
||||
public void valueChanged( ListSelectionEvent e )
|
||||
{
|
||||
if( !e.getValueIsAdjusting() )
|
||||
{
|
||||
int [] indexes = tableRiscos.getSelectedRows();
|
||||
if( indexes != null && indexes.length > 0 )
|
||||
{
|
||||
buttonRiscoRemover.setEnabled( true );
|
||||
if( indexes.length == 1 )
|
||||
{
|
||||
buttonMedidaCriar.setEnabled( true );
|
||||
buttonMedidaAdicionar.setEnabled( true );
|
||||
buttonMedidaRemover.setEnabled( true );
|
||||
buttonValorGuardar.setEnabled( true );
|
||||
fieldValor.setEnabled( true );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buttonMedidaCriar.setEnabled( false );
|
||||
buttonRiscoRemover.setEnabled( false );
|
||||
buttonMedidaAdicionar.setEnabled( false );
|
||||
buttonMedidaRemover.setEnabled( false );
|
||||
buttonValorGuardar.setEnabled( false );
|
||||
fieldValor.setEnabled( false );
|
||||
}
|
||||
refreshValor();
|
||||
refreshMedidas();
|
||||
}
|
||||
}
|
||||
} );
|
||||
fieldValor.addCaretListener( new CaretListener()
|
||||
{
|
||||
@Override
|
||||
public void caretUpdate( CaretEvent e )
|
||||
{
|
||||
buttonValorGuardar.setEnabled( valueChanged() );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private void placeComponents()
|
||||
{
|
||||
panelRiscos.add( scrollRiscos, new TableLayoutConstraints( 0, 0, 2, 0 ) );
|
||||
panelRiscos.add( buttonRiscoCriar, new TableLayoutConstraints( 0, 1 ) );
|
||||
panelRiscos.add( buttonRiscoRemover, new TableLayoutConstraints( 1, 1 ) );
|
||||
|
||||
panelMedidas.add( scrollMedidas, new TableLayoutConstraints( 0, 0, 3, 0 ) );
|
||||
panelMedidas.add( buttonMedidaCriar, new TableLayoutConstraints( 0, 1 ) );
|
||||
panelMedidas.add( buttonMedidaAdicionar, new TableLayoutConstraints( 1, 1 ) );
|
||||
panelMedidas.add( buttonMedidaRemover, new TableLayoutConstraints( 2, 1 ) );
|
||||
|
||||
panelValor.add( labelValor, new TableLayoutConstraints( 0, 0 ) );
|
||||
panelValor.add( fieldValor, new TableLayoutConstraints( 1, 0 ) );
|
||||
panelValor.add( buttonValorGuardar, new TableLayoutConstraints( 2, 0 ) );
|
||||
|
||||
this.getContentPane().add( panelRiscos, new TableLayoutConstraints( 0, 0, 0, 1 ) );
|
||||
this.getContentPane().add( panelValor, new TableLayoutConstraints( 1, 0 ) );
|
||||
this.getContentPane().add( panelMedidas, new TableLayoutConstraints( 1, 1 ) );
|
||||
}
|
||||
|
||||
private void alterarValor()
|
||||
{
|
||||
Risco risco = getSelectedRisco();
|
||||
if( risco != null )
|
||||
{
|
||||
String valorString = fieldValor.getText();
|
||||
if( valorString != null )
|
||||
{
|
||||
if( valorString.trim().length() > 0 )
|
||||
{
|
||||
valorString = valorString.trim();
|
||||
try
|
||||
{
|
||||
Integer valorInt = new Integer(valorString);
|
||||
risco.setValue( valorInt );
|
||||
provider.commit();
|
||||
refreshValor();
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "O valor tem de ser um n" + uacute + "mero", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "O valor n" + atilde + "o pode ser nulo", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean valueChanged()
|
||||
{
|
||||
boolean result = false;
|
||||
Risco risco = getSelectedRisco();
|
||||
String valorString = fieldValor.getText();
|
||||
if( valorString != null && valorString.trim().length() > 0 )
|
||||
{
|
||||
valorString = valorString.trim();
|
||||
try
|
||||
{
|
||||
result = !risco.getValue().equals( new Integer(valorString) );
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void removerRiscos()
|
||||
{
|
||||
int indexes [] = tableRiscos.getSelectedRows();
|
||||
if( indexes != null && indexes.length > 0 )
|
||||
{
|
||||
for( int i = 0; i < indexes.length; ++i )
|
||||
{
|
||||
provider.delete( modelRiscos.getRowAt( indexes[i] ) );
|
||||
}
|
||||
refreshRiscos();
|
||||
}
|
||||
}
|
||||
|
||||
private void removerMedidas( )
|
||||
{
|
||||
int indexes [] = tableMedidas.getSelectedRows();
|
||||
if( indexes != null && indexes.length > 0 )
|
||||
{
|
||||
for( int i = 0; i < indexes.length; ++i )
|
||||
{
|
||||
provider.delete( modelMedidas.getRowAt( indexes[i] ) );
|
||||
}
|
||||
refreshMedidas();
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshMedidas()
|
||||
{
|
||||
modelMedidas.clearAll();
|
||||
Risco risco = getSelectedRisco();
|
||||
if( risco != null )
|
||||
{
|
||||
modelMedidas.setValues( new Vector<RiscoMedida>( risco.getRiscoMedidaArray() ) );
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshValor()
|
||||
{
|
||||
fieldValor.setText( "" );
|
||||
Risco risco = getSelectedRisco();
|
||||
if( risco != null )
|
||||
{
|
||||
fieldValor.setText( risco.getValue() + "" );
|
||||
}
|
||||
buttonValorGuardar.setEnabled( valueChanged() );
|
||||
}
|
||||
|
||||
private void refreshRiscos()
|
||||
{
|
||||
modelRiscos.clearAll();
|
||||
modelRiscos.setValues( new Vector<Risco>( provider.getAllRiscos() ) );
|
||||
}
|
||||
|
||||
private Risco getSelectedRisco()
|
||||
{
|
||||
Risco result = null;
|
||||
int indexes[] = tableRiscos.getSelectedRows();
|
||||
if( indexes != null && indexes.length == 1 )
|
||||
{
|
||||
result = modelRiscos.getRowAt( indexes[0] );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void criarRisco()
|
||||
{
|
||||
String risco = JOptionPane.showInputDialog( "Criar risco" );
|
||||
String valor = JOptionPane.showInputDialog( "Valor" );
|
||||
criarRisco( risco, valor );
|
||||
refreshRiscos();
|
||||
}
|
||||
|
||||
private void criarRisco( String risco, String valor )
|
||||
{
|
||||
if( risco != null && valor != null )
|
||||
{
|
||||
if( risco.trim().length() > 0 )
|
||||
{
|
||||
if( valor.trim().length() > 0 )
|
||||
{
|
||||
risco = risco.trim();
|
||||
valor = valor.trim();
|
||||
try
|
||||
{
|
||||
Integer valorInt = new Integer(valor);
|
||||
provider.createRisco( risco, valorInt );
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "O valor tem de ser um n" + uacute + "mero", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "O valor n" + atilde + "o pode ser nulo", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "Risco inv" + aacute + "lido", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void criarMedida( Risco risco )
|
||||
{
|
||||
String medida = JOptionPane.showInputDialog( "Criar medida" );
|
||||
Medida medidaObject = criarMedida( medida );
|
||||
if( medidaObject != null )
|
||||
{
|
||||
adicionarMedida( medidaObject, risco );
|
||||
}
|
||||
}
|
||||
|
||||
private Medida criarMedida( String medida )
|
||||
{
|
||||
Medida result = null;
|
||||
if( medida != null )
|
||||
{
|
||||
if( medida.trim().length() > 0 )
|
||||
{
|
||||
medida = medida.trim();
|
||||
result = provider.createMedida( medida );
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "Medida inv" + aacute + "lida", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void adicionarMedida( Risco risco )
|
||||
{
|
||||
Medida [] medidas = provider.getAllMedidasNotInRisco( risco );
|
||||
Medida medida = (Medida) JOptionPane.showInputDialog(
|
||||
this,
|
||||
"Medidas",
|
||||
"Adicionar medida", JOptionPane.QUESTION_MESSAGE, null, medidas, null );
|
||||
adicionarMedida( medida, risco );
|
||||
}
|
||||
|
||||
private void adicionarMedida( Medida medida, Risco risco )
|
||||
{
|
||||
if( medida != null )
|
||||
{
|
||||
provider.addMedidaToRisco( medida, risco );
|
||||
refreshMedidas();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue