Why do developers need Code-Snippets

Many of you may already have noticed that we started a Snippet collection for JFace-Elements like the one you know from SWT. Yesterday I updated the wiki-page to show all snippets with a short description.

Are Snippets only something we provide because we love our users soooo much? To me the answer is no.

There are multiple reasons why snippets are a great thing:

  • Help for users to get started easily (Users)
  • Useful templates for users to log bugs (Users/Developers)
    Without a reproduceable testcase it is very hard to track bugs. Be prepared that JFace-Devs will insist even more on Snippets because now you have starting points to create Snippets easily. Insisting on snippets we often found out that we are not the one to blame but someone different (including the bugreporter itself)
  • Useful to mark bugs as invalid (Developers)
  • Useful to check no regressions are introduced (Developers)
    We also added snippets to our collection although their Classes are deprecated because this way we had an easy test case that we don’t break any backwards contract. In our case we did this for TableTreeViewer
  • Development example for new features (Developers)
    A good example is Snippet026 which was used to develop Cell-Navigation and Editor-Activation throughout the whole 3.3 cycle.
  • Advertise new API
Posted in 3.x | 3 Comments

I’m perFLEXed

Yesterday Adobe released it’s Flex 3 and naturally I went there to see this new cool technology. I downloaded it and got an Eclipse with a GUI-Builder to create SWF-Applications. The nice thing is that Flex uses an XML-Format to store the GUI, … .

I guess you know what’s coming now, don’t you? I sat down and looked closer to find out that it would be fairly easy to write a Transformer to convert this in my custom EXSWT-Format used to build SWT/JFace-GUIs. After about 2 hours I now have a minimal XSLT-Stylesheet transforming MXML-Elements (currently mx:label, mx:combo, mx:button) to EXSWT and feeding it into my EXSWT-Lib. The only problem are different heights and widths needed by SWT and SWF but those are minor problems.

Here’s the original Flex-Project View one can export as an SWF-Application:

And here the one displayed using SWT-Widgets:

This is only a simple example but it shows how easy it is to define highlevel dialects on top of EXSWT which is a more or less lowlevel XML-Language.

You can as always get the sources from my SVN-Repository and I have once more prepared a .psf-File for all Subclipse users.

Posted in Eclipse | 10 Comments

Splash-Screen and Threads

Today I thought it would make my application look much more professional if the login-dialog is part of the splash-screen. The new extension point added in eclipse makes this possible and even provided an template implementation I could use without problem.

The process is straight forward:

  • Create a Product Configuration (or use your existing one)
  • Switch to the Splash-Tab
  • Select in the Customization-Section the Interactive-Template
  • Switch to the Overview and “Launch an Eclipse application”

You are done. That was easy, wasn’t it? To make this work you wouldn’t need any help. The tricky thing I faced starts now. In my case I’m authentificating using a database-server and to not block the UI im doing this in a seperate thread and showing a ProgressBar in the mean while.

When the application starts up the splash should look like this:

And while Logging into Database:

So the first action is to add the “Check Login”-Label and the Progress bar like this:

private Label progressLabel;
private ProgressBar progressBar;

/** many lines of code */

private void createProgressInfo() {
  progressLabel = new Label(fCompositeLogin,SWT.NONE);
  progressLabel.setText("Überprüfung läuft");
  GridData data = new GridData();
  data.horizontalIndent = F_LABEL_HORIZONTAL_INDENT - 50;
  progressLabel.setLayoutData(data);
  progressLabel.setVisible(false);

  progressBar = new ProgressBar(fCompositeLogin,SWT.NONE|SWT.INDETERMINATE);
  data = new GridData(SWT.NONE, SWT.NONE, false, false);
  data.widthHint = F_TEXT_WIDTH_HINT;
  data.horizontalSpan = 2;
  progressBar.setLayoutData(data);
  progressBar.setVisible(false);
}

private void toggelCheckProgress(boolean state) {
  progressLabel.setVisible(state);
  progressBar.setVisible(state);
  fCompositeLogin.layout();
}

We initially set the those two widgets invisible and show them later when we start the authentification. To make this easy we add helper method to turn the visibility on and off.

The next part is to modify the handleButtonOKWidgetSelected()-method like this:

private volatile int loginStatus = -1;

/** many lines of code */

private void handleButtonOKWidgetSelected() {
  final String username = fTextUsername.getText();
  final String password = fTextPassword.getText();

  toggelCheckProgress(true);
  Thread t = new Thread() {
    public void run() {
      if( login(username,password) ) {
        loginStatus = 1;
      } else {
        loginStatus = 2;
      }
    }
  }
  t.start();
}

The content of the method is straight forward. It starts a thread and executes a potentially long running task in our case login(String,String). Our task is now to sync back to the gui-thread and:

  1. Proceed with start up (hiding the login details from the splash-screen)
  2. Display Login-Failure to the user

Normally you do this using Display#(a)syncExec() but that’s not available in the splash-screen. The work-around I used as you see above is setting a special variable named loginStatus. The trick is now that you add checks for this variable to the Event-Loop method which looks like this afterwards:

