001 package market.stdform;
002
003 import java.awt.Dimension;
004 import java.awt.GridBagConstraints;
005 import java.awt.GridBagLayout;
006 import java.awt.event.ActionListener;
007
008 import javax.swing.JButton;
009 import javax.swing.JPanel;
010
011 import market.SMarket;
012 import market.event.MarketEventAdapter;
013 import sale.Action;
014 import sale.ActionActionListener;
015 import sale.FormSheet;
016 import sale.FormSheetContentCreator;
017 import sale.SaleProcess;
018 import sale.SalesPoint;
019
020 /**
021 * This FormSheet is used by the manager to open and close the market, announce the leisure-time
022 * and change the date to the next day.
023 */
024 public class FSManagerOpenClose extends FormSheet {
025
026 JButton jb = new JButton();
027 private static String OPEN = "Feierabend ankündigen";
028 private static String TOBECLOSED = "Ankündigung zurücknehmen";
029
030 public FSManagerOpenClose() {
031 super("Schließen", new FormSheetContentCreator() {public void createFormSheetContent(final FormSheet fs) {
032 fs.setComponent(fs.getComponent());
033 }}, false);
034 this.addContentCreator(new FormSheetContentCreator() {public void createFormSheetContent(final FormSheet fs) {
035 //define components
036 JPanel jpMain = new JPanel();
037 JPanel jpBigButton = new JPanel();
038 GridBagConstraints c = new GridBagConstraints();
039 GridBagLayout gridbag = new GridBagLayout();
040 //add components
041 jpMain.setLayout(gridbag);
042 c.anchor = GridBagConstraints.CENTER;
043 gridbag.setConstraints(jpBigButton, c);
044 jb.setText(SMarket.isToBeClosed() ? TOBECLOSED : OPEN);
045 jb.setVisible(SMarket.isOpen());
046 jb.setPreferredSize(new Dimension(250, 100));
047 //add action listener if there is none yet
048 //(checking for action listeners prevents from adding them twice
049 //(This would happen when persistence file is loaded))
050 if (jb.getListeners(ActionListener.class).length == 0) {
051 jb.addActionListener(new ActionActionListener(fs, new Action() {
052 public void doAction(SaleProcess p, SalesPoint sp) {
053 if (SMarket.isToBeClosed()) {
054 SMarket.getTheMarket().setOpen(0);
055 SMarket.fireMarketNotClosing();
056 } else {
057 SMarket.getTheMarket().setOpen(1);
058 }
059 }
060 }));
061 }
062 jpBigButton.add(jb);
063 jpMain.add(jpBigButton);
064 fs.setComponent(jpMain);
065 removeAllButtons();
066 fs.addButton("Markt öffnen", ButtonIDs.BTN_START, null);
067 fs.addButton("Markt schließen", ButtonIDs.BTN_END, null);
068 fs.addButton("Zum nächsten Tag", ButtonIDs.BTN_NEXT, null);
069 fs.getButton(ButtonIDs.BTN_START).setEnabled(SMarket.hasTimeAdvanced());
070 fs.getButton(ButtonIDs.BTN_END).setEnabled(SMarket.isOpen());
071 fs.getButton(ButtonIDs.BTN_NEXT).setVisible(!SMarket.isOpen());
072 }});
073 SMarket.addEventListener(new MarketEventAdapter() {
074 public void notifyOnMarketClosing() {
075 jb.setText(TOBECLOSED);
076 };
077
078 public void notifyOnMarketNotClosing() {
079 jb.setText(OPEN);
080 }
081
082 public void marketClosed() {
083 jb.setEnabled(false);
084 jb.setVisible(false);
085 getButton(ButtonIDs.BTN_END).setEnabled(false);
086 getButton(ButtonIDs.BTN_NEXT).setVisible(true);
087 }
088
089 public void marketOpened() {
090 jb.setEnabled(true);
091 jb.setVisible(true);
092 jb.setText("Feierabend ankündigen");
093 getButton(ButtonIDs.BTN_START).setEnabled(false);
094 getButton(ButtonIDs.BTN_END).setEnabled(true);
095 getButton(ButtonIDs.BTN_NEXT).setVisible(false);
096 }
097
098 public void timeAdvanced() {
099 getButton(ButtonIDs.BTN_START).setEnabled(true);
100 }
101
102 public void offerEmpty(String s, int who) {
103 }
104
105 public void workerInformationChanged() {
106 }
107 });
108 }
109 }
110