DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > A JavaFX Script Calendar that Displays a Google Calendar Feed

A JavaFX Script Calendar that Displays a Google Calendar Feed

James Weaver user avatar by
James Weaver
·
Mar. 19, 08 · Java Zone · Interview
Like (0)
Save
Tweet
14.01K Views

Join the DZone community and get the full member experience.

Join For Free

today i'd like to show you the humble beginnings of a javafx script calendar that displays a google calendar feed.  it is currently pointing to my google calendar, which i've sparsely populated with some sample data.  you can change the program to point to your google calendar if you choose.

i'll improve this calendar program over time, but wanted to demonstrate javafx script's ability to easily parse an xml stream over the internet.  you may also want to take a look at the compiled javafx script now speaks json post which demonstrates parsing json streams over the internet.  here's a screenshot of today's example, followed by the program's code:

calendarjfx


the code behind the user interface

obviously there are many improvements that i can make to this calendar, but here are the four source files that comprise this example in its current state:


calendarjfx.fx

/*
* calendarjfx.fx -
* the main program for a compiled javafx calendar program
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a compiled javafx script example.
*/

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.system;

frame {
var calmodel =
calendarmodel {}
title: "calendarjfx"
width: 600
height: 600
visible: true
content:
borderpanel {
top:
borderpanel {
left:
flowpanel {
content: [
button {
text: "<<"
action:
function():void {
calmodel.prevyear();
}
},
button {
text: "<"
action:
function():void {
calmodel.prevmonth();
}
}
]
}
center:
flowpanel {
content:
simplelabel {
text: bind "{calmodel.selectedmonthstr} {calmodel.selectedyearstr}"
font:
font {
size: 24
style: fontstyle.bold
}
}
}
right:
flowpanel {
content: [
button {
text: ">"
action:
function():void {
calmodel.nextmonth();
}
},
button {
text: ">>"
action:
function():void {
calmodel.nextyear();
}
}
]
}
bottom:
gridpanel {
// todo: internationalize days of the week
var days = ["sun", "mon", "tue",
"wed", "thu", "fri", "sat"]
rows: 1
columns: 7
cells:
for (day in days) {
simplelabel {
text: day
font:
font {
size: 18
style: fontstyle.bold
}
horizontalalignment:
horizontalalignment.center
}
}
}
}
center:
gridpanel {
vgap: 1
hgap: 1
rows: 6
columns: 7
cells:
for (idx in [1..42]) {
calendarcell {
calmodel: calmodel
dayofmonthstr: bind calmodel.getdayinmonthstrforcell(idx as integer);
cellgregcal: bind calmodel.getdateforcell(idx as integer);
}
}
}
}
onclose:
function():void {
system.exit(0);
}
}


calendarcell.fx

/*
* calendarjfx.fx -
* the main program for a compiled javafx calendar program
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a compiled javafx script example.
*/

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.system;

frame {
var calmodel =
calendarmodel {}
title: "calendarjfx"
width: 600
height: 600
visible: true
content:
borderpanel {
top:
borderpanel {
left:
flowpanel {
content: [
button {
text: "<<"
action:
function():void {
calmodel.prevyear();
}
},
button {
text: "<"
action:
function():void {
calmodel.prevmonth();
}
}
]
}
center:
flowpanel {
content:
simplelabel {
text: bind "{calmodel.selectedmonthstr} {calmodel.selectedyearstr}"
font:
font {
size: 24
style: fontstyle.bold
}
}
}
right:
flowpanel {
content: [
button {
text: ">"
action:
function():void {
calmodel.nextmonth();
}
},
button {
text: ">>"
action:
function():void {
calmodel.nextyear();
}
}
]
}
bottom:
gridpanel {
// todo: internationalize days of the week
var days = ["sun", "mon", "tue",
"wed", "thu", "fri", "sat"]
rows: 1
columns: 7
cells:
for (day in days) {
simplelabel {
text: day
font:
font {
size: 18
style: fontstyle.bold
}
horizontalalignment:
horizontalalignment.center
}
}
}
}
center:
gridpanel {
vgap: 1
hgap: 1
rows: 6
columns: 7
cells:
for (idx in [1..42]) {
calendarcell {
calmodel: calmodel
dayofmonthstr: bind calmodel.getdayinmonthstrforcell(idx as integer);
cellgregcal: bind calmodel.getdateforcell(idx as integer);
}
}
}
}
onclose:
function():void {
system.exit(0);
}
}



calendarmodel.fx

/*
* calendarmodel.fx -
* the model behind a compiled javafx calendar program
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a compiled javafx script example.
*/

