import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class InventoryGUI extends JFrame {
private static final long serialVersionUID = 1L;
private static final int NAVIGATION_MODE = 0;
private static final int ADDITION_MODE = 1;
private static final int MODIFICATION_MODE = 2;
private int currentMode;
JTextField itemNumber = new JTextField();
JTextField name = new JTextField();
JTextField unitsInStock = new JTextField();
JTextField unitPrice = new JTextField();
JTextField valueOfInventory = new JTextField();
JTextField restockingFee = new JTextField();
JTextField imageRes = new JTextField();
JButton next = new JButton("Next >");
JButton prior = new JButton("< Prior");
JButton first = new JButton("<< First");
JButton last = new JButton("Last >>");
JButton add = new JButton("Add");
JButton delete = new JButton("Delete");
JButton modify = new JButton("Modify");
JButton saveOrApply = new JButton("Save");
JButton searchOrCancel = new JButton("Search");
JTextField valueOfEntireInventory = new JTextField();
private Inventory inventory;
private int currentIndex = 0;
public InventoryGUI(Inventory inventory) {
super();
this.inventory = inventory;
setTitle("Inventory GUI - Part 6");
setBounds(0, 0, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGUI();
pack();
setLocationRelativeTo(null); // center window
first.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showFirst();
}
});
prior.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showPrior();
}
});
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showNext();
}
});
last.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showLast();
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addItem();
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteItem();
}
});
modify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modifyItem();
}
});
saveOrApply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (getCurrentMode() == NAVIGATION_MODE) {
saveInventory();
} else {
apply();
}
}
});
searchOrCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (getCurrentMode() == NAVIGATION_MODE) {
searchInventory();
} else {
cancel();
}
}
});
updateGUI();
}
protected void addItem() {
clearFields();
Integer newItemNumber = inventory.getMaxItemNumber() + 1;
itemNumber.setText(newItemNumber.toString());
name.requestFocus();
setCurrentMode(ADDITION_MODE);
}
protected void apply() {
int newItem = Integer.valueOf(itemNumber.getText());
String newName;
if ("".equals(name.getText().trim())) {
showErrorMessage("Please fill the Item Name.");
name.requestFocus();
return;
} else {
newName = name.getText();
}
int newUnits;
try {
newUnits = Integer.valueOf(unitsInStock.getText());
} catch (Exception e) {
showErrorMessage("Invalid value for the field Units In Stock.");
unitsInStock.requestFocus();
return;
}
double newUnitPrice;
try {
newUnitPrice = Double.parseDouble(unitPrice.getText());
} catch (Exception e) {
showErrorMessage("Invalid value for the field Unit Price.");
unitPrice.requestFocus();
return;
}
double newImageRes;
try {
newImageRes = Double.parseDouble(imageRes.getText());
} catch (Exception e) {
showErrorMessage("Invalid value for the field Image Resolution.");
imageRes.requestFocus();
return;
}
if (currentMode == ADDITION_MODE) {
DigitalFeature df = new DigitalFeature(newItem, newName, newUnits,
newUnitPrice, newImageRes);
inventory.addItem(df);
currentIndex = inventory.getItemCount() - 1;
} else if (currentMode == MODIFICATION_MODE) {
DigitalFeature df = inventory.getItem(currentIndex);
df.setName(newName);
df.setUnitPrice(newUnitPrice);
df.setImageRes(newImageRes);
df.setUnitsInStock(newUnits);
}
setCurrentMode(NAVIGATION_MODE);
updateGUI();
}
protected void cancel() {
setCurrentMode(NAVIGATION_MODE);
updateGUI();
}
private void clearFields() {
itemNumber.setText("");
name.setText("");
unitsInStock.setText("");
unitPrice.setText("");
valueOfInventory.setText("");
restockingFee.setText("");
imageRes.setText("");
}
private void createGUI() {
JPanel itemPanel = new JPanel(new GridLayout(0, 4, 5, 5));
itemPanel.add(new JLabel("Item Number:"));
itemPanel.add(itemNumber);
itemNumber.setEditable(false);
itemPanel.add(new JLabel("Item Name:"));
itemPanel.add(name);
itemPanel.add(new JLabel("Units In Stock: $"));
itemPanel.add(unitsInStock);
itemPanel.add(new JLabel("Unit Price: $"));
itemPanel.add(unitPrice);
itemPanel.add(new JLabel("Value of Inventory: $"));
itemPanel.add(valueOfInventory);
valueOfInventory.setEditable(false);
itemPanel.add(new JLabel("Restocking Fee: $"));
itemPanel.add(restockingFee);
restockingFee.setEditable(false);
itemPanel.add(new JLabel("Image Resolution:"));
itemPanel.add(imageRes);
itemPanel.add(new JLabel("Value of Entire Inventory: $"));
itemPanel.add(valueOfEntireInventory);
valueOfEntireInventory.setEditable(false);
JPanel inventoryPanel = new JPanel(new BorderLayout(5, 5));
inventoryPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
inventoryPanel.add(new LogoPanel(getWidth(), 80), BorderLayout.NORTH);
inventoryPanel.add(itemPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new GridLayout(2, 1, 5, 5));
JPanel navigationPanel = new JPanel(new GridLayout(1, 7, 5, 5));
navigationPanel.add(first);
navigationPanel.add(prior);
navigationPanel.add(add);
navigationPanel.add(delete);
navigationPanel.add(modify);
navigationPanel.add(next);
navigationPanel.add(last);
bottomPanel.add(navigationPanel);
JPanel extraPanel = new JPanel(new GridLayout(0, 2, 5, 5));
extraPanel.add(saveOrApply);
extraPanel.add(searchOrCancel);
bottomPanel.add(extraPanel);
inventoryPanel.add(bottomPanel, BorderLayout.SOUTH);
setContentPane(inventoryPanel);
setCurrentMode(NAVIGATION_MODE);
}
protected void deleteItem() {
if (isInventoryEmpty()) {
return;
}
if (JOptionPane.showConfirmDialog(this,
"Do you really want to delete this item?", "Confirmation",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
inventory.deleteItem(currentIndex);
currentIndex = inventory.getItemCount() - 1;
updateGUI();
}
}
public int getCurrentMode() {
return currentMode;
}
private boolean isInventoryEmpty() {
if (inventory.getItemCount() == 0) {
JOptionPane.showMessageDialog(this, "The Inventory is Empty!");
return true;
}
return false;
}
protected void modifyItem() {
if (isInventoryEmpty()) {
return;
}
setCurrentMode(MODIFICATION_MODE);
}
protected void saveInventory() {
if (isInventoryEmpty()) {
return;
}
String[] items = inventory.serialize();
File file;
file = new File("c:\\data");
if (!file.exists()) {
file.mkdir();
}
file = new File("c:\\data\\inventory.dat");
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
} catch (IOException e) {
showErrorMessage("Failed to create the Inventory file.");
return;
}
try {
FileWriter fw = new FileWriter(file);
for (int i = 0; i < items.length; i++) {
fw.write(items[i] + "\n");
}
fw.close();
JOptionPane.showMessageDialog(this, "File successfully saved!");
} catch (IOException e) {
showErrorMessage("Failed to write the Inventory file.");
}
}
protected void searchInventory() {
if (isInventoryEmpty()) {
return;
}
String itemName = JOptionPane.showInputDialog(this,
"Enter the Item Name", "Search Item",
JOptionPane.QUESTION_MESSAGE);
if (itemName != null) {
int item = inventory.getItemByName(itemName);
if (item != -1) {
currentIndex = item;
updateGUI();
} else {
showErrorMessage(String.format("Item \"%s\" not found.",
itemName));
}
}
}
public void setCurrentMode(int currentMode) {
this.currentMode = currentMode;
switch (currentMode) {
case ADDITION_MODE:
case MODIFICATION_MODE:
first.setEnabled(false);
prior.setEnabled(false);
add.setEnabled(false);
modify.setEnabled(false);
delete.setEnabled(false);
next.setEnabled(false);
last.setEnabled(false);
saveOrApply.setText("Apply");
searchOrCancel.setText("Cancel");
setFieldsEditable(true);
break;
case NAVIGATION_MODE:
first.setEnabled(true);
prior.setEnabled(true);
add.setEnabled(true);
modify.setEnabled(true);
delete.setEnabled(true);
next.setEnabled(true);
last.setEnabled(true);
saveOrApply.setText("Save");
searchOrCancel.setText("Search");
setFieldsEditable(false);
break;
}
}
protected void setFieldsEditable(boolean editable) {
name.setEditable(editable);
unitsInStock.setEditable(editable);
unitPrice.setEditable(editable);
imageRes.setEditable(editable);
}
private void showErrorMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
}
protected void showFirst() {
if (isInventoryEmpty()) {
return;
}
currentIndex = 0;
updateGUI();
}
protected void showLast() {
if (isInventoryEmpty()) {
return;
}
currentIndex = inventory.getItemCount() - 1;
updateGUI();
}
private void showNext() {
if (isInventoryEmpty()) {
return;
}
if (currentIndex < inventory.getItemCount() - 1) {
currentIndex++;
updateGUI();
} else {
showFirst();
}
}
private void showPrior() {
if (isInventoryEmpty()) {
return;
}
if (currentIndex > 0) {
currentIndex--;
updateGUI();
} else {
showLast();
}
}
public void updateGUI() {
if (currentIndex != -1) {
DigitalFeature item = inventory.getItem(currentIndex);
itemNumber.setText(String.valueOf(item.getItemNumber()));
name.setText(item.getName());
unitsInStock.setText(String.valueOf(item.getUnitsInStock()));
unitPrice.setText(String.format("%.2f", item.getUnitPrice()));
valueOfInventory.setText(String.format("%.2f", item
.getValueOfInventory()));
restockingFee.setText(String
.format("%.2f", item.getRestockingFee()));
} else {
clearFields();
}
valueOfEntireInventory.setText(String.format("%.2f", inventory
.getValue()));
}
}