Saturday, November 20, 2010

Ron Paul on TSA Screenings

Thank you Rom Paul for standing up for my rights. In this video he points out the ridiculousness of the new TSA screening. My hope is that more and more people wake up to this fiasco and stand up for their basic human rights. What can we do? Sign petitions, support politicians like Ron Paul, write to representatives, don't fly, write to the president, educate yourself, spread the word...

Monday, November 1, 2010

A Quirk of Evolution

The following video shows an example of where the process of evolution didn't take the most efficient design path.

Friday, October 22, 2010

1,000,000 Frames Per Second Bullet Impact Footage

It's more or less the same thing over and over, but nonetheless always intriguing...

Saturday, October 16, 2010

Benoît Mandelbrot Lives


News of Mandelbrot's death at the ripe age of 85 spread like a wildfire through the geek community (which I'm a proud member of) today. In highschool I remember seeing an image of the Mandelbrot Set and since then have been fascinated with it. In college I wrote a computer program to generate the Mandelbrot Set myself and became even more fascinated because of the complexity of the Set that comes from an absurdly simple equation:

z = z^2 + c.

In fact, the broader implications of his (as well as Fibinocci's) discovery has been a key component is how I view the world and try to make sense of it all.

As a sidenote, I also recently learned German and discovered that his name means Mr. Almondbread in English! :)

Anyway, here's a video I found an share with you in honor of Mr. Mandelbrot. I always wanted to make a video like this but never got around to it. Enjoy.



Mandelbrot Fractal Set Trip To e214 HD from teamfresh on Vimeo.

See also: Fibonacci Sequence

Wednesday, October 13, 2010

Super Hydrophobic Carbon Nanotube Surface

Wow, that's hydrophobic. Very cool fluid dynamics video they made with their new hydrophobic surface...

Sunday, October 3, 2010

Non-marsupial Birth

In light of my previous post on Kangaroos, which, being marsupials, give birth to a fetus the size of a jellybean, here's a medical animation video of a human birth.

Kangaroos

Marsupials. What a cool evolutionary track! From the Marsupials Wikipedia page: "Marsupials have an extremely short gestation period (about 4–5 weeks), and the joey is 'born' basically in a fetal state. The blind, furless, miniature newborn, the size of a jelly bean, crawls across its mother's fur to make its way into the pouch, where it latches onto a teat for food." The whole article is interesting. Here are some pics I took of the kangaroos at the zoo in Munich, Germany.







See also: Wolf Fight


Submit Photos to Shutterstock and make $$$!

Monday, September 13, 2010

Java Quartz - Disable Update Check

I noticed that when my applications start up which use the Quartz job scheduling api, Quartz was pinging www.terracotta.org to see if there were any updates available. The check was also throwing an error:

Quartz version update check failed: java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.terracotta.org/kit/reflector?kitID=quartz&pageID=update.properties&id=-1062731163&os-name=Mac+OS+X&jvm-name=Java+HotSpot%28TM%29+Client+VM&jvm-version=1.5.0_24&platform=i386&tc-version=1.8.3&tc-product=Quartz&source=Quartz&uptime-secs=1&patch=UNKNOWN

The following code snippet shows how to prevent Quartz from doing an update check on startup. This works for sure with Quartz version 1.8.3.

In quartz.properties, add the following line:

org.quartz.scheduler.skipUpdateCheck = true




Piece of Cake!!!

Friday, September 10, 2010

Quartz Jobs.xml Example - Adding a Durable Job

The following sample XML shows how to set up a single Quartz job without a trigger in a jobs.xml configuration file. I first tried just creating a job without any trigger associated with it, but I got a runtime error:

Error scheduling jobs: A new job defined without any triggers must be durable...A new job defined without any triggers must be durable...

The following scheduled job is called SampleJob. The one tricky catch is that in order to set the job as durable, you also have to include the volatility AND recover elements. This works for sure with Quartz version 1.8.3.

<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  version="1.8">

    <schedule>
        <job>  
            <name>SampleJob</name>
            <group>DEFAULT</group>
            <job-class>com.xyz.SampleJob</job-class>
            <volatility>false</volatility> 
            <durability>true</durability> 
            <recover>false</recover> 
            <job-data-map>
                <entry>
                    <key>Param1</key>
                    <value>N</value>
                </entry>
            </job-data-map>
        </job>            
    </schedule>
</job-scheduling-data>



Piece of Cake!!!

Saturday, August 21, 2010

Quartz Jobs.xml Example - Single Job Multiple Triggers with CRON Expression

The following sample XML shows how to set up a single Quartz job with multiple cron triggers in a jobs.xml configuration file. The scheduled job is called SampleJob and it has two triggers Trigger1 and Trigger2. The job has its own parameter and each trigger has its own parameter. Trigger1 runs every day at 9:00 and Trigger2 runs every day at 10:00. This works for sure with Quartz version 1.8.3.

<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  version="1.8">

    <schedule>
        <job>
            <name>SampleJob</name>
            <group>DEFAULT</group>
            <job-class>com.xyz.SampleJob</job-class>
            <job-data-map>
                <entry>
                    <key>Param1</key>
                    <value>HOT</value>
                </entry>
            </job-data-map>
        </job>
        <trigger>
            <cron>
                <name>Trigger1</name>
                <group>DEFAULT</group>
                <job-name>SampleJob</job-name>
                <job-group>DEFAULT</job-group>
                <job-data-map>
                <entry>
                    <key>Param2</key>
                    <value>Y</value>
                </entry>
                </job-data-map>
                <cron-expression>0 0 9 * * ?</cron-expression>
            </cron>
        </trigger>
        <trigger>
            <cron>
                <name>Trigger2</name>
                <group>DEFAULT</group>
                <job-name>SampleJob</job-name>
                <job-group>DEFAULT</job-group>
                <job-data-map>
                <entry>
                    <key>Param2</key>
                    <value>N</value>
                </entry>
                </job-data-map>
                <cron-expression>0 0 10 * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>



Piece of Cake!!!

Friday, August 20, 2010

Place an Inline Link to an Amazon Product with Your Associates ID

In addition to using the Amazon widgets, links, and banners directly available in your Amazon Associates account to place Amazon advertisements on your website or blog, you can also create your own inline links with a simple trick. Keep reading to see how...

Step 0: Figure out your Amazon associates ID (tracking ID). Log into your associates account, and in the upper-left corner of the webpage you should see your Tracking ID. Mine is obscuclari-20, which I will use below. You will need to use YOUR tracking ID.



Step 1: Get the URL of the webpage on Amazon.com that you want to link to. For example, here's a link to some fish food:

