Скачиваний:
63
Добавлен:
15.06.2014
Размер:
15.5 Кб
Скачать
package dialogs;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import lists.DepartmentListModel;

import products.BaseProduct;
import tables.MyMainTableModel;

public class AddProductDialog {
//JDialog object
private JDialog addproductDialog;

//JLabel objects
private JLabel namejlabel;
private JLabel arrivaldatejlabel;
private JLabel makerjlabel;
private JLabel pricejlabel;
private JLabel typejlabel;
private JLabel shelflifejlabel;
private JLabel weightjlabel;
private JLabel numberjlabel;
private JLabel countryjlabel;
private JLabel makeraddressjlabel;
private JLabel makersitejlabel;
private JLabel makermailjlabel;
private JLabel makerphonejlabel;
private JLabel departmentjlabel;

//JTextField objects
private JTextField namejtextfield;
private JTextField arrivaldatejtextfield;
private JTextField makerjtextfield;
private JTextField pricejtextfield;
private JTextField typejtextfield;
private JTextField shelflifejtextfield;
private JTextField weightjtextfield;
private JTextField numberjtextfield;
private JTextField countryjtextfield;
private JTextField makeraddressjtextfield;
private JTextField makersitejtextfield;
private JTextField makermailjtextfield;
private JTextField makerphonejtextfield;
private JTextField departmentjtextfield;

//JButton objects
JButton cancelButton;
JButton okButton;

//base constructor
AddProductDialog() {

}

//constructor
public AddProductDialog(JFrame myapplication,String name,boolean var,MyMainTableModel tablemodel,DepartmentListModel listmodel) {
//create dialog for adding product
addproductDialog = new JDialog(myapplication,name,var);

//set size for dialog
addproductDialog.setSize(400,600);

//not to change the dialog window size
addproductDialog.setResizable(false);

//add components to pane
addComponentsToPane(addproductDialog.getContentPane(),tablemodel,listmodel);

//set visible for "Add product" dialog
addproductDialog.setVisible(true);
}

private void addComponentsToPane(Container pane,MyMainTableModel tablemodel,DepartmentListModel listmodel) {
pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));

//add for name
addProductNameJLabel(pane);
addProductNameJTextField(pane);
//add for arrival date
addProductArrivalDateJLabel(pane);
addProductArrivalDateJTextField(pane);
//add for maker
addProductMakerJLabel(pane);
addProductMakerJTextField(pane);
//add for price
addProductPriceJLabel(pane);
addProductPriceJTextField(pane);
//add for type
addProductTypeJLabel(pane);
addProductTypeJTextField(pane);
//add for shelflife
addProductShelfLifeJLabel(pane);
addProductShelfLifeJTextField(pane);
//add for weight
addProductWeightJLabel(pane);
addProductWeightJTextField(pane);
//add for number
addProductNumberJLabel(pane);
addProductNumberJTextField(pane);
//add for country
addProductCountryJLabel(pane);
addProductCountryJTextField(pane);
//add for makeraddress
addProductMakerAddressJLabel(pane);
addProductMakerAddressJTextField(pane);
//add for makersite
addProductMakerSiteJLabel(pane);
addProductMakerSiteJTextField(pane);
//add for makermail
addProductMakerMailJLabel(pane);
addProductMakerMailJTextField(pane);
//add for makerphone
addProductMakerPhoneJLabel(pane);
addProductMakerPhoneJTextField(pane);
//add for department
addProductDepartmentJLabel(pane);
addProductDepartmentJTextField(pane);

//add OK button
addOkButtonToAddProductDialog(pane,tablemodel,listmodel);

//add CANCEL button
addCancelButtonToAddProductDialog(pane);
}
//ADD JLABEL HANDLES METHODS
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

//add JLabel for Enter product name field
private void addProductNameJLabel(Container container) {
namejlabel = new JLabel("Enter name of product:");
namejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(namejlabel);
}

//add JLabel for Enter arrival date of field
private void addProductArrivalDateJLabel(Container container) {
arrivaldatejlabel = new JLabel("Enter arrival date of product:");
arrivaldatejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(arrivaldatejlabel);
}