import javafx.xml.*;
import java.text.simpledateformat;
import java.lang.system;
import java.util.calendar;
import java.util.gregoriancalendar;

class calendarmodel {
attribute calendarfeeduri =
"http://www.google.com/calendar/feeds/james.l.weaver%40gmail.com/public/full";
attribute docbuilder =
documentbuilder {
namespaceaware:true
validating:true
ignoringcomments:false
};
attribute document =
docbuilder.parseuri(calendarfeeduri);
attribute calentries:calendarentry[];
attribute selectedgregcal:gregoriancalendar = new gregoriancalendar();
attribute utilgregcal:gregoriancalendar = new gregoriancalendar();
attribute selectedmonth:integer;
attribute selectedyear:integer;
attribute dayinmonthfmt = new simpledateformat("d");
attribute monthfmt = new simpledateformat("mmmm");
attribute yearfmt = new simpledateformat("yyyy");
attribute cellnumber:integer;
attribute selecteddayinmonthstr:string;
attribute selectedmonthstr:string;
attribute selectedyearstr:string;

postinit {
var calelements = document.getelementsbytagname("entry");
calentries = for (calelement in calelements)
calendarentry {
title: calelement.querystring("title")
starttimestr: calelement.querystring("when/@starttime")
endtimestr: calelement.querystring("when/@endtime")
location: calelement.querystring("where/@valuestring")
};
populatedateparts();
}

function prevmonth():void {
selectedgregcal.add(calendar.month, -1);
selectedmonth = selectedgregcal.get(calendar.month);
populatedateparts();
}

function nextmonth():void {
selectedgregcal.add(calendar.month, 1);
selectedmonth = selectedgregcal.get(calendar.month);
populatedateparts();
}

function prevyear():void {
selectedgregcal.add(calendar.year, -1);
selectedyear = selectedgregcal.get(calendar.year);
populatedateparts();
}

function nextyear():void {
selectedgregcal.add(calendar.year, 1);
selectedyear = selectedgregcal.get(calendar.year);
populatedateparts();
}

function populatedateparts():void {
var seldate = selectedgregcal.gettime();
selecteddayinmonthstr = dayinmonthfmt.format(seldate);
selectedmonthstr = monthfmt.format(seldate);
selectedyearstr = yearfmt.format(seldate);
}

function getdateforcell(cellnumber:integer):gregoriancalendar {
utilgregcal.set(calendar.year, selectedyear);
utilgregcal.set(calendar.month, selectedmonth);
utilgregcal.set(calendar.day_of_month, 1);
var firstdayoffset = utilgregcal.get(calendar.day_of_week);
utilgregcal.add(calendar.day_of_month, firstdayoffset * -1 + cellnumber);
return utilgregcal;
}

function getdayinmonthstrforcell(cellnumber:integer):string {
utilgregcal.set(calendar.year, selectedyear);
utilgregcal.set(calendar.month, selectedmonth);
utilgregcal.set(calendar.day_of_month, 1);
var firstdayoffset = utilgregcal.get(calendar.day_of_week);
utilgregcal.add(calendar.day_of_month, firstdayoffset * -1 + cellnumber);
dayinmonthfmt.format(utilgregcal.gettime());
}
}



calendarentry.fx

/*
* calendarentry.fx -
* a calendar entry, which is part of the model.
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a compiled javafx script example.
*/

import java.lang.system;
import java.text.simpledateformat;
import java.util.locale;
import java.util.calendar;
import java.util.date;

class calendarentry {
private attribute sdf = new simpledateformat
("yyyy-mm-dd't'hh:mm:ss.sssz");
attribute title:string;
attribute starttime:calendar;
attribute endtime:calendar;
attribute starttimestr:string on replace {
//todo: accommodate all-day events
var d:date = sdf.parse("{starttimestr.substring(0, 26)}{starttimestr.substring(27)}");
var cal:calendar = calendar.getinstance();
cal.settime(d);
starttime = cal;
};
attribute endtimestr:string on replace {
var d:date = sdf.parse("{endtimestr.substring(0, 26)}{endtimestr.substring(27)}");
var cal:calendar = calendar.getinstance();
cal.settime(d);
endtime = cal;
};
attribute location:string;
}

enjoy, and as always, please post a comment if you have any questions.

regards,
jim weaver
javafx script: dynamic java scripting for rich internet/client-side applications

immediate ebook (pdf) download available at the book's apress site

Google Calendar JavaFX Script JavaFX

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is Java Still Relevant?
  • 10 Steps to Become an Outstanding Java Developer
  • How to Submit a Post to DZone
  • How to Generate Fake Test Data

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo