forked from Coded/SIPRP
				
			git-svn-id: https://svn.coded.pt/svn/SIPRP@754 bb69d46d-e84e-40c8-a05a-06db0d633741
	
		
	
				
					
				
			
							parent
							
								
									fae6aae398
								
							
						
					
					
						commit
						434d616dd4
					
				| @ -0,0 +1,400 @@ | |||||||
|  | package siprp.higiene.legislacoes; | ||||||
|  | 
 | ||||||
|  | import static com.evolute.utils.strings.UnicodeLatin1Map.atilde; | ||||||
|  | import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil; | ||||||
|  | import static com.evolute.utils.strings.UnicodeLatin1Map.otilde; | ||||||
|  | import info.clearthought.layout.TableLayout; | ||||||
|  | import info.clearthought.layout.TableLayoutConstraints; | ||||||
|  | 
 | ||||||
|  | import java.awt.Color; | ||||||
|  | import java.awt.Dimension; | ||||||
|  | import java.awt.GridLayout; | ||||||
|  | import java.awt.event.ActionEvent; | ||||||
|  | import java.awt.event.ActionListener; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import javax.swing.BorderFactory; | ||||||
|  | import javax.swing.JButton; | ||||||
|  | import javax.swing.JFrame; | ||||||
|  | import javax.swing.JPanel; | ||||||
|  | import javax.swing.JScrollPane; | ||||||
|  | import javax.swing.JTextArea; | ||||||
|  | import javax.swing.JTree; | ||||||
|  | import javax.swing.SwingUtilities; | ||||||
|  | import javax.swing.event.CaretEvent; | ||||||
|  | import javax.swing.event.CaretListener; | ||||||
|  | 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 siprp.database.cayenne.objects.Legislacao; | ||||||
|  | import siprp.database.cayenne.providers.PlanoActuacaoDAO; | ||||||
|  | 
 | ||||||
|  | import com.evolute.utils.tracker.TrackableWindow; | ||||||
|  | 
 | ||||||
