August 8, 2008
I was just asked if there’s a way to layout Visual Library Widgets in a Grid. There’s no layout like that in the API, but if you need that, here’s a very basic solution to get started. The Layout is not very smart and all cells are of the same size ( maxWidth & maxHeight ), but it’s a start and it gives an idea how Layouts work:
package de.eppleton.tablewidget;
import org.netbeans.api.visual.layout.Layout;
import org.netbeans.api.visual.widget.Widget;
import java.awt.*;
import java.util.*;
class TableLayout implements Layout {
private int columns;
public TableLayout(int columns) {
this.columns = columns;
}
public void layout(Widget widget) {
Collection<Widget> children = widget.getChildren();
int posX = 0;
int posY = 0;
int col = 0;
int maxWidth = 0;
int maxHeight = 0;
for (Widget child : children) {
Rectangle preferredBounds = child.getPreferredBounds();
int width = preferredBounds.width;
int height = preferredBounds.height;
if (height > maxHeight) {
maxHeight = height;
}
if (width > maxWidth) {
maxWidth = width;
}
}
for (Widget child : children) {
Rectangle preferredBounds = child.getPreferredBounds();
int x = preferredBounds.x;
int y = preferredBounds.y;
int width = preferredBounds.width;
int height = preferredBounds.height;
int lx = posX - x;
int ly = posY - y;
if (child.isVisible()) {
child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, width, height));
posX += maxWidth;
} else {
child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, 0, 0));
}
col++;
if (col == columns) {
col = 0;
posX = 0;
posY += maxHeight;
}
}
}
public boolean requiresJustification(Widget widget) {
return false;
}
public void justify(Widget widget) {
}
}
You can use it like that:
package de.eppleton.tablewidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
public class TableWidget extends Widget {
public TableWidget(Scene scene, int columns) {
super(scene);
setLayout(new TableLayout(columns));
}
}
To test it create a Scene and add this:
TableWidget table = new TableWidget(scene, 3);
table.addChild(new LabelWidget(scene, "1"));
table.addChild(new LabelWidget(scene, "2."));
table.addChild(new LabelWidget(scene, "3"));
table.addChild(new LabelWidget(scene, "4."));
table.addChild(new LabelWidget(scene, "5"));
table.addChild(new LabelWidget(scene, "6"));
table.addChild(new LabelWidget(scene, "7"));
table.addChild(new LabelWidget(scene, "8"));
table.addChild(new LabelWidget(scene, "9..."));
scene.addChild(table);
This should give you a simple grid to start with.

Posted in netbeans
No Comments »
May 25, 2008
An issue that really could be quite annoying for NetBeans Platform developers, Issue 96711, has been fixed in 6.1. Sounds like great news, but better have a look at your code so you don’t get in trouble!
The bug, or feature as some might see it, was that some classes were intentionally not loaded from the rt.jar in NetBeans platform applications although they are part of the standard JRE. That can be a good idea in some cases, where there are already newer implementations or your application depends on a specific one (JAXB is a good example). But if you didn’t know about this bug/feature it could be quite strange to have the correct jre in place but the classes simply wouldn’t get loaded. As soon as I had found out why, I liked it as a feature and used it for my Webservice clients. I provided a library wrapper module containing exactly the version of JAX-WS that I needed, and I didn’t have to care which version of java 6 my users had (or torture them with endorsed dirs ).
I just found that out that the bug has been fixed a few days ago when I tried to port an application from 6.0 to 6.1. Suddenly my Webservice clients didn’t work any more, complaining that they need JAXB 2.1 instead of 2.0 which was loaded from the bootstrap classloader. “bootstrap????? what the …?”. Then -again- I tried to find out what went wrong and -again- I had to search a lot until I finally found out that 96711 got me again.
Luckily it’s very easy to get everything to work as intended again. The attachment Jesse Glick has added to the issue shows how to prevent loading the packages via bootstrap:
OpenIDE-Module-Hide-Classpath-Packages: javax.jws.**, javax.xml.bind.**, javax.xml.stream.**, javax.xml.ws.**, javax.xml.soap.**, javax.annotation.**
When I knew the property name I also found some more information here. You simpy need to add this to your library wrapper and all dependent modules will use the classes you provided in the library wrapper instead of the standard ones. Great solution, now it’s definitely a feature and not a bug!
But what would have happened if I had used the latest version of Java 6 to test? The latest edition contains all the jars and I would probably never have recognized that my application suddenly has a bug and I would have shipped the broken version. So watch out and check your code if you have used the same workaround as I did!

