1 4 package org.oddjob.designer.elements; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.PrintStream ; 8 import java.util.Date ; 9 import java.util.Observable ; 10 import java.util.Observer ; 11 import java.util.TimeZone ; 12 13 import org.oddjob.arooa.ArooaRuntime; 14 import org.oddjob.arooa.ArooaContext; 15 import org.oddjob.arooa.ArooaConstants; 16 import org.oddjob.arooa.RuntimeConfiguration; 17 import org.oddjob.arooa.handlers.DocumentStartHandler; 18 import org.oddjob.arooa.handlers.MainHandler; 19 import org.oddjob.designer.arooa.DesignParser; 20 import org.oddjob.designer.arooa.ElementWrapper; 21 import org.oddjob.designer.elements.date.TimeZoneAttribute; 22 import org.oddjob.designer.elements.schedule.ScheduleFactory; 23 import org.oddjob.designer.model.DesignDefinition; 24 import org.oddjob.designer.model.DesignElementAttribute; 25 import org.oddjob.designer.model.DesignElementType; 26 import org.oddjob.designer.model.ElementField; 27 import org.oddjob.designer.model.FieldGroup; 28 import org.oddjob.designer.model.MultiTypeTree; 29 import org.oddjob.designer.model.TextInput; 30 import org.oddjob.schedules.DateUtils; 31 import org.oddjob.schedules.Interval; 32 import org.oddjob.schedules.Schedule; 33 import org.oddjob.schedules.ScheduleContext; 34 import org.oddjob.schedules.ScheduleElement; 35 import org.oddjob.values.types.DateType; 36 import org.xml.sax.SAXParseException ; 37 38 41 public class ScheduleDE extends DesignElementAttribute { 42 43 public DesignDefinition detail() { 44 return new MultiTypeTree("Schedule", this) 45 .setDetail(new CheckSchedule().detail()); 46 } 47 48 public String [] supportedTypes() { 49 return ScheduleFactory.allTypes(); 50 } 51 52 public DesignElementType createType(String type) { 53 return ScheduleFactory.createType(type); 54 } 55 56 public ElementWrapper[] elements() { 57 return childElements(); 58 } 59 60 public String toString() { 61 return "Schedule"; 62 } 63 64 68 class CheckSchedule { 69 private DateDE date = new DateDE(); 70 private TimeZoneAttribute timeZone = new TimeZoneAttribute(); 71 private SimpleDE count = new SimpleDE(); 72 73 private String results = "Change the date to check your schedule."; 74 75 private TextInput textInput = new TextInput("Results") { 76 public String getText() { 77 return results; 78 } 79 public void setText(String text) { 80 setChanged(); 81 notifyObservers(); 82 } 83 }; 84 85 public CheckSchedule() { 86 count.attribute("10"); 87 Observer observer = new Observer () { 88 public void update(Observable o, Object arg) { 89 Date d = null; 90 try { 91 DateType dateType = date.toType(); 92 d = dateType.valueFor(Object .class); 93 } catch (Exception e) { 94 setResults(exceptionAsString(e)); 95 return; 96 } 97 if (d == null) { 98 return; 99 } 100 int c = 0; 101 try { 102 c = Integer.parseInt(count.attribute()); 103 } catch (NumberFormatException e) { 104 setResults(exceptionAsString(e)); 105 return; 106 } 107 TimeZone tz = TimeZone.getDefault(); 108 if (timeZone.attribute() != null) { 109 tz = TimeZone.getTimeZone(timeZone.attribute()); 110 } 111 try { 112 setResults(scheduleDump(d, c, tz)); 113 } catch (SAXParseException e) { 114 setResults(exceptionAsString(e)); 115 return; 116 } 117 } 118 }; 119 date.addObserver(observer); 120 timeZone.addObserver(observer); 121 count.addObserver(observer); 122 ScheduleDE.this.addObserver(observer); 123 } 124 125 void setResults(String results) { 126 this.results = results; 127 textInput.setText(""); 128 } 129 130 public DesignDefinition detail() { 131 return new FieldGroup("Check Schedule") 132 .add(new ElementField("Date", date)) 133 .add(new ElementField("Time Zone", timeZone)) 134 .add(new ElementField("Number", count)) 135 .add(textInput); 136 } 137 138 } 139 140 public Schedule toType() throws SAXParseException { 141 142 ScheduleElement scheduleElement = new ScheduleElement(); 143 144 ArooaContext context = new ArooaContext(); 145 RuntimeConfiguration rtc = new ArooaRuntime( 146 scheduleElement, "schedule", context); 147 148 context.set(ArooaConstants.CURRENTLY_CONFIGURING, rtc); 149 150 DesignParser dp = new DesignParser(context); 151 dp.parse("schedule", this, new MainHandler( 152 new DocumentStartHandler(scheduleElement))); 153 154 rtc.configure(); 155 156 return scheduleElement; 157 } 158 159 String scheduleDump(Date testDate, 160 int occurrences, TimeZone timeZone) 161 throws SAXParseException { 162 Schedule schedule = null; 163 schedule = toType(); 164 165 StringBuffer buffer = new StringBuffer (); 166 ScheduleContext context = new ScheduleContext( 167 testDate, timeZone); 168 for (int i = 0; i < occurrences; ++i) { 169 Interval interval = schedule.nextDue(context); 170 if (interval == null) { 171 buffer.append("That's it."); 172 break; 173 } 174 buffer.append(interval.toString()); 175 buffer.append('\n'); 176 testDate = DateUtils.oneMillisAfter(interval.getToDate()); 177 context = context.spawn(testDate); 178 } 179 return buffer.toString(); 180 } 181 182 static String exceptionAsString(Exception e) { 183 ByteArrayOutputStream out = new ByteArrayOutputStream (); 184 e.printStackTrace(new PrintStream (out)); 185 return out.toString(); 186 } 187 } 188 | Popular Tags |