//add JLabel for Enter maker of product
private void addProductMakerJLabel(Container container) {
makerjlabel = new JLabel("Enter maker of product:");
makerjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(makerjlabel);
}

//add JLabel for Enter product price
private void addProductPriceJLabel(Container container) {
pricejlabel = new JLabel("Enter price of product:");
pricejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(pricejlabel);
}

//add JLabel for Enter product type
private void addProductTypeJLabel(Container container) {
typejlabel = new JLabel("Enter type of product:");
typejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(typejlabel);
}

//add JLabel for Enter product shelflife
private void addProductShelfLifeJLabel(Container container) {
shelflifejlabel = new JLabel("Enter shelf life of product:");
shelflifejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(shelflifejlabel);
}

//add JLabel for Enter product weight
private void addProductWeightJLabel(Container container) {
weightjlabel = new JLabel("Enter weight of product:");
weightjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(weightjlabel);
}

//add JLabel for Enter product number
private void addProductNumberJLabel(Container container) {
numberjlabel = new JLabel("Enter number of product:");
numberjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(numberjlabel);
}

//add JLabel for Enter product country
private void addProductCountryJLabel(Container container) {
countryjlabel = new JLabel("Enter country of product:");
countryjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(countryjlabel);
}

//add JLabel for Enter maker address
private void addProductMakerAddressJLabel(Container container) {
makeraddressjlabel = new JLabel("Enter maker address:");
makeraddressjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(makeraddressjlabel);
}

//add JLabel for Enter maker site
private void addProductMakerSiteJLabel(Container container) {
makersitejlabel = new JLabel("Enter maker site:");
makersitejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(makersitejlabel);
}

//add JLabel for Enter maker mail
private void addProductMakerMailJLabel(Container container) {
makermailjlabel = new JLabel("Enter maker mail:");
makermailjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(makermailjlabel);
}

//add JLabel for Enter maker phone
private void addProductMakerPhoneJLabel(Container container) {
makerphonejlabel = new JLabel("Enter maker phone:");
makerphonejlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(makerphonejlabel);
}