Posted in Allgemein, NUGM, java, netbeans, xml
No Comments »
May 25, 2008
One of the main benefits of the NetBeans platform is the module system. Regardless of which module system is best ( my guess is there will soon be a version of NeBeans that can also run as OSGi bundles ) it’s important to have a system that enables you to create a modular architecture for your application. It’s an invitation to create a clean and maintainable architecture with defined dependencies and to create nice APIs with clearly defined and easy to use extension points. If you follow these principles others can extend your application easily. Maybe the easiest way to provide extension points in NetBeans is via the layer.xml.
In NetBeans modules the layer file is the central configuration file. NetBeans IDE uses the layer a lot to provide extension points for APIs. Objects can be created declaratively there and you can use the lookup to listen for changes. You will use it whenever you create new Actions or TopComponents. This quick tutorial shows how you can provide your own extension points via the layer:
Prerequisites:
- NetBeans (I’m using 6.1, but this will also work with older versions)
- Create a new ModuleSuite :
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Plug-in Modules. Under projects, select “NetBeans Platform Application” or ( “Module Suite Project” on older versions ) and click Next.
- In the Name and Location panel, type “layerextensionpoints” in Project Name. Change the Project Location to any directory on your computer, such as c:\mymodules. Click Finish.
- Now create four modules inside the suite “extensionpointinterface”, “messagereader”, “messageprovider1″ and “messageprovider2″:
- Choose File > New Project (Ctrl-Shift-N) again. Under Categories, select NetBeans Plug-in Modules. Under projects, select Module Project and click Next.
- In the Name and Location panel, type the name in Project Name. The default in the wizard should be to create the module underneath the directory where you just created the suite, which is fine. Click Next.
- In the Basic Module Configuration panel, replace the Code Name Base with de.eppleton.<modulename>. Click Finish.
Create a Service interface
We will use module “extensionpointinterface” to define an interface that will be used by the Service Providers as well as by the module that uses the extension point. Inside that module create interface “MessageProviderInterface” with the single method getMessage() that returns a String:
public interface MessageProviderInterface {
public String getMessage();
}
To make this part of the public API right click the project node, select API Versioning and select the check box of de.eppleton.extensionpointinterface. Now this package is accessible by other modules.
Create Service Provider implementations
Now we need some modules that implement the ExtensionPointInterface. We will use the modules “messageprovider1″ and “messageprovider2″ to do that. To implement the interface they both need a dependency on module “extensionpointinterface”. For each of them do the following:
- Right click the project node, select the “Libraries” category.
- Click “Add Dependency”.
- Select “extensionpointinterface”, and click “OK”.
Now that we have access to the interface in our modules we can implement it. Again for both modules do the following:
- Select “New” > “Java Class”.
- In the Wizard type “MessageProvider1″ or “MessageProvider2″ in the name field respectively.
- Implement the interface. Each of them should provide a different String e.g. “Hello ” in MessageProvider1 and “World!” in MessageProvider2:
8<———–MessageProvider1——————->8
import de.eppleton.extensionpointinterface.MessageProviderInterface;
public class MessageProvider1 implements MessageProviderInterface {
public String getMessage() {
return "Hello ";
}
}
8<——————————>8
8<———–MessageProvider2——————->8
import de.eppleton.extensionpointinterface.MessageProviderInterface;
public class MessageProvider2 implements MessageProviderInterface {
public String getMessage() {
return "World!";
}
}
8<——————————>8
In order to make our MessageProviders available as services add these entries in the layer of the two modules. In 6.0 and earlier version the layer should be there anyway. In 6.1 you will need to create the layer.xml yourself:
<ol>
<li>In “Important files” open “Module Manifest”.</li>
<li>In the manifest add this line: OpenIDE-Module-Layer: de/eppleton/messageprovider2/layer.xml or OpenIDE-Module-Layer: de/eppleton/messageprovider1/layer.xml respectively.
That indicates the location where you put the layer.xml later on.</li>
<li>Create the layer.xml files in their respective package. (”New” > “XML” > “XML Document”) , use “layer” in the name field and add:</li>
</ol>
<xml version="1.0" encoding="UTF-8">
<DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<ilesystem></filesystem>
In the layer files <filesystem> add:
<folder name = "MessageProviders>
<file name="de-eppleton-messageprovider1-MessageProvider1.instance" >
</file>
</folder>
and
<folder name = "MessageProviders">
<file name="de-eppleton-messageprovider2-MessageProvider2.instance" ></file>
</folder>
respectively. This will create an instance of our MessageProviders using the standard constructor. The trick is that those instances will be accessible from outside via the SystemFileSystem. The next step shows how you can access these Services without a module dependency.
<h3 id=”section-VisualDatabaseExplorer-CreateACookieAction”>Find and use Service Providers</h3>
We will use module “messagereader” to display messages from all MessageProviders. To do so we will create a TopComponent:
<ol>
<li>Set a dependency on “extensionpointinterface” as shown above.</li>
<li>Click “New” > “WindowComponent”. “Output” is ok for the location. Make it show on startup by ticking the box.</li>
<li>Enter “MessageReader” for the class name prefix and click “Finish”.</li>
<li>The TopComponent class will open in Design View. Drop a JScrollPane from the palette in the window, make it fill the whole area and add a JTextPane to it.</li>
<li>In the source view add this to the end of the constructor:</li>
</ol>
Lookup lkp = Lookups.forPath("MessageProviders");
Collection &lt;MessageProviderInterface&gt; coll = (Collection&lt;MessageProviderInterface&gt;) lkp.lookupAll(MessageProviderInterface.class);
for (Iterator&lt;MessageProviderInterface&gt; it = coll.iterator(); it.hasNext();) {
MessageProviderInterface messageProviderInterface = it.next();
jTextArea1.append(messageProviderInterface.getMessage());
}
This will lookup the folder you created via the layer file ( Lookups.forPath(”MessageProviders”) ), search for classes implementing the interface ( lookupAll(MessageProviderInterface.class) ) and call the interface method on all instances. Let’s try it out:
Run the ModuleSuite. You will see the window either displaying “Hello World!” or “World!Hello “. As we can see in this very simple example, the order in which the ServiceProviders are called can be important for the result. So in the next step I will show you a trick to guarantee the correct order:
Sort Service Providers
NetBeans provides a way to sort layer entries. This mechanism is e. g. used to provide an order for actions in menus and toolbars. Since 6.0 (I think) this is done via the position attribute. So this probably won’t work in older versions:
In the layer of the two modules add these entries:
<folder name = "MessageProviders">
<file name="de-eppleton-messageprovider1-MessageProvider1.instance" >
<attr name="position" intvalue="30"/>
</file>
</folder>
and
<folder name = "MessageProviders">
<file name="de-eppleton-messageprovider2-MessageProvider2.instance">
<attr name="position" intvalue="40"/>
</file>
</folder>
respectively.
Now the messages will always be displayed in the correct order “Hello world!”. Simply swap these values to reverse the order of the messages. When you do something like this make sure that you choose your values big enough to add Services in between the initial ones so there’s room for your application to grow. Try what happens when you set the same value for both attributes!
Summary
The intention of this tutorial is to illustrate how simple it is to implement loose coupling via the layer.xml in a NetBeans Platform application. Note that the module that uses the services has no dependencies on the modules that provide the services. And not only does NetBeans provide a very simple mechanism to provide and lookup Services, but also an easy way to declaratively order them.

