1 package net.jaekl.frank;
3 import java.io.PrintWriter;
4 import java.text.DateFormat;
5 import java.text.MessageFormat;
6 import java.text.SimpleDateFormat;
8 import java.util.Locale;
10 import net.jaekl.frank.octranspo.Route;
11 import net.jaekl.frank.octranspo.StopInfo;
12 import net.jaekl.frank.octranspo.Trip;
14 public class Schedule {
17 DateFormat m_hourMinFmt;
18 DateFormat m_hourMinSecFmt;
20 public Schedule(Locale locale) {
22 m_bundle = FrankBundle.getInst(locale);
23 m_hourMinFmt = new SimpleDateFormat("hh:mma", locale);
24 m_hourMinSecFmt = new SimpleDateFormat("hh:mm:ssa", locale);
27 String trans(String key) {
28 return m_bundle.get(key);
31 String mapUrl(double latitude, double longitude) {
32 return "http://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude + "&zoom=15";
35 void writeStyle(PrintWriter pw) {
36 pw.println("<STYLE>");
37 pw.println(" body {background-color: #F0F0C0; font-size: 1.5em; }");
38 pw.println(" #trips {border-collapse: collapse; font-size: 1.5em; }");
39 pw.println(" #trips td, #trips th {border: 1px solid #600000; padding: 3px 3px 3px 3px; text-align: center;}");
40 pw.println(" #trips th {background-color: #800000; color: #FFFFFF; }");
41 pw.println(" #trips tr.ghost td {background-color: #C0C0C0;}");
42 pw.println("</STYLE>");
45 // Countdown timer that updates time remaining until each bus is expected.
46 void writeScript(PrintWriter pw, String remainArray, int remainCount) {
47 String min = trans(FrankBundle.MINUTES);
48 String sec = trans(FrankBundle.SECONDS);
50 pw.println("<SCRIPT>");
51 pw.println("var start; var interval; var remain;");
52 pw.println("window.onload = function() { start = new Date(); interval = setInterval(\"Tick()\", 1000); remain = " + remainArray + "; }");
53 pw.println("function Tick() { ");
54 pw.println(" var now = new Date(); var delta = Math.floor((now.getTime() - start.getTime()) / 1000); ");
55 pw.println(" var secs = delta % 60; var mins = Math.floor(delta / 60);");
56 pw.println(" document.getElementById(\"elapsed\").innerHTML = mins + \"" + min + " \" + secs + \"" + sec + "\";");
57 pw.println(" for (var i = 0; i < " + remainCount + "; ++i) {");
58 pw.println(" var id = \"in_\" + i;");
59 pw.println(" var element = document.getElementById(id);");
60 pw.println(" var remain_delta = (remain[i] * 60) + 30 - delta;");
61 pw.println(" if (remain_delta <= 0) {");
62 pw.println(" element.innerHTML = \"----\";");
63 pw.println(" } else {");
64 pw.println(" element.innerHTML = (Math.floor(remain_delta / 60)) + \"" + trans(min) + "\";");
68 pw.println("</SCRIPT>");
72 void writeHeader(PrintWriter pw, String title) {
75 pw.println("<TITLE>" + title + "</TITLE>");
77 pw.println("</HEAD>");
80 void writePage(PrintWriter pw, StopInfo stopInfo)
82 StringBuilder remainArray = new StringBuilder("[ ");
85 String title = stopInfo.getDescr() + " (" + stopInfo.getStopNo() + ")";
86 String min = trans(FrankBundle.MINUTES);
88 writeHeader(pw, trans(FrankBundle.FRANK) + ": " + title);
91 pw.println(" <TABLE ID=\"trips\">");
92 pw.println(" <TR><TH COLSPAN=\"5\">" + title + "</TH></TR>");
93 pw.println(" <TR><TH>" +
94 trans(FrankBundle.ROUTE) +
96 trans(FrankBundle.DESTINATION) +
98 trans(FrankBundle.ETA) +
100 trans(FrankBundle.REMAIN) +
102 trans(FrankBundle.GPS_READ) +
105 for (int routeIdx = 0; routeIdx < stopInfo.getNumRoutes(); ++routeIdx) {
106 Route route = stopInfo.getRoute(routeIdx);
107 for (int tripIdx = 0; tripIdx < route.getNumTrips(); ++tripIdx) {
108 Trip trip = route.getTrip(tripIdx);
109 boolean isGhost = ((-1) == trip.getAdjAge());
111 // GPS is off. This bus may not exist.
112 pw.println(" <TR CLASS=\"ghost\">");
116 pw.println(" <TD>" + route.getRouteNo() + "</TD>");
117 pw.println(" <TD>" + trip.getDest() + "</TD>");
118 pw.println(" <TD>" + m_hourMinFmt.format(trip.getETA()) + "</TD>");
120 pw.println(" <TD><SPAN ID=\"in_" + remainCount + "\">"+ trip.getAdjTime()+min+"</SPAN></TD>");
121 if (remainCount > 0) {
122 remainArray.append(", ");
124 remainArray.append(trip.getAdjTime());
127 if (trip.getAdjAge() < 0) {
128 pw.println(" <TD>" + trans(FrankBundle.GPS_OFF) + "</TD>");
131 pw.println(" <TD><A HREF=\"" + mapUrl(trip.getLatitude(), trip.getLongitude()) +
132 "\">" + m_hourMinSecFmt.format(trip.getGPSTime()) + "</A></TD>");
134 pw.println(" </TR>");
138 pw.println(" </TABLE>");
140 String dataCollectedFormat = trans(FrankBundle.DATA_COLLECTED);
141 String dataCollected = MessageFormat.format(dataCollectedFormat, m_hourMinSecFmt.format(new Date()));
142 pw.println(" <P>" + dataCollected + "</P>");
144 remainArray.append(" ]");
145 writeScript(pw, remainArray.toString(), remainCount);
146 pw.println("</BODY></HTML>");