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

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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import Products.Product;
import by.WorkingWithFiles.MainTableFileReader;
import by.WorkingWithFiles.MainTableFileWriter;

public class MagazineHelper
implements ActionListener//,TableModelListener
{
//create main frame of our application
JFrame myapplication;


//create ProductData object for storaging info about products
private ProductData productdata = new ProductData();

//create object for reading info from file
private MainTableFileReader maintablefilereader = new MainTableFileReader();
private MainTableFileWriter maintablefilewriter = new MainTableFileWriter();
//ADD PRODUCT DIALOG CODE BLOCK
///////////////////////////////////
///////////////////////////////////

//define dialog for adding products
private JDialog addproductDialog;
//buttons for adding product dialog
private JButton okButtonAddProductDialog;
private JButton cancelButtonAddProductDialog;
//edittext field for adding dialog
private JTextField enterproductnamefield;
private JTextField enterproductpricefield;
private JTextField enterproductarrivaldatefield;
private JTextField enterproductmakerfield;
private JTextField enterproductweightfield;
private JTextField enterproductbruttoweightfield;
private JTextField enterproductnettoweightfield;
private JTextField enterproducttypefield;
private JTextField enterproductrestrictiononagefield;
private JTextField enterproductdepartmentfield;
private JTextField enterproductshelflifefield;
private JTextField enterproductnumberfield;
//JLabels for adding product dialog
private JLabel enterproductnamelabel;
private JLabel enterproductpricelabel;
private JLabel enterproductarrivaldatelabel;
private JLabel enterproductmakerlabel;
private JLabel enterproductweightlabel;
private JLabel enterproductbruttoweightlabel;
private JLabel enterproductnettoweightlabel;
private JLabel enterproducttypelabel;
private JLabel enterproductrestrictiononagelabel;
private JLabel enterproductdepartmentlabel;
private JLabel enterproductshelflifelabel;
private JLabel enterproductnumberlabel;
///////////////////////////////////
///////////////////////////////////





//create MainTableModel object and put in it ProductData object with all information about products
MainTableModel maintablemodel;

//create table and set for it TableModel
JTable maintable;

//create constructor for our program
MagazineHelper()
{
//create new JFrame container
myapplication = new JFrame("Magazine Helper");

//set start frame size
myapplication.setSize(1280,700);

//when user close application
myapplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

//CREATE MAIN TABLE
///////////////

//read information from xml file and add it into ProductData object
maintablefilereader.readFromFile(productdata);

//add ProductData object to MainTableModel
maintablemodel = new MainTableModel(productdata);

//add to Table our MainTableModel object
maintable = new JTable(maintablemodel);

//add scroll and our table in our frame
myapplication.add(new JScrollPane(maintable));

///////////////

//CREATE MENU
///////////////

//create menu string
JMenuBar menubar = new JMenuBar();

//create menu "File"
JMenu menuFile = new JMenu("File");
//file includes pod-menus
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem saveasItem = new JMenuItem("Save as");
JMenuItem exitItem = new JMenuItem("Exit");
//including items into "File" menu
menuFile.add(openItem);
menuFile.add(saveItem);
menuFile.add(saveasItem);
menuFile.add(exitItem);

//create menu "Edit"
JMenu menuEdit = new JMenu("Edit");
//edit includes pod-menus
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
JMenuItem cutItem = new JMenuItem("Cut");
//including items into "Edit" menu
menuEdit.add(copyItem);
menuEdit.add(pasteItem);
menuEdit.add(cutItem);

//create menu "Magazine"
JMenu menuMagazine = new JMenu("Magazine");
//Magazine includes pod-menus
JMenuItem magazinesItem = new JMenuItem("Magazines");
JMenuItem addmagazineItem = new JMenuItem("Add magazine");
JMenuItem deletemagazineItem = new JMenuItem("Delete magazine");
JMenuItem searchmagazineItem = new JMenuItem("Search magazine");
//including items into "Magazine" menu
menuMagazine.add(magazinesItem);
menuMagazine.add(addmagazineItem);
menuMagazine.add(deletemagazineItem);
menuMagazine.add(searchmagazineItem);

//create menu "Department"
JMenu menuDepartment = new JMenu("Department");
//Department includes pod-menus
JMenuItem departmentsItem = new JMenuItem("Departments");
JMenuItem adddepartmentItem = new JMenuItem("Add department");
JMenuItem deletedepartmentItem = new JMenuItem("Delete department");
JMenuItem searchdepartmentItem = new JMenuItem("Search department");
//including items into "Department" menu
menuDepartment.add(departmentsItem);
menuDepartment.add(adddepartmentItem);
menuDepartment.add(deletedepartmentItem);
menuDepartment.add(searchdepartmentItem);

//create menu "Product"
JMenu menuProduct = new JMenu("Product");
//Product includes pod-menus
JMenuItem addproductItem = new JMenuItem("Add product");
JMenuItem deleteproductItem = new JMenuItem("Delete product");
JMenuItem searchproductItem = new JMenuItem("Search element");
//including items into "Product" menu
menuProduct.add(addproductItem);
menuProduct.add(deleteproductItem);
menuProduct.add(searchproductItem);

//create menu "Sorting"
JMenu menuSorting = new JMenu("Sorting");
//Sorting includes pod-menus
JMenuItem sortbynameItem = new JMenuItem("Sort by name");
JMenuItem sortbypriceItem = new JMenuItem("Sort by name");
JMenuItem sortbyarrivaldateItem = new JMenuItem("Sort by arrival date");
JMenuItem sortbymakerItem = new JMenuItem("Sort by maker");
JMenuItem sortbyweightItem = new JMenuItem("Sort by weight");
JMenuItem sortbytypeItem = new JMenuItem("Sort by type");
JMenuItem sortbyrestrictiononageItem = new JMenuItem("Sort by restriction on age");
JMenuItem sortbydepartmentItem = new JMenuItem("Sort by department");
JMenuItem sortbyshelflifeItem = new JMenuItem("Sort by shelf life");
JMenuItem sortbynumberItem = new JMenuItem("Sort by number");
//including items into "Sorting" menu
menuSorting.add(sortbynameItem);
menuSorting.add(sortbypriceItem);
menuSorting.add(sortbyarrivaldateItem);
menuSorting.add(sortbymakerItem);
menuSorting.add(sortbyweightItem);
menuSorting.add(sortbytypeItem);
menuSorting.add(sortbyrestrictiononageItem);
menuSorting.add(sortbydepartmentItem);
menuSorting.add(sortbyshelflifeItem);
menuSorting.add(sortbynumberItem);

//create menu "Buy"
JMenu menuBuy = new JMenu("Buy");
//Buy includes pod-menus
JMenuItem buyItem = new JMenuItem("Buy");
//including items into "Buy" menu
menuBuy.add(buyItem);

//create menu "Profit"
JMenu menuProfit = new JMenu("Profit");
//Profit includes pod-menus
JMenuItem getprofitbymagazine = new JMenuItem("Get profit by magazine");
JMenuItem getprofitbydepartment = new JMenuItem("Get profit by department");
JMenuItem getalistofitemssold = new JMenuItem("Get a list of items sold");
//including items into "Profit" menu
menuProfit.add(getprofitbymagazine);
menuProfit.add(getprofitbydepartment);
menuProfit.add(getalistofitemssold);

//create menu "Options"
JMenu menuOptions = new JMenu("Options");
//Options includes pod-menus
JMenuItem windowoptionsItem = new JMenuItem("Window options");
JMenuItem languageoptionsItem = new JMenuItem("Language options");
JMenuItem propertiesoptionsItem = new JMenuItem("Properties options");
//including items into "Options" menu
menuOptions.add(windowoptionsItem);
menuOptions.add(languageoptionsItem);
menuOptions.add(propertiesoptionsItem);

//create menu "Help"
JMenu menuHelp = new JMenu("Help");
//Help includes pod-menus
JMenuItem helpItem = new JMenuItem("Help");
JMenuItem aboutItem = new JMenuItem("About");
//including items into "Help" menu
menuHelp.add(helpItem);
menuHelp.add(aboutItem);

//link menus into menu bar
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuMagazine);
menubar.add(menuDepartment);
menubar.add(menuProduct);
menubar.add(menuSorting);
menubar.add(menuBuy);
menubar.add(menuProfit);
menubar.add(menuOptions);
menubar.add(menuHelp);

///////////////

//BINDING AN EVENT HANDLER TO THE MENU
//////////////////////////////////////

//for exit from program
exitItem.addActionListener(this);
//for adding product into our product storage
addproductItem.addActionListener(this);

//////////////////////////////////////


//include menu in frame myapplication
myapplication.setJMenuBar(menubar);

//set visible our application
myapplication.setVisible(true);
}