Posted in NUGM, java, netbeans, xml
No Comments »
February 16, 2008

In the last blog entry of this series I explored how to connect to an imap server and show the message subjects in alist. Here’s how to read a selected message. As a prerequisite you’ll need to add the java.awt.datatransfer package to your sources as described here.
Then create a new Activity to show the Message. I named mine “MessageDisplay” and registered it in the AndroidManifest.xml:
8<————————————————->8
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”de.eppleton.mail.imap”>
<application android:icon=”@drawable/icon”>
<activity class=”.ShowInbox” android:label=”@string/app_name”>
<intent-filter>
<action android:value=”android.intent.action.MAIN” />
<category android:value=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity class=”.MessageDisplay” android:label=”message”></activity>
</application>
</manifest>
8<————————————————->8
Then implement the class:
8<————————————————->8
package de.eppleton.mail.imap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MessageDisplay extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView resultView = new TextView(this);
Bundle extras = getIntent().getExtras();
String value = “empty”;
if (extras != null) {
value = extras.getString(”message”);
}
resultView.setText(value);
setContentView(resultView);
}
}
8<————————————————->8
It simply displays the message String in a TextView. The trickiest part is to get data from your Main Activity to the subactivity. This is done via the Intent. Data is added in the main Activity to the Intent by calling putExtra(String key, Object value), and retrieved as shown above (in boldface).
Now we can use this view from the main activity (Changed part in boldface):
8<————————————————>8
package de.eppleton.mail.imap;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.mail.Message;
import javax.mail.MessagingException;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ShowInbox extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Message[] messages = new Message[] {};
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = getItem(position);
TextView resultView = null;
if (null == convertView || !(convertView instanceof TextView)) {
resultView = new TextView(super.getContext());
}
try {
resultView.setText(message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
return resultView;
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Message message = (Message) l.getAdapter().getItem(position);
String messageString = readMessage(message);
showMessage(messageString);
}
private void showMessage(String messageString) {
Intent i = new Intent(this, MessageDisplay.class);
i.putExtra(”message”, messageString);
startSubActivity(i, ACTIVITY_CREATE);
}
private String readMessage(Message message) {
String messageString = “”;
String contentType;
try {
contentType = message.getContentType();
if (contentType.startsWith(”text/plain”)
|| contentType.startsWith(”text/html”)) {
Object content = message.getContent();
if (content instanceof String) {
messageString = (String) message.getContent();
} else if (content instanceof InputStream) {
BufferedReader in = new BufferedReader(
new InputStreamReader((InputStream) content));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append(”\n”);
}
messageString = buffer.toString();
} else {
messageString = “Can’t read this!”;
}
} else
messageString = contentType;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return messageString;
}
}
8<————————————————>8
The only interesting thing here is how to receive the event from the list -that is done via overriding onListItemClick-, and how to start the MessageDisplay activity - that is done in showMessage. First the Intent is created, then the message is added to the Bundle via putExtra, and startSubActivity launches MessageDisplay. Actually startActivity should work equally well here, since we don’t implement onActivityResult() yet.
Now you can launch the Application and click the subjects to display the message text.

