001 package market;
002
003 import java.util.Calendar;
004
005 import javax.swing.JOptionPane;
006
007 import market.stdform.ButtonIDs;
008 import market.stdform.FSCheckable;
009 import market.stdform.FSTime;
010 import sale.Action;
011 import sale.FormSheet;
012 import sale.FormSheetContentCreator;
013 import sale.SaleProcess;
014 import sale.SalesPoint;
015
016 /**
017 * The tiny window in which one can set the market's time.
018 */
019 public class SPTime extends SalesPoint {
020
021 /**
022 * ID for serialization.
023 */
024 private static final long serialVersionUID = 8231169080529401808L;
025
026 public SPTime() {
027 super("Zeitsimulation");
028 }
029
030 /**
031 * @return <code>true</code>, so this SalesPoint can always be closed.
032 */
033 public boolean onCanQuit() {
034 return true;
035 }
036
037 /**
038 * Creates and returns the default FormSheet.
039 * @return the default FormSheet.
040 */
041 public FormSheet getDefaultFormSheet() {
042 final FSCheckable fsc = new FSCheckable(new FSTime());
043 fsc.addContentCreator(new FormSheetContentCreator() {
044 private static final long serialVersionUID = -1594771979953825127L;
045
046 public void createFormSheetContent(final FormSheet fs) {
047 fs.getButton(ButtonIDs.BTN_OK).setAction(new Action() {
048 private static final long serialVersionUID = -3133318410100230937L;
049
050 public void doAction(SaleProcess p, SalesPoint sp) {
051 if (fsc.checkTextFields(FSCheckable.ALL_ERRORMESSAGES_AT_ONCE, false)) {
052 String sDate = fsc.getEntry(1);
053 Calendar dSet = Conversions.stringToCalendar(sDate);
054 Calendar today = SMarket.getTime();
055 if (!dSet.after(today)) {
056 JOptionPane.showMessageDialog(fs.getComponent(),
057 "Es wird ein künftiges Datum erwartet.", "Fehler", 0);
058 } else {
059 SMarket.setTime(dSet);
060 closeTime();
061 }
062 }
063 }
064 });
065 }});
066 return fsc;
067 }
068
069 /**
070 * Closes this SalesPoint.
071 */
072 private void closeTime() {
073 //This is written as an extra method because it uses <this>, but it is called from within
074 //an anonymous inner class
075 SMarket.getTheShop().removeSalesPoint(this);
076 }
077 }