//event handling actions for menu items
public void actionPerformed(ActionEvent ae)
{
//get string-comand if menu item was pushed
String pushedItem = ae.getActionCommand();

//if was pushed exit item
if(pushedItem.equals("Exit"))
System.exit(0);

//if was pushed add product item
if(pushedItem.equals("Add product"))
{
//create dialog for adding product
addproductDialog = new JDialog(myapplication,"Add product",true);

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

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

addComponentsToPane(addproductDialog.getContentPane());

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

//METHODS FOR ADDING ELEMENTS INTO ADD PRODUCT DIALOG
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

//add to to dialog all elements
public void addComponentsToPane(Container pane)
{
pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));

//add for name
addProductNameJLabelToAddProductDialog(pane);
addProductNameJTextFieldToAddProductDialog(pane);

//add for price
addProductPriceNameJLabelToAddProductDialog(pane);
addProductPriceJTextFieldToAddProductDialog(pane);

//add for arrival date
addProductArrivalDateJLabelToAddProductDialog(pane);
addProductArrivalDateJTextFieldToAddProductDialog(pane);

//add for maker
addProductMakerJLabelToAddProductDialog(pane);
addProductMakerJTextFieldToAddProductDialog(pane);

//add for weight
addProductWeightJLabelToAddProductDialog(pane);
addProductWeightJTextFieldToAddProductDialog(pane);

//add for brutto weight
addProductBruttoWeightJLabelToAddProductDialog(pane);
addProductBruttoWeightJTextFieldToAddProductDialog(pane);

//add for netto weight
addProductNettoWeightJLabelToAddProductDialog(pane);
addProductNettoWeightJTextFieldToAddProductDialog(pane);

//add for type
addProductTypeJLabelToAddDialogProductDialog(pane);
addProductTypeJTextFieldToAddProductDialog(pane);

//add for restriction on age
addProductRestrictionOnAgeJLabelToAddProductDialog(pane);
addProductTypeJTextFieldToAddProductDialog(pane);

//add for department
addProductDepartmentJLabelToAddProductDialog(pane);
addProductDepartmentJTextFieldToAddProductDialog(pane);

//add for shelf life
addProductShelfLifeJLabelToAddProductDialog(pane);
addProductShelfLifeJTextFieldToAddProductDialog(pane);

//add for number
addProductNumberJLabelToAddProductDialog(pane);
addProductNumberJTextFieldToAddProductDialog(pane);

//add OK button
addOkButtonToAddProductDialog(pane);

//add CANCEL button
addCancelButtonToAddProductDialog(pane);
}

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