http://www.amazon.com/Tetra-BettaMin-Flakes-0-81-Ounces/dp/B00025K10M/ref=sr_1_11?ie=UTF8&s=pet-supplies&qid=1282284515&sr=1-11

Step 2: Add your Amazon associates ID (tracking ID) to the link. This is so that Amazon knows that someone arrived at Amazon.com from your website. All you have to do is add &tag=obscuclari-20 to the end of the link in step 1, where obscuclari-20 is replaced by YOUR Tracking ID. The link in Step 1 should then look like this:

http://www.amazon.com/Tetra-BettaMin-Flakes-0-81-Ounces/dp/B00025K10M/ref=sr_1_11?ie=UTF8&s=pet-supplies&qid=1282284515&sr=1-11&tag=obscuclari-20

Step 3: Test it! Add your inline link to your website, click it and verify that your Tracker ID properly appears in the URL in the browser's address bar. Here's the inline link I just created: Click me!


I have another working example here, where I've added my own inline links to some harddrive hardware, and it's been working out for me for well over a year.

Piece of Cake!!!

See also: How to Add an Amazon Widget to Your Blog to Make Some Extra Cash

Sunday, August 15, 2010

Java Ant - Build .War file with Auto-Incrementing Build Number

To auto-increment a build number while building a .war file with ANT, we can create a .properties file using the propertyfile ant task in combination with the buildnumber ant task. The buildnumber task creates a build.version file and increments the build number contained within during each build. The following task in my Ant build file creates a properties file with auto-incrementing build information along with the person who built the .war file and a timestamp. The ${major.minor.version} variable comes out of a build.properties file I use for the build.


<target name="buildinfo">   
    <buildnumber />
    <propertyfile file="${build.dir}/build-info.properties"
        comment="This file is automatically generated - DO NOT EDIT">
        <entry key="Built-By" value="${user.name}"/>
        <entry key="Build-Version" value="${major.minor.version}-b${build.number}"/>
    </propertyfile>
</target>



Here is the contents of the build-info.properties file:
#This file is automatically generated - DO NOT EDIT
#Sun Aug 15 17:04:03 CEST 2010
Build-Version=0.1-b15
Built-By=tim

See also:
Hello World Java Web Application in Eclipse (Part 3)

Java Ant - Build Jar with Auto-Incrementing Build Number

To auto-increment a build number while building a Jar file with Ant, we can use the "manifest" Ant task with the "buildnumber" task. The buildnumber task creates a build.version file and increments the build number contained within during each build. The build number along with other information can be integrated right into the MANIFEST.MF file (or anywhere else for that matter). Here is the Ant code I use to auto-increment the build number during a Jar file build:


    <target name="manifest">
        <echo>Creating manifest</echo>
     <mkdir dir="${build.dir}/META-INF"/>
        <buildnumber file="build.version"/>
        <tstamp>
           <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss" />
        </tstamp>
        <manifest file="${build.dir}/META-INF/MANIFEST.MF">
           <attribute name="Built-By" value="${user.name}"/>
           <attribute name="Build-Version" value="0.4-b${build.number}"/> 
           <attribute name="Build-Date" value="${timestamp}"/>                 
       </manifest>
    </target>



Here is the contents of the MANIFEST.MF file:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_24-149 (Apple Inc.)
Built-By: tim
Build-Version: 0.4-b2
Build-Date: 2010-08-15 16:13:49

See also:
Hello World Java Web Application in Eclipse (Part 3)


Sunday, August 8, 2010

Error Occured While Converting Date Log4j Slf4j Quartz Tomcat Java

Today I spent way too many hours fixing a bug I ran across while getting Quartz integrated into a Java Webapp running on Tomcat with integrated log4j. The Quartz api uses slf4j logging and Quartz was throwing null pointer exceptions when starting up relating to logging. Below is the solution in my particular case. I hope this helps someone else!



log4j:ERROR Error occured while converting date.
java.lang.NullPointerException
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:488)
at java.lang.StringBuffer.append(StringBuffer.java:302)
at org.apache.log4j.helpers.ISO8601DateFormat.format(ISO8601DateFormat.java:132)
at java.text.DateFormat.format(DateFormat.java:314)
at org.apache.log4j.helpers.PatternParser$DatePatternConverter.convert(PatternParser.java:444)
at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:64)
at org.apache.log4j.PatternLayout.format(PatternLayout.java:503)
at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:301)
at org.apache.log4j.WriterAppender.append(WriterAppender.java:159)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:230)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:65)
at org.apache.log4j.Category.callAppenders(Category.java:203)
at org.apache.log4j.Category.forcedLog(Category.java:388)
at org.apache.log4j.Category.log(Category.java:853)
at org.slf4j.impl.Log4jLoggerAdapter.debug(Log4jLoggerAdapter.java:204)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:595)
[] DEBUG org.quartz.simpl.SimpleThreadPool WorkerThread is shut down.



The solution is to replace "ISO8601" in the following log4j.xml file with "yyyy-MM-dd HH:mm:ss.SSS". AHHHHRRRRRRRR!

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
 <param name="Target" value="System.out" />
 <layout class="org.apache.log4j.PatternLayout">
 <param name="ConversionPattern" value="[%d{ISO8601}] %-5p %c %m %n" />
 </layout>
 <filter class="org.apache.log4j.varia.LevelRangeFilter">
  <param name="LevelMin" value="WARN"/>
  <param name="LevelMax" value="FATAL"/>
 </filter>
</appender>


Tuesday, August 3, 2010

Get Previous Business Day Date Object in Java

To get a java.util.Date object representing the previous business day,
we can use the java.util.Calendar class. First, create a Calendar
object and set it's time with a Data object. Next, check what day of
the week it is and decrement it accordingly to the previous weekday.
Finally, create a new Date object with the Calendar's getTime()
method.


Get Previous Business Day Date Object in Java - Example Code

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//Java 1.4+ Compatible
//
// The following example code demonstrates how to determine
// the next business day given a Date object.

public class GetPreviousBusinessDay {

    public static void main(String[] args) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if (dayOfWeek == Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -3);
        } else if (dayOfWeek == Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, -2);
        } else {
            calendar.add(Calendar.DATE, -1);
        }

        Date previousBusinessDay = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("EEEE");
        System.out.println("Today                : " + sdf.format(today));
        System.out.println("Previous business day: " + sdf.format(previousBusinessDay));
    }

}

Here is the output of the example code:
Today                : Tuesday
Previous business day: Monday

Get Next Business Day Date Object in Java

To get a java.util.Date object representing the next business day, we
can use the java.util.Calendar class. First, create a Calendar object
and set it's time with a Data object. Next, check what day of the week
it is and increment it accordingly to the next weekday. Finally,
create a new Date object with the Calendar's getTime() method.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

// Java 1.4+ Compatible
//
// The following example code demonstrates how to determine
// the next business day given a Date object.

public class GetNextBusinessDay {

    public static void main(String[] args) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if (dayOfWeek == Calendar.FRIDAY) {
            calendar.add(Calendar.DATE, 3);
        } else if (dayOfWeek == Calendar.SATURDAY) {
            calendar.add(Calendar.DATE, 2);
        } else {
            calendar.add(Calendar.DATE, 1);
        }

        Date nextBusinessDay = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("EEEE");
        System.out.println("Today            : " + sdf.format(today));
        System.out.println("Next business day: " + sdf.format(nextBusinessDay));

    }

}

Here is the output of the example code:
Today            : Tuesday
Next business day: Wednesday

Get Last Day of Month Date Object in Java

To get a java.util.Date object representing the last day of the month,
we can use the java.util.Calendar class. First, create a Calendar
object and set it's time with a Data object. Next, add one month, set
the day of month to 1, and subtract one day. Finally, create a new
Date object with the Calendar's getTime() method.


import java.text.DateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//Java 1.4+ Compatible
//
// The following example code demonstrates how to get
// a Date object representing the last day of the month
// relative to a given Date object.

public class GetLastDayOfMonth {

    public static void main(String[] args) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        Date firstDayOfMonth = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("Today            : " + sdf.format(today));
        System.out.println("Last Day of Month: " + sdf.format(firstDayOfMonth));
    }

}

Here is the output of the example code:
Today            : 2010-08-03
Last Day of Month: 2010-08-31

Get First Day of Month Date Object in Java

To get a java.util.Date object representing the first day of the
month, we can use the java.util.Calendar class. First, create a
Calendar object and set it's time with a Data object. Next, set the
Calendar's 'day of month' to 1. Finally, create a new Date object with
the Calendar's getTime() method.


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

// Java 1.4+ Compatible
//
// The following example code demonstrates how to get
// a Date object representing the first day of the month
// relative to a given Date object.

public class GetFirstDayOfMonth {

    public static void main(String[] args) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        Date firstDayOfMonth = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("Today             : " + sdf.format(today));
        System.out.println("First Day of Month: " + sdf.format(firstDayOfMonth));
    }
}

Here is the output of the example code:
Today             : 2010-08-03
First Day of Month: 2010-08-01

Monday, August 2, 2010

A Man on a Quest to Locate Einstein's Brain

If you have an hour to kill, watch all 6 parts.

Get All Timezones Available in the TimeZone Class in Java

To get all the timezones available in the java.util.TimeZone class in Java, we use the static method TimeZone.getAvailableIDs(). The following example code also sorts them alphabetically and prints them out to the console.


import java.util.Arrays;
import java.util.TimeZone;

//Java 1.4+ Compatible
//
// The following example code demonstrates how to display
// all available timezones provided by the java.util.TimeZone
// class.

public class GetAvailableTimezones {

public static void main(String[] args) {

String[] allTimeZones = TimeZone.getAvailableIDs();

Arrays.sort(allTimeZones);

for (int i = 0; i < allTimeZones.length; i++) {
System.out.println(allTimeZones[i]);
}
}
}