//add JLabel for Enter department
private void addProductDepartmentJLabel(Container container) {
departmentjlabel = new JLabel("Enter department of product:");
departmentjlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(departmentjlabel);
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

//ADD JTEXTFIELD HANDLES METHODS
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

//add JTextField for Enter name of product
private void addProductNameJTextField(Container container) {
namejtextfield = new JTextField();
namejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
namejtextfield.setMaximumSize(new Dimension(400,20));
container.add(namejtextfield);
}

//add JTextField for Enter arrival date of product
private void addProductArrivalDateJTextField(Container container) {
arrivaldatejtextfield = new JTextField();
arrivaldatejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
arrivaldatejtextfield.setMaximumSize(new Dimension(400,20));
container.add(arrivaldatejtextfield);
}

//add JTextField for Enter maker of product
private void addProductMakerJTextField(Container container) {
makerjtextfield = new JTextField();
makerjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
makerjtextfield.setMaximumSize(new Dimension(400,20));
container.add(makerjtextfield);
}

//add JTextField for Enter price of product
private void addProductPriceJTextField(Container container) {
pricejtextfield = new JTextField();
pricejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
pricejtextfield.setMaximumSize(new Dimension(400,20));
container.add(pricejtextfield);
}

//add JTextField for Enter type pf product
private void addProductTypeJTextField(Container container) {
typejtextfield = new JTextField();
typejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
typejtextfield.setMaximumSize(new Dimension(400,20));
container.add(typejtextfield);
}

//add JTextField for Enter shelflife of product
private void addProductShelfLifeJTextField(Container container) {
shelflifejtextfield = new JTextField();
shelflifejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
shelflifejtextfield.setMaximumSize(new Dimension(400,20));
container.add(shelflifejtextfield);
}

//add JTextField for Enter weight of product
private void addProductWeightJTextField(Container container) {
weightjtextfield = new JTextField();
weightjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
weightjtextfield.setMaximumSize(new Dimension(400,20));
container.add(weightjtextfield);
}

//add JTextField for Enter number of products
private void addProductNumberJTextField(Container container) {
numberjtextfield = new JTextField();
numberjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
numberjtextfield.setMaximumSize(new Dimension(400,20));
container.add(numberjtextfield);
}

//add JTextField for Enter country of products
private void addProductCountryJTextField(Container container) {
countryjtextfield = new JTextField();
countryjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
countryjtextfield.setMaximumSize(new Dimension(400,20));
container.add(countryjtextfield);
}

//add JTextField for Enter makeraddress of product
private void addProductMakerAddressJTextField(Container container) {
makeraddressjtextfield = new JTextField();
makeraddressjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
makeraddressjtextfield.setMaximumSize(new Dimension(400,20));
container.add(makeraddressjtextfield);
}

//add JTextField for Enter makersite of product
private void addProductMakerSiteJTextField(Container container) {
makersitejtextfield = new JTextField();
makersitejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
makersitejtextfield.setMaximumSize(new Dimension(400,20));
container.add(makersitejtextfield);
}

//add JTextField for Enter makermail of product
private void addProductMakerMailJTextField(Container container) {
makermailjtextfield = new JTextField();
makermailjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
makermailjtextfield.setMaximumSize(new Dimension(400,20));
container.add(makermailjtextfield);
}

//add JTextField for Enter makerphone of product
private void addProductMakerPhoneJTextField(Container container) {
makerphonejtextfield = new JTextField();
makerphonejtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
makerphonejtextfield.setMaximumSize(new Dimension(400,20));
container.add(makerphonejtextfield);
}

//add JTextField for Enter department of product
private void addProductDepartmentJTextField(Container container) {
departmentjtextfield = new JTextField();
departmentjtextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
departmentjtextfield.setMaximumSize(new Dimension(400,20));
container.add(departmentjtextfield);
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

//ADD BUTTONS TO ADD PRODUCT DIALOG
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

//add Cancel button to Add Product Dialog
private void addCancelButtonToAddProductDialog(Container container) {
cancelButton = new JButton("Cancel");
cancelButton.setAlignmentX(Container.LEFT_ALIGNMENT);
cancelButton.setMaximumSize(new Dimension(400,45));
container.add(cancelButton);

//action listener for cancel button
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
addproductDialog.setVisible(false);
}
});
}

//add Ok button to Add Product Dialog
private void addOkButtonToAddProductDialog(Container container,final MyMainTableModel tablemodel,final DepartmentListModel listmodel) {
okButton = new JButton("OK");
okButton.setAlignmentX(Container.LEFT_ALIGNMENT);
okButton.setMaximumSize(new Dimension(400,45));
container.add(okButton);

//handler for ok button
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//create BaseProduct object for writing info into it
BaseProduct product = new BaseProduct();

//get text from fields and write it into Product object
product.setBaseProductName(namejtextfield.getText());
product.setBaseProductArrivalDate(arrivaldatejtextfield.getText());
product.setBaseProductMaker(makerjtextfield.getText());
product.setBaseProductPrice(pricejtextfield.getText());
product.setBaseProductType(typejtextfield.getText());
product.setBaseProductShelfLife(shelflifejtextfield.getText());
product.setBaseProductWeight(weightjtextfield.getText());
product.setBaseProductNumber(numberjtextfield.getText());
product.setBaseProductCountry(countryjtextfield.getText());
product.setBaseProductMakerAddress(makeraddressjtextfield.getText());
product.setBaseProductMakerSite(makersitejtextfield.getText());
product.setBaseProductMakerMail(makermailjtextfield.getText());
product.setBaseProductMakerPhone(makerphonejtextfield.getText());
product.setBaseProductDepartment(departmentjtextfield.getText());

//add to main table
tablemodel.addOneElement(product);
listmodel.updateAllNames(tablemodel.getDepartmentContainer().getDepartmentNamesForUnboughtProducts());
addproductDialog.setVisible(false);
}
});
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
}
Соседние файлы в папке dialogs