//add JLabel for Enter product price field
public void addProductPriceNameJLabelToAddProductDialog(Container container)
{
enterproductpricelabel = new JLabel("Enter price of product:");
enterproductpricelabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(enterproductpricelabel);
}

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

//add JLabel for Enter product maker field
public void addProductMakerJLabelToAddProductDialog(Container container)
{
enterproductmakerlabel = new JLabel("Enter maker of product:");
enterproductmakerlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(enterproductmakerlabel);
}

//add JLabel for Enter product weight field
public void addProductWeightJLabelToAddProductDialog(Container container)
{
enterproductweightlabel = new JLabel("Enter weigth of product:");
enterproductweightlabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductweightlabel);
}

//add JLabel for Enter product bruttoweight field
public void addProductBruttoWeightJLabelToAddProductDialog(Container container)
{
enterproductbruttoweightlabel = new JLabel("Enter brutto weight of product:");
enterproductbruttoweightlabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductbruttoweightlabel);
}

//add JLabel for Enter product nettoweight field
public void addProductNettoWeightJLabelToAddProductDialog(Container container)
{
enterproductnettoweightlabel = new JLabel("Enter netto weight of product:");
enterproductnettoweightlabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductnettoweightlabel);
}

//add JLabel for Enter product type field
public void addProductTypeJLabelToAddDialogProductDialog(Container container)
{
enterproducttypelabel = new JLabel("Enter type of product:");
enterproducttypelabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproducttypelabel);
}