Here is the output of the example code:
ACT
AET
AGT
ART
AST
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Kampala
Africa/Khartoum
Africa/Kigali
Africa/Kinshasa
Africa/Lagos
Africa/Libreville
Africa/Lome
Africa/Luanda
Africa/Lubumbashi
Africa/Lusaka
Africa/Malabo
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Mogadishu
Africa/Monrovia
Africa/Nairobi
Africa/Ndjamena
Africa/Niamey
Africa/Nouakchott
Africa/Ouagadougou
Africa/Porto-Novo
Africa/Sao_Tome
Africa/Timbuktu
Africa/Tripoli
Africa/Tunis
Africa/Windhoek
America/Adak
America/Anchorage
America/Anguilla
America/Antigua
America/Araguaina
America/Argentina/Buenos_Aires
America/Argentina/Catamarca
America/Argentina/ComodRivadavia
America/Argentina/Cordoba
America/Argentina/Jujuy
America/Argentina/La_Rioja
America/Argentina/Mendoza
America/Argentina/Rio_Gallegos
America/Argentina/Salta
America/Argentina/San_Juan
America/Argentina/San_Luis
America/Argentina/Tucuman
America/Argentina/Ushuaia
America/Aruba
America/Asuncion
America/Atikokan
America/Atka
America/Bahia
America/Barbados
America/Belem
America/Belize
America/Blanc-Sablon
America/Boa_Vista
America/Bogota
America/Boise
America/Buenos_Aires
America/Cambridge_Bay
America/Campo_Grande
America/Cancun
America/Caracas
America/Catamarca
America/Cayenne
America/Cayman
America/Chicago
America/Chihuahua
America/Coral_Harbour
America/Cordoba
America/Costa_Rica
America/Cuiaba
America/Curacao
America/Danmarkshavn
America/Dawson
America/Dawson_Creek
America/Denver
America/Detroit
America/Dominica
America/Edmonton
America/Eirunepe
America/El_Salvador
America/Ensenada
America/Fort_Wayne
America/Fortaleza
America/Glace_Bay
America/Godthab
America/Goose_Bay
America/Grand_Turk
America/Grenada
America/Guadeloupe
America/Guatemala
America/Guayaquil
America/Guyana
America/Halifax
America/Havana
America/Hermosillo
America/Indiana/Indianapolis
America/Indiana/Knox
America/Indiana/Marengo
America/Indiana/Petersburg
America/Indiana/Tell_City
America/Indiana/Vevay
America/Indiana/Vincennes
America/Indiana/Winamac
America/Indianapolis
America/Inuvik
America/Iqaluit
America/Jamaica
America/Jujuy
America/Juneau
America/Kentucky/Louisville
America/Kentucky/Monticello
America/Knox_IN
America/La_Paz
America/Lima
America/Los_Angeles
America/Louisville
America/Maceio
America/Managua
America/Manaus
America/Marigot
America/Martinique
America/Matamoros
America/Mazatlan
America/Mendoza
America/Menominee
America/Merida
America/Mexico_City
America/Miquelon
America/Moncton
America/Monterrey
America/Montevideo
America/Montreal
America/Montserrat
America/Nassau
America/New_York
America/Nipigon
America/Nome
America/Noronha
America/North_Dakota/Center
America/North_Dakota/New_Salem
America/Ojinaga
America/Panama
America/Pangnirtung
America/Paramaribo
America/Phoenix
America/Port-au-Prince
America/Port_of_Spain
America/Porto_Acre
America/Porto_Velho
America/Puerto_Rico
America/Rainy_River
America/Rankin_Inlet
America/Recife
America/Regina
America/Resolute
America/Rio_Branco
America/Rosario
America/Santa_Isabel
America/Santarem
America/Santiago
America/Santo_Domingo
America/Sao_Paulo
America/Scoresbysund
America/Shiprock
America/St_Barthelemy
America/St_Johns
America/St_Kitts
America/St_Lucia
America/St_Thomas
America/St_Vincent
America/Swift_Current
America/Tegucigalpa
America/Thule
America/Thunder_Bay
America/Tijuana
America/Toronto
America/Tortola
America/Vancouver
America/Virgin
America/Whitehorse
America/Winnipeg
America/Yakutat
America/Yellowknife
Antarctica/Casey
Antarctica/Davis
Antarctica/DumontDUrville
Antarctica/Mawson
Antarctica/McMurdo
Antarctica/Palmer
Antarctica/Rothera
Antarctica/South_Pole
Antarctica/Syowa
Antarctica/Vostok
Arctic/Longyearbyen
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
Asia/Ashkhabad
Asia/Baghdad
Asia/Bahrain
Asia/Baku
Asia/Bangkok
Asia/Beirut
Asia/Bishkek
Asia/Brunei
Asia/Calcutta
Asia/Choibalsan
Asia/Chongqing
Asia/Chungking
Asia/Colombo
Asia/Dacca
Asia/Damascus
Asia/Dhaka
Asia/Dili
Asia/Dubai
Asia/Dushanbe
Asia/Gaza
Asia/Harbin
Asia/Ho_Chi_Minh
Asia/Hong_Kong
Asia/Hovd
Asia/Irkutsk
Asia/Istanbul
Asia/Jakarta
Asia/Jayapura
Asia/Jerusalem
Asia/Kabul
Asia/Kamchatka
Asia/Karachi
Asia/Kashgar
Asia/Kathmandu
Asia/Katmandu
Asia/Kolkata
Asia/Krasnoyarsk
Asia/Kuala_Lumpur
Asia/Kuching
Asia/Kuwait
Asia/Macao
Asia/Macau
Asia/Magadan
Asia/Makassar
Asia/Manila
Asia/Muscat
Asia/Nicosia
Asia/Novokuznetsk
Asia/Novosibirsk
Asia/Omsk
Asia/Oral
Asia/Phnom_Penh
Asia/Pontianak
Asia/Pyongyang
Asia/Qatar
Asia/Qyzylorda
Asia/Rangoon
Asia/Riyadh
Asia/Riyadh87
Asia/Riyadh88
Asia/Riyadh89
Asia/Saigon
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Taipei
Asia/Tashkent
Asia/Tbilisi
Asia/Tehran
Asia/Tel_Aviv
Asia/Thimbu
Asia/Thimphu
Asia/Tokyo
Asia/Ujung_Pandang
Asia/Ulaanbaatar
Asia/Ulan_Bator
Asia/Urumqi
Asia/Vientiane
Asia/Vladivostok
Asia/Yakutsk
Asia/Yekaterinburg
Asia/Yerevan
Atlantic/Azores
Atlantic/Bermuda
Atlantic/Canary
Atlantic/Cape_Verde
Atlantic/Faeroe
Atlantic/Faroe
Atlantic/Jan_Mayen
Atlantic/Madeira
Atlantic/Reykjavik
Atlantic/South_Georgia
Atlantic/St_Helena
Atlantic/Stanley
Australia/ACT
Australia/Adelaide
Australia/Brisbane
Australia/Broken_Hill
Australia/Canberra
Australia/Currie
Australia/Darwin
Australia/Eucla
Australia/Hobart
Australia/LHI
Australia/Lindeman
Australia/Lord_Howe
Australia/Melbourne
Australia/NSW
Australia/North
Australia/Perth
Australia/Queensland
Australia/South
Australia/Sydney
Australia/Tasmania
Australia/Victoria
Australia/West
Australia/Yancowinna
BET
BST
Brazil/Acre
Brazil/DeNoronha
Brazil/East
Brazil/West
CAT
CET
CNT
CST
CST6CDT
CTT
Canada/Atlantic
Canada/Central
Canada/East-Saskatchewan
Canada/Eastern
Canada/Mountain
Canada/Newfoundland
Canada/Pacific
Canada/Saskatchewan
Canada/Yukon
Chile/Continental
Chile/EasterIsland
Cuba
EAT
ECT
EET
EST
EST5EDT
Egypt
Eire
Etc/GMT
Etc/GMT+0
Etc/GMT+1
Etc/GMT+10
Etc/GMT+11
Etc/GMT+12
Etc/GMT+2
Etc/GMT+3
Etc/GMT+4
Etc/GMT+5
Etc/GMT+6
Etc/GMT+7
Etc/GMT+8
Etc/GMT+9
Etc/GMT-0
Etc/GMT-1
Etc/GMT-10
Etc/GMT-11
Etc/GMT-12
Etc/GMT-13
Etc/GMT-14
Etc/GMT-2
Etc/GMT-3
Etc/GMT-4
Etc/GMT-5
Etc/GMT-6
Etc/GMT-7
Etc/GMT-8
Etc/GMT-9
Etc/GMT0
Etc/Greenwich
Etc/UCT
Etc/UTC
Etc/Universal
Etc/Zulu
Europe/Amsterdam
Europe/Andorra
Europe/Athens
Europe/Belfast
Europe/Belgrade
Europe/Berlin
Europe/Bratislava
Europe/Brussels
Europe/Bucharest
Europe/Budapest
Europe/Chisinau
Europe/Copenhagen
Europe/Dublin
Europe/Gibraltar
Europe/Guernsey
Europe/Helsinki
Europe/Isle_of_Man
Europe/Istanbul
Europe/Jersey
Europe/Kaliningrad
Europe/Kiev
Europe/Lisbon
Europe/Ljubljana
Europe/London
Europe/Luxembourg
Europe/Madrid
Europe/Malta
Europe/Mariehamn
Europe/Minsk
Europe/Monaco
Europe/Moscow
Europe/Nicosia
Europe/Oslo
Europe/Paris
Europe/Podgorica
Europe/Prague
Europe/Riga
Europe/Rome
Europe/Samara
Europe/San_Marino
Europe/Sarajevo
Europe/Simferopol
Europe/Skopje
Europe/Sofia
Europe/Stockholm
Europe/Tallinn
Europe/Tirane
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vaduz
Europe/Vatican
Europe/Vienna
Europe/Vilnius
Europe/Volgograd
Europe/Warsaw
Europe/Zagreb
Europe/Zaporozhye
Europe/Zurich
GB
GB-Eire
GMT
GMT0
Greenwich
HST
Hongkong
IET
IST
Iceland
Indian/Antananarivo
Indian/Chagos
Indian/Christmas
Indian/Cocos
Indian/Comoro
Indian/Kerguelen
Indian/Mahe
Indian/Maldives
Indian/Mauritius
Indian/Mayotte
Indian/Reunion
Iran
Israel
JST
Jamaica
Japan
Kwajalein
Libya
MET
MIT
MST
MST7MDT
Mexico/BajaNorte
Mexico/BajaSur
Mexico/General
Mideast/Riyadh87
Mideast/Riyadh88
Mideast/Riyadh89
NET
NST
NZ
NZ-CHAT
Navajo
PLT
PNT
PRC
PRT
PST
PST8PDT
Pacific/Apia
Pacific/Auckland
Pacific/Chatham
Pacific/Easter
Pacific/Efate
Pacific/Enderbury
Pacific/Fakaofo
Pacific/Fiji
Pacific/Funafuti
Pacific/Galapagos
Pacific/Gambier
Pacific/Guadalcanal
Pacific/Guam
Pacific/Honolulu
Pacific/Johnston
Pacific/Kiritimati
Pacific/Kosrae
Pacific/Kwajalein
Pacific/Majuro
Pacific/Marquesas
Pacific/Midway
Pacific/Nauru
Pacific/Niue
Pacific/Norfolk
Pacific/Noumea
Pacific/Pago_Pago
Pacific/Palau
Pacific/Pitcairn
Pacific/Ponape
Pacific/Port_Moresby
Pacific/Rarotonga
Pacific/Saipan
Pacific/Samoa
Pacific/Tahiti
Pacific/Tarawa
Pacific/Tongatapu
Pacific/Truk
Pacific/Wake
Pacific/Wallis
Pacific/Yap
Poland
Portugal
ROK
SST
Singapore
SystemV/AST4
SystemV/AST4ADT
SystemV/CST6
SystemV/CST6CDT
SystemV/EST5
SystemV/EST5EDT
SystemV/HST10
SystemV/MST7
SystemV/MST7MDT
SystemV/PST8
SystemV/PST8PDT
SystemV/YST9
SystemV/YST9YDT
Turkey
UCT
US/Alaska
US/Aleutian
US/Arizona
US/Central
US/East-Indiana
US/Eastern
US/Hawaii
US/Indiana-Starke
US/Michigan
US/Mountain
US/Pacific
US/Pacific-New
US/Samoa
UTC
Universal
VST
W-SU
WET
Zulu