Posted in android, java
No Comments »
February 10, 2008
In a recent posting I showed how to read messages from an imap server and show them in a ListView. So far the ListView only showed the subject line from a String Array. There is no easy way back to the message object, so we need to change this.
I’ve improved this a little bit now after reading this article on how to customize an ArrayAdapter. The getMessages() method now returns the Message [], which is wrapped in the ArrayAdapter. To show the subject line in the list we simply override the getView method:
8<———————————————->8
package de.eppleton.mail;
import javax.mail.Message;
import javax.mail.MessagingException;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ShowInbox extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
Message[] messages = new Message[] { };
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages){
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
Message message =getItem(position);
TextView resultView = null;
if (null == convertView ||!(convertView instanceof TextView)){
resultView = new TextView(super.getContext());
}
try {
resultView.setText(message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
return resultView;
}
});
}
}
8<———————————————->8
And the ImapClient looks like this:
8<———————————————->8
package de.eppleton.mail.imap;
import java.net.ConnectException;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
public class ImapClient {
static {
Security
.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
static Session session;
static Store store;
public static Message[] getMail() throws MessagingException {
connect();
Folder folder = store.getFolder(”INBOX”);
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
return messages;
}
private static void connect() throws MessagingException {
if (null != session)
return;
java.util.Properties props = new java.util.Properties();
props.setProperty(”mail.imap.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
props.setProperty(”mail.imap.socketFactory.fallback”, “false”);
props.setProperty(”mail.imap.socketFactory.port”, “993″);
session = Session.getDefaultInstance(props);
store = session.getStore(”imap”);
store.connect(”mailserver”,”userid”,”password”);
}
}
8<———————————————->8
Well, the result looks almost the same, except multiline subbjects are supported:


Posted in android, java
4 Comments »
February 10, 2008

Here’s how to view your email inbox with Android. You need to add additional libraries to the project to get this running. Unfortunately I couldn’t get the java mail and activation libraries on the phone with the NetBeans Undroid module. If you know how to do that, let me know. I added the two mail and ativation libraries to the project and the build went fine, but when I launch the application I get a “NoClassDefFoundError” for javax/mail/Session. So I’m using the Eclipse plugin for this, until I figure out how to do it in NetBeans:
1. Create a new Android Project (new Project -> Android -> Android Project), name it “inbox”, Package “de.eppleton.inbox”, Activity Name “ShowInbox”, Application Name “inbox”
2. Create a new Class ImapClient and replace the boldface entries with your credentials:
8<———————————->8
package de.eppleton.mail;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class ImapClient {
static {
Security.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
public static String [] getMail() throws MessagingException {
java.util.Properties props = new java.util.Properties();
props.setProperty(”mail.imap.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
props.setProperty(”mail.imap.socketFactory.fallback”, “false”);
props.setProperty(”mail.imap.socketFactory.port”, “993″);
Session session = Session.getDefaultInstance(props);
Store store = session.getStore(”imap”);
store.connect(”mail.mailserver.com”, “you”, “yourpassword”);
Folder folder = store.getFolder(”INBOX”);
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
String [] titles = new String[message.length];
for (int i = 0, n = message.length; i < n; i++) {
titles[i] = message[i].getSubject();
}
// Close connection
folder.close(false);
store.close();
return titles;
}
}
8<———————————->8
3. Add the missing libraries ( Referenced Libraries -> build path -> configure build path -> Add External JARs… ). Y ou’ll need activation-1.1.jar and mail-1.4.jar from sun
4. Change the ShowInbox class to a ListActivity and add this code:
8<———————————->8
package de.eppleton.mail;
import javax.mail.MessagingException;
import de.eppleton.mail.ImapClient;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ShowInbox extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] messages = new String[] { “empty” };
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, messages));
}
}
8<———————————->8
5. Launch the application, and your done.
Update: here’s the next step, using a Message[] in the ListView
If you want to send mail check this out:
Android - Send email via GMail (actually via SMTP) « Show me the code!

Posted in android, netbeans
3 Comments »
February 10, 2008
JavaTools is a Community at dev.java.net where lots of cool projects are hosted, e.g. Hudson and it’s NetBeans plugin. This week our JavaTools Community Newsletter has lots of NetBeans news. In this issue:
- NetBeans-Blogger (Embed blogging features into NetBeans)
- Mogwai ER-Designer NG NetBeans Plugin available
- Android plugin for NetBeans
- Solaris Express developer Edition (SXDE), The only OS a developer ever needs
- NBPython (Python support for Netbeans)
… and many other tips and tricks, enjoy!