import sale.*;
import data.*;
import data.ooimpl.*;
import data.stdforms.*;
import data.swing.*;
import data.events.*;
import users.*;
import java.util.*;
import javax.swing.*;
public class SeeVideoStockProcess extends SaleProcess
{
protected UIGate selectionGate;
protected Transition newVideoTransition;
protected Transition removeVideoTransition;
public SeeVideoStockProcess()
{
super ("SeeVideoProcess");
}
protected void setupMachine()
{
selectionGate = new UIGate(null, null);
final SingleTableFormSheet stfs =
SingleTableFormSheet.create("See videos in stock and edit Process",
(CountingStockImpl)Shop.getTheShop().getStock("Video-Countingstock"),
selectionGate,
getBasket(),
true,
new EditableVideoStockTED((CountingStockImpl)
Shop.getTheShop().getStock("Video-Countingstock"), getBasket()));
newVideoTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JPanel jTextPanel = new JPanel();
jTextPanel.setLayout(new BoxLayout(jTextPanel,
BoxLayout.Y_AXIS));
jTextPanel.add(new JLabel("Name"));
jTextPanel.add(jTextField1);
jTextPanel.add(new JLabel("Buy (in Pf)"));
jTextPanel.add(jTextField2);
jTextPanel.add(new JLabel("Sell (in Pf)"));
jTextPanel.add(jTextField3);
jTextPanel.add(new JLabel("Amount"));
jTextPanel.add(jTextField4);
JOptionPane.showMessageDialog (null,
jTextPanel,
"New Video-Cassette",
JOptionPane.QUESTION_MESSAGE);
Catalog videoCatalog =
Shop.getTheShop().getCatalog("Video-Catalog");
CountingStock videoCountingStock =
(CountingStock)Shop.getTheShop().getStock("Video-Countingstock");
String name = jTextField1.getText();
int buy = 0;
try {
buy = (new Integer(jTextField2.getText())).intValue();
}
catch (NumberFormatException ne) {}
int sell = 0;
try {
sell = (new Integer(jTextField3.getText())).intValue();
}
catch (NumberFormatException ne) {}
int amount = 0;
try {
amount = (new Integer(jTextField4.getText())).intValue();
}
catch (NumberFormatException ne) {}
if (sell > 0 && buy > 0 && amount > 0 && !name.equals("")) {
try {
videoCatalog.add(new VideoCassette(name, new QuoteValue
(new IntegerValue (buy),
new IntegerValue (sell))), null);
videoCountingStock.add(name, amount, null);
}
catch (DuplicateKeyException dke) {
JOptionPane.showMessageDialog(null,
"Element exists");
}
}
else {
JOptionPane.showMessageDialog(null,
"The given input was not correct!");
}
return selectionGate;
}
};
removeVideoTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
Object record = stfs.getSelectedRecord();
VideoCassette videoCassette =
(VideoCassette)((data.swing.CountingStockTableModel.Record)
record).getDescriptor();
int rented = 0;
try {
Iterator i = VideoMachine.getAllCustomer().iterator();
while (i.hasNext()) {
rented = rented +
((Customer)i.next()).getStoringStock().countItems(
videoCassette.getName(), null);
}
}
catch (NullPointerException npe) {
}
if (rented <= 0) {
try {
Shop.getTheShop().getCatalog("Video-Catalog").remove(
videoCassette, null);
}
catch (VetoException ve) {
ve.printStackTrace();
JOptionPane.showMessageDialog(null,
"The selected item can't be removed!");
}
}
else {
JOptionPane.showMessageDialog(null,
"There are still rented videos!");
}
return selectionGate;
}
};
stfs.addContentCreator(new FormSheetContentCreator()
{
protected void createFormSheetContent(FormSheet fs)
{
fs.removeAllButtons();
fs.addButton ("New", 100, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(newVideoTransition);
}
});
fs.addButton ("Remove", 101, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(removeVideoTransition);
}
});
fs.addButton ("Ok", 102, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(
GateChangeTransition.CHANGE_TO_COMMIT_GATE);
}
});
fs.addButton ("Cancel", 103, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
});
}
});
}
public Gate getInitialGate()
{
setupMachine();
return selectionGate;
}
public Gate getLogGate()
{
return getStopGate();
}
}