Determine the Time Difference Between Two Timezones in Java

To calculate the time difference between two different timezones, we can use a java.util.Calendar class to get the current time at a given timezone, including the local timezone. The difference in hours can then be calculated by subtracting the hour of day of two Calendar instances. Looking at the day of month allows you to correct for the case when the current time spans two days by adding 24.


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

// Java 1.4+ Compatible
//
//The following example code demonstrates how to calculate
//the time difference between two different timezones, in
// particular New York and Alaska, given the current time.
// Since daylight savings time effective dates are different
// between countries, it's important to calculate the
// timezone difference at a particular date. Calculating
// the difference between the local timezone and a different
// one is also demonstrated.
// and a different one is also demonstrated.

public class DifferenceBetweenTwoTimezones {

public static void main(String[] args) {

// East Coast Time
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
c.setTimeInMillis(new Date().getTime());
int EastCoastHourOfDay = c.get(Calendar.HOUR_OF_DAY);
int EastCoastDayOfMonth = c.get(Calendar.DAY_OF_MONTH);

// Alaska
c = new GregorianCalendar(TimeZone.getTimeZone("America/Anchorage"));
c.setTimeInMillis(new Date().getTime());
int AlaskaHourOfDay = c.get(Calendar.HOUR_OF_DAY);
int AlaskaDayOfMonth = c.get(Calendar.DAY_OF_MONTH);

// Difference between New York and Alaska
int hourDifference = EastCoastHourOfDay - AlaskaHourOfDay;
int dayDifference = EastCoastDayOfMonth - AlaskaDayOfMonth;
if (dayDifference != 0) {
hourDifference = hourDifference + 24;
}
System.out.println(hourDifference);

// Local Time
int localHourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int localDayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

// Difference between New York and Local Time (for me Germany)
hourDifference = EastCoastHourOfDay - localHourOfDay;
dayDifference = EastCoastDayOfMonth - localDayOfMonth;
if (dayDifference != 0) {
hourDifference = hourDifference + 24;
}
System.out.println(hourDifference);

}

}




Here is the output of the example code:
4
-6

See also: Get All Timezones Available in the TimeZone Class in Java

Tuesday, July 13, 2010

Format a Decimal Number According to Local Formatting Standards in Java

To format a decimal number (double) according to different number formatting standards in different countries we can use the java.text.DecimalFormat, java.text.NumberFormat, and java.util.Locale classes. The following code example demonstrates formatting a decimal number in various country-specific styles.


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

//Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String according to different 
// number formatting standards in various countries
//
public class FormatDecimalLocalFormat {

public static void main(String[] args) {

// circumference of earth in km
double number = 40075.168776;

// Germany
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
System.out.println(df.format(number));

// United states
df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
System.out.println(df.format(number));

// China
df = (DecimalFormat) NumberFormat.getInstance(Locale.CHINESE);
System.out.println(df.format(number));

// France
df = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);
System.out.println(df.format(number));

}
}



Here is the output of the example code:
40.075,169
40,075.169
40,075.169
40 075,169

Format a Decimal Number Using Scientific Notation in Java

To format a decimal number (double) using scientific notation in Java we can use the java.text.DecimalFormat class. The following code example demonstrates formatting the number of decimal digits and the number exponent digits of a decimal number in scientific notation.


import java.text.DecimalFormat;

// Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String in scientific notation setting
// the number of digits after the decimal place and in the exponent
//
public class FormatDecimalScientificNotation {

public static void main(String[] args) {

// speed of light in m/s
double number = 299792458;

// scientific notation, three decimal places, one exponent digit
DecimalFormat df = new DecimalFormat("0.000E0");
System.out.println(df.format(number));

// scientific notation, lower case e, two decimal places, two exponent digits
df = new DecimalFormat("0.00E00");
System.out.println(df.format(number).toLowerCase());

}

}



Here is the output of the example code:
2.998E8
3.00e08

Format a Decimal Number (Double) in Java

To format a decimal number (double) in Java we can use the java.text.DecimalFormat class. The following code example demonstrates formatting the number of decimal digits and the thousands separator of a decimal number.


import java.text.DecimalFormat;

// Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String setting the number of digits 
// after the decimal place and the separator between the thousands groups
//
public class FormatDecimalNumber {

public static void main(String[] args) {

// circumference of earth in km
double number = 40075.1646;

// thousands separator, three decimal places
DecimalFormat df = new DecimalFormat("###,###.###");
System.out.println(df.format(number));

// no thousands separator, two decimal places
df = new DecimalFormat(".##");
System.out.println(df.format(number));

}
}



Here is the output of the example code:
40,075.165
40075.16

Saturday, July 10, 2010

Convert an Array into a List in Java

To convert an Array into a java.util.List we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a List of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* Java 1.4+ Compatible 

* The following example code demonstrates converting an Array of Strings to a List of Strings
*/
public class Array2List {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to List
List l = Arrays.asList(sa);

// iterate over each element in List
Iterator iterator = l.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into a LinkedList in Java

To convert an Array into a java.util.LinkedList we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a LinkedList of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

/**
* Java 1.4+ Compatible
* The following example code demonstrates converting an Array of Strings to a LinkedList of Strings
*/
public class Array2LinkedList {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to LinkedList
LinkedList ll = new LinkedList(Arrays.asList(sa));

// iterate over each element in LinkedList
Iterator iterator = ll.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into a Vector in Java

To convert an Array into a java.util.Vector we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a Vector of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
* Java 1.4+ Compatible
* The following example code demonstrates converting an Array of Strings to a Vector of Strings
*/
public class Array2Vector {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to Vector
Vector v = new Vector(Arrays.asList(sa));

// iterate over each element in Vector
Iterator iterator = v.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into an ArrayList in Java

To convert an Array into an java.util.ArrayList we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to an ArrayList of Strings.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
* Java 1.4+ Compatible 

* The following example code demonstrates converting an Array of Strings to an ArrayList of Strings
*/
public class Array2ArrayList {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to ArrayList
ArrayList al = new ArrayList(Arrays.asList(sa));

// iterate over each element in ArrayList
Iterator iterator = al.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Tuesday, July 6, 2010

E to the 0th Power Equals 1

Here's a little mathematical proof of e^0=1 that I had laying around for a while. It's generalized to x^0=1 because anything to the power of zero equals one.



See also: Number Patterns

Tuesday, June 29, 2010

Beyond Transistor Scaling

A friend recently sent me this link about the future of transistors and some ideas of how to reduce chip power consumption once transistors can't be made any smaller. A good one-hour (pre)review of electrical engineering...

Monday, June 21, 2010

Using Simple JFace Message Boxes in an Eclipse RCP Application

This article shows how to use JFace messages boxes in an RCP application and builds off of a clean RCP Application with a View. The full source code is found at the end of the article. Most if not all GUI applications frequently use message boxes to give or get information to or from the user. Jface provides us with 5 commonly used message boxes which can be conveniently used via a single static method. The available message boxes are:

  • MessageDialog.openQuestion()
  • MessageDialog.openInformation()
  • MessageDialog.openWarning()
  • MessageDialog.openConfirm()
  • MessageDialog.openError()
Each method takes as arguments a Shell that the message box belongs to, the title of the message box, and the message to be displayed in the message box. They each display an icon such as a question mark or a exclamation mark, etc. (For some reason the openQuestion message box displayes an exclamation point on a Mac?!?) and a button or buttons for the user to close the box or respond to a question. The openQuestion and openConfirm message boxes return a boolean corresponding to the users selection.



To demonstrate the JFace Message Boxes used in an Eclipse RCP application, a view containing a single button is created. When the button is clicked, a Question message box pops up, and depending on the boolean value returned, opens up either a Warning or an Information message box.

Step 0: Create an Eclipse RCP Application with a View.

Step 1: Add code to the createPartControl() method in the application's view to add a button and set its action listener, coding the calls to the JFace message box static methods.

package com.eclipsercptutorials.jfacemessageboxes;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

public static final String ID = "com.eclipsercptutorials.JFaceMessageBoxes.MainView"; // the ID needs to match the id set in the view's properties

public MainView() {}

public void createPartControl(Composite parent) {

Composite lMainViewComposite = new Composite(parent, SWT.NONE);

Button lButton = new Button(lMainViewComposite, SWT.PUSH);
lButton.setText("Question");
lButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){

Shell lShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

boolean isHavingFun = MessageDialog.openQuestion(lShell, "Information", "Are you having fun?");

if(isHavingFun){

MessageDialog.openInformation(lShell, "Information", "You are having fun! Java is awesome.");

}else{

MessageDialog.openWarning(lShell, "Warning", "You are NOT having fun! Go for a walk and get some fresh air.");

}
}
});

lMainViewComposite.setLayout(new RowLayout()); // this sets a Layout for the view's Composite

}

public void setFocus() {}

}


Step 2. Run the application and test if everything worked. Your application should now have a view with a single button that triggers a JFace question message box to pop up, and depending on the boolean value returned, opens up either a Warning or an Information message box:



Piece of Cake!!

<--- Previous - Add a Menu to a View in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

Sunday, June 6, 2010

Add a Menu to a View in an Eclipse RCP Application

This article shows how to add a menu to a View and builds off of a clean RCP Application with a View. The full source code is found at the end of the article. Eclipse RCP applications such as the Eclipse IDE, can have a toolbar and/or, as demonstrated here, a drop down menu located at the top of any View. With just a few lines of code you can easily add a menu to a View.

Step 0: Create a Eclipse RCP Application with a View



Step 1: Create an Action that defines what will happen when the action in the menu is clicked. Make a new class that extends org.eclipse.jface.action.Action and implements IWorkbenchAction. The class needs a private static final String property used to set the ID in the constructor. Inside the run() method is where you put your custom code. In this "CustomAction" example, code is added to the run() method that opens up a JFace MessageDialog box using a handy static convenience method of the MessageDialog class.
package com.eclipsercptutorials.addviewmenu;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

public class CustomAction extends Action implements IWorkbenchAction{

private static final String ID = "com.timmolter.helloWorld.CustomAction";

public CustomAction(){
setId(ID);
}

public void run() {

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
String dialogBoxTitle = "Message";
String message = "You clicked the custom action from the menu!";
MessageDialog.openInformation(shell, dialogBoxTitle, message);
}

public void dispose() {}

}


Step 2: Add the menu with its Action and icon to the View. In the View's createPartControl method new-up the CustomAction, and set the Action's tool-tip text and icon. In this example, a .png icon image called "bomb.png" was added to the icons folder in the plugin project. To find out where to find nice icons for Eclipse RCP projects see: Add an Icon to an Eclipse RCP Application View. Add the Action to the View's ToolBarManager using the getViewSite().getActionBars().getMenuManager().add(lCustomAction) method. Here the CustomAction was added to the same menu three times just to fill out the menu a bit, but you would of course add a set of several different Actions. Use a Separator in the menu to organize the Actions.
package com.eclipsercptutorials.addviewmenu;

