Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JOptionPane;
javax.swing.JTextField;
javax.swing.JButton;
import org.apache.commons.lang3.Validate;
import
import
import
import
java.awt.event.MouseAdapter;
java.awt.event.MouseEvent;
java.io.File;
java.io.IOException;
JFrame frmCreateSmartPhone;
JTextField textFieldSerialNumber;
JTextField textFieldManufacturerModel;
JTextField textFieldScreen;
JTextField textFieldSize;
JTextField textFieldCPU;
JTextField textFieldMemory;
JTextField textFieldWireless;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateSmphoneDb window = new CreateSmpho
neDb();
window.frmCreateSmartPhone.setVisible(tr
ue);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CreateSmphoneDb() {
initialize();
}
/**
textFieldCPU.setColumns(10);
textFieldCPU.setBounds(230, 193, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldCPU);
textFieldMemory = new JTextField();
textFieldMemory.setColumns(10);
textFieldMemory.setBounds(230, 232, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldMemory);
textFieldWireless = new JTextField();
textFieldWireless.setColumns(10);
textFieldWireless.setBounds(230, 275, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldWireless);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
btnSubmit_CLICK();
}
catch (Exception arg0){
System.out.println(arg0.toString());
}
}
});
btnSubmit.setBounds(107, 339, 138, 31);
frmCreateSmartPhone.getContentPane().add(btnSubmit);
}
private void btnSubmit_CLICK() {
//-------------- First, validate all the text fields
// If any problem, a dialog warning pops up to stop the program
boolean isValidated = validateTextFields();
if (! isValidated) return;
//-------------- All the text fields have been validated
FileIO fileIOHandler = new FileIO();
// Declare output file database: bookDatabase.txt
// MUST use \ to qualify '\' in the path of the file
File outputFile = new File("H:\\\\JAVA\\OUTPUTS\\SMPhoneDatabase
.txt");
String strSerialNumber = textFieldSerialNumber.getText();
long SerialNumber = Long.parseLong(strSerialNumber);
String strManufacturerModel = textFieldManufacturerModel.getText
();
String
String
String
String
String
strScreen = textFieldScreen.getText();
strSize = textFieldSize.getText();
strCPU = textFieldCPU.getText();
strMemory = textFieldMemory.getText();
strWireless = textFieldWireless.getText();
aSMPHONE.setScreen(strScreen);
aSMPHONE.setSize(strSize);
aSMPHONE.setCPU(strCPU);
aSMPHONE.setMemory(strMemory);
aSMPHONE.setWireless(strWireless);
// Get the string of book data
String strSMPHONEInfo = aSMPHONE.toString();
try {
// Write the string to the book database file
// by adding a line to the file
fileIOHandler.appendOneLineToFile(outputFile, strSMPHONE
Info);
}
catch (IOException ex){
ex.printStackTrace();
}
// After successfully inserting a new book record to the databas
e
// refresh all the text fields to prepare for the next record
textFieldSerialNumber.setText("");
textFieldManufacturerModel.setText("");
textFieldScreen.setText("");
textFieldSize.setText("");
textFieldCPU.setText("");
textFieldMemory.setText("");
textFieldWireless.setText("");
} // End of btnSubmit_CLICK
private boolean validateTextFields() {
boolean isValidated = true;
//----------- Validate ISBN text field
try{
Validate.notBlank(textFieldSerialNumber.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Serial Number must have a Numeric Value of 10 D
igits.");
textFieldSerialNumber.requestFocusInWindow(); // make it ready t
o enter the value
textFieldSerialNumber.selectAll(); // select all text in the tex
t field to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
// For ISBN, also need to verify the entered value is a valid numeric
try{
long tempLong = Long.parseLong(textFieldSerialNumber.getText());
}catch(Exception e){
try{
Validate.notBlank(textFieldCPU.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - CPU cannot be blank !!!.");
textFieldCPU.requestFocusInWindow(); // make it ready to enter t
he value
textFieldCPU.selectAll(); // select all text in the text field t
o delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Edition text field
try{
Validate.notBlank(textFieldMemory.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Memory cannot be blank !!!.");
textFieldMemory.requestFocusInWindow(); // make it ready to ente
r the value
textFieldMemory.selectAll(); // select all text in the text fiel
d to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Edition text field
try{
Validate.notBlank(textFieldWireless.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Wireless cannot be blank !!!.");
textFieldWireless.requestFocusInWindow(); // make it ready to en
ter the value
textFieldWireless.selectAll(); // select all text in the text fi
eld to delete it or to replace it
isValidated = false;
}
return (isValidated);
} // validateTextFields
}