forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
541 lines
15 KiB
541 lines
15 KiB
package siprp.higiene.relatorio;
|
|
|
|
import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
|
|
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
|
|
import info.clearthought.layout.TableLayout;
|
|
import info.clearthought.layout.TableLayoutConstraints;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Component;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JTree;
|
|
import javax.swing.event.TreeSelectionEvent;
|
|
import javax.swing.event.TreeSelectionListener;
|
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
import javax.swing.tree.DefaultTreeCellRenderer;
|
|
import javax.swing.tree.DefaultTreeModel;
|
|
import javax.swing.tree.TreePath;
|
|
import javax.swing.tree.TreeSelectionModel;
|
|
|
|
import leaf.ui.LeafButton;
|
|
import leaf.ui.LeafDialog;
|
|
import leaf.ui.LeafIconButton;
|
|
import leaf.ui.LeafTree;
|
|
import leaf.ui.TreeInserterDialog;
|
|
import siprp.data.outer.HsPostoRiscoData;
|
|
import siprp.data.outer.HsRelatorioMedidaData;
|
|
import siprp.data.outer.HsRelatorioPostoData;
|
|
import siprp.data.outer.HsRelatorioPostoMedidaData;
|
|
import siprp.data.outer.HsRelatorioPostoRiscoData;
|
|
import siprp.data.outer.HsRelatorioRiscoData;
|
|
import siprp.data.outer.HsRiscoData;
|
|
import siprp.higiene.gestao.SIPRPLazyLoadedPanel;
|
|
import siprp.logic.HigieneSegurancaLogic;
|
|
import siprp.logic.node.MedidaRelatorioNode;
|
|
import siprp.logic.node.NodeRisco;
|
|
import siprp.logic.node.RiscoRelatorioNode;
|
|
|
|
import com.evolute.entity.evo.EvoJoinObject;
|
|
import com.evolute.utils.error.ErrorLogger;
|
|
import com.evolute.utils.ui.trees.TreeTools;
|
|
|
|
public class GerirMedidasRelatorioPanel extends SIPRPLazyLoadedPanel
|
|
{
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
public static final String SELECTION_CHANGED = "RISCOS_SELECTION_CHANGED";
|
|
|
|
public static final String VALUE_CHANGED = "VALUE_CHANGED";
|
|
|
|
private static final String ICON_NAME_SAVE = "siprp/higiene/gestao/add.png";
|
|
|
|
private static final String ICON_NAME_REVERT = "siprp/higiene/gestao/delete.png";
|
|
|
|
private final LeafButton buttonAdicionar = LeafIconButton.createButton( ICON_NAME_SAVE );
|
|
|
|
private final LeafButton buttonRemover = LeafIconButton.createButton( ICON_NAME_REVERT );
|
|
|
|
private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
|
|
|
|
private final DefaultTreeModel model = new DefaultTreeModel( root );
|
|
|
|
public final LeafTree tree = new LeafTree( model );
|
|
|
|
private final JScrollPane scroll = new JScrollPane( tree );
|
|
|
|
private HsRelatorioPostoData posto = null;
|
|
|
|
private HsRelatorioPostoMedidaData selectedMedida = null;
|
|
|
|
public GerirMedidasRelatorioPanel()
|
|
{
|
|
startupComponents();
|
|
setupLayout();
|
|
placeComponents();
|
|
startupListeners();
|
|
}
|
|
|
|
private void startupComponents()
|
|
{
|
|
buttonAdicionar.setEnabled( false );
|
|
buttonRemover.setEnabled( false );
|
|
TreeTools.registerTreeKeepExpandState( tree );
|
|
tree.setRootVisible( false );
|
|
tree.getSelectionModel().setSelectionMode( TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION );
|
|
tree.setCellRenderer( new DefaultTreeCellRenderer()
|
|
{
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
@Override
|
|
public Component getTreeCellRendererComponent( JTree pTree, Object pValue, boolean pIsSelected, boolean pIsExpanded, boolean pIsLeaf, int pRow, boolean pHasFocus )
|
|
{
|
|
super.getTreeCellRendererComponent( pTree, pValue, pIsSelected, pIsExpanded, pIsLeaf, pRow, pHasFocus );
|
|
boolean highlight = false;
|
|
Color highColor = null;
|
|
Color lowColor = null;
|
|
if( pValue instanceof RiscoRelatorioNode )
|
|
{
|
|
Object userObject = ((RiscoRelatorioNode) pValue).getUserObject();
|
|
if( userObject instanceof HsRelatorioPostoRiscoData )
|
|
{
|
|
highlight = HigieneSegurancaLogic.isRelatorioRiscoPreenchido( (HsRelatorioPostoRiscoData) userObject );
|
|
highColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK_SEL;
|
|
lowColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK;
|
|
}
|
|
}
|
|
else if( pValue instanceof MedidaRelatorioNode )
|
|
{
|
|
Object userObject = ((MedidaRelatorioNode) pValue).getUserObject();
|
|
if( userObject instanceof HsRelatorioPostoMedidaData )
|
|
{
|
|
HsRelatorioPostoMedidaData medida = (HsRelatorioPostoMedidaData) userObject;
|
|
if( medida.getIs_plano_actuacao() != null )
|
|
{
|
|
highlight = medida.getIs_plano_actuacao();
|
|
highColor = RelatorioHigieneSegurancaWindow.COLOR_MEDIDA_OK_SEL;
|
|
lowColor = RelatorioHigieneSegurancaWindow.COLOR_MEDIDA_OK;
|
|
}
|
|
}
|
|
}
|
|
if( highlight )
|
|
{
|
|
setOpaque( true );
|
|
setBackground( pIsSelected ? highColor : lowColor );
|
|
}
|
|
else
|
|
{
|
|
setOpaque( false );
|
|
}
|
|
return (this);
|
|
}
|
|
} );
|
|
}
|
|
|
|
private void setupLayout()
|
|
{
|
|
TableLayout layout = new TableLayout(
|
|
new double[] { TableLayout.FILL },
|
|
new double[] { TableLayout.MINIMUM, TableLayout.FILL }
|
|
);
|
|
layout.setHGap( 5 );
|
|
layout.setVGap( 5 );
|
|
setLayout( layout );
|
|
}
|
|
|
|
private void placeComponents()
|
|
{
|
|
JPanel panel = new JPanel();
|
|
TableLayout layout = new TableLayout(
|
|
new double[] { TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL },
|
|
new double[] { TableLayout.MINIMUM }
|
|
);
|
|
layout.setHGap( 5 );
|
|
layout.setVGap( 5 );
|
|
panel.setLayout( layout );
|
|
panel.add( buttonAdicionar, new TableLayoutConstraints( 0, 0 ) );
|
|
panel.add( buttonRemover, new TableLayoutConstraints( 1, 0 ) );
|
|
|
|
add( panel, new TableLayoutConstraints( 0, 0 ) );
|
|
add( scroll, new TableLayoutConstraints( 0, 1 ) );
|
|
}
|
|
|
|
private void startupListeners()
|
|
{
|
|
tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener()
|
|
{
|
|
@Override
|
|
public void valueChanged( TreeSelectionEvent e )
|
|
{
|
|
Object object = null;
|
|
TreePath path = tree.getSelectionPath();
|
|
if( path != null )
|
|
{
|
|
Object selection = path.getLastPathComponent();
|
|
if( selection instanceof RiscoRelatorioNode )
|
|
{
|
|
object = selection == null ? null : ((RiscoRelatorioNode) selection).getUserObject();
|
|
|
|
}
|
|
else if( selection instanceof MedidaRelatorioNode )
|
|
{
|
|
object = selection == null ? null : ((MedidaRelatorioNode) selection).getUserObject();
|
|
selectedMedida = (HsRelatorioPostoMedidaData) object;
|
|
}
|
|
}
|
|
setEnabled();
|
|
firePropertyChange( SELECTION_CHANGED, null, object );
|
|
}
|
|
} );
|
|
buttonAdicionar.addActionListener( new ActionListener()
|
|
{
|
|
|
|
@Override
|
|
public void actionPerformed( ActionEvent e )
|
|
{
|
|
add();
|
|
|
|
}
|
|
|
|
} );
|
|
buttonRemover.addActionListener( new ActionListener()
|
|
{
|
|
|
|
@Override
|
|
public void actionPerformed( ActionEvent e )
|
|
{
|
|
rem();
|
|
}
|
|
|
|
} );
|
|
}
|
|
|
|
private HsRelatorioPostoRiscoData getSelectedRisco()
|
|
{
|
|
HsRelatorioPostoRiscoData result = null;
|
|
TreePath path = tree.getSelectionPath();
|
|
if( path != null )
|
|
{
|
|
Object selection = path.getLastPathComponent();
|
|
if( selection instanceof RiscoRelatorioNode )
|
|
{
|
|
result = selection == null ? null : (HsRelatorioPostoRiscoData) ((RiscoRelatorioNode) selection).getUserObject();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private HsRelatorioPostoMedidaData getSelectedMedida()
|
|
{
|
|
HsRelatorioPostoMedidaData result = null;
|
|
TreePath path = tree.getSelectionPath();
|
|
if( path != null )
|
|
{
|
|
Object selection = path.getLastPathComponent();
|
|
if( selection instanceof MedidaRelatorioNode )
|
|
{
|
|
result = selection == null ? null : (HsRelatorioPostoMedidaData) ((MedidaRelatorioNode) selection).getUserObject();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void add()
|
|
{
|
|
try
|
|
{
|
|
DefaultMutableTreeNode allRiscos = getAllRiscos();
|
|
TreeTools.removeAll( allRiscos, getRiscosTree() );
|
|
TreeInserterDialog dialog = new TreeInserterDialog( "Adicionar Riscos", allRiscos );
|
|
DefaultMutableTreeNode result = dialog.getResult();
|
|
if( result != null )
|
|
{
|
|
addResult( result );
|
|
doRefresh();
|
|
}
|
|
setEnabled();
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
LeafDialog.error( e );
|
|
}
|
|
}
|
|
|
|
private DefaultMutableTreeNode getAllRiscos()
|
|
{
|
|
DefaultMutableTreeNode result = new DefaultMutableTreeNode();
|
|
if( posto != null )
|
|
{
|
|
result = HigieneSegurancaLogic.getRiscosTree( posto.toArea_id().toRelatorio_id().toMarcacao_id().toEstabelecimento_id().toEmpresa_id() );
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private DefaultMutableTreeNode getRiscosTree()
|
|
{
|
|
DefaultMutableTreeNode result = new DefaultMutableTreeNode();
|
|
for( int i = 0; i < root.getChildCount(); ++i )
|
|
{
|
|
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) root.getChildAt( i );
|
|
HsRelatorioPostoRiscoData relPostoRisco = null;
|
|
if( childNode instanceof RiscoRelatorioNode )
|
|
{
|
|
relPostoRisco = (HsRelatorioPostoRiscoData) ((RiscoRelatorioNode) childNode).getUserObject();
|
|
}
|
|
HsRelatorioRiscoData relRisco = relPostoRisco == null ? null : relPostoRisco.toRisco_id();
|
|
if( relRisco != null )
|
|
{
|
|
HsRiscoData risco = HigieneSegurancaLogic.findHsRiscoFor( relRisco );
|
|
if( risco != null )
|
|
{
|
|
result.add( new NodeRisco( risco ) );
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void addResult( DefaultMutableTreeNode root )
|
|
{
|
|
if( root != null )
|
|
{
|
|
if( root instanceof NodeRisco )
|
|
{
|
|
HsRiscoData risco = (HsRiscoData) ((NodeRisco)root).getUserObject();
|
|
addRisco( risco );
|
|
}
|
|
for( int i = 0; i < root.getChildCount(); ++i )
|
|
{
|
|
addResult( (DefaultMutableTreeNode) root.getChildAt( i ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
private void addRisco( HsRiscoData risco )
|
|
{
|
|
if( posto != null )
|
|
{
|
|
Integer order = 0;
|
|
List<HsPostoRiscoData> toPostos = risco.fromHsPostoRisco_risco_id();
|
|
if( !toPostos.isEmpty() )
|
|
{
|
|
for (HsPostoRiscoData hsPostoRisco : toPostos)
|
|
{
|
|
order = Math.max(order, hsPostoRisco.getOrdem() == null ? 0 : hsPostoRisco.getOrdem() );
|
|
}
|
|
}
|
|
try
|
|
{
|
|
HigieneSegurancaLogic.addRiscoToRelatorioPosto( risco, order+1, posto );
|
|
}
|
|
catch ( Exception e )
|
|
{
|
|
ErrorLogger.logExceptionAndShow( e );
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean confirm( String message )
|
|
{
|
|
return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog( this, message, "Confirma"+ccedil+atilde+"o", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null );
|
|
}
|
|
|
|
private void rem()
|
|
{
|
|
for( TreePath path : tree.getSelectionPaths() )
|
|
{
|
|
if( !rem(path) )
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
doRefresh();
|
|
setEnabled();
|
|
}
|
|
|
|
|
|
private String shortenString( String string )
|
|
{
|
|
return string == null ? null : (string.length() <= 40 ? string : (string.substring(0,40) + "..."));
|
|
}
|
|
|
|
|
|
private boolean rem(TreePath path)
|
|
{
|
|
boolean outcome = false;
|
|
try
|
|
{
|
|
if( path != null )
|
|
{
|
|
Object selection = path.getLastPathComponent();
|
|
if( selection instanceof MedidaRelatorioNode )
|
|
{
|
|
HsRelatorioPostoMedidaData rel = selection == null ? null : (HsRelatorioPostoMedidaData) ((MedidaRelatorioNode) selection).getUserObject();
|
|
if( confirm("Tem a certeza que deseja remover o requisito '" + shortenString( rel.toMedida_id().toString() ) + "'?") )
|
|
{
|
|
HsRelatorioMedidaData medidaData = rel.toMedida_id();
|
|
medidaData.delete();
|
|
|
|
rel.delete();
|
|
outcome = true;
|
|
}
|
|
}
|
|
else if( selection instanceof RiscoRelatorioNode )
|
|
{
|
|
HsRelatorioPostoRiscoData rel = (HsRelatorioPostoRiscoData) ((RiscoRelatorioNode)selection).getUserObject();
|
|
if( confirm("Tem a certeza que deseja remover o risco '" + shortenString( rel.toRisco_id().toString() ) + "'?") )
|
|
{
|
|
HsRelatorioRiscoData riscoData = rel.toRisco_id();
|
|
for( HsRelatorioMedidaData medida : riscoData.fromHsRelatorioMedida_risco_id() )
|
|
{
|
|
for ( HsRelatorioPostoMedidaData relMedida : medida.fromHsRelatorioPostoMedida_medida_id() )
|
|
{
|
|
relMedida.delete();
|
|
}
|
|
medida.delete();
|
|
}
|
|
riscoData.delete();
|
|
|
|
rel.delete();
|
|
outcome = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
LeafDialog.error( e );
|
|
}
|
|
return outcome;
|
|
}
|
|
|
|
private void setEnabled()
|
|
{
|
|
boolean add = false;
|
|
boolean rem = false;
|
|
if( posto != null )
|
|
{
|
|
if( posto.toArea_id() != null && posto.toArea_id().toRelatorio_id() != null && posto.toArea_id().toRelatorio_id().getIs_submetido() == null )
|
|
{
|
|
add = true;
|
|
rem = true;
|
|
}
|
|
}
|
|
buttonAdicionar.setEnabled( add );
|
|
buttonRemover.setEnabled( rem && ( getSelectedMedida() != null || getSelectedRisco() != null ) );
|
|
}
|
|
|
|
@Override
|
|
public void refresh()
|
|
{
|
|
root.removeAllChildren();
|
|
|
|
if( posto != null )
|
|
{
|
|
try
|
|
{
|
|
List<EvoJoinObject> objects = HigieneSegurancaLogic.getRelatorioMedidasForPosto( posto );
|
|
RiscoRelatorioNode riscoNode = null;
|
|
for( EvoJoinObject evoJoinObject : objects )
|
|
{
|
|
HsRelatorioPostoRiscoData postoRisco = evoJoinObject.getObject( HsRelatorioPostoRiscoData.class );
|
|
HsRelatorioPostoMedidaData postoMedida = evoJoinObject.getObject( HsRelatorioPostoMedidaData.class );
|
|
if( riscoNode == null || !postoRisco.equals( riscoNode.getUserObject() ) )
|
|
{
|
|
riscoNode = new RiscoRelatorioNode( postoRisco );
|
|
root.add( riscoNode );
|
|
}
|
|
riscoNode.add( new MedidaRelatorioNode( postoMedida ) );
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ErrorLogger.logExceptionAndShow( e );
|
|
}
|
|
|
|
// List<HsRelatorioPostoRiscoData> list = posto.fromHsRelatorioPostoRisco_posto_id();
|
|
// Collections.sort( list );
|
|
//
|
|
// for( HsRelatorioPostoRiscoData rel : list )
|
|
// {
|
|
// RiscoRelatorioNode node = new RiscoRelatorioNode( rel );
|
|
// HsRelatorioRiscoData risco = rel.toRisco_id();
|
|
// if( risco.getDeleted_date() == null )
|
|
// {
|
|
// List< HsRelatorioMedidaData > listMedidas = risco.fromHsRelatorioMedida_risco_id();
|
|
// for( HsRelatorioMedidaData medida : listMedidas )
|
|
// {
|
|
// List< HsRelatorioPostoMedidaData > listRels = medida.fromHsRelatorioPostoMedida_medida_id();
|
|
// for( HsRelatorioPostoMedidaData medidaRel : listRels )
|
|
// {
|
|
// node.add( new MedidaRelatorioNode( medidaRel ) );
|
|
// }
|
|
// }
|
|
// root.add( node );
|
|
// }
|
|
// }
|
|
}
|
|
setEnabled();
|
|
|
|
TreeTools.sort( root, new Comparator<DefaultMutableTreeNode>()
|
|
{
|
|
@Override
|
|
public int compare( DefaultMutableTreeNode o1, DefaultMutableTreeNode o2 )
|
|
{
|
|
Integer result = null;
|
|
if( (o1 instanceof RiscoRelatorioNode) && o2 instanceof RiscoRelatorioNode )
|
|
{
|
|
HsRelatorioPostoRiscoData risco1 = (HsRelatorioPostoRiscoData) o1.getUserObject();
|
|
HsRelatorioPostoRiscoData risco2 = (HsRelatorioPostoRiscoData) o2.getUserObject();
|
|
if( risco1 != null )
|
|
{
|
|
return risco1.compareTo( risco2 == null ? null : risco2 );
|
|
}
|
|
}
|
|
return result == null ? o1.toString().compareTo( o2.toString() ) : result;
|
|
}
|
|
});
|
|
|
|
TreeTools.refreshTree( tree, root, false );
|
|
if( selectedMedida != null )
|
|
{
|
|
selectMedida();
|
|
}
|
|
}
|
|
|
|
private void selectMedida( )
|
|
{
|
|
DefaultMutableTreeNode node = TreeTools.findNodeWithUserObject( selectedMedida, root );
|
|
if( node != null )
|
|
{
|
|
TreePath path = TreeTools.getPathFor( node );
|
|
if( path != null )
|
|
{
|
|
tree.setSelectionPath( path );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
selectedMedida = null;
|
|
}
|
|
}
|
|
|
|
public void setPosto( HsRelatorioPostoData posto )
|
|
{
|
|
this.posto = posto;
|
|
this.selectedMedida = null;
|
|
doRefresh();
|
|
setEnabled();
|
|
}
|
|
|
|
}
|