import org.eclipse.jface.action.Separator;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

public static final String ID = "com.eclipsercptutorials.addViewMenu.MainView"; // the ID needs to match the id set in the view's properties

public MainView() { }

public void createPartControl(Composite parent) {

// Custom Action for the View's Menu
CustomAction lCustomAction = new CustomAction();
lCustomAction.setText("Open Dialog Box");
lCustomAction.setImageDescriptor(Activator.getImageDescriptor("icons/bomb.png"));
getViewSite().getActionBars().getMenuManager().add(lCustomAction);
getViewSite().getActionBars().getMenuManager().add(new Separator()); //Add a horizontal separator
getViewSite().getActionBars().getMenuManager().add(lCustomAction);
getViewSite().getActionBars().getMenuManager().add(lCustomAction);

}

public void setFocus() { }

}


Step 3: Run the application and test if everything worked. Your application should now have a View with a menu containing several Actions with a Separator. Clicking on one of the Actions opens up a MessageDialog. If you're not seeing it, nor the View's tab and title for that matter, make sure you specify that the title of the View should be shown in the layout.addStandaloneView() method of the Perspective's createInitialLayout() method - the second argument of the addStandaloneView() should be set to true.
package com.eclipsercptutorials.addviewmenu;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

import com.eclipsercptutorials.addviewmenu.MainView;

public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {

layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f, layout.getEditorArea());
layout.setEditorAreaVisible(false); //hide the editor in the perspective
}

}




Piece of Cake!!

<--- Previous - Add a Toolbar to a View in an Eclipse RCP Application
---> Next - Using Simple JFace Message Boxes in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

Saturday, May 22, 2010

Add a Toolbar to a View in an Eclipse RCP Application

This article shows how to add a toolbar to a View and builds off of a clean RCP Application with a View. The full source code is found at the end of the article. Eclipse RCP applications such as the Eclipse IDE, can have a drop down menu and/or, as demonstrated here, a toolbar located at the top of any View. With just a few lines of code you can easily add an icon to a View's toolbar and have it activate an Action when clicked.

Step 0: Create a RCP Application with a View.

Step 1: Create an Action that defines what will happen when the icon in the toolbar is clicked. Make a new class that extends org.eclipse.jface.action.Action and implements IWorkbenchAction. The class needs a private static final String property used to set the ID in the constructor. Inside the run() method is where you put your custom code. In this "CustomAction" example, code is added to the run() method that opens up a JFace MessageDialog box using a handy static convenience method of the MessageDialog class.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

public class CustomAction extends Action implements IWorkbenchAction{

private static final String ID = "com.timmolter.helloWorld.CustomAction";

public CustomAction(){
setId(ID);
}

public void run() {

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
String dialogBoxTitle = "Message";
String message = "You clicked something!";
MessageDialog.openInformation(shell, dialogBoxTitle, message);

}

public void dispose() {}

}



Step 2:Add the toolbar with its Action and icon to the View. In the View's createPartControl method new-up the Action, and set the Action's tool-tip text and icon. In this example, a .png icon image called "bomb.png" was added to the icons folder in the plugin project. To find out where to find nice icons for Eclipse RCP projects see: Add an Icon to an Eclipse RCP Application View. Add the Action to the View's ToolBarManager using the getViewSite().getActionBars().getToolBarManager().add(lCustomAction) method.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

public static final String ID = "com.eclipsercptutorials.addViewToolBar.MainView"; // the ID needs to match the id set in the view's properties

public MainView() { }

public void createPartControl(Composite parent) {

// Custom Action for the View's Menu
CustomAction lCustomAction = new CustomAction();
lCustomAction.setText("Open Dialog Box");
lCustomAction.setImageDescriptor(Activator.getImageDescriptor("icons/bomb.png"));
getViewSite().getActionBars().getToolBarManager().add(lCustomAction);

}

public void setFocus() { }

}

Step 3: Run the application and test if everything worked. Your application should now have a View with an icon in the View's toolbar, which opens up a MessageDialog when clicked. If you're not seeing it, nor the View's tab and title for that matter, make sure you specify that the title of the View should be shown in the layout.addStandaloneView() method of the Perspective's createInitialLayout() method - the second argument of the addStandaloneView() should be set to true.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {

layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f, layout.getEditorArea());
layout.setEditorAreaVisible(false); //hide the editor in the perspective

}
}




Piece of Cake!!

<--- Previous - Animation in Eclipse RCP Applications - A Bouncing Ball
---> Next - Add a Menu to a View in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

Wednesday, May 12, 2010

Animation in Eclipse RCP Applications - A Bouncing Ball

This article shows how to add animation to an Eclipse RCP Application and builds off of a clean RCP Application with a View. To demonstrate the animation, code is created that makes an image of the moon bounce around the view, using real physics equations to control the acceleration and spin. If you want a more in depth explanation of adding an image or updating the GUI from a worker thread please see the previous articles: Add an Image to an Eclipse RCP Application and Updating a Widget in an Eclipse RCP Application from a Worker Thread.

Step 0: Create a Hello World with Eclipse RCP Application and add a View to it.

Step 1: Add an Image to the view . For this tutorial, an image of the moon with a transparent background called moon.png was prepared and added to the project.





