/* * MedicoEditor.java * * Created on 10 de Abril de 2004, 20:50 */ package siprp.ficha; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import com.evolute.utils.metadb.*; import com.evolute.utils.tables.*; import com.evolute.utils.ui.*; import siprp.*; import siprp.data.*; /** * * @author Administrator */ public class MedicoEditor extends CustomJDialog implements ActionListener, ListSelectionListener { static { MedicoData.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, MedicoData.class ); } private FichaDataProvider provider; private BaseTable table; private VectorTableModel model; private JButton okButton; private JButton cancelarButton; private JButton novoButton; private JTextField nomeText; private JTextField numeroText; private Integer id; private boolean isNew = false; /** Creates a new instance of MedicoEditor */ public MedicoEditor( JFrame owner ) throws Exception { super( owner, true ); provider = (FichaDataProvider)FichaDataProvider.getProvider(); setupComponents(); } private void setupComponents() throws Exception { setSize( 600, 300 ); setResizable( false ); setTitle( "Criar/Editar M\u00e9dico" ); centerSuper(); model = new VectorTableModel( new String[]{ "Nome", "NC" } ); table = new BaseTable( model ); table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); JScrollPane scp = new JScrollPane(); scp.setMinimumSize( new Dimension( 300, 200 ) ); scp.setMaximumSize( new Dimension( 300, 200 ) ); scp.setPreferredSize( new Dimension( 300, 200 ) ); scp.setSize( new Dimension( 300, 200 ) ); scp.setViewportView( table ); scp.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); scp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); table.fixColumnWidth( 0, 200 ); table.setNonResizableNorReordable(); table.getSelectionModel().addListSelectionListener( this ); Object obj[][] = provider.getAllMedicos(); Vector medicos = new Vector(); for( int n = 0; n < obj.length; n++ ) { medicos.add( new MedicoColumnizedObject( (Integer)obj[n][0], (String)obj[n][1], (String)obj[n][2] ) ); } model.setValues( medicos ); okButton = new JButton( "OK" ); cancelarButton = new JButton( "Cancelar" ); novoButton = new JButton( "Novo" ); okButton.addActionListener( this ); cancelarButton.addActionListener( this ); novoButton.addActionListener( this ); nomeText = new JTextField(); nomeText.setEnabled( false ); numeroText = new JTextField(); numeroText.setEnabled( false ); GridBagLayout gridbag = new GridBagLayout(); getContentPane().setLayout( gridbag ); GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets( 1, 1, 1, 1 ); constraints.fill = GridBagConstraints.BOTH; constraints.weighty = 1; constraints.weightx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; JPanel upperPanel = new JPanel(); upperPanel.setLayout( new GridLayout( 1, 2 ) ); upperPanel.add( scp ); JPanel textPanel = new JPanel(); upperPanel.add( textPanel ); gridbag.setConstraints( upperPanel, constraints ); getContentPane().add( upperPanel ); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.weighty = 0; constraints.weightx = 0.3; constraints.gridwidth = 1; constraints.gridheight = GridBagConstraints.REMAINDER;; gridbag.setConstraints( novoButton, constraints ); getContentPane().add( novoButton ); gridbag.setConstraints( okButton, constraints ); getContentPane().add( okButton ); constraints.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( cancelarButton, constraints ); getContentPane().add( cancelarButton ); gridbag = new GridBagLayout(); textPanel.setLayout( gridbag ); constraints.weighty = 0; constraints.weightx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; JLabel nomeLabel = new JLabel( "Nome" ); gridbag.setConstraints( nomeLabel, constraints ); textPanel.add( nomeLabel ); gridbag.setConstraints( nomeText, constraints ); textPanel.add( nomeText ); JLabel numeroLabel = new JLabel( "N\u00famero" ); gridbag.setConstraints( numeroLabel, constraints ); textPanel.add( numeroLabel ); gridbag.setConstraints( numeroText, constraints ); textPanel.add( numeroText ); constraints.weighty = 1; constraints.gridheight = GridBagConstraints.REMAINDER;; JPanel pad = new JPanel(); gridbag.setConstraints( pad, constraints ); textPanel.add( pad ); } public void actionPerformed( ActionEvent e ) { if( e.getSource().equals( okButton ) ) { if( save() ) { close(); } } else if( e.getSource().equals( cancelarButton ) ) { close(); } if( e.getSource().equals( novoButton ) ) { table.getSelectionModel().removeListSelectionListener( this ); nomeText.setText( "" ); numeroText.setText( "" ); nomeText.setEnabled( true ); numeroText.setEnabled( true ); id = null; isNew = true; } } public void valueChanged(javax.swing.event.ListSelectionEvent listSelectionEvent) { int selected = table.getSelectedRow(); if( selected == -1 ) { return; } MedicoColumnizedObject row = (MedicoColumnizedObject)model.getRowAt( selected ); id = row.getID(); String nome = (String) row.getValue( 0 ); String numero = (String) row.getValue( 1 ); nomeText.setText( nome ); numeroText.setText( numero ); nomeText.setEnabled( true ); numeroText.setEnabled( true ); } private void close() { setVisible( false ); dispose(); } public boolean getIsNew() { return isNew; } public Integer getID() { return id; } private boolean save() { String nome = nomeText.getText().trim(); String numero = numeroText.getText().trim(); if( nome.length() == 0 ) { JOptionPane.showMessageDialog( this, "O Nome n\u00e3o pode ser vazio.", "Erro...", JOptionPane.ERROR_MESSAGE ); return false; } try { MedicoData medico; if( id != null ) { medico = ( MedicoData ) MedicoData.load( MedicoData.class, id ); // medico = provider.load( provider.MEDICOS, new DBKey( id ) ); } else { medico = new MedicoData(); // medico = provider.createObject( provider.MEDICOS ); } // medico.setProperty( provider.NOME, nome ); // medico.setProperty( provider.NUMERO_CEDULA, numero ); medico.set( MedicoData.NOME, nome ); medico.set( MedicoData.NUMERO_CEDULA, numero ); medico.save(); if( id == null ) { // DBKey key = medico.getPrimaryKeyValue(); // DBField fields[] = provider.MEDICOS.getPrimaryKey(); // id = new Integer( ((Number)key.getFieldValue( fields[ 0 ] )).intValue() ); id = (Integer)medico.get( MedicoData.ID ); } } catch( Exception ex ) { // JOptionPane.showMessageDialog( this, "Erro a gravar...", "Erro...", // JOptionPane.ERROR_MESSAGE ); DialogException.showExceptionMessage( ex, "Erro a gravar...", false ); return false; } return true; } }