diff --git a/trunk/siprp/ficha/MedicoColumnizedObject.java b/trunk/siprp/ficha/MedicoColumnizedObject.java new file mode 100644 index 00000000..4253604d --- /dev/null +++ b/trunk/siprp/ficha/MedicoColumnizedObject.java @@ -0,0 +1,46 @@ +/* + * MedicoColumnizedObject.java + * + * Created on 12 de Abril de 2004, 11:53 + */ + +package siprp.ficha; + +import com.evolute.utils.tables.*; + +/** + * + * @author fpalma + */ +public class MedicoColumnizedObject implements ColumnizedObject +{ + private Integer id; + private String nome; + private String numero; + + /** Creates a new instance of MedicoColumnizedObject */ + public MedicoColumnizedObject( Integer id, String nome, String numero ) + { + this.id = id; + this.nome = nome; + this.numero = numero; + } + + public Object getValue( int col ) + { + switch( col ) + { + case 0: + return nome; + + case 1: + return numero; + } + return null; + } + + public Integer getID() + { + return id; + } +} diff --git a/trunk/siprp/ficha/MedicoEditor.java b/trunk/siprp/ficha/MedicoEditor.java index b9b0e694..e3c533f1 100644 --- a/trunk/siprp/ficha/MedicoEditor.java +++ b/trunk/siprp/ficha/MedicoEditor.java @@ -9,17 +9,23 @@ package siprp.ficha; import java.awt.*; import java.awt.event.*; import javax.swing.*; -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.*; /** * * @author Administrator */ -public class MedicoEditor extends JDialog - implements ActionListener +public class MedicoEditor extends CustomJDialog + implements ActionListener, ListSelectionListener { + private FichaDataProvider provider; private BaseTable table; private VectorTableModel model; private JButton okButton; @@ -27,27 +33,49 @@ public class MedicoEditor extends JDialog private JButton novoButton; private JTextField nomeText; private JTextField numeroText; - + private Integer id; + private boolean isNew = false; + /** Creates a new instance of MedicoEditor */ - public MedicoEditor() + public MedicoEditor( JFrame owner ) + throws Exception { + super( owner, true ); + provider = (FichaDataProvider)FichaDataProvider.getProvider(); setupComponents(); } private void setupComponents() + throws Exception { - setSize( 300, 300 ); + 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" ); @@ -57,11 +85,168 @@ public class MedicoEditor extends JDialog 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 actionPerformed(java.awt.event.ActionEvent actionEvent) + 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 + { + MetaObject medico; + if( id != null ) + { + medico = provider.load( provider.MEDICOS, new DBKey( id ) ); + } + else + { + medico = provider.createObject( provider.MEDICOS ); + } + medico.setProperty( provider.NOME, nome ); + medico.setProperty( provider.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() ); + } + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( this, "Erro a gravar...", "Erro...", + JOptionPane.ERROR_MESSAGE ); + return false; + } + return true; + } }