Step 2: Create an inner class that implements Runnable, that updates the contents of the view at a regular interval. Later, during the initialization of the view, this worker thread is started, which calls the update() method of the view every ~15 milliseconds. The speed of the animation can be controlled by the TIMER_INTERVAL variable, which defines how long the thread should sleep before waking up and calling the update() method again.
class AnimatorThread implements Runnable{

// The timer interval in milliseconds
private static final int   TIMER_INTERVAL = 14;

public void go(){

Thread t = new Thread(this);
t.start();

}

public void run() {
try {
while(true){
animate();
Thread.sleep(TIMER_INTERVAL);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


Step 3: Create the view's contents and start the worker thread. In the createPartControl() method of the view, create a Canvas where the image will be painted. Set the canvas's background color and add a paintListener to it with code defining where the image of the moon should be painted. Finally, new up the worker thread class and start it.

public void createPartControl(Composite parent) {

parent.setBackground(new Color(parent.getDisplay(), 205, 38, 38));

// Create the canvas for drawing
canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
canvas.setBackground(new Color(parent.getDisplay(), 0,0,0));
canvas.addPaintListener( new PaintListener() {

public void paintControl(PaintEvent e) {

GC gc = e.gc;
Transform trans = new Transform(e.display );
gc.getTransform( trans );
trans.translate( x, y );
trans.translate( IMAGE_WIDTH / 2f, IMAGE_WIDTH / 2f );
trans.rotate( a );
trans.translate( -IMAGE_WIDTH / 2f, -IMAGE_WIDTH / 2f );
gc.setTransform( trans );
trans.dispose();

gc.drawImage( moon, 0, 0, moon.getBounds().width, moon.getBounds().height, 0, 0, IMAGE_WIDTH, IMAGE_WIDTH); // Draw the moon

}
});

AnimatorThread at = new AnimatorThread();
at.go();
}


Step 4: Create the physics for the bouncing of the ball. Add some constants and variables as private members of the view class and an animate() method which calculates the next position of the moon. Last but not least, force a redraw of the canvas at the end of the animate() method. This invokes the code that was defined in the canvas's PaintListener.

public void animate() {

Display.getDefault().asyncExec(new Runnable(){

public void run(){

try{

float left = x;
float top = y;

// Determine the ball's location
directionY += GRAVITY;
x += directionX;
y += directionY;
a += directionA;

// Determine out of bounds
Rectangle rect = canvas.getClientArea();
if ( x > rect.width - IMAGE_WIDTH ) {
x = rect.width - IMAGE_WIDTH;
directionX = -directionX;
directionA -= ( directionY - directionA ) * FRICTION_WALL;
}
if ( x < 0 ) {
x = 0;
directionX = -directionX;
directionA += ( directionY - directionA ) * FRICTION_WALL;
}

if ( y > rect.height - IMAGE_WIDTH ) {
directionY = (int) ( -GRAVITY * Math.sqrt( ( 1 + 8 * ( rect.height - IMAGE_WIDTH ) / GRAVITY ) ) / 2 );
y = rect.height - IMAGE_WIDTH;
directionA += ( directionX - directionA ) * FRICTION_FLOOR;
}

float right = left + IMAGE_WIDTH;
float bottom = top + IMAGE_WIDTH;
if ( x < left )
left = x;
else
right = x + IMAGE_WIDTH;
if ( y < top )
top = y;
else
bottom = y + IMAGE_WIDTH;

// Force a redraw
canvas.redraw( (int) Math.floor( left ) - 1, (int) Math.floor( top ) - 1, (int) ( Math.ceil( right ) - Math.floor( left ) ) + 2, (int) ( Math.ceil( bottom ) - Math.floor( top ) ) + 2, false );

}catch(SWTException e){
//eat it!
}
}
});

}
Step 5: Run the application and test if everything worked. Your application should now have an image of the moon in the view that bounces back and forth across the view. As you resize the view, the moon's boundaries are recalculated. Here's the full code of the view:
package com.eclipsercptutorials.animation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

public static final String ID = "com.eclipsercptutorials.animation.mainView"; // the ID needs to match the id set in the view's properties

// The image
private Image moon;

// The image width
private static int IMAGE_WIDTH = 85;

// Rate of downward acceleration per frame
private static final float GRAVITY        = .25f;

// Coefficient of friction
private static final float FRICTION_FLOOR = 5f / 9f;
private static final float FRICTION_WALL  = 5f / 11f;

// The location of the "ball"
private float              x              = 0;
private float              y              = 0;
private float              a              = 0;

// The direction the "ball" is moving
private float              directionX     = 4;
private float              directionY     = 0;
private float              directionA     = 0;

// We draw everything on this canvas
private Canvas             canvas;

public MainView() {

moon = Activator.getImageDescriptor("icons/moon.png").createImage();

}

public void createPartControl(Composite parent) {

parent.setBackground(new Color(parent.getDisplay(), 205, 38, 38));

// Create the canvas for drawing
canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
canvas.setBackground(new Color(parent.getDisplay(), 0,0,0));
canvas.addPaintListener( new PaintListener() {

public void paintControl(PaintEvent e) {

GC gc = e.gc;
Transform trans = new Transform(e.display );
gc.getTransform( trans );
trans.translate( x, y );
trans.translate( IMAGE_WIDTH / 2f, IMAGE_WIDTH / 2f );
trans.rotate( a );
trans.translate( -IMAGE_WIDTH / 2f, -IMAGE_WIDTH / 2f );
gc.setTransform( trans );
trans.dispose();

gc.drawImage( moon, 0, 0, moon.getBounds().width, moon.getBounds().height, 0, 0, IMAGE_WIDTH, IMAGE_WIDTH); // Draw the moon

}
});

AnimatorThread at = new AnimatorThread();
at.go();
}

public void animate() {

Display.getDefault().asyncExec(new Runnable(){

public void run(){

try{

float left = x;
float top = y;

// Determine the ball's location
directionY += GRAVITY;
x += directionX;
y += directionY;
a += directionA;

// Determine out of bounds
Rectangle rect = canvas.getClientArea();
if ( x > rect.width - IMAGE_WIDTH ) {
x = rect.width - IMAGE_WIDTH;
directionX = -directionX;
directionA -= ( directionY - directionA ) * FRICTION_WALL;
}
if ( x < 0 ) {
x = 0;
directionX = -directionX;
directionA += ( directionY - directionA ) * FRICTION_WALL;
}

if ( y > rect.height - IMAGE_WIDTH ) {
directionY = (int) ( -GRAVITY * Math.sqrt( ( 1 + 8 * ( rect.height - IMAGE_WIDTH ) / GRAVITY ) ) / 2 );
y = rect.height - IMAGE_WIDTH;
directionA += ( directionX - directionA ) * FRICTION_FLOOR;
}

float right = left + IMAGE_WIDTH;
float bottom = top + IMAGE_WIDTH;
if ( x < left )
left = x;
else
right = x + IMAGE_WIDTH;
if ( y < top )
top = y;
else
bottom = y + IMAGE_WIDTH;

// Force a redraw
canvas.redraw( (int) Math.floor( left ) - 1, (int) Math.floor( top ) - 1, (int) ( Math.ceil( right ) - Math.floor( left ) ) + 2, (int) ( Math.ceil( bottom ) - Math.floor( top ) ) + 2, false );

}catch(SWTException e){
//eat it!
}
}
});

}


public void setFocus() {}

class AnimatorThread implements Runnable{

// The timer interval in milliseconds
private static final int   TIMER_INTERVAL = 14;

public void go(){

Thread t = new Thread(this);
t.start();

}

public void run() {
try {
while(true){
animate();
Thread.sleep(TIMER_INTERVAL);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


Piece of Cake!! <--- Previous - Updating a Widget in an Eclipse RCP Application from a Worker Thread ---> Next - Add a Toolbar to a View in an Eclipse RCP Application Also see: Eclipse RCP Tutorial Table of Contents