Roger's Blog | Well, I think it's interesting.

May/10

3

Experimenting With Android – Part 3

I’ve been particularly fond of using Visual Studio with a dark color scheme and the Consolas font and I wanted to use the same settings in Eclipse. It took a bit of Googling and hunting as the settings for fonts and colors are a bit more scattered in Eclipse than they are in Visual Studio.

To change the default editor font in Eclipse, select “Preferences” from the “Window” menu. From the tree on the left side of the Preferences dialog, select General->Appearance->Colors and Fonts. In the right pane tree select Basic->Text Font then click “Edit…” Select your desired font (I use Consolas 9pt.) After you’ve selected the font, several other font settings in the Preferences dialog will default to using the text font. If you expand the Java node you will see that “Java Editor Text Font” and “Properties File Editor Text Font” have both changed to match the basic text tont while “Javadoc display font” has not. You can override each font setting and have each one use a different font if you really want. Several bloggers have mentioned their preference for using the Dina font for the Console window in Visual Studio 2010; you can use it in Eclipse too for the device & emulator log (called ‘”LogCat.”) You can download the Dina font from the author’s website. To change the device & emulator log font, select Android->LogCat in the Preferences dialog, click Change and select the Dina font you downloaded.

Most of the color preferences for Java coding are under Java->Editor->Syntax Coloring and General->Editors->Text Editors but the rest are spread throughout the Preferences dialog. The easiest way I found to work with them is to start editing something (Java code, or an XML file,) then right-click the editor and select “Preferences” from the context menu. What will appear is a Preferences dialog that has been filtered to only show the entries that are relevant to the current editor. For a quick start using pre-made color themes, see this post and this post.

One other set of changes you may want to make is to change brace handling to be more like C#. The convention in Java is to place opening braces on the same line as the structure they open:

public static MainClass {
  public static void main(String[] args) {
    ...
  }
}

rather than the C# style:

public static MainClass
{
  public static void main(string[] args)
  {
    ...
  }
}

That said, I’m a believer in using the conventions of the language rather than trying to force one language to act like another.

These settings are located in the Preferences dialog under Java->Code Style->Formatter. Within this settings page you can create multiple sets of formatting options (each with their own settings) and even have different formatting sets for different projects. In fact, the level and degree of code formatting options for Java far surpasses Visual Studio for C#.

I encourage you to review the other Java preference settings. There are code style analysis settings under Java->Compiler and several CodeRush-like settings under Java->Editor. The ? icon in the lower left corner of the Preferences dialog will open context-sensitive help about the current panel (most dialogs in Eclipse have this.)

You can download my tweaks and color settings in the zip file at the bottom of this post. It includes a readme file that details how to install it into Eclipse. It’s not completely necessary but without it your work with Eclipse won’t look like the screenshots. Up to you. Now let’s move on to some code.

When we last left the coding we had a Java project called JRERestletServiceRefGenerator with a package called com.droidbaseball.svcgenerator and a class with just template code called MainClass.java:

image

At the top is the package declaration. Again, think of this like a .NET namespace. In fact if you hover the mouse over the main method the full name of the method is com.droidbaseball.svcgenerator.MainClass.main.

