codice:
package iCtek.ui;
import iCtek.ui.DialogResult;
import iCtek.controller.MyController;
import iCtek.model.HardDisk;
import iCtek.model.Hardware;
import iCtek.model.SchedaVideo;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import Persistence.MyCatalogoWriter;
public class NewComponentDialog extends JDialog implements ActionListener , DocumentListener
{
private static final long serialVersionUID = 1L;
private JTextField cat ;
private JTextField marca ;
private JTextField model ;
private JTextField prezzo ;
private JLabel lCat ;
private JLabel lMarca ;
private JLabel lModel ;
private JLabel lPrezzo ;
private JButton ok ;
private JButton annulla ;
private ArrayList <String> lista ; // array di stringhe recuperate dai TextField
private Hardware hardware ; // oggetto solo di utilità
private MyController controller ;
private DialogResult chooce ;
private String valueCat ;
private String valueMarca ;
private String valueModel ;
private String valuePrezzo ;
public NewComponentDialog()
{
//super();
lista = new ArrayList <String> () ;
initGUI() ;
setVisible(true); // da rivedere
}
private void initGUI()
{
setTitle("Inserimento Componente");
setLayout(new BorderLayout ());
this.setSize(400, 200) ;
//setVisible(true) ;
JPanel pan1 = new JPanel () ;
pan1.setLayout(new GridLayout(5 ,1 )) ;
getContentPane().add(pan1 , BorderLayout.CENTER) ;
JPanel panA = new JPanel () ;
panA.setLayout(new GridLayout (1 , 2)) ;
{
cat = new JTextField () ;
cat.getDocument().addDocumentListener(this) ;
cat.setEditable(true) ;
lCat = new JLabel () ;
lCat.setText("Categoria") ;
panA.add(lCat ) ;
panA.add(cat ) ;
}
JPanel panB = new JPanel () ;
panB.setLayout(new GridLayout (1 , 2)) ;
{
marca = new JTextField ();
marca.getDocument().addDocumentListener(this) ;
lMarca = new JLabel () ;
lMarca.setText("Marca") ;
panB.add(lMarca) ;
panB.add(marca ) ;
}
JPanel panC = new JPanel () ;
panC.setLayout(new GridLayout (1 , 2)) ;
{
model = new JTextField () ;
model.getDocument().addDocumentListener(this) ;
lModel = new JLabel () ;
lModel.setText("Model") ;
panC.add(lModel) ;
panC.add(model) ;
}
JPanel panD = new JPanel () ;
panD.setLayout(new GridLayout (1 , 2)) ;
{
prezzo = new JTextField () ;
prezzo.getDocument().addDocumentListener(this) ;
lPrezzo = new JLabel () ;
lPrezzo.setText("Fascia Prezzo") ;
panD.add(lPrezzo) ;
panD.add(prezzo ) ;
}
JPanel panE = new JPanel () ;
panE.setLayout(new FlowLayout ()) ;
{
ok = new JButton () ;
ok.setText("Ok") ;
ok.addActionListener(this) ;
annulla = new JButton () ;
annulla.setText("Annulla") ;
annulla.addActionListener(this) ;
panE.add(ok) ;
panE.add(annulla) ;
}
pan1.add(panA) ;
pan1.add(panB) ; // uso 4 FlowLayout
pan1.add(panC) ;
pan1.add(panD) ;
pan1.add(panE) ;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok)
{
lista.add(valueCat) ;
lista.add(valueMarca) ;
lista.add(valueModel) ;
lista.add(valuePrezzo) ;
// devo recuperare il testo scritto nelle textField e creare una serie d Stringhe e dopo un nuovo hardware
if (lista.size () != 0)
{
String cat = lista.get(0) ;
String marca = lista.get(1) ;
String model = lista.get (2) ;
String price = lista.get(3) ;
if (cat.contains ("SchedaVideo"))
{
setModalResultOk(); // setto il result a ok
try {
hardware = new SchedaVideo (Double.parseDouble(price) , cat , marca , model) ;
// salvo l istanza dell hardware in modo da poter fare l update a RunTime
MyCatalogoWriter writer = new MyCatalogoWriter () ; // qui effettivamente scrive su disco e aggiorna il catalogo
writer.Scrivi(hardware) ;
controller.setLastUpdate(hardware) ;
Close () ;
JOptionPane.showMessageDialog(null, "Componente inserito correttamente") ;
}
catch (IOException e1) {
System.out.println("errore" + e) ;
e1.printStackTrace();
}
} // fine else
else if ( cat.contains( "HardDisk" ))
{
hardware = new HardDisk (Double.parseDouble(price) , cat , marca , model) ;
controller.setLastUpdate(hardware) ;
MyCatalogoWriter write = new MyCatalogoWriter () ;
try {
write.Scrivi(hardware) ;
JOptionPane.showMessageDialog(null, "Componente inserito correttamente") ;
Close () ;
}
catch (IOException e1) {
System.out.println ("errore" + e) ;
}
} // fine else
else
{
throw new IllegalArgumentException ("errore hardware " + lista.get(0)) ;
}
} // fine if
} // fine pulsante ok
else if (e.getSource() == annulla)
{
Close () ; // se è annulla chiamo il metodo close () ;
}
}
private void setModalResultOk()
{
chooce = DialogResult.Ok ;
}
public DialogResult getModalResult()
{
return chooce ;
}
private void textFieldUpdated ()
{
try
{
valueCat = cat.getText () ;
valueMarca = marca.getText () ;
valueModel = model.getText () ;
valuePrezzo = prezzo.getText () ;
}
catch (Exception ex)
{
System.out.println ("errore " + ex) ;
}
}
@Override
public void insertUpdate(DocumentEvent e) {
textFieldUpdated () ;
}
@Override
public void removeUpdate(DocumentEvent e) {
textFieldUpdated () ;
}
@Override
public void changedUpdate(DocumentEvent e) {
textFieldUpdated () ;
}
public void Close() // metodo per chiudere la finestra di dialogo
{
this.setVisible(false);
if (getDefaultCloseOperation() == WindowConstants.EXIT_ON_CLOSE)
{
System.exit(0);
}
if (getDefaultCloseOperation() == WindowConstants.DISPOSE_ON_CLOSE)
{
dispose();
}
}
public void setController (MyController controller)
{
this.controller = controller ;
}
}
qui c'è la Finestra Main che premendo il pulsante inserisci chiede al controller di creare il Dialog
[/CODE]
Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == inserisci)
{
controller.inserisciHardware() ; // metodo che chiama il controller per fare un insert
}
[/CODE]