private void doEventLoop() {
  Shell splash = getSplash();
  while (fAuthenticated == false) {
    if (splash.getDisplay().readAndDispatch() == false) {
      if( loginStatus == 1 ) {
        loginSuccess();
      } else if( loginStatus == 2 ) {
        loginFailure();
      }

      splash.getDisplay().sleep();
    }
  }
}

Your are nearly done now the only two methods missing are:

private void loginSuccess() {
  toggelCheckProgress(false);
  fCompositeLogin.setVisible(false);
  fAuthenticated = true;
  loginStatus = -1;
}

private void loginFailure() {
  toggelCheckProgress(false);
  loginStatus = -1;
  MessageDialog.openError(getSplash(),"Authentification failed","Your username or password was wrong");
}

Well that’s all. You are done and have a splash screen who is authenticating without blocking the UI-thread without Display#(a)syncExec. I need to thank Kim for pointing me to this solution and she promised that there will be added API in 3.4 to make this more easier.

Posted in 3.x | 16 Comments

Things are evolving

It’s been now a few months I started reworking my the first draft implementation which already provided many interesting things but there was still Java-Code needed to bind-POJOs and to interact with the Datasource.

What’s new:

  • XBL (XmlBindingLanguage) to connect your model with the help of the databinding framework to SWT-Widgets
  • Switched to work on top of the Eclipse-Command-Framework
  • Added themeing framework
  • API cleanup
  • Refactoring, refactoring

I still see this as a research project which is not useable for production in the near future but if the community likes what I’m doing here these are the next things on my list:

  • implement INSERT/UPDATE/DELETE on top of the COMMAND-Framework
  • evolve XBL (new features, refactoring, …)
  • Transformer-Framework for EXSWT and XBL (use a highlevel dialect translated to those low-level ones)
  • Implement new provider which is able to fetch XBL and EXSWT files via HTTP. This would make changing the GUI without the need to redeploy your application to the client (what a cool feature RAP without the Browser 🙂
  • Refactoring, Refactoring, ….

If you are interested you can checkout the current project from my companies SVN-Repository. When using the Subclipse-Plugin I’ve added you a .psf-File to the project helping you to easily checkout all needed projects from SVN.

Posted in Eclipse | 1 Comment

New JFace API is in place

After ~8 months of work we checked in the new JFace-API with many exciting features in to CVS just a few hours ago. To see how powerful this new API is take a look at the this snippet. Test it and report us back problems if there are any.

Posted in 3.x | 5 Comments

XSWT, Databinding, … => DataForms

It’s sometime ago I started rewriting the current XSWT implementation from David Orme. Now I saw the Blog from Alex about Databinding and thought it’s time to let you all know that I’m working on a set of plugins which have the following focus:

  • Use XSWT (or any other Definition Language) to create the GUI
  • Dynamically link together Forms (Master/Detail, …) using Eclipse Extension Points
  • Provide an XML-Dialect to define the binding and data retrieval in XML

Although it’s still a long way to go and many many things are not as smooth as they could be but I finally have something to present using a small application.

Here’s a screenshot of the small application. Currently you can only add items to it but that’s working really fine.


The key point is how this all works technically and what should I say it simply works. There are still about 200 lines of code needed to make this work because the binding has to be done manually at the moment. Go and get the code from our companies SVN-Repository and give me feedback, I’ve added a projectSet.psf so you can easily check out all needed modules.

The next things I want to address are the following:

  • Databinding:
    • Adding missing features (Deletion, …)
    • XML-Language for Data-Retrieval and Binding (XBL)
    • Finishing Event System (= needed for scripting support)
    • Finalizing/Stabiallizing ExtensionPoints and API
    • Adding API for a highly Configurable Privileges System
    • Provide XDOM (= DOM you know from Browsers to easily script Dataforms)
  • EXSWT:
    • Transformation Chain (other highlevel dialects transformed to EXSWT which near to SWT/JFace)
    • Work on XSD-Schema and XML-Editor with Live-Preview
    • WYSIWYG-Editor

Did I spark your interest? Give me your comments, I know there are other projects around with a fairly similar focus but some of the features are unique to this idea I think but are essential for datacentric applications.

Oh in the end I have had written a design document which is not up-to-date but provide informations where I’m coming from and where I’m going to.

Posted in Eclipse | 4 Comments

EclipseCon community awards

I just read the post from Alex about the community awards and couldn’t believe finding my name under the ones finally winning the award. Thanks to everybody who voted for me. Now I’m even more disappointed that I couldn’t afford attending the eclipse con but maybe I can make it to the Eclipse Summit Europe.

Posted in Announcements | 5 Comments

My first commit

What a great day my first commit to the eclipse cvs-repository. It’s a small one patching JavaDoc for a not working API but it’s a start. Here’s the patch.

Posted in Announcements | Leave a comment

TableViewers and Nativelooking Checkboxes

Many of us have faced the same problem that if you needed to use checkboxes in your TableViewer/TreeViewer they look not native because they are pictures. I had some free minutes and thought that it’s time to create a LabelProvider which is able to create platform look-and-feel images out of the box. It’s not 100% native but it’s not far away. The trick is to automatically create screenshots from CheckBox-Buttons and use them. This way the checkboxes look native on all platforms and correspond to the current look and feel of the platform.

Here’s the code if someone is interested:

package at.bestsolution.jface.viewers;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;

public abstract class EmulatedNativeCheckBoxLabelProvider extends ColumnLabelProvider {
  private static final String CHECKED_KEY = "CHECKED";
  private static final String UNCHECK_KEY = "UNCHECKED";

  public EmulatedNativeCheckBoxLabelProvider(ColumnViewer viewer) {
    if( JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY) == null ) {
      JFaceResources.getImageRegistry().put(UNCHECK_KEY, makeShot(viewer.getControl().getShell(),false));
      JFaceResources.getImageRegistry().put(CHECKED_KEY, makeShot(viewer.getControl().getShell(),true));
    }
  }

  private Image makeShot(Shell shell, boolean type) {
    Shell s = new Shell(shell,SWT.NO_TRIM);
    Button b = new Button(s,SWT.CHECK);
    b.setSelection(type);
    Point bsize = b.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    b.setSize(bsize);
    b.setLocation(0, 0);
    s.setSize(bsize);
    s.open();

    GC gc = new GC(b);
    Image image = new Image(shell.getDisplay(), bsize.x, bsize.y);
    gc.copyArea(image, 0, 0);
    gc.dispose();

    s.close();

    return image;
  }

  public Image getImage(Object element) {
    if( isChecked(element) ) {
      return JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY).createImage();
    } else {
      return JFaceResources.getImageRegistry().getDescriptor(UNCHECK_KEY).createImage();
    }
  }

  protected abstract boolean isChecked(Object element);
}

