Protected variables cause subtle bugs – don’t use them

This has been said before: Protected variables are evil (blog post gone). But apparently as I’ve been doing some debugging into the Eclipse code I’m reminded it needs saying again.

Let’s suppose we write a base class

public class SimpleBase {
   protected Object value = new Integer(10);
   public printValue() {
      system.out.println(value.toString());
   }
}

public class Derived extends SimpleBase {    public Derived() {      value = null;    } } ... main() {   new SimpleBase().printNumber();   new Derived().printNumber(); }

Read More…

Be Sociable, Share!
Comments { 4 }

Don’t call overridable methods in constructors

MS has a rule about this in FxCop. PMD has a rule:

ConstructorCallsOverridableMethod. In both cases the point is to discourage the following weird behaviour. From Eclipse:
public abstract class CellEditor {
    protected CellEditor(Composite parent, int style) {
        this.style = style;
	System.out.println("CellEditor constructor");
        create(parent);
    }

    public void create(Composite parent) {         Assert.isTrue(control == null);         control = createControl(parent);    }     protected abstract Control createControl(Composite parent) }

Read More…

Be Sociable, Share!
Comments Off

Acceptance Testing and Eclipse Rich Client Applications – How to do it?

In my last post I promised a post about something other than Eclipse for my next post. I’m sorry I lied, I had the best of intentions. However the problem of acceptance testing and Rich Client applications has been rattling around in my head for some years now. A couple of years ago I tried this in .NET and didn’t have any great success. This time driven by the needs of several projects in my group and reading Bret Pettichord’s excellent seminar “Homebrew Test Automation” (PDF) I’m inspired to try again.

Read More…

Be Sociable, Share!
Comments { 1 }

Eclipse Madness – Why do fragment plugins work for some Developers and not others?

If you don’t do development work in Eclipse – stop reading this post now, come back tomorrow I promise that I will write about something else.

We’re writing our test plugins as fragments to be hosted inside the plugin they’re testing. Most of the time this works without a hitch and gives the benefit of being able to test classes and methods with package (default) visibility. We have several of these plugins that work with no problem both in my Eclipse IDE and our PDE builds. However for one developer one fragment plugin suffers the problem: Host bundle ‘xxx.xxx.xxx.common’ exists but is unresolved.

What does this mean? What is Eclipse saying? How on Earth do I debug this?

Read More…

Be Sociable, Share!
Comments { 3 }

Developing Eclipse RCP apps? Want access to Display etc will running your Unit Tests

For those of you spend your days working in Eclipse and trying to build rich client applications (RCP) the following piece of arcana will be useful. As your developing RCP applications you will undoubtedly need to test classes that require the Display, access to Graphics or other classes that require a running Application. When I first encountered this problem I thought that I was boxed in by Eclipse – but thanks for the work of David Saff you can. The secret sauce: JUnit Plugin tests.

  1. To run the tests choose ‘Run…’ either via the context menu in the package explorer or via the Run menu item.
  2. Choose JUnit Plug-in test (not plain old Junit) This provides the tests with access to their plug-in’s plugin class – very important if you want to access the filesystem or other resources under eclipse’s control (Display, …)
  3. Use the new button either top left of the dialog or via the context menu. A new test will be created that should have the plugin/package/testcase that you selected as its target.
  4. Switch to the main tab and under ‘Program to run’ switch from org.eclipse.ui.workbench to [No Application] – Headless. This avoids have an IDE popup and disappear at the start of your tests. On my machine this shaves 3-4 seconds from the test startup time.
  5. Switch to the plug-ins tab and the ‘Choose plug-ins and fragments’ radio button
  6. Click ‘Deselect All’ the existing plugins.
  7. Select your test Plugin.
  8. Click ‘Add Required’. In theory this shouldn’t make any difference in startup times since Eclipse shouldn’t be loading these extra plug-ins. In reality I can spot a one to two second difference

I strongly recommend creating one configuration for the each test plugin so that you can run all the tests for that plugin to see if changes you’ve made break any tests.

One last thought: Test plugins should be created as plug-in fragments to gain the benefit of being able to the benefit of package level access to the classes they’re testing.

BTW if you’re getting tired of the Eclipse related stuff don’t worry this will be the last for a while.

Be Sociable, Share!
Comments { 1 }

Narrow Figures – Wide Labels – More GEF discoveries

Most of you aren’t developing GEF applications on top of Eclipse RCP – in your case move along there’s nothing to see here. However if you’re one of the three people out doing GEF development, sit down I have story to tell.

I wanted a label attached to my figures that was wider than the parent figure. The label should be tied to the parent – but not be included in the parents grab handle. The previously suggested solution that I found was to stack the original figure and label in a composite (using a Flow/Toolbar Layout to position the children). Sadly that just created the Ugly Duckling (left figure) and not the Elegant one (see right).

