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 /** 027 * ID for serialization. 028 */ 029 private static final long serialVersionUID = 4286324234183400617L; 030 031 JButton jb = new JButton(); 032 private static String OPEN = "Feierabend ankündigen"; 033 private static String TOBECLOSED = "Ankündigung zurücknehmen"; 034 035 public FSManagerOpenClose() { 036 super("Schließen", new FormSheetContentCreator() { 037 private static final long serialVersionUID = -4684305453802863083L; 038 public void createFormSheetContent(final FormSheet fs) { 039 fs.setComponent(fs.getComponent()); 040 } 041 }, false); 042 this.addContentCreator(new FormSheetContentCreator() { 043 private static final long serialVersionUID = -3650582914895465285L; 044 public void createFormSheetContent(final FormSheet fs) { 045 //define components 046 JPanel jpMain = new JPanel(); 047 JPanel jpBigButton = new JPanel(); 048 GridBagConstraints c = new GridBagConstraints(); 049 GridBagLayout gridbag = new GridBagLayout(); 050 //add components 051 jpMain.setLayout(gridbag); 052 c.anchor = GridBagConstraints.CENTER; 053 gridbag.setConstraints(jpBigButton, c); 054 jb.setText(SMarket.isToBeClosed() ? TOBECLOSED : OPEN); 055 jb.setVisible(SMarket.isOpen()); 056 jb.setPreferredSize(new Dimension(250, 100)); 057 //add action listener if there is none yet 058 //(checking for action listeners prevents from adding them twice 059 //(This would happen when persistence file is loaded)) 060 if (jb.getListeners(ActionListener.class).length == 0) { 061 jb.addActionListener(new ActionActionListener(fs, new Action() { 062 private static final long serialVersionUID = -896860110835567930L; 063 public void doAction(SaleProcess p, SalesPoint sp) { 064 if (SMarket.isToBeClosed()) { 065 SMarket.getTheMarket().setOpen(0); 066 SMarket.fireMarketNotClosing(); 067 } else { 068 SMarket.getTheMarket().setOpen(1); 069 } 070 } 071 })); 072 } 073 jpBigButton.add(jb); 074 jpMain.add(jpBigButton); 075 fs.setComponent(jpMain); 076 removeAllButtons(); 077 fs.addButton("Markt öffnen", ButtonIDs.BTN_START, null); 078 fs.addButton("Markt schließen", ButtonIDs.BTN_END, null); 079 fs.addButton("Zum nächsten Tag", ButtonIDs.BTN_NEXT, null); 080 fs.getButton(ButtonIDs.BTN_START).setEnabled(SMarket.hasTimeAdvanced()); 081 fs.getButton(ButtonIDs.BTN_END).setEnabled(SMarket.isOpen()); 082 fs.getButton(ButtonIDs.BTN_NEXT).setVisible(!SMarket.isOpen()); 083 } 084 }); 085 SMarket.addEventListener(new MarketEventAdapter() { 086 /** 087 * ID for serialization. 088 */ 089 private static final long serialVersionUID = 19465954596631138L; 090 091 public void notifyOnMarketClosing() { 092 jb.setText(TOBECLOSED); 093 }; 094 095 public void notifyOnMarketNotClosing() { 096 jb.setText(OPEN); 097 } 098 099 public void marketClosed() { 100 jb.setEnabled(false); 101 jb.setVisible(false); 102 getButton(ButtonIDs.BTN_END).setEnabled(false); 103 getButton(ButtonIDs.BTN_NEXT).setVisible(true); 104 } 105 106 public void marketOpened() { 107 jb.setEnabled(true); 108 jb.setVisible(true); 109 jb.setText("Feierabend ankündigen"); 110 getButton(ButtonIDs.BTN_START).setEnabled(false); 111 getButton(ButtonIDs.BTN_END).setEnabled(true); 112 getButton(ButtonIDs.BTN_NEXT).setVisible(false); 113 } 114 115 public void timeAdvanced() { 116 getButton(ButtonIDs.BTN_START).setEnabled(true); 117 } 118 119 public void workerInformationChanged() { 120 } 121 }); 122 } 123 } 124