//add JLabel for Enter product restriction on age field
public void addProductRestrictionOnAgeJLabelToAddProductDialog(Container container)
{
enterproductrestrictiononagelabel = new JLabel("Enter restriction on age:");
enterproductrestrictiononagelabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductrestrictiononagelabel);
}

//add JLabel for Enter product department field
public void addProductDepartmentJLabelToAddProductDialog(Container container)
{
enterproductdepartmentlabel = new JLabel("Enter department of product:");
enterproductdepartmentlabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductdepartmentlabel);
}

//add JLabel for Enter product shelf life field
public void addProductShelfLifeJLabelToAddProductDialog(Container container)
{
enterproductshelflifelabel = new JLabel("Enter shelflife of product:");
enterproductshelflifelabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductshelflifelabel);
}

//add JLabel for Enter product number field
public void addProductNumberJLabelToAddProductDialog(Container container)
{
enterproductnumberlabel = new JLabel("Enter number of product:");
enterproductnumberlabel.setAlignmentX(Container.LEFT_ALIGNMENT);
container.add(enterproductnumberlabel);
}

//add JTextField for Product name entering
public void addProductNameJTextFieldToAddProductDialog(Container container)
{
enterproductnamefield = new JTextField();
enterproductnamefield.setAlignmentX(Component.LEFT_ALIGNMENT);
enterproductnamefield.setMaximumSize(new Dimension(400,20));
container.add(enterproductnamefield);
}

//add JTextField for Product price entering
public void addProductPriceJTextFieldToAddProductDialog(Container container)
{
enterproductpricefield = new JTextField();
enterproductpricefield.setAlignmentX(Component.LEFT_ALIGNMENT);
enterproductpricefield.setMaximumSize(new Dimension(400,20));
container.add(enterproductpricefield);
}

//add JTextField for Product arrival date entering
public void addProductArrivalDateJTextFieldToAddProductDialog(Container container)
{
enterproductarrivaldatefield = new JTextField();
enterproductarrivaldatefield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductarrivaldatefield.setMaximumSize(new Dimension(400,20));
container.add(enterproductarrivaldatefield);

}

//add JTextField for Product maker entering
public void addProductMakerJTextFieldToAddProductDialog(Container container)
{
enterproductmakerfield = new JTextField();
enterproductmakerfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductmakerfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductmakerfield);
}

//add JTextField for Product weight field
public void addProductWeightJTextFieldToAddProductDialog(Container container)
{
enterproductweightfield = new JTextField();
enterproductweightfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductweightfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductweightfield);
}

//add JTextField for Product brutto weight field
public void addProductBruttoWeightJTextFieldToAddProductDialog(Container container)
{
enterproductbruttoweightfield = new JTextField();
enterproductbruttoweightfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductbruttoweightfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductbruttoweightfield);
}