I haven’t really tested this (currently only on WinXP) but I suppose it’s working on all platforms. You can also get the code from my svn-repository which holds some other interesting utilities and viewer classes.

Posted in 3.x | 15 Comments

The new faces of JFace (part2)

The last time I showed you the new programming model we introduced to JFace to make it feel like programming SWT.

But has this been the only reason that we decided to add this new API beside the existing one (ILabelProvider, ITableLabelProvider, IColorProvider, ITableColorProvider, IFontProvider, ITableFontProvider) .
Sure enough it was not the only reason. First of all many newcomers have been confused by all those NON MANDATORY interfaces to control different aspects of items and we faced the problem that whenever SWTProvided a new feature e.g. Owner Draw Support in 3.2 we would have to add a new interface type and to support new features e.g. ToolTip support for table/tree cells we also had to provide a new interface. We decided that this is not the way to go for the future.

So we sat down and thought about a complete new structure below JFace tree and table support. We added the idea of rows (ViewerRow) and cells (ViewerCell) abstracting common things provided of TreeItem and TableItem and completely hiding the widget specific things into this class. The abstraction level this provided to us made it possible to provide a common class named ColumnViewer.

So what have we learned so far:

  1. We don‘t need all those interfaces any more
  2. We have a new abstraction level for column based viewers (ViewerRow, ViewerCell)
  3. We have a new base class named ColumnViewer

So you may ask what you should use if you can‘t use the old interfaces any more. Well the answer is ColumnLabelProvider or if you want to implement the whole cell behaviour your own it‘s abstract base class named CellLabelProvider. The new ColumnLabelProvider combines all currently know interfaces (ILabelProvider, IFontProvider, IColorProvider). There‘s no base class visible to you supporting ITable*interfaces because those interface put limitations to tables I’ll show you later let‘s now explore how to use the new ColumnLabelProvider interface and how it is used.

TableViewer viewer = new TableViewer(parent,SWT.FULL_SELECTION);

TableViewerColumn column = new TableViewerColumn(viewer,SWT.NONE);
column.setLabelProvider(new StockNameProvider());
column.getColumn().setText("Name");

TableViewerColumn column</span> = new TableViewerColumn(viewer,SWT.NONE);
column.setLabelProvider(new StockValueProvider</span>());
column.getColumn().setText("Modification");

public class StockNameProvider extends ColumnLableProvider {
  @Override
  public String getText(Object element) {
    return ((Stock)element).name;
  }
}

public class StockValueProvider extends ColumnLabelProvider {
  @Override
  public String getText(Object element) {
    return ((Stock)element).modification.doubleValue() + "%";
  }

  @Override 
  public Color getForeground(Object element) {
    if( ((Stock)element)modiciation.doubleValue() &lt; 0 ) {
      return getDisplay().getSystemColor(SWT.COLOR_RED);
    } 
    else {
      return getDisplay().getSystemColor(SWT.COLOR_GREEN);
    }
  }
}

You may now argue that this was easier with the old ITable*-API and you are true but from the point of reusability this version is much more flexible and you can reuse your LabelProviders for many different TableViewers because they don‘t hold any index informations. Another thing is that you needed a bunch of custom code if you wanted the columns to be reordable with the old API this is not an issue any more with the new API because the LabelProvider is directly connected to the column and moves with it.

Next time I’ll continue with some nice new LabelProvider features like ToolTip support and OwnerDraw.

Posted in 3.x | 6 Comments