commit f3171781e347a14290a9042ec7b0a33ddbc6ce3f Author: Daniil Gentili Date: Sat Nov 4 16:35:38 2017 +0300 First public version diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..0f8ce1e --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project WarehouseClient. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..c78befc --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..143ae66 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=2973ab54 +build.xml.script.CRC32=905fd709 +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2973ab54 +nbproject/build-impl.xml.script.CRC32=9958223d +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..7174121 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,81 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=WarehouseClient +application.vendor=root +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/WarehouseClient.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.HTTP.jar=/root/NetBeansProjects/HTTPPayloads/dist/HTTP.jar +file.reference.WarehouseLibrary.jar=/root/NetBeansProjects/WarehouseLibrary/dist/WarehouseLibrary.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.WarehouseLibrary.jar}:\ + ${file.reference.HTTP.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=warehouseclient.WarehouseClient +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +project.license=gpl30 +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..6fc1cac --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,16 @@ + + + org.netbeans.modules.java.j2seproject + + + WarehouseClient + + + + + + + + + + diff --git a/src/ui/CheckoutButton.java b/src/ui/CheckoutButton.java new file mode 100644 index 0000000..da590d1 --- /dev/null +++ b/src/ui/CheckoutButton.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import Main.Client; +import Main.ClientException; +import Payloads.ServerException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import static javax.swing.JOptionPane.ERROR_MESSAGE; +import javax.swing.JPanel; + +/** + * + * @author root + */ +public class CheckoutButton extends JButton implements ActionListener { + private Client client; + public CheckoutButton(Client client) { + super("Checkout"); + this.client = client; + this.addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + try { + client.commit(); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null, ex.getMessage(), "Errore I/O", ERROR_MESSAGE); + } catch (ServerException ex) { + JOptionPane.showMessageDialog(null, String.format("%d: %s", ex.getPayload().getResponseCode(), ex.getPayload().getResponseDescription()), "Errore del server", ERROR_MESSAGE); + } + JOptionPane.showMessageDialog(null, "OK!"); + } +} diff --git a/src/ui/TableButton.java b/src/ui/TableButton.java new file mode 100644 index 0000000..c9cce8a --- /dev/null +++ b/src/ui/TableButton.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import Main.Client; +import Main.ClientException; +import Main.Container; +import Main.Warehouse; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JButton; +import javax.swing.JOptionPane; +import static javax.swing.JOptionPane.ERROR_MESSAGE; +import javax.swing.JTable; +import javax.swing.table.TableCellRenderer; + +/** + * + * @author root + */ +public class TableButton extends JButton { + private Boolean isWarehouse = true; + private Client client; + public TableButton(Boolean isWarehouse, Client client) { + super(); + this.isWarehouse = isWarehouse; + this.client = client; + this.setText(isWarehouse ? "Buy" : "Remove"); + } + + public void press(Integer productID) { + try { + if (isWarehouse) { + client.addToCart(productID); + } else { + client.removeFromCart(productID); + } + } catch (ClientException ex) { + JOptionPane.showMessageDialog(this, ex.getMessage(), "Errore", ERROR_MESSAGE); + } + } + +} diff --git a/src/ui/TableButtonRenderer.java b/src/ui/TableButtonRenderer.java new file mode 100644 index 0000000..8818a34 --- /dev/null +++ b/src/ui/TableButtonRenderer.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import java.awt.Component; +import javax.swing.JButton; +import javax.swing.JTable; +import javax.swing.table.TableCellRenderer; + +/** + * + * @author root + */ +public class TableButtonRenderer implements TableCellRenderer { + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + return (JButton) value; + } +} diff --git a/src/ui/TableModel.java b/src/ui/TableModel.java new file mode 100644 index 0000000..3894c4c --- /dev/null +++ b/src/ui/TableModel.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import Main.Client; +import Main.Container; +import Main.Warehouse; +import javax.swing.table.AbstractTableModel; + +/** + * + * @author root + */ +public class TableModel extends AbstractTableModel { + private String[] columnNames = new String [] {"ID", "Description", "Price", "Count", "Buy"}; + private final Container container; + private final TableButton button; + + TableModel(Client client, Container container) { + Boolean isWarehouse = container instanceof Warehouse; + this.columnNames[4] = isWarehouse ? "Buy" : "Remove"; + this.container = container; + this.button = new TableButton(isWarehouse, client); + } + public TableButton getButton() { + return button; + } + /* + Table model methods + */ + @Override + public int getRowCount() { + return container.getProductCount(); + } + + @Override + public int getColumnCount() { + return this.columnNames.length; + } + + + @Override + public String getColumnName(int col) { + return columnNames[col]; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + switch (columnIndex) { + case 0: + return container.getNthProductCollection(rowIndex).getID(); + case 1: + return container.getNthProductCollection(rowIndex).getDescription(); + case 2: + return container.getNthProductCollection(rowIndex).getPrice(); + case 3: + return container.getNthProductCollection(rowIndex).getCount(); + case 4: + return button; + default: + return null; + } + } +} diff --git a/src/ui/TableMouseListener.java b/src/ui/TableMouseListener.java new file mode 100644 index 0000000..2418234 --- /dev/null +++ b/src/ui/TableMouseListener.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import javax.swing.JTable; + +/** + * + * @author root + */ +public class TableMouseListener extends MouseAdapter { + private final JTable table; + + public TableMouseListener(JTable table) { + this.table = table; + } + + public void mouseClicked(MouseEvent e) { + int column = table.getColumnModel().getColumnIndexAtX(e.getX()); // get the coloum of the button + int row = e.getY() / table.getRowHeight(); //get the row of the button + System.out.println("got"); + /*Checking the row or column is valid or not*/ + if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) { + Object value = table.getValueAt(row, column); + if (value instanceof TableButton) { + ((TableButton) value).press((Integer) table.getValueAt(row, 0)); + table.getRootPane().repaint(); // Redraw all tables + } + } + } +} diff --git a/src/ui/TableScrollPane.java b/src/ui/TableScrollPane.java new file mode 100644 index 0000000..3a3fdb8 --- /dev/null +++ b/src/ui/TableScrollPane.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ui; + +import Main.Client; +import Main.Container; +import java.awt.Dimension; +import javax.swing.BorderFactory; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.border.TitledBorder; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumn; + +/** + * + * @author root + */ +public class TableScrollPane extends JScrollPane { + public TableScrollPane(Client client, Container container) { + super(); + final JTable table = new JTable(new TableModel(client, container)); + final TableCellRenderer renderer = new TableButtonRenderer(); + final TableMouseListener listener = new TableMouseListener(table); + table.setRowHeight(30); + TableColumn column; + for (int i = 0; i < table.getColumnCount(); i++) { + column = table.getColumnModel().getColumn(i); + switch (i) { + case 1: + column.setPreferredWidth(150); // second column is bigger + break; + case 4: + column.setPreferredWidth(30); // second column is bigger + column.setCellRenderer(renderer); + break; + default: + column.setPreferredWidth(1); + break; + } + } + table.setPreferredScrollableViewportSize(new Dimension(500, 70)); + table.setFillsViewportHeight(true); + table.addMouseListener(listener); + + this.setBorder(BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (), + container.getClass().getSimpleName(), + TitledBorder.CENTER, + TitledBorder.TOP)); + + this.setViewportView(table); + } +} diff --git a/src/warehouseclient/WarehouseClient.java b/src/warehouseclient/WarehouseClient.java new file mode 100644 index 0000000..272c4e4 --- /dev/null +++ b/src/warehouseclient/WarehouseClient.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2017 Daniil Gentili + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package warehouseclient; + +import Main.Client; +import java.awt.BorderLayout; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFrame; +import javax.swing.JPanel; +import ui.CheckoutButton; +import ui.TableScrollPane; +/** + * + * @author Studente + */ +public class WarehouseClient { +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + public static void createAndShowGUI(Client client) { + JFrame frame = new JFrame("Client"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JPanel panel = new JPanel(); + panel.add(new CheckoutButton(client), BorderLayout.CENTER); + + frame.getContentPane().add(new TableScrollPane(client, client.getWarehouse()),BorderLayout.PAGE_START); + frame.getContentPane().add(new TableScrollPane(client, client.getCart()),BorderLayout.CENTER ); + frame.getContentPane().add(panel, BorderLayout.PAGE_END); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.pack(); + frame.setVisible(true); + } + + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(WarehouseClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(WarehouseClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(WarehouseClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(WarehouseClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + java.awt.EventQueue.invokeLater(new Runnable() { + @Override + public void run() { + try { + createAndShowGUI(new Client("localhost", 9090)); + } catch (IOException ex) { + Logger.getLogger(WarehouseClient.class.getName()).log(Level.SEVERE, null, ex); + } + } + }); + } +}