The main method is decorated with a Javadoc block (signaled by the /** comment) and includes the @param Javadoc tag. This is similar to the XML comment construct in C# signaled by the /// comment. For details about Javadoc see here. This automatic decoration of the generated code can be controlled in the Preferences dialog under Java->Code Style->Code Templates->Comments. The “TODO” comment in the method body is a task tag and performs a similar function as the task list in Visual Studio. You can control the generation of these generated comments in Preferences->Java->Code Style->Code Templates->Code and you can add additional task tags under Preferences->Java->Compiler->Task Tags.

The little marks to the left and right of the TODO line are annotations. The left margin is the vertical ruler and the right margin is the overview ruler. When there is an error, for example, that prevents the code from compiling it will show up in the overview ruler as a red mark; hovering over it will show a summary of the error and clicking it will jump to that line in the source file. There are several annotations; they can be controlled in Preferences->General->Editors->Text Editors->Annotations.

We’re going to add some code to the main method to call the OData service generator. Since this is pretty much a throw-away app we’ll hard code it. Remove the TODO line from the main method and add one line to the main method:

Generator.main(new String[] {
  "http://173.46.159.199/baseballstats/BaseballStats.svc",
  "C:\\projects\\BaseballService"
});

Save the file. It will look like this:

image

There are a couple of things to notice about this code. First, the Generator.main static method accepts one parameter: an array of String (arrays in Java are zero based just as in C#.) The syntax for creating an array “on-the-fly” is identical to C#. The first array element is the URL of the service and the second array element is the destination directory for the generated code (this example assumes you have the path “C:\projects\BaseballService” created. If you don’t, change this parameter as desired.) Notice the double backslashes. In Java the backslash is the escape character same as it is in C#. But there’s one important difference. In C# you could write this line like this:

Generator.main(new string[] {
  "http://173.46.159.199/baseballstats/BaseballStats.svc",
  @"C:\projects\BaseballService"
  });

The @ sign in front of the destination directory indicates a verbatim string in C#. Java has no equivalent syntax; you need to use the double-backslash syntax.

Second, notice there’s a red underline beneath Generator, and red annotations in the vertical ruler, overview ruler, and the title tab. These are all error indicators. Hovering over either ruler annotation will display the error but hovering over the red underline will show a “quick fix” window. This is a context-sensitive list of suggestions by Eclipse to fix the error (you can also display this window by putting the caret in the underlined section and pressing Control + 1.) The quick fix window for this problem is this:

image

Usually the first one in the list is the most appropriate. In this case it is complaining that it cannot resolve the reference to “Generator” and is suggesting to import the Generator class from the org.restlet.ext.odata package. If this sounds familiar, it should. It’s the same as Visual Studio complaining that the “type or namespace name could not be found” and suggesting adding a using directive. Pick the first item in the list: Import ‘Generator’ (org.restlet.ext.odata) but don’t save the file yet. It will add the line “import org.restlet.ext.odata.Generator” to the top of the source file. Notice that the error indicator in the overview ruler has disappeared, but the error indicator in the vertical ruler has simply grayed out while the error indicators in the editor’s tab and in the package explorer are still displayed in red. This shows a subtle difference between background compiling in Eclipse and Visual Studio. If you perform the equivalent steps in Visual Studio, the error annotation will go away as soon as you add the using line. This shows that background compilation occurs as soon as you stop typing for more than a moment or two. Now in Eclipse save the file. Notice now that all the error indicators disappear. This shows that, in Eclipse, the compilation will not occur until you save the file. If there is a setting in Eclipse to make it work like Visual Studio I haven’t found it yet. But I actually prefer the Eclipse way.

Now that we’ve added the import and fixed our error we can run it. By default Run is Control + F11 and Debug is F11; if you want to remap that (and the debug Step, Step-Over, and Step-Into keys) to match Visual Studio you can do that in Preferences->General->Keys. Press F11 to run it in Debug mode.

Crap! We crashed. Why? Well, here’s what we got in the console window:

Exception in thread “main” java.lang.NoClassDefFoundError: freemarker/template/Configuration

at org.restlet.ext.odata.Generator.generate(Generator.java:236)

at org.restlet.ext.odata.Generator.main(Generator.java:134)

at com.droidbaseball.svcgenerator.MainClass.main(MainClass.java:14)

Caused by: java.lang.ClassNotFoundException: freemarker.template.Configuration

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

… 3 more

There’s a “ClassNotFoundException” in there. Basically that’s Java runtime saying “there’s a reference to a class in here that the compiler didn’t know about and I don’t know where it is.” There is a reference within the Generator.main() method to the “freemarker.template.Configuration” class. If you look in the Restlet lib directory (C:\lib\restlet-jse-2.0rc3\lib) there is a readme file that details the dependencies. According to the readme when using the OData extension we need a reference to org.freemarker_2.3. To add it, right-click the project in the Package Explorer, select Build Path->Configure Build Path. Select the Libraries tab and add the external Jar C:\lib\restlet-jse-2.0rc3\lib\org.freemarker_2.3\org.freemarker.jar. After clicking OK re-run the app using F11 and it will run to completion.

So what did we end up with? If you look in C:\projects\BaseballService you’ll see BaseballStatsOdataOrcswebService.java and a directory named baseballstatsmodel that contains 18 .java files. BaseballStatsOdataOrcswebService.java is essentially the service proxy; this is equivalent to what Visual Studio’s “Add service reference” feature would generate. The 18 files in the baseballstatsmodel directory are the entity objects.

Next time we’ll setup the Android project and review its parts. Then we’ll setup the layout of the first screen.

DefaultEclipsePreferences

· ·

<<

>>

Theme Design by devolux.nh2.me