forked from Coded/SIPRP
				
			git-svn-id: https://svn.coded.pt/svn/SIPRP@894 bb69d46d-e84e-40c8-a05a-06db0d633741
	
		
	
				
					
				
			
							parent
							
								
									59465d738d
								
							
						
					
					
						commit
						67b3a90097
					
				| @ -0,0 +1,128 @@ | |||||||
|  | package siprp.higiene.gestao; | ||||||
|  | 
 | ||||||
|  | import info.clearthought.layout.TableLayout; | ||||||
|  | import info.clearthought.layout.TableLayoutConstraints; | ||||||
|  | 
 | ||||||
|  | import java.awt.event.ActionEvent; | ||||||
|  | import java.awt.event.ActionListener; | ||||||
|  | 
 | ||||||
|  | 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.DefaultTreeModel; | ||||||
|  | import javax.swing.tree.TreePath; | ||||||
|  | import javax.swing.tree.TreeSelectionModel; | ||||||
|  | 
 | ||||||
|  | import leaf.ui.LeafButton; | ||||||
|  | 
 | ||||||
|  | public abstract class AdicionarPanel extends JPanel | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  | 	private static final long serialVersionUID = 1L; | ||||||
|  | 
 | ||||||
|  | 	public static final String SELECTION_CHANGED = "SELECTION_CHANGED"; | ||||||
|  | 
 | ||||||
|  | 	protected final LeafButton buttonAdicionar = new LeafButton( "Adicionar" ); | ||||||
|  | 
 | ||||||
|  | 	protected final LeafButton buttonRemover = new LeafButton( "Remover" ); | ||||||
|  | 
 | ||||||
|  | 	protected final DefaultMutableTreeNode root = new DefaultMutableTreeNode(); | ||||||
|  | 
 | ||||||
|  | 	protected final DefaultTreeModel model = new DefaultTreeModel( root ); | ||||||
|  | 
 | ||||||
|  | 	protected final JTree tree = new JTree( model ); | ||||||
|  | 
 | ||||||
|  | 	protected final JScrollPane scroll = new JScrollPane( tree ); | ||||||
|  | 
 | ||||||