//add JTextField for Product netto weight field
public void addProductNettoWeightJTextFieldToAddProductDialog(Container container)
{
enterproductnettoweightfield = new JTextField();
enterproductnettoweightfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductnettoweightfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductnettoweightfield);
}

//add JTextField for Product type field
public void addProductTypeJTextFieldToAddProductDialog(Container container)
{
enterproducttypefield = new JTextField();
enterproducttypefield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproducttypefield.setMaximumSize(new Dimension(400,20));
container.add(enterproducttypefield);
}

//add JTextField for Product restriction on age field
public void addProductRestrictionOnAgeJTextFieldToAddProductDialog(Container container)
{
enterproductrestrictiononagefield = new JTextField();
enterproductrestrictiononagefield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductrestrictiononagefield.setMaximumSize(new Dimension(400,20));
container.add(enterproductrestrictiononagefield);
}

//add JTextField for Product department field
public void addProductDepartmentJTextFieldToAddProductDialog(Container container)
{
enterproductdepartmentfield = new JTextField();
enterproductdepartmentfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductdepartmentfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductdepartmentfield);
}

//add JTextField for Product shelf life field
public void addProductShelfLifeJTextFieldToAddProductDialog(Container container)
{
enterproductshelflifefield = new JTextField();
enterproductshelflifefield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductshelflifefield.setMaximumSize(new Dimension(400,20));
container.add(enterproductshelflifefield);
}

//add JTextField for Product number field
public void addProductNumberJTextFieldToAddProductDialog(Container container)
{
enterproductnumberfield = new JTextField();
enterproductnumberfield.setAlignmentX(Container.LEFT_ALIGNMENT);
enterproductnumberfield.setMaximumSize(new Dimension(400,20));
container.add(enterproductnumberfield);
}

//add OK button to Add Product Dialog
public void addOkButtonToAddProductDialog(Container container)
{
okButtonAddProductDialog = new JButton("OK");
okButtonAddProductDialog.setAlignmentX(Container.LEFT_ALIGNMENT);
okButtonAddProductDialog.setMaximumSize(new Dimension(400,45));
container.add(okButtonAddProductDialog);

//handler for ok button
okButtonAddProductDialog.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent le)
{
//create Product object for writing into it info from JTextFields
Product input_product = new Product();

//get text from fields and write it into Product object
input_product.setProductName(enterproductnamefield.getText());
input_product.setProductPrice(enterproductpricefield.getText());
input_product.setProductArrivalDate(enterproductarrivaldatefield.getText());
input_product.setProductMaker(enterproductmakerfield.getText());
input_product.setProductWeight(enterproductweightfield.getText());
input_product.setProductBruttoWeight(enterproductbruttoweightfield.getText());
input_product.setProductNettoWeight(enterproductnettoweightfield.getText());
input_product.setProductType(enterproducttypefield.getText());
input_product.setProductDepartment(enterproductdepartmentfield.getText());
input_product.setProductShelfLife(enterproductshelflifefield.getText());
input_product.setProductNumber(enterproductnumberfield.getText());
//input_product.setProductRestrictionOnAge(enterproductrestrictiononagefield.getText());
//доделать тут по возрасту ошибка вылетает
//add Product object into ProductData object
productdata.addElement(input_product);

//write all info into xml file
maintablefilewriter.writeToFile(productdata);

maintablemodel.setProductData(productdata);



//maintablemodel.s
maintable.setModel(maintablemodel);


//set unvisible Add Product Dialog
addproductDialog.setVisible(false);
}
});
}

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

//handler for cancel button
cancelButtonAddProductDialog.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent le)
{
addproductDialog.setVisible(false);
}
});
}

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////


public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new MagazineHelper();
}
});
}

//i add this method that was overrided
/*
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model
}*/

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