@ -1,6 +1,6 @@
package siprp.higiene.gestao.riscos ;
import static com.evolute.utils.strings.UnicodeLatin1Map. atilde ;
import static com.evolute.utils.strings.UnicodeLatin1Map. * ;
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil ;
import info.clearthought.layout.TableLayout ;
import info.clearthought.layout.TableLayoutConstraints ;
@ -26,6 +26,7 @@ import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel ;
import leaf.ui.LeafButton ;
import leaf.ui.LeafError ;
import leaf.ui.TreeTools ;
import siprp.database.cayenne.objects.HsMedida ;
import siprp.database.cayenne.objects.HsRisco ;
@ -37,49 +38,49 @@ public class GerirRiscosPanel extends JPanel
{
private static final long serialVersionUID = 1L ;
private static final String PANEL_TEMA = "PANEL_TEMA" ;
private static final String PANEL_RISCO = "PANEL_RISCO" ;
private static final String PANEL_MEDIDA = "PANEL_MEDIDA" ;
private final JPanel panelButtons = new JPanel ( ) ;
private final JPanel panelTree = new JPanel ( ) ;
private final JPanel panelData = new JPanel ( ) ;
private final JPanel panelDataTema = new JPanel ( ) ;
private final JPanel panelDataRisco = new JPanel ( ) ;
private final GerirMedidaPanel panelDataMedida = new GerirMedidaPanel ( ) ;
private final LeafButton buttonTemaCriar = new LeafButton ( "Criar" ) ;
private final LeafButton buttonTemaEditar = new LeafButton ( "Editar" ) ;
private final LeafButton buttonTemaRemover = new LeafButton ( "Remover" ) ;
private final LeafButton buttonRiscoCriar = new LeafButton ( "Criar" ) ;
private final LeafButton buttonRiscoEditar = new LeafButton ( "Editar" ) ;
private final LeafButton buttonRiscoRemover = new LeafButton ( "Remover" ) ;
private final LeafButton buttonMedidaCriar = new LeafButton ( "Criar" ) ;
private final LeafButton buttonMedidaRemover = new LeafButton ( "Remover" ) ;
private final DefaultMutableTreeNode root = new DefaultMutableTreeNode ( ) ;
private final DefaultTreeModel model = new DefaultTreeModel ( root ) ;
private final JTree tree = new JTree ( model ) ;
private final JScrollPane scroll = new JScrollPane ( tree ) ;
private final CardLayout cardLayout = new CardLayout ( ) ;
public GerirRiscosPanel ( )
@ -90,7 +91,7 @@ public class GerirRiscosPanel extends JPanel
setupListeners ( ) ;
refresh ( ) ;
}
private void setupListeners ( )
{
buttonTemaCriar . addActionListener ( new ActionListener ( )
@ -157,14 +158,15 @@ public class GerirRiscosPanel extends JPanel
removerMedida ( ) ;
}
} ) ;
tree . getSelectionModel ( ) . addTreeSelectionListener ( new TreeSelectionListener ( ) {
tree . getSelectionModel ( ) . addTreeSelectionListener ( new TreeSelectionListener ( )
{
@Override
public void valueChanged ( TreeSelectionEvent e )
{
setEnabled ( ) ;
selectCard ( ) ;
}
} ) ;
} ) ;
panelDataMedida . addPropertyChangeListener ( GerirMedidaPanel . MEDIDA_CHANGED , new PropertyChangeListener ( )
{
@Override
@ -175,163 +177,225 @@ public class GerirRiscosPanel extends JPanel
refresh ( ) ;
}
}
} ) ;
} ) ;
}
private void criarTema ( )
{
String temaString = JOptionPane . showInputDialog ( this , "Criar tema" , "Descri" + ccedil + atilde + "o" , JOptionPane . QUESTION_MESSAGE ) ;
if ( temaString ! = null )
try
{
String temaString = JOptionPane . showInputDialog ( this , "Criar tema" , "Descri" + ccedil + atilde + "o" , JOptionPane . QUESTION_MESSAGE ) ;
if ( temaString ! = null )
{
HsRiscoTema tema = new HsRiscoTema ( ) ;
tema . setDescription ( temaString ) ;
tema . save ( ) ;
refresh ( ) ;
}
} catch ( Exception e )
{
HsRiscoTema tema = new HsRiscoTema ( ) ;
tema . setDescription ( temaString ) ;
tema . save ( ) ;
refresh ( ) ;
LeafError . error ( e ) ;
}
}
private void editarTema ( )
{
HsRiscoTema tema = getSelectedTema ( ) ;
if ( tema ! = null )
try
{
String temaString = JOptionPane . showInputDialog ( this , "Editar tema" , tema . getDescription ( ) ) ;
if ( tema String ! = null )
HsRiscoTema tema = getSelectedTema ( ) ;
if ( tema ! = null )
{
tema . setDescription ( temaString ) ;
tema . save ( ) ;
refresh ( ) ;
String temaString = JOptionPane . showInputDialog ( this , "Editar tema" , tema . getDescription ( ) ) ;
if ( temaString ! = null )
{
tema . setDescription ( temaString ) ;
tema . save ( ) ;
refresh ( ) ;
}
}
} catch ( Exception e )
{
LeafError . error ( e ) ;
}
}
private void removerTema ( )
{
HsRiscoTema tema = getSelectedTema ( ) ;
if ( tema ! = null )
try
{
HsRiscoTema tema = getSelectedTema ( ) ;
if ( tema ! = null )
{
tema . delete ( ) ;
refresh ( ) ;
}
} catch ( Exception e )
{
tema . delete ( ) ;
refresh ( ) ;
LeafError . error ( e ) ;
}
}
private void criarRisco ( )
{
HsRiscoTema tema = getSelectedTema ( ) ;
if ( tema = = null )
try
{
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null )
HsRisco Tema tema = getSelectedTema ( ) ;
if ( tema = = null )
{
tema = risco . getToHsRiscoTema ( ) ;
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null )
{
tema = risco . getToHsRiscoTema ( ) ;
}
else
{
HsRiscoMedida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
{
tema = medida . getToHsRisco ( ) . getToHsRiscoTema ( ) ;
}
}
}
else
if( tema ! = null )
{
HsRiscoMedida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
String riscoString = JOptionPane . showInputDialog ( this , "Criar risco" , "Descri" + ccedil + atilde + "o" , JOptionPane . QUESTION_MESSAGE ) ;
if ( riscoString ! = null )
{
tema = medida . getToHsRisco ( ) . getToHsRiscoTema ( ) ;
HsRisco risco = new HsRisco ( ) ;
risco . setDescription ( riscoString ) ;
risco . setToHsRiscoTema ( tema ) ;
tema . save ( ) ;
refresh ( ) ;
}
}
} catch ( Exception e )
{
LeafError . error ( e ) ;
}
if ( tema ! = null )
}
private void editarRisco ( )
{
try
{
String riscoString = JOptionPane . showInputDialog ( this , "Criar risco" , "Descri" + ccedil + atilde + "o" , JOptionPane . QUESTION_MESSAGE ) ;
if ( riscoString ! = null )
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null )
{
HsRisco risco = new HsRisco ( ) ;
risco . setDescription ( riscoString ) ;
risco . setToHsRiscoTema ( tema ) ;
tema . save ( ) ;
refresh ( ) ;
String riscoString = JOptionPane . showInputDialog ( this , "Editar risco" , risco . getDescription ( ) ) ;
if ( riscoString ! = null )
{
risco . setDescription ( riscoString ) ;
risco . save ( ) ;
refresh ( ) ;
}
}
} catch ( Exception e )
{
LeafError . error ( e ) ;
}
}
private void edita rRisco( )
private void remov erRisco( )
{
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null )
try
{
String riscoString = JOptionPane . showInputDialog ( this , "Editar risco" , risco . getDescription ( ) ) ;
if ( risco String ! = null )
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null & & removerRiscoOK ( risco ) )
{
risco . setDescription ( riscoString ) ;
risco . save ( ) ;
risco . delete ( ) ;
refresh ( ) ;
}
}
catch ( Exception e )
{
LeafError . error ( e ) ;
}
}
private void removerRisco ( )
private boolean removerRiscoOK ( HsRisco risco )
{
HsRisco risco = getSelectedRisco ( ) ;
if ( risco ! = null )
boolean result = true ;
if ( risco . getHsRiscoEmpresaArray ( ) . size ( ) > 0 )
{
risco . delete ( ) ;
refresh ( ) ;
if ( JOptionPane . NO_OPTION = = JOptionPane . showConfirmDialog ( this , "Este risco est" + aacute + " associado a uma ou mais empresas, \n tem a certeza que o deseja remover?" , "Aviso" , JOptionPane . YES_NO_OPTION , JOptionPane . WARNING_MESSAGE ) )
{
result = false ;
}
}
return result ;
}
private void criarMedida ( )
{
HsRisco risco = getSelectedRisco ( ) ;
if ( risco = = null )
try
{
HsRisco Medida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
HsRisco risco = getSelectedRisco ( ) ;
if ( risco = = null )
{
risco = medida . getToHsRisco ( ) ;
HsRiscoMedida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
{
risco = medida . getToHsRisco ( ) ;
}
}
}
if ( risco ! = null )
if ( risco ! = null )
{
HsMedida medida = new HsMedida ( ) ;
medida . setDescription ( "" ) ;
medida . setRequesitosLegais ( "" ) ;
medida . save ( ) ;
HsRiscoMedida rel = new HsRiscoMedida ( ) ;
rel . setToHsRisco ( risco ) ;
rel . setToHsMedida ( medida ) ;
rel . save ( ) ;
refresh ( ) ;
}
} catch ( Exception e )
{
HsMedida medida = new HsMedida ( ) ;
medida . setDescription ( "" ) ;
medida . setRequesitosLegais ( "" ) ;
medida . save ( ) ;
HsRiscoMedida rel = new HsRiscoMedida ( ) ;
rel . setToHsRisco ( risco ) ;
rel . setToHsMedida ( medida ) ;
rel . save ( ) ;
refresh ( ) ;
LeafError . error ( e ) ;
}
}
private void removerMedida ( )
{
HsRiscoMedida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
try
{
medida . delete ( ) ;
refresh ( ) ;
HsRiscoMedida medida = getSelectedMedida ( ) ;
if ( medida ! = null )
{
medida . delete ( ) ;
refresh ( ) ;
}
} catch ( Exception e )
{
LeafError . error ( e ) ;
}
}
private HsRiscoTema getSelectedTema ( )
{
Object object = getSelectedObject ( ) ;
return object = = null ? null : ( ( object instanceof HsRiscoTema ) ? ( HsRiscoTema ) object : null ) ;
}
private HsRisco getSelectedRisco ( )
{
Object object = getSelectedObject ( ) ;
return object = = null ? null : ( ( object instanceof HsRisco ) ? ( HsRisco ) object : null ) ;
}
private HsRiscoMedida getSelectedMedida ( )
{
Object object = getSelectedObject ( ) ;
return object = = null ? null : ( ( object instanceof HsRiscoMedida ) ? ( HsRiscoMedida ) object : null ) ;
}
private Object getSelectedObject ( )
{
DefaultMutableTreeNode node = getSelectedNode ( ) ;
return node = = null ? null : node . getUserObject ( ) ;
}
private DefaultMutableTreeNode getSelectedNode ( )
{
TreePath path = tree . getSelectionPath ( ) ;
@ -345,7 +409,7 @@ public class GerirRiscosPanel extends JPanel
tree . setRootVisible ( false ) ;
tree . getSelectionModel ( ) . setSelectionMode ( TreeSelectionModel . SINGLE_TREE_SELECTION ) ;
}
private void setEnabled ( )
{
HsRiscoTema tema = getSelectedTema ( ) ;
@ -353,60 +417,65 @@ public class GerirRiscosPanel extends JPanel
HsRiscoMedida medida = getSelectedMedida ( ) ;
buttonMedidaCriar . setEnabled ( risco ! = null | | medida ! = null ) ;
buttonRiscoCriar . setEnabled ( tema ! = null | | risco ! = null | | medida ! = null ) ;
buttonTemaEditar . setEnabled ( tema ! = null ) ;
buttonRiscoEditar . setEnabled ( risco ! = null ) ;
buttonMedidaRemover . setEnabled ( medida ! = null ) ;
buttonRiscoRemover . setEnabled ( risco ! = null ) ;
buttonTemaRemover . setEnabled ( tema ! = null ) ;
}
private void startupLayout ( )
{
TableLayout layout = new TableLayout (
new double [ ] { TableLayout . MINIMUM , TableLayout . PREFERRED , TableLayout . FILL } ,
new double [ ] { TableLayout . FILL }
) ;
TableLayout layout = new TableLayout ( new double [ ] {
TableLayout . MINIMUM , TableLayout . PREFERRED , TableLayout . FILL
} , new double [ ] {
TableLayout . FILL
} ) ;
layout . setHGap ( 5 ) ;
layout . setVGap ( 5 ) ;
setLayout ( layout ) ;
layout = new TableLayout (
new double [ ] { TableLayout . MINIMUM } ,
new double [ ] { TableLayout . MINIMUM , TableLayout . MINIMUM , TableLayout . MINIMUM , TableLayout . FILL }
) ;
layout = new TableLayout ( new double [ ] {
TableLayout . MINIMUM
} , new double [ ] {
TableLayout . MINIMUM , TableLayout . MINIMUM , TableLayout . MINIMUM , TableLayout . FILL
} ) ;
layout . setHGap ( 5 ) ;
layout . setVGap ( 5 ) ;
panelButtons . setLayout ( layout ) ;
layout = new TableLayout (
new double [ ] { TableLayout . FILL } ,
new double [ ] { TableLayout . FILL }
) ;
layout = new TableLayout ( new double [ ] {
TableLayout . FILL
} , new double [ ] {
TableLayout . FILL
} ) ;
layout . setHGap ( 5 ) ;
layout . setVGap ( 5 ) ;
panelTree . setLayout ( layout ) ;
layout = new TableLayout (
new double [ ] { TableLayout . FILL } ,
new double [ ] { TableLayout . FILL }
) ;
layout = new TableLayout ( new double [ ] {
TableLayout . FILL
} , new double [ ] {
TableLayout . FILL
} ) ;
layout . setHGap ( 5 ) ;
layout . setVGap ( 5 ) ;
panelDataTema . setLayout ( layout ) ;
layout = new TableLayout (
new double [ ] { TableLayout . FILL } ,
new double [ ] { TableLayout . FILL }
) ;
layout = new TableLayout ( new double [ ] {
TableLayout . FILL
} , new double [ ] {
TableLayout . FILL
} ) ;
layout . setHGap ( 5 ) ;
layout . setVGap ( 5 ) ;
panelDataRisco . setLayout ( layout ) ;
panelData . setLayout ( cardLayout ) ;
}
private void placeComponents ( )
{
JPanel panel = new JPanel ( ) ;
@ -429,19 +498,18 @@ public class GerirRiscosPanel extends JPanel
panel . add ( buttonMedidaCriar ) ;
panel . add ( buttonMedidaRemover ) ;
panelButtons . add ( panel , new TableLayoutConstraints ( 0 , 2 ) ) ;
panelTree . add ( scroll , new TableLayoutConstraints ( 0 , 0 ) ) ;
panelData . add ( panelDataTema , PANEL_TEMA ) ;
panelData . add ( panelDataRisco , PANEL_RISCO ) ;
panelData . add ( panelDataMedida , PANEL_MEDIDA ) ;
add ( panelButtons , new TableLayoutConstraints ( 0 , 0 ) ) ;
add ( panelTree , new TableLayoutConstraints ( 1 , 0 ) ) ;
add ( panelData , new TableLayoutConstraints ( 2 , 0 ) ) ;
}
private void selectCard ( )
{
HsRiscoTema tema = getSelectedTema ( ) ;
@ -453,15 +521,15 @@ public class GerirRiscosPanel extends JPanel
}
else if ( risco ! = null )
{
cardLayout . show ( panelData , PANEL_RISCO ) ;
cardLayout . show ( panelData , PANEL_RISCO ) ;
}
else if ( medida ! = null )
{
cardLayout . show ( panelData , PANEL_MEDIDA ) ;
panelDataMedida . setMedida ( medida . getToHsMedida ( ) ) ;
}
}
}
private void refresh ( )
{
root . removeAllChildren ( ) ;