UglyDuckling

After some more digging I discovered an interface HandleBounds (dig the crazy name), its tailor made for this situation:

HandleBounds Identifies figures which use an alternative rectangle to place their handles. Normally, handles will appear around a GraphicalEditPart’s figure’s bounds.  However, if that figure has an irregular shape, it may implement this interface to indicate that some rectangle other than its bounding rectangle should be used to place handles.

If you ignore the word irregular this solution is tailor made for our problem.

Read More…

Be Sociable, Share!
Comments Off

Eclipse/GEF More questions than answers

I’m struggling to build an GEF application in Eclipse. Unfortunately the documentation for GEF consists of half a dozen tutorials that were written between 2003-4. These tutorials are excellent as far as they go but don’t really shed light in alot of areas. So this is Anti-FAQ – its a batch of questions that should (but don’t currently have answers). I will update this posting with new questions and answers over time.

GEF Tutorials/Documentation

Current list. My favorites so far: A Shape Diagram Editor by Bo Majewski. The source code for this tutorial is included in the GEF Examples download (download missing :-(.

What’s missing no tutorial/sample covers a standalone RCP/GEF application. In particular there is no information on how to integrate into the RCP commands and menu structure.

API Questions

Retarget Actions

  1. What is a retarget action? It appears to be a wrapper around the existing SWT actions. Retarget actions Javadocs only tell you that it “tracks the active part in the workbench”. Not why this would be useful.
  2. Why do we need an action that is different from IAction?
  3. Does retarget action play nicely with 3.3 new handler code?
  4. Which actions need retarget actions and which don’t? For example the Copy has one and Print doesn’t. Why?

Integration with a plain RCP application

  1. Why use ActionContributor vs. adding Actions in the ActionBarAdvisor?

ActionBarContributor

  1. If you’re using the ActionBarContributor how do you contribute to a menu that already exists – perhaps file? In a specific place in that menu?
  2. When adding actions via an ActionBarContributor should you add the actions  to a ‘global handler’:
    • pageSite.getActionBars().setGlobalActionHandler() as per the logic editor sample **or** editor handler
    • getEditorSite().getService(IHandlerService.class).
    activateHandler() as per the shapes example?

Drawing

  1. When drawing your own figure what method should you override? paint() or paintClientArea() or paintFigure(). Finally one where I have answer (even though none of the methods provide any hints in the docs). From the Logic Designer sample code, I noticed that one that gets paintFigure() gets overridden.
  2. Need to turn on antialaising? Just do graphics.setAntialias(SWT.ON); If its inside paintFigure you don’t need to turn it off since paint() does a graphics.restoreState(); immediately after calling paintFigure();
  3. Need a linear diagonal gradient fill so that you graphical objects can mimic the correct Windows XP look and feel?imageWant to mimic the style of the rectangle to your left? Try this code:
    protected void paintFigure(Graphics graphics) {	Display display = Display.getCurrent();	Rectangle boundingRect = getBounds();
    
    	Point topLeft = boundingRect.getTopLeft();	Point bottomRight = boundingRect.getBottomRight();
    
    	Pattern pattern = new Pattern(display, topLeft.x, topLeft.y,		bottomRight.x, bottomRight.y,		ColorConstants.lightBlue, ColorConstants.darkBlue);	graphics.setBackgroundPattern(pattern);	graphics.fillRectangle(boundingRect);	graphics.setBackgroundPattern(null);	pattern.dispose();}
    
    
  4. Which naturally leads us to discover that in Eclipse 3.2, GEF 3.2.2 setBackgroundPattern() is not implemented for Scaled or Printer graphics – so you will get an exception if you try to use it.

Labels underneath Figures?

See:  Narrow Figures – Wide Labels – More GEF discoveries

Misc

Developing Eclipse RCP apps? Want access to Display etc will running your Unit Tests?

Be Sociable, Share!
Comments Off

Eclipse 0, YourKit 1

Or my pain with the Eclipse Profiling package.

My task for today – find a performance problem with our Eclipse application. Rather than use the sampling profiler built-in in to the VM (-hprof option) I wanted to use a more accurate tracing profiler. So my painful journey began.

  1. Find the Eclipse Test and Performance Tools Platform website (http://www.eclipse.org/tptp/)
  2. TPTP website hides the installation instructions – however Google found them here (http://www.eclipse.org/tptp/home/downloads/installguide/InstallGuide42.html)
  3. Using the update site (http://eclipse.org/tptp/updates/site.xml) recommend in the installation instructions, try to find the features. It’s slow, insanely slow.
  4. From 4.2.1 select the Tracing and Profiling package – get warned of errors because I don’t have all required plugins
  5. Use the Select required – get errors because it can’t find the required EMF dependency (among others)

Read More…

Be Sociable, Share!
Comments { 6 }