|  | public class GestaoLegislacoesWindow extends JFrame implements TrackableWindow | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  | 	private static final long serialVersionUID = 1L; | ||||||
|  | 	 | ||||||
|  | 	public static final String TITLE = "Gest" + atilde + "o de Legisla" + ccedil + otilde + "es"; | ||||||
|  | 	 | ||||||
|  | 	private static final Dimension SIZE = new Dimension( 800, 400 ); | ||||||
|  | 	 | ||||||
|  | 	private 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 JButton buttonCriar = new JButton("Criar"); | ||||||
|  | 	 | ||||||
|  | 	private final JButton buttonRemover = new JButton("Remover"); | ||||||
|  | 	 | ||||||
|  | 	private final JButton buttonGuardar = new JButton("Guardar"); | ||||||
|  | 	 | ||||||
|  | 	private final JButton buttonCancelar = new JButton("Cancelar"); | ||||||
|  | 	 | ||||||
|  | 	private final JTextArea text = new JTextArea(); | ||||||
|  | 	 | ||||||
|  | 	private final JPanel panelTree = new JPanel(); | ||||||
|  | 	 | ||||||
|  | 	private final JPanel panelText = new JPanel(); | ||||||
|  | 	 | ||||||
|  | 	private final PlanoActuacaoDAO provider = new PlanoActuacaoDAO(); | ||||||
|  | 	 | ||||||
|  | 	private Legislacao newLegislacao = null; | ||||||
|  | 	 | ||||||
|  | 	public static void main( String[] args ) | ||||||
|  | 	{ | ||||||
|  | 		GestaoLegislacoesWindow window = new GestaoLegislacoesWindow(); | ||||||
|  | 		window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); | ||||||
|  | 		window.setVisible( true ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public GestaoLegislacoesWindow() | ||||||
|  | 	{ | ||||||
|  | 		this.setTitle( TITLE ); | ||||||
|  | 		this.setSize( SIZE ); | ||||||
|  | 		startupComponents(); | ||||||
|  | 		startupLayout(); | ||||||
|  | 		startupListeners(); | ||||||
|  | 		placeComponents(); | ||||||
|  | 		refresh(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void startupComponents() | ||||||
|  | 	{ | ||||||
|  | 		panelText.setBorder( BorderFactory.createEtchedBorder() ); | ||||||
|  | 		buttonRemover.setEnabled( false ); | ||||||
|  | 		buttonGuardar.setEnabled( false ); | ||||||
|  | 		buttonCancelar.setEnabled( false ); | ||||||
|  | 		text.setEnabled( false ); | ||||||
|  | 		tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); | ||||||
|  | 		tree.setRootVisible( false ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void startupLayout() | ||||||
|  | 	{ | ||||||
|  | 		startupPanelTextLayout(); | ||||||
|  | 		startupPanelTreeLayout(); | ||||||
|  | 		this.getContentPane().setLayout( new GridLayout(1,0,5,5) ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void startupPanelTextLayout() | ||||||
|  | 	{ | ||||||
|  | 		TableLayout layout = new TableLayout( | ||||||
|  | 				new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL }, | ||||||
|  | 				new double[]{ TableLayout.FILL, TableLayout.MINIMUM } | ||||||
|  | 				); | ||||||
|  | 		layout.setHGap( 5 ); | ||||||
|  | 		layout.setVGap( 5 ); | ||||||
|  | 		panelText.setLayout( layout ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void startupPanelTreeLayout() | ||||||
|  | 	{ | ||||||
|  | 		TableLayout layout = new TableLayout( | ||||||
|  | 				new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL }, | ||||||
|  | 				new double[]{ TableLayout.FILL, TableLayout.MINIMUM } | ||||||
|  | 				); | ||||||
|  | 		layout.setHGap( 5 ); | ||||||
|  | 		layout.setVGap( 5 ); | ||||||
|  | 		panelTree.setLayout( layout ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void startupListeners() | ||||||
|  | 	{ | ||||||
|  | 		buttonCriar.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				criarLegislacao( ); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		buttonRemover.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				removerLegislacao(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		buttonGuardar.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				guardarLegislacao(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		buttonCancelar.addActionListener( new ActionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void actionPerformed( ActionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				cancelarEdicao(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void valueChanged( TreeSelectionEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				TreePath path = tree.getSelectionPath(); | ||||||
|  | 				Legislacao legislacao = getSelectedLegislacao(); | ||||||
|  | 				if( path != null ) | ||||||
|  | 				{ | ||||||
|  | 					buttonRemover.setEnabled( !root.equals( path.getLastPathComponent() ) ); | ||||||
|  | 					text.setEnabled( !root.equals( path.getLastPathComponent() ) ); | ||||||
|  | 					text.setText( legislacao == null ? "" : legislacao.getDescription() ); | ||||||
|  | 				} | ||||||
|  | 				else | ||||||
|  | 				{ | ||||||
|  | 					text.setEnabled( false ); | ||||||
|  | 					buttonRemover.setEnabled( false ); | ||||||
|  | 					text.setText( "" ); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 		text.addCaretListener( new CaretListener() | ||||||
|  | 		{ | ||||||
|  | 			@Override | ||||||
|  | 			public void caretUpdate( CaretEvent e ) | ||||||
|  | 			{ | ||||||
|  | 				if( text.getText().length() > 0) | ||||||
|  | 				{ | ||||||
|  | 					Legislacao legislacao = null; | ||||||
|  | 					if( newLegislacao != null ) | ||||||
|  | 					{ | ||||||
|  | 						legislacao = newLegislacao; | ||||||
|  | 					} | ||||||
|  | 					else | ||||||
|  | 					{ | ||||||
|  | 						legislacao = getSelectedLegislacao(); | ||||||
|  | 					} | ||||||
|  | 					boolean changed = legislacao == null ? false : !legislacao.getDescription().equals( text.getText() ); | ||||||
|  | 					buttonGuardar.setEnabled( changed ); | ||||||
|  | 					text.setBorder( changed ? BorderFactory.createLineBorder( Color.RED, 2 ) : BorderFactory.createEtchedBorder() ); | ||||||
|  | 				} | ||||||
|  | 				else | ||||||
|  | 				{ | ||||||
|  | 					buttonGuardar.setEnabled( false ); | ||||||
|  | 					text.setBorder( BorderFactory.createEtchedBorder() ); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void placeComponents() | ||||||
|  | 	{ | ||||||
|  | 		panelTree.add( scroll, new TableLayoutConstraints( 0, 0, 2, 0 ) ); | ||||||
|  | 		panelTree.add( buttonCriar, new TableLayoutConstraints( 0, 1 ) ); | ||||||
|  | 		panelTree.add( buttonRemover, new TableLayoutConstraints( 1, 1 ) ); | ||||||
|  | 		 | ||||||
|  | 		panelText.add( text, new TableLayoutConstraints( 0, 0, 2, 0 ) ); | ||||||
|  | 		panelText.add( buttonGuardar, new TableLayoutConstraints( 0, 1 ) ); | ||||||
|  | 		panelText.add( buttonCancelar, new TableLayoutConstraints( 1, 1 ) ); | ||||||
|  | 		 | ||||||
|  | 		this.getContentPane().add( panelTree ); | ||||||
|  | 		this.getContentPane().add( panelText ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void criarLegislacao() | ||||||
|  | 	{ | ||||||
|  | 		text.setText( "" ); | ||||||
|  | 		Legislacao selected = getSelectedLegislacao(); | ||||||
|  | 		if( selected != null ) | ||||||
|  | 		{ | ||||||
|  | 			newLegislacao = provider.createNovaLegislacao( selected ); | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			newLegislacao = provider.createNovaLegislacao( getSelectedLegislacao() ); | ||||||
|  | 		} | ||||||
|  | 		prepareEditar( false ); | ||||||
|  | 		textInputOnly( true ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void prepareEditar( boolean editar ) | ||||||
|  | 	{ | ||||||
|  | 		buttonGuardar.setText( editar ? "Editar" : "Criar" ); | ||||||
|  | 		buttonCancelar.setText( editar ? "Reverter" : "Cancelar" ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void textInputOnly( boolean doTextInput ) | ||||||
|  | 	{ | ||||||
|  | 		text.setEnabled( doTextInput ); | ||||||
|  | 		buttonCancelar.setEnabled( doTextInput ); | ||||||
|  | 		buttonCriar.setEnabled( !doTextInput ); | ||||||
|  | 		tree.setEnabled( !doTextInput ); | ||||||
|  | 		text.setBorder( doTextInput ? BorderFactory.createLineBorder( Color.RED, 2 ) : BorderFactory.createEtchedBorder()); | ||||||
|  | 		if( doTextInput ){ | ||||||
|  | 			text.setCaretPosition( 0 ); | ||||||
|  | 			text.requestFocus(); | ||||||
|  | 			buttonRemover.setEnabled( false ); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void guardarLegislacao() | ||||||
|  | 	{ | ||||||
|  | 		TreePath path = tree.getSelectionPath(); | ||||||
|  | 		if( newLegislacao != null ) | ||||||
|  | 		{ | ||||||
|  | 			newLegislacao.setDescription( text.getText() ); | ||||||
|  | 			provider.commit(); | ||||||
|  | 			newLegislacao = null; | ||||||
|  | 			prepareEditar( true ); | ||||||
|  | 			textInputOnly( false ); | ||||||
|  | 			refresh(); | ||||||
|  | 		} | ||||||
|  | 		else if( path != null ) | ||||||
|  | 		{ | ||||||
|  | 			Legislacao legislacao = getSelectedLegislacao(); | ||||||
|  | 			legislacao.setDescription( text.getText() ); | ||||||
|  | 			provider.commit(); | ||||||
|  | 			prepareEditar( true ); | ||||||
|  | 			textInputOnly( false ); | ||||||
|  | 			refresh(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void cancelarEdicao() | ||||||
|  | 	{ | ||||||
|  | 		TreePath path = tree.getSelectionPath(); | ||||||
|  | 		if( newLegislacao != null ) | ||||||
|  | 		{ | ||||||
|  | 			newLegislacao = null; | ||||||
|  | 			prepareEditar( true ); | ||||||
|  | 			textInputOnly( false ); | ||||||
|  | 			refresh(); | ||||||
|  | 			tree.setSelectionPath( path ); | ||||||
|  | 		} | ||||||
|  | 		else if( path != null ) | ||||||
|  | 		{ | ||||||
|  | 			prepareEditar( true ); | ||||||
|  | 			textInputOnly( false ); | ||||||
|  | 			refresh(); | ||||||
|  | 			tree.setSelectionPath( path ); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private Legislacao getSelectedLegislacao() | ||||||
|  | 	{ | ||||||
|  | 		Legislacao result = null; | ||||||
|  | 		TreePath path = tree.getSelectionPath(); | ||||||
|  | 		if( path != null ) | ||||||
|  | 		{ | ||||||
|  | 			Object o = ( (DefaultMutableTreeNode) path.getLastPathComponent() ).getUserObject(); | ||||||
|  | 			if( o instanceof Legislacao ) | ||||||
|  | 			{ | ||||||
|  | 				result = (Legislacao) o; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void removerLegislacao() | ||||||
|  | 	{ | ||||||
|  | 		Legislacao legislacao = getSelectedLegislacao(); | ||||||
|  | 		if( legislacao != null ) | ||||||
|  | 		{ | ||||||
|  | 			provider.delete( legislacao ); | ||||||
|  | 		} | ||||||
|  | 		refresh(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public void refresh() | ||||||
|  | 	{ | ||||||
|  | 		clearTree(); | ||||||
|  | 		fillTree(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void clearTree() | ||||||
|  | 	{ | ||||||
|  | 		root.removeAllChildren(); | ||||||
|  | 		refreshTree(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private void fillTree() | ||||||
|  | 	{ | ||||||
|  | 		List<Legislacao> legislacoes = provider.getAllLegislacoes( null ); | ||||||
|  | 		for( Legislacao legislacao : legislacoes ) | ||||||
|  | 		{ | ||||||
|  | 			DefaultMutableTreeNode node = new DefaultMutableTreeNode( legislacao ); | ||||||
|  | 			fillWithChildren( node, legislacao ); | ||||||
|  | 			root.add( node ); | ||||||
|  | 		} | ||||||
|  | 		refreshTree(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void fillWithChildren( DefaultMutableTreeNode node, Legislacao legislacao ) | ||||||
|  | 	{ | ||||||
|  | 		if( legislacao != null ) | ||||||
|  | 		{ | ||||||
|  | 			for( Legislacao child : legislacao.getChildrenArray() ) | ||||||
|  | 			{ | ||||||
|  | 				DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); | ||||||
|  | 				fillWithChildren( childNode, child ); | ||||||
|  | 				node.add( childNode ); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void refreshTree() | ||||||
|  | 	{ | ||||||
|  | 		model.nodeStructureChanged( root ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public boolean closeIfPossible() | ||||||
|  | 	{ | ||||||
|  | 		close(); | ||||||
|  | 		return true; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private void close() | ||||||
|  | 	{ | ||||||
|  | 		SwingUtilities.invokeLater( new Runnable() { | ||||||
|  | 			public void run() | ||||||
|  | 			{ | ||||||
|  | 				setVisible( false ); | ||||||
|  | 				dispose(); | ||||||
|  | 			} | ||||||
|  | 		} ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public void open() | ||||||
|  | 	{ | ||||||
|  | 		setVisible( true ); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue