KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > elements > ScheduleDE


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.elements;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.PrintStream JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Observable JavaDoc;
10 import java.util.Observer JavaDoc;
11 import java.util.TimeZone JavaDoc;
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 JavaDoc;
37
38 /**
39  *
40  */

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 JavaDoc[] supportedTypes() {
49         return ScheduleFactory.allTypes();
50     }
51     
52     public DesignElementType createType(String JavaDoc type) {
53         return ScheduleFactory.createType(type);
54     }
55     
56     public ElementWrapper[] elements() {
57         return childElements();
58     }
59         
60     public String JavaDoc toString() {
61         return "Schedule";
62     }
63     
64     /**
65      * A utility class which provides a form for checking the schedule.
66      *
67      */

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 JavaDoc results = "Change the date to check your schedule.";
74
75         private TextInput textInput = new TextInput("Results") {
76             public String JavaDoc getText() {
77                 return results;
78             }
79             public void setText(String JavaDoc text) {
80                 setChanged();
81                 notifyObservers();
82             }
83         };
84         
85         public CheckSchedule() {
86             count.attribute("10");
87             Observer JavaDoc observer = new Observer JavaDoc() {
88                 public void update(Observable JavaDoc o, Object JavaDoc arg) {
89                     Date JavaDoc d = null;
90                     try {
91                         DateType dateType = date.toType();
92                         d = dateType.valueFor(Object JavaDoc.class);
93                     } catch (Exception JavaDoc 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 JavaDoc e) {
104                         setResults(exceptionAsString(e));
105                         return;
106                     }
107                     TimeZone JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc scheduleDump(Date JavaDoc testDate,
160                 int occurrences, TimeZone JavaDoc timeZone)
161     throws SAXParseException JavaDoc {
162         Schedule schedule = null;
163         schedule = toType();
164         
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc exceptionAsString(Exception JavaDoc e) {
183         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
184         e.printStackTrace(new PrintStream JavaDoc(out));
185         return out.toString();
186     }
187 }
188
Popular Tags