|  | 	public AdicionarPanel() | ||||||
|  | 	{ | ||||||
|  | 		startupComponents(); | ||||||
|  | 		setupLayout(); | ||||||
|  | 		placeComponents(); | ||||||
|  | 		startupListeners(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private void startupComponents() | ||||||
|  | 	{ | ||||||
|  | 		buttonAdicionar.setEnabled( false ); | ||||||
|  | 		buttonRemover.setEnabled( false ); | ||||||
|  | 		tree.setRootVisible( false ); | ||||||
|  | 		tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	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.FILL, TableLayout.MINIMUM | ||||||
|  | 		}, 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( 2, 0 ) ); | ||||||
|  | 
 | ||||||
|  | 		add( panel, new TableLayoutConstraints( 0, 0 ) ); | ||||||
|  | 		add( scroll, new TableLayoutConstraints( 0, 1 ) ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private void startupListeners() | ||||||
|  | 	{ | ||||||
|  | 		buttonAdicionar.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				add(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		buttonRemover.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				rem(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void valueChanged( TreeSelectionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				setEnabled(); | ||||||
|  | 				TreePath path = tree.getSelectionPath(); | ||||||
|  | 				if( path != null ) | ||||||
|  | 				{ | ||||||
|  | 					Object selection = path.getLastPathComponent(); | ||||||
|  | 					firePropertyChange( SELECTION_CHANGED, null, selection ); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	protected abstract void rem(); | ||||||
|  | 
 | ||||||
|  | 	protected abstract void add(); | ||||||
|  | 
 | ||||||
|  | 	protected abstract void setEnabled(); | ||||||
|  | 
 | ||||||
|  | 	public abstract void refresh(); | ||||||
|  | 	 | ||||||
|  | } | ||||||
| @ -0,0 +1,202 @@ | |||||||
|  | package siprp.higiene.gestao.normalizacao; | ||||||
|  | 
 | ||||||
|  | import static com.evolute.utils.strings.UnicodeLatin1Map.atilde; | ||||||
|  | import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil; | ||||||
|  | 
 | ||||||
|  | import javax.swing.tree.DefaultMutableTreeNode; | ||||||
|  | import javax.swing.tree.TreePath; | ||||||
|  | 
 | ||||||
|  | import leaf.ui.LeafError; | ||||||
|  | import leaf.ui.TreeInserterDialog; | ||||||
|  | import leaf.ui.TreeTools; | ||||||
|  | import siprp.database.cayenne.objects.Empresas; | ||||||
|  | import siprp.database.cayenne.objects.Estabelecimentos; | ||||||
|  | import siprp.database.cayenne.objects.HsNormalizacao; | ||||||
|  | import siprp.database.cayenne.objects.HsNormalizacaoEmpresa; | ||||||
|  | import siprp.database.cayenne.objects.HsNormalizacaoEstabelecimento; | ||||||
|  | import siprp.higiene.gestao.AdicionarPanel; | ||||||
|  | import siprp.logic.HigieneSegurancaLogic; | ||||||
|  | 
 | ||||||
|  | public class AdicionarNormalizacaoPanel extends AdicionarPanel | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  | 	private static final long serialVersionUID = 1L; | ||||||
|  | 	 | ||||||
|  | 	private Empresas empresa = null; | ||||||
|  | 	 | ||||||
|  | 	private Estabelecimentos estabelecimento = null; | ||||||
|  | 
 | ||||||
|  | 	public void setEmpresa( Empresas empresa ) | ||||||
|  | 	{ | ||||||
|  | 		this.empresa = empresa; | ||||||
|  | 		this.estabelecimento = null; | ||||||
|  | 		refresh(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void setEstabelecimento( Estabelecimentos estabelecimento ) | ||||||
|  | 	{ | ||||||
|  | 		this.empresa = null; | ||||||
|  | 		this.estabelecimento = estabelecimento; | ||||||
|  | 		refresh(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected void add() | ||||||
|  | 	{ | ||||||
|  | 		DefaultMutableTreeNode allNormalizacao = getAllNormalizacao(); | ||||||
|  | 		removeCurrent( allNormalizacao ); | ||||||
|  | 		TreeInserterDialog dialog = new TreeInserterDialog( this, "Adicionar Normaliza" + ccedil + atilde + "o", allNormalizacao ); | ||||||
|  | 		DefaultMutableTreeNode result = dialog.getResult(); | ||||||
|  | 		if( result != null ) | ||||||
|  | 		{ | ||||||
|  | 			addResult( result ); | ||||||
|  | 			refresh(); | ||||||
|  | 		} | ||||||
|  | 		setEnabled(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private DefaultMutableTreeNode getAllNormalizacao() | ||||||
|  | 	{ | ||||||
|  | 		DefaultMutableTreeNode result = new DefaultMutableTreeNode(); | ||||||
|  | 		if( empresa != null ) | ||||||
|  | 		{ | ||||||
|  | 			result = HigieneSegurancaLogic.getNormalizacaoTree( null ); | ||||||
|  | 		} | ||||||
|  | 		else if( estabelecimento != null ) | ||||||
|  | 		{ | ||||||
|  | 			result = HigieneSegurancaLogic.getNormalizacaoTree( estabelecimento.getToEmpresas() ); | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void removeCurrent( DefaultMutableTreeNode all ) | ||||||
|  | 	{ | ||||||
|  | 		if( all != null && root.getChildCount() == 2 && all.getChildCount() == 2 ) | ||||||
|  | 		{ | ||||||
|  | 			DefaultMutableTreeNode portuguesaAll = (DefaultMutableTreeNode) all.getChildAt( 0 ); | ||||||
|  | 			DefaultMutableTreeNode internacionalAll = (DefaultMutableTreeNode) all.getChildAt( 1 ); | ||||||
|  | 			DefaultMutableTreeNode portuguesaCurrent = (DefaultMutableTreeNode) root.getChildAt( 0 ); | ||||||
|  | 			DefaultMutableTreeNode internacionalCurrent = (DefaultMutableTreeNode) root.getChildAt( 1 ); | ||||||
|  | 			TreeTools.remove( portuguesaAll, portuguesaCurrent ); | ||||||
|  | 			TreeTools.remove( internacionalAll, internacionalCurrent ); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void addResult( DefaultMutableTreeNode root ) | ||||||
|  | 	{ | ||||||
|  | 		try | ||||||
|  | 		{ | ||||||
|  | 			if( root != null ) | ||||||
|  | 			{ | ||||||
|  | 				Object userObject = root.getUserObject(); | ||||||
|  | 				if( userObject instanceof HsNormalizacao ) | ||||||
|  | 				{ | ||||||
|  | 					if( empresa != null ) | ||||||
|  | 					{ | ||||||
|  | 						HsNormalizacaoEmpresa rel = new HsNormalizacaoEmpresa(); | ||||||
|  | 						rel.setToEmpresa( empresa ); | ||||||
|  | 						rel.setToHsNormalizacao( ((HsNormalizacao) userObject ) ); | ||||||
|  | 						rel.save(); | ||||||
|  | 					} | ||||||
|  | 					else if( estabelecimento != null ) | ||||||
|  | 					{ | ||||||
|  | 						HsNormalizacaoEstabelecimento rel = new HsNormalizacaoEstabelecimento(); | ||||||
|  | 						rel.setToEstabelecimento( estabelecimento ); | ||||||
|  | 						rel.setToHsNormalizacao( ((HsNormalizacao) userObject ) ); | ||||||
|  | 						rel.save(); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			for( int i = 0; i < root.getChildCount(); ++i ) | ||||||
|  | 			{ | ||||||
|  | 				DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt( i ); | ||||||
|  | 				addResult( child ); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		catch( Exception e ) | ||||||
|  | 		{ | ||||||
|  | 			LeafError.error( e ); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public void refresh() | ||||||
|  | 	{ | ||||||
|  | 		root.removeAllChildren(); | ||||||
|  | 		if( empresa != null || estabelecimento != null ) | ||||||
|  | 		{ | ||||||
|  | 			TreeTools.merge( root, HigieneSegurancaLogic.getNormalizacaoTree( empresa != null ? empresa : estabelecimento.getToEmpresas() ) ); | ||||||
|  | 		} | ||||||
|  | 		setEnabled(); | ||||||
|  | 		TreeTools.refreshTree( tree, root ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected void setEnabled() | ||||||
|  | 	{ | ||||||
|  | 		buttonAdicionar.setEnabled( empresa != null || estabelecimento != null ); | ||||||
|  | 		buttonRemover.setEnabled( tree.getSelectionCount() > 0 ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected void rem() | ||||||
|  | 	{ | ||||||
|  | 		try | ||||||
|  | 		{ | ||||||
|  | 			TreePath path = tree.getSelectionPath(); | ||||||
|  | 			if( path != null ) | ||||||
|  | 			{ | ||||||
|  | 				DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); | ||||||
|  | 				Object obj = node.getUserObject(); | ||||||
|  | 				if( obj != null && (obj instanceof HsNormalizacao) ) | ||||||
|  | 				{ | ||||||
|  | 					if( empresa != null ) | ||||||
|  | 					{ | ||||||
|  | 						removeFromEmpresa( ((HsNormalizacao) obj ) ); | ||||||
|  | 					} | ||||||
|  | 					else if( estabelecimento != null ) | ||||||
|  | 					{ | ||||||
|  | 						removeFromEstabelecimento( ((HsNormalizacao) obj )); | ||||||
|  | 					} | ||||||
|  | 					refresh(); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		catch( Exception e ) | ||||||
|  | 		{ | ||||||
|  | 			LeafError.error( e ); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void removeFromEmpresa( HsNormalizacao normalizacao ) throws Exception | ||||||
|  | 	{ | ||||||
|  | 		if( normalizacao != null && empresa != null ) | ||||||
|  | 		{ | ||||||
|  | 			for( HsNormalizacaoEmpresa rel : empresa.getHsNormalizacaoEmpresaArray() ) | ||||||
|  | 			{ | ||||||
|  | 				if( normalizacao.equals( rel.getToHsNormalizacao() ) ) | ||||||
|  | 				{ | ||||||
|  | 					rel.delete(); | ||||||
|  | 					break; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void removeFromEstabelecimento( HsNormalizacao normalizacao ) throws Exception | ||||||
|  | 	{ | ||||||
|  | 		if( normalizacao != null && estabelecimento != null ) | ||||||
|  | 		{ | ||||||
|  | 			for( HsNormalizacaoEstabelecimento rel : estabelecimento.getHsNormalizacaoEstabelecimentoArray() ) | ||||||
|  | 			{ | ||||||
|  | 				if( normalizacao.equals( rel.getToHsNormalizacao() ) ) | ||||||
|  | 				{ | ||||||
|  | 					rel.delete(); | ||||||
|  | 					break; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue