<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hexapixel &#187; Java</title>
	<atom:link href="http://hexapixel.com/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://hexapixel.com</link>
	<description>Programming, Eclipse, SWT and everything inbetween</description>
	<lastBuildDate>Fri, 05 Mar 2010 15:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Creating a Notification Popup Widget</title>
		<link>http://hexapixel.com/2009/06/30/creating-a-notification-popup-widget</link>
		<comments>http://hexapixel.com/2009/06/30/creating-a-notification-popup-widget#comments</comments>
		<pubDate>Tue, 30 Jun 2009 12:00:24 +0000</pubDate>
		<dc:creator>Emil</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Eclipse Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SWT]]></category>

		<guid isPermaLink="false">http://hexapixel.com/?p=760</guid>
		<description><![CDATA[In this article I will explain how to create a custom widget that displays a popup notification dialog in the bottom right corner of your screen (usually above the toolbar on Windows). Here&#8217;s what it will look like when we&#8217;re done: We could custom draw everything which would allow us more control over the widget, [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I will explain how to create a custom widget that displays a popup notification dialog in the bottom right corner of your screen (usually above the toolbar on Windows). Here&#8217;s what it will look like when we&#8217;re done:</p>
<p><img src="http://hexapixel.com/wp-content/uploads/2009/06/tray_popup_example.gif" alt="Example Image" title="Example Image" width="397" height="147" /></p>
<p>We could custom draw everything which would allow us more control over the widget, but for the sake of this article lets stick to using basic SWT components inside a normal Shell that we will make a bit prettier than your typical SWT Shell.</p>
<p>(Do note this has only been tested on Windows, it&#8217;s quite possible tweaks are necessary for other platforms).</p>
<p><span id="more-760"></span><br />
<h3>The Shell</h3>
<p>What we want is a Shell that stays on top of our application (but not on top of all applications) and doesn&#8217;t steal the active focus or else it would show up in your task manager or move focus away from where the user is now. We also don&#8217;t any normal Shell &#8220;trim&#8221; borders, we&#8217;ll draw our own. Creating it is easy as all of our requirements are available via SWT flags. Thus we can create the Shell as follows:
<pre class="java" name="code">
<pre class="syntax-highlight:java">
_shell = new Shell(Display.getDefault().getActiveShell(), SWT.NO_FOCUS | SWT.NO_TRIM);
</pre>
</pre>
<p>As we have SWT widgets inside our shell and we intend to use a gradient background on the shell itself, all the widgets need to be transparent. This is done by setting the Background Mode on the shell to <code>SWT.INHERIT_DEFAULT</code>. This tells the widgets to inherit the background of their parents, and gives us what we want. So after we create the shell we simply call;
<pre class="java" name="code">
<pre class="syntax-highlight:java">
_shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
</pre>
</pre>
<p>So how do we get a gradient background on a shell? It certainly don&#8217;t support it by default. You can set a foreground and a background but that&#8217;s about it. The trick is to paint an image with the colors we want and use that as the shell&#8217;s background. To set it at the right time, we listen to the <code>SWT.Resize</code> event on the shell and do it then with the following code;</p>
<pre class="java" name="code">
<pre class="syntax-highlight:java">
_shell.addListener(SWT.Resize, new Listener() {
    @Override
    public void handleEvent(Event e) {
	try {
	    // get the size of the drawing area
	    Rectangle rect = _shell.getClientArea();
	    // create a new image with that size
	    Image newImage = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
	    // create a GC object we can use to draw with
	    GC gc = new GC(newImage);

	    // fill background
	    gc.setForeground(_bgFgGradient);
	    gc.setBackground(_bgBgGradient);
	    gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, true);

	    // draw shell edge
	    gc.setLineWidth(2);
	    gc.setForeground(_borderColor);
	    gc.drawRectangle(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);
	    // remember to dipose the GC object!
	    gc.dispose();

	    // now set the background image on the shell
	    _shell.setBackgroundImage(newImage);

	    // remember/dispose old used iamge
	    if (_oldImage != null) {
		_oldImage.dispose();
	    }
	    _oldImage = newImage;
	}
	catch (Exception err) {
	    err.printStackTrace();
	}
    }
});
</pre>
</pre>
<h3>The Contents</h3>
<p>Our shell consists of 3 sections, an image at the top left (which is a normal CLabel). Then a label that contains the header text (also a CLabel), and finally a label which represents our message (a normal Label). We use a Label at the end instead of a CLabel as Labels support wrapping and multi-lines better than the CLabel does. You could of course use pretty much any widgets you want. Here&#8217;s an outline of what section each label covers:</p>
<p><img src="http://hexapixel.com/wp-content/uploads/2009/06/tray_popup_example_lines.gif" alt="tray_popup_example_lines" title="tray_popup_example_lines" width="350" height="99" /></p>
<p>We use a GridLayout with some 5px margins (except at the top) and the widgets are created normally without any special magic, so I won&#8217;t show that code here (it&#8217;s all at the bottom for you to download). </p>
<h3>Some Eye Candy &#8211; Fading</h3>
<p>Shells support alpha values as of SWT 3.4, so we&#8217;ll use this to make our &#8220;appearing&#8221; and &#8220;disappearing&#8221; a bit prettier. All we need to do to make a shell fade in and out is to have a thread that loops and increases/decreases the alpha channel value for the shell. As we need to be on the Display thread to do this it&#8217;s easiest to do it all via a runnable that we let the Display object run for us at a given interval. </p>
<p>We need to remember to continuously check to see if the shell is disposed as it can happen when we&#8217;re on a thread. When we&#8217;re finished fading in, we want the shell to stay visible for a certain amount of time, and then we want to fade out. So basically the chain is this:</p>
<ul>
<li>Create shell</li>
<li>Fade in shell</li>
<li>Stay visible for X seconds</li>
<li>Fade out shell</li>
<li>Dispose shell</li>
</ul>
<p>All these timers are more or less similar in terms of how the code looks, so I&#8217;ll show one example here and for the rest you can look at the complete source downloadable below. We have some global variables here that should be explained. </p>
<ul>
<li><code>FADE_IN_STEP</code> is how much we increase the alpha value for each iteration.</li>
<li><code>FINAL_ALPHA</code> is a value that declares what the final alpha value will be for the shell when it stays visible. I find that a slightly transparent shell gives a better effect, so by default this value is 225 (out of 255 which is a solid [opaque] shell).</li>
<li><code>FADE_TIMER</code> is how long we wait until increasing the Alpha value again. When the shell has reached it&#8217;s max-alpha value we call the startTimer() method, which is a thread that sleeps for a certain number of seconds before calling the fadeOut() method.</li>
</ul>
<pre class="java" name="code">
<pre class="syntax-highlight:java">
private static void fadeIn(final Shell _shell) {
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                if (_shell == null || _shell.isDisposed()) { return; }

                int cur = _shell.getAlpha();
                cur += FADE_IN_STEP;

                if (cur &gt; FINAL_ALPHA) {
                    _shell.setAlpha(FINAL_ALPHA);
                    startTimer(_shell);
                    return;
                }

                _shell.setAlpha(cur);
                Display.getDefault().timerExec(FADE_TIMER, this);
            }
            catch (Exception err) {
                err.printStackTrace();
            }
        }

    };
    Display.getDefault().timerExec(FADE_TIMER, run);
}
</pre>
</pre>
<h3>Stacked Notifications</h3>
<p>There&#8217;s one last thing we want to support, which is stacked notifications. Assume our notification shell&#8217;s entire life-span is about 5 seconds, what if we have another notify call before the first one has finished showing? We could dispose the first shell and replace it with the new one of course, but a much prettier approach is to simply move the previous shell up, and show the new notification below it. The effect of this is something like this (this screenshot was taken just as the old shells started to fade out, hence the higher alpha on them):</p>
<p><img src="http://hexapixel.com/wp-content/uploads/2009/06/tray_stacked.gif" alt="tray_stacked" title="tray_stacked" width="362" height="431" /></p>
<p>What we do to achieve this effect is rather simple. Every time a notification shell is opened, we keep a reference of it in a static array of shells. When it&#8217;s faded out (and disposed), it&#8217;s removed from the list. Thus, when a new shell is created we simply check to see what&#8217;s already in the array and then set the location for each old shell in the array to <code>-heightOfNewNotificationShell</code> (so to speak). Thus, the old shells move up to make room, but continue their fade-in/fade-out cycles just like before. </p>
<h3>Further Improvements</h3>
<p>Everything could be made prettier than it already is of course, perhaps you want an &#8220;X&#8221; close button, or different colored text, or rounded shell corners. None of that is in the current code, but the code should give you enough to go on to implement most of that without any major problems. </p>
<p>Also note that I currently use <code>Display.getDefault().getActiveShell()</code> for the parent shell of the popup. You may want to refactor it to use a more permanent shell as otherwise a disposal of whatever the parent shell was whenever the popup was displayed will cause the notification to dispose as well.</p>
<p>The code is not perfect, it&#8217;s an example to build on (if you so wish).</p>
<h3>Download Code</h3>
<p>I&#8217;ve created an Eclipse project with the code and also the ImageCache/FontCache/ColorCache classes I use in the code (all they do is keep already used images etc in memory so that they don&#8217;t need to be re-created). There&#8217;s also a jar with the images I&#8217;ve used in this example. If you like them and want more like it, there&#8217;s a massive image pack with exactly these icons and more here: <a href="http://code.google.com/p/gnome-colors/" target="_new">Gnome Colors Icons</a>.</p>
<p>Do note you may need to adjust your CLASSPATH settings for the project to reflect the location of your SWT jars.</p>
<p>To run the code, simply run the <code>Tester.java</code> class and push the big button to create a notification. Push it again before the first one fades away to see the stacking. A random image will be used every time you push it.</p>
<p><a href='http://hexapixel.com/wp-content/uploads/2009/06/notification_widget.zip'>DOWNLOAD CODE</a></p>
<h3>Conclusion</h3>
<p>Hopefully this gives you an insight into how to create a &#8220;custom widget&#8221; (without much custom painting). Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://hexapixel.com/2009/06/30/creating-a-notification-popup-widget/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>The Secondary ID</title>
		<link>http://hexapixel.com/2009/06/16/the-secondary-id</link>
		<comments>http://hexapixel.com/2009/06/16/the-secondary-id#comments</comments>
		<pubDate>Tue, 16 Jun 2009 13:36:25 +0000</pubDate>
		<dc:creator>Emil</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Eclipse Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JFace]]></category>

		<guid isPermaLink="false">http://hexapixel.com/?p=739</guid>
		<description><![CDATA[In many RCP applications your views (ViewParts) will not be singletons. Perhaps you have a tree or project view which is probably a &#8220;show only once&#8221; view, but for others it&#8217;s more likely you have them flagged in the plugin xml to say allowMultiple="true" in the &#60;view /&#62; tag. For multiple views sharing the same [...]]]></description>
			<content:encoded><![CDATA[<p>In many RCP applications your views (ViewParts) will not be singletons. Perhaps you have a tree or project view which is probably a &#8220;show only once&#8221; view, but for others it&#8217;s more likely you have them flagged in the <code>plugin xml</code> to say <code>allowMultiple="true"</code> in the <code>&lt;view /&gt;</code> tag. For multiple views sharing the same primary ID you are probably already aware that you can (and have to) set a secondary id on them to make them unique. This secondary id will stay with the view as long as that view is open (even if you restart your application as the secondary id is saved in the Eclipse metadata and then re-set when your RCP application starts up again &#8211; assuming the view is set to be restorable). </p>
<p>But there&#8217;s some things that can be a bit frustrating, which is the point of writing this article. First, as your view has to be defined in the <code>plugin.xml</code> and [should you want to] Eclipse/RCP has currently no way of hiding views in &#8220;Show View&#8221; (see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=249451">Bug 249451</a> for details and possible workaround to hiding views from &#8220;show view&#8221;). Any view opened with Show View won&#8217;t have a secondary id. </p>
<p>Second, you may want to control what that secondary id looks like, as you may want to use it as an &#8220;ID&#8221; for saving custom view-specific settings (for example, grid-specific data such as column widths, column orders etc). </p>
<p>Third, you want a central place where the creation of those multi-views are handled where you know that the ID set on them will be of the format you defined. </p>
<p>And lastly, you probably want some of them to be automatically opened in your &#8220;default&#8221; perspective with proper secondary ids. So let&#8217;s get to it and see how we can make all this work for us with the goal of us not having to think about it any more once we&#8217;ve created the necessary code.</p>
<p><span id="more-739"></span><br />
<h2><b>The ActionNewView Action</b></h2>
<p>As we will continuously reference &#8220;the place&#8221; where the secondary id is set on the view, lets start with the &#8220;view creator&#8221;. This could be considered a Factory, but as it&#8217;s kind of nice to tie it in with <code>Actions</code> lets make this factory an Action which also allows us to use it in menus, toolbars, etc.</p>
<p>The action will take the [primary] ID of the view that should be opened as a parameter in the constructor (the same ID you specify in <code>plugin.xml</code>) and in the <code>run()</code> method it will create the view with a secondary id of our choice. This is all quite easy and we don&#8217;t have to write much code to accomplish it, in fact, you&#8217;ve undoubtedly done something like this already. In this article we will use a <a href="http://en.wikipedia.org/wiki/UUID" target="_new">UUID</a> as our secondary ID as it&#8217;s unique and the structure of it is good as it&#8217;s a long string without strange characters (except the &#8216;-&#8217; character) and it&#8217;s more or less guaranteed to be unique. You may of course use whatever you prefer as a unique ID.</p>
<p>Our action class will look like this:</p>
<pre class="java" name="code">
<pre class="syntax-highlight:java">
public class ActionNewView extends Action {

    private String _viewIdToOpen = null;

    public ActionNewView(String viewIdToOpen) {
        _viewIdToOpen = viewIdToOpen;
    }

    @Override
    public void run() {
        try {
            // this will become the secondary ID used in the view
            String secondaryId = UUID.randomUUID().toString();

            IWorkbenchWindow window = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
            if (window != null) {
                IWorkbenchPage page = window.getActivePage();
                if (page != null) {
                    // these two lines open (create) and focus on the view
                    page.showView(_viewIdToOpen, secondaryId, IWorkbenchPage.VIEW_CREATE);
                    IViewPart justActivated = page.showView(_viewIdToOpen, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);

                    // ... do something with justActivated if needed
                }
            }

        }
        catch (Exception err) {
            //TODO: deal with exception
        }
    }
}
</pre>
</pre>
<p>I don&#8217;t think it needs much explanation as it&#8217;s a pretty straight forward &#8220;view creation&#8221; call with a secondary id, as specified in the Eclipse manual etc. </p>
<h2><b>Dealing With &#8220;Show View&#8221;</b></h2>
<p>Now that we have our &#8220;view opener class&#8221; we&#8217;re ready to deal with some of the aspects mentioned in the beginning. The next stop is to get secondary id&#8217;s onto views opened via &#8220;Show View&#8221; as they don&#8217;t get one by default. If you don&#8217;t already have an abstract parent class for all of your multi views, perhaps now is the time to create one as it will make it easier, but you can of course put the same snippet of code elsewhere. </p>
<p>One method is always called upon view creation, and that&#8217;s where we want to check if the view being created has a secondary id. If it doesn&#8217;t, we want to dispose (hide) that view right away and call our ActionNewView instead and open the same view again but <b>with</b> a secondary id. The method we check in is the <code>createPartControl(Composite parent)</code> method which is an implementation of the <a href="http://help.eclipse.org/ganymede/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/IWorkbenchPart.html#createPartControl(org.eclipse.swt.widgets.Composite)" target="_new">IWorkbenchPart</a> interface. Here&#8217;s what our code will look like:</p>
<pre class="java" name="code">
<pre class="syntax-highlight:java">
@Override
public void createPartControl(final Composite parent) {
    try {
        // get the secondary id that the view was created with
        String secondaryId = getViewSite().getSecondaryId();

        // If the secondary id was null that means that Eclipse opened the view, and not us. That&#039;s bad because it
        // creates a view without a secondary ID and causes the view to be &quot;different&quot; from one that we opened ourselves.
        // Thus, if we see a view opened without an ID, we quickly close it and tell the &quot;open new action&quot; to open
        // the same view right away. The result may be a very slight flicker as the old view comes to existance and is
        // destroyed right away, but it&#039;s mostly not noticeable by the end user.
        if (secondaryId == null) {
            // we need to do it asynchronously as we can&#039;t close the view until the create code has finished
            Display.getDefault().asyncExec(new Runnable() {
                @Override
                public void run() {
                    // destroy ourselves
                    getViewSite().getPage().hideView(ThisAbstractClass.this);
                    // open the same view our way
                    ActionNewView anv = new ActionNewView(getViewSite().getId());
                    anv.run();
                }
            });

            // return now as we&#039;re done with the non-secondary-id view
            return;
        }

        // do normal view creation stuff here...
    }
    catch (Exception err) {
        //TODO: deal with exception
    }
}
</pre>
</pre>
<p><code>ThisAbstractClass</code> is of course whatever class you put this code in, but most likely it&#8217;s your abstract multi-view parent class.</p>
<p>So, as the comment states, when a view is created and it doesn&#8217;t have a secondary id, that view is discarded as soon as it finished being created and the same view is opened again but this time via our action, which in turn ensures it gets a secondary id to our standards. </p>
<h2><b>Dealing With Perspectives</b></h2>
<p>We&#8217;re almost done, but there&#8217;s one more place where views may get secondary id&#8217;s that aren&#8217;t what we want, and that&#8217;s the views that are opened when the <code>Perspective</code> is new or reset. In our <code>DefaultPerspective</code> class (or whatever perspective class you want) we define the layout in the <code>createInitialLayout(IPageLayout layout)</code> and we also set whatever views are open at the start and where non-opened views go (placeholders). In here we need to change a few things so that whatever views are opened from the start get opened with a secondary id like above.</p>
<p>The API documentation for <a href="http://help.eclipse.org/ganymede/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/IPageLayout.html" target="_new">IPageLayout</a> states:</p>
<p><i>In some cases, multiple instances of a particular view may need to be added to the same layout. These are disambiguated using a secondary id. In layout methods taking a view id, the id can have the compound form: primaryId [':' secondaryId]. If a secondary id is given, the view must allow multiple instances by having specified <code>allowMultiple="true"</code> in its extension. View placeholders may also have a secondary id.</i></p>
<p>So we can either create the secondary id on the fly and call <code>layout.showView("primaryId:secondaryId")</code> or we can just add placeholders and asynchronously call our Action to create the views. It&#8217;s really up to you how you want to do it, if your ID generator is a simple static call, you may just want to put the secondary ID&#8217;s in there and be done. But for the sake of the article, lets have everything go through our action. </p>
<p>Presume we have a perspective layout of 1 folder on the left, and 3 folders on the right, each taking up 33% of the space vertically. Something like this:</p>
<pre>
+-----------+--------+
|           |folder 1|
|           +--------+
| folder 0  |folder 2|
|           +--------+
|           |folder 3|
+-----------+--------+
</pre>
<p>The &#8220;folder 0&#8243; will get a non-multi view, and folder 1,2,3 will get multi-views, 1 each for this example. We will tell the folder 1,2,3 that they contain view placeholders, and folder 0 will get a view right away. Our code will look like this:</p>
<pre class="java" name="code">
<pre class="syntax-highlight:java">
public class DefaultPerspective implements IPerspectiveFactory {

    // Marker for saying &quot;any id following the primary id&quot; (which is the secondary id)
    private static final String MULTI_VIEW = &quot;:*&quot;;

    @Override
    public void createInitialLayout(IPageLayout layout) {
        String editorArea = layout.getEditorArea();
        layout.setEditorAreaVisible(false);

        // list of ID&#039;s that will be created at &quot;reset perspective&quot; / first startup
        final String[] ids = new String[] { ViewForFolder1.ID, ViewForFolder2.ID, ViewForFolder3.ID };

        layout.addShowViewShortcut(OurWorkspaceView.ID);
        layout.addShowViewShortcut(ViewForFolder1.ID);
        layout.addShowViewShortcut(ViewForFolder2.ID);
        layout.addShowViewShortcut(ViewForFolder3.ID);

        // left side
        IFolderLayout workspace = layout.createFolder(&quot;Folder0&quot;, IPageLayout.LEFT, 0.2f, editorArea);
        workspace.addView(OurWorkspaceView.ID);

        // right side
        IFolderLayout f1 = layout.createFolder(&quot;Folder1&quot;, IPageLayout.RIGHT, 0.25f, editorArea);
        f1.addPlaceholder(ViewForFolder1.ID + MULTI_VIEW);

        IFolderLayout f2 = layout.createFolder(&quot;Folder2&quot;, IPageLayout.BOTTOM, 0.33f, &quot;Folder1&quot;);
        f2.addPlaceholder(ViewForFolder2.ID + MULTI_VIEW);

        IFolderLayout f3 = layout.createFolder(&quot;Folder3&quot;, IPageLayout.BOTTOM, 0.5f, &quot;Folder2&quot;);
        f3.addPlaceholder(ViewForFolder3.ID + MULTI_VIEW);

        // create all views asynchronously as they need to be created after the folders etc
        // have been laid-out. This ensures they have a secondary id according to our standards.
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                for (String id : ids) {
                    ActionNewView anv = new ActionNewView(id);
                    anv.run();
                }
            }

        });

        // rest of your code...
    }
}
</pre>
</pre>
<p>So when the Perspective is opened for the first time (or reset) we create the layout, set multi-view placeholders and then asynchronously call our action to create the views. As we told them where to go, they will show up in each respective folder in the order specified by the string array. As mentioned previously you can of course call <code>addView</code> directly with a secondary id, but I&#8217;m showing the &#8220;route it through our ActionNewView class&#8221; way here.</p>
<h2><b>Done!</b></h2>
<p>This concludes the article on harnessing the Secondary ID in your RCP (or Eclipse) applications. This is by no means the only way to do it as everyone seem to find their own way of doing similar things. This is simply &#8220;one way&#8221;, but the above way works well. Hopefully the article gave you an insight into how the Secondary ID is used in a multi-view environment and the places you may need to &#8220;catch it&#8221; as views can be created from the Eclipse framework without your direct interaction.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexapixel.com/2009/06/16/the-secondary-id/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Maven Version Clash Detection (and Software)</title>
		<link>http://hexapixel.com/2009/03/17/maven-and-dependency-version-clash-detection</link>
		<comments>http://hexapixel.com/2009/03/17/maven-and-dependency-version-clash-detection#comments</comments>
		<pubDate>Tue, 17 Mar 2009 17:26:46 +0000</pubDate>
		<dc:creator>Emil</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://hexapixel.com/?p=603</guid>
		<description><![CDATA[Maven is a great tool for building Java projects, there&#8217;s no doubt about that. Unfortunately there is one great feature still lacking of Maven which is important when you use third party dependencies, namely detecting when your components use dependencies of different versions without your knowledge. What does that mean? It&#8217;s probably easier to explain [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://maven.apache.org/" target="_new">Maven</a> is a great tool for building Java projects, there&#8217;s no doubt about that. Unfortunately there is one great feature still lacking of Maven which is important when you use third party dependencies, namely detecting when your components use dependencies of different versions without your knowledge. </p>
<p>What does that mean? It&#8217;s probably easier to explain by example. Imagine you have a project that has two dependencies (can be your own created jars or third party, or anything Maven-buildable really). Your first dependency uses Log4J v1.0. Your second dependency uses Log4J v1.1.  When you compile this project Maven will by default use the &#8220;nearest wins&#8221; strategy, meaning it might pick Log4J v1.0 or Log4J v1.1, whichever is nearer in the <a href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html" target="_new">transitive hull of dependencies</a>. What you probably wanted however, was for the compilation to tell you that you have two &#8220;modules&#8221; depending on the same library, but different versions of that library. Log4J is perhaps not the best example, but imagine that the dependency in question is a mission-critical dependency, and that any version discrepancy may cause catastrophic failures that can be very hard to track down via debugging.</p>
<p>To solve this I wrote a Maven plugin nearly a year ago that we are now using for every Maven-built project in our company. It&#8217;s a simple plugin that does the complex task of detecting all version clashes (even deeply nested ones) and will report them back to you either as warnings or errors (or ignore them). This project was submitted to the Maven team a while back but has not been integrated as a default Maven plugin yet, so it will now become a downloadable Software Project on Hexapixel until it hopefully becomes a standard module. </p>
<p><span id="more-603"></span></p>
<p>So how do you use it? Here&#8217;s a brief explanation and some examples of usage. If you&#8217;d rather skip to the plugin &#8220;homepage&#8221;, please follow <a href="/software/maven-version-clash-plugin">this link</a>.</p>
<h2><strong>Usage / Configuration</strong></h2>
<p>You can either run the clash-detector manually from the command line, or (the best option) is to add it to your POM (ideally your parent POM that all your projects use, you do have one right?) so that it is run automatically whenever you do builds.</p>
<p>There are only two options you can configure, one is the level of warning the plugin should do. By default the level is <code>FAIL</code>, meaning your build will fail if the plugin detects any clashes. The level can be changed either on the command line by supplying the parameter <code>-Dhalt=</code> where the right hand of the equals sign is one of <code>FAIL, WARN or DEBUG</code>. For the POM the same parameter is configured by supplying the same keyword inside a <code>&lt;halt /&gt;</code> tag such as: <code>&lt;halt&gt;WARN&lt;/halt&gt;</code></p>
<p>The second parameter is the level of detection the plugin should do. There are two modes, <code>SHALLOW and DEEP</code>. By default the level is <code>DEEP</code> where clash detection is not limited to the immediate level of the dependency, but also deeply nested dependencies. Shallow detection is only one level down and will not detect deeply nested dependency clashes. This is for example good for when you only care about clashes of your own jars (although I would strongly advice never using it unless really necessary).</p>
<p>Here are two snippets showing how to run the clash detector from the command line and how to configure it inside your POM.</p>
<p><b><strong>Command Line</strong></b></p>
<p>To invoke it from the command line you simply run it like this: </p>
<pre>
<pre class="syntax-highlight:xml">
mvn breakonversionclash:clash-detection -Dhalt=FAIL -Ddepth=SHALLOW
</pre>
</pre>
<p><b>In your POM</b></p>
<pre>
<pre class="syntax-highlight:xml">
&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;com.hexapixel.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;
				maven-breakonversionclash-plugin
			&lt;/artifactId&gt;
			&lt;version&gt;1.0.1&lt;/version&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;configuration&gt;
						&lt;!-- Options: FAIL, WARN, DEBUG (default is FAIL) --&gt;
						&lt;halt&gt;WARN&lt;/halt&gt;
						&lt;!-- Options: SHALLOW, DEEP (default is DEEP) --&gt;
						&lt;depth&gt;DEEP&lt;/depth&gt;
					&lt;/configuration&gt;
					&lt;phase&gt;compile&lt;/phase&gt;
					&lt;goals&gt;
						&lt;goal&gt;clash-detection&lt;/goal&gt;
					&lt;/goals&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;
</pre>
</pre>
<h2><strong>Plugin in Action</strong></h2>
<p>Here is a typical output the plugin will produce:</p>
<pre>
<pre class="syntax-highlight:xml">
 [INFO] [breakonversionclash:clash-detection {execution: default}]
 [INFO] Checking for dependency version clashes...
 [INFO] Halt mode is: &#039;warn&#039;. Depth mode is: &#039;deep&#039;.
 [WARNING] ------------------------------------------------------------------------
 [WARNING] Dependency version clash on &#039;commons-logging&#039; [1.0.4, 1.1.1]
 [WARNING] ------------------------------------------------------------------------
 [WARNING] Clash 1: commons-logging:commons-logging:1.0.4
 [WARNING] Clash 2: commons-logging:commons-logging:1.1.1
 [WARNING]
 [WARNING] Artifacts using different versions of &#039;commons-logging:commons-logging&#039;:
 [WARNING] - org.hibernate:hibernate:jar:3.2.6.ga
 [WARNING] - com.our.project:plugin-domain:jar:1.0.0.0-SNAPSHOT
 [WARNING]
 [WARNING] Dependency trail for &#039;commons-logging:commons-logging:1.0.4&#039;
 [WARNING] - Node 1: com.our.project:plugin-testdata:pom:1.0.0.0-SNAPSHOT
 [WARNING] - Node 2: com.our.project:plugin-testdata-source:zip:small-datasets:1.0.0.0-SNAPSHOT
 [WARNING] - Node 3: com.our.project:plugin-testdata-generator:jar:1.0.0.0-SNAPSHOT
 [WARNING] - Node 4: com.our.project:plugin-domain:jar:1.0.0.0-SNAPSHOT
 [WARNING] - Node 5: org.hibernate:hibernate:jar:3.2.6.ga
 [WARNING]
 [WARNING] Dependency trail for &#039;commons-logging:commons-logging:1.1.1&#039;
 [WARNING] - Node 1: com.our.project:plugin-testdata:pom:1.0.0.0-SNAPSHOT
 [WARNING] - Node 2: com.our.project:plugin-testdata-source:zip:small-datasets:1.0.0.0-SNAPSHOT
 [WARNING] - Node 3: com.our.project:plugin-testdata-generator:jar:1.0.0.0-SNAPSHOT
 [WARNING] - Node 4: com.our.project:plugin-domain:jar:1.0.0.0-SNAPSHOT
 [WARNING] Found 1 dependency version clash
</pre>
</pre>
<p>Let&#8217;s go over this even though it might be mostly self explanatory. </p>
<p>We have configured the plugin to only warn us when there is a clash, and to do a &#8220;deep&#8221; check, meaning it will not only check immediate dependencies, but also dependencies of dependencies.</p>
<p>It tells us that we have a clash on the usage of &#8216;commons-logging&#8217; and that the two clashing versions are version 1.0.4 and version 1.1.1 of commons-logging.</p>
<p>Then it tells us what jars are using commons-logging, in this case it&#8217;s our dependency on Hibernate and our own plugin-domain project. </p>
<p>It then shows us the dependency &#8220;trail&#8221; for each clash, meaning, it shows what depends on what all the way to the actual dependency of &#8216;commons-logging&#8217;, so that you can easily pinpoint what jars need adjusting so that the clash goes away.</p>
<h2><strong>Download / More Info</strong></h2>
<p>For downloads (binary and source) and more info, please go to the <a href="/software/maven-version-clash-plugin">Version Clash Plugin Page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexapixel.com/2009/03/17/maven-and-dependency-version-clash-detection/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glazed Lists + SWT Tables == True</title>
		<link>http://hexapixel.com/2009/01/02/glazed-lists-swt-tables-true</link>
		<comments>http://hexapixel.com/2009/01/02/glazed-lists-swt-tables-true#comments</comments>
		<pubDate>Fri, 02 Jan 2009 18:01:19 +0000</pubDate>
		<dc:creator>Emil</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Eclipse Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JFace]]></category>
		<category><![CDATA[SWT]]></category>

		<guid isPermaLink="false">http://hexapixel.com/?p=387</guid>
		<description><![CDATA[If you haven&#8217;t heard about Glazed Lists until now, it&#8217;s about time you do, because it&#8217;s probably the last tables and grid sorting/filtering API you will ever need. It will also be the last time you rip your hair out when someone asks you for a multi-sort-multi-thread-asynchronously-updated-filtered-unique-table (which happens all the time, right?). From here [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t heard about <a href="http://www.publicobject.com/glazedlists/">Glazed Lists</a> until now, it&#8217;s about time you do, because it&#8217;s probably the last tables and grid sorting/filtering API you will ever need. It will also be the last time you rip your hair out when someone asks you for a multi-sort-multi-thread-asynchronously-updated-filtered-unique-table (which happens all the time, right?). From here on out you&#8217;ll just be saying &#8220;oh, sure, give me 30 minutes&#8221; when you need to implement something along those lines. Sounds too good to be true doesn&#8217;t it? It is! So, what are Glazed Lists?</p>
<p>Glazed Lists is an API built on top of <code>java.util.List</code> which adds a notification framework and allows you to &#8220;stack&#8221; or &#8220;chain&#8221; lists together. This in and by itself may not sound so exciting. What this gives you though is the ability to take a Sorted list, stack it with a Filtered list, stack it with a Unique list, stack it with a asynchronously updated Unique list (and keep stacking), and whenever you modify the source list, it automatically tells the other lists in the chain to update so that their lists are always up to date. Last but not least, the list reports back to you and tells you to update your view with a specific list of changes. So imagine this &#8220;chained list&#8221; is the input for your TableViewer and perhaps you&#8217;re starting to see the potential.</p>
<p>I&#8217;ve been using Glazed Lists continuously for the last year, and it&#8217;s no short of astounding how powerful it is. I&#8217;ve used them on JFace TableViewers, The <a href="http://www.eclipse.org/nebula/widgets/grid/grid.php" target="_blank">Nebula Grid</a> widget, and the great rising-star <a href="http://nattable.org/drupal/" target="_blank">NatTable</a> which deserves it&#8217;s own article down the road (the only table widget I&#8217;ve found that can handle huge amounts of data without choking). On each of those widgets it was only a matter of creating a couple of smaller classes, a few lines of code, and I had the most powerful sorting and filtering API at my fingertips, ready for anything upper management could throw at me in terms of features. So often I see posts on the Eclipse Newsgroups asking about multi column sorting, and usually few concrete replies. &#8220;Glazed Lists&#8221; would be my reply.  To give an idea of what kind of sorting/filtering you can do, here&#8217;s a snapshot of  some of the available list types:</p>
<p><i>Sorted List. Filtered List. Unique List. Transformed List. Range List. Threshold List. Collection List. Freezable List. Popularity List. Separator List. Sequence List. Transaction List. Observable Lists</i>.. and the list goes on.</p>
<p>So instead of talking about how great they are in an entire post, how about some example implementations and actual code? Alright, let&#8217;s get to it!</p>
<p><span id="more-387"></span></p>
<p>To make this article &#8220;worth it&#8221; for everyone, I will cover implementing Glazed Lists on no less than 3 table/grid widgets. The standard JFace TableViewer (which is the least feature rich table component out there as it uses the SWT Table). The Nebula Grid widget (which is feature rich and very good, but has a big memory footprint on large tables) and the NatTable widget (as it will undoubtedly rise to fame soon with the next few versions (once some of the bigger bugs are ironed out)). Before you start copying and pasting code, all code is downloadable in each respective section and at the bottom of the article.</p>
<p>If you prefer to follow along or want to get a head start on downloading required jars, here are links to all the third party packages you might need:</p>
<div id="bulletlist">
<ol>
<li><a href="http://www.publicobject.com/glazedlists/glazedlists-1.7.0/" target="_new">Glazed Lists v1.7</a> (you do need this one)</li>
<li><a href="http://www.eclipse.org/nebula/widgets/grid/grid.php" target="_new">Nebula Grid (and Grid Viewer, required for the Nebula framework)</a></li>
<li><a href="http://sourceforge.net/project/showfiles.php?group_id=207809" target="_new">Nat Table v1.6.3 (grab both jars)</a></li>
<li><a href="http://logging.apache.org/log4j/1.2/download.html" target="_new">Apache Log4J</a> (Nat table needs this)</li>
<li><a href="http://commons.apache.org/downloads/download_lang.cgi" target="_new">Apache Commons Lang</a> (Nat table uses this internally for some odd reason)</li>
<li><a href="http://www.eclipse.org/downloads/" target="_new">Eclipse IDE / SWT</a></li>
</ol>
</div>
<p>Let&#8217;s start with the basics of Glazed Lists, as everything starts there.</p>
<h2><strong>Glazed Lists In a Nutshell</strong></h2>
<p>I could send you to the <a href="http://sites.google.com/site/glazedlists/documentation/tutorial" target="_new">Glazed Lists Tutorials</a> and tell you to follow those and come back, but I hate that kind of &#8220;teaching&#8221;, and the small &#8220;problem&#8221; with the tutorials is that they are all Java Swing based which is of little use to us (but by all means read them, they&#8217;re good). You might also want to check out the very good screen casts located <a href="http://publicobject.com/glazedlistsdeveloper/" target="_new">here</a> as they will give you a good idea of the different types of lists that can be used in Glazed Lists. In any case, everything in Glazed Lists starts with one thing, the root list. The root list contains all your items that you want to display before they are filtered or sorted. This list is probably in some random you-don&#8217;t-care order  which is perfectly fine. This list will serve as the input for all other chained lists. Think of it as layers, where the bottom layer is the raw list, and the higher up you go, the more refined and specific each layer is. So you have a sorting layer, a filter layer, etc. They can be active or not active, a bit like Photoshop. As we are using Glazed Lists, we need to take our root list and turn it into a Glazed Lists &#8220;EventList&#8221;, which is the Glazed Lists root-list equivalent. Here&#8217;s how that&#8217;s done:</p>
<pre>
<pre class="syntax-highlight:java">
List&lt;?&gt; rootList = .... // this comes from you
EventList&lt;?&gt; eventList = GlazedLists.eventList(rootList);
</pre>
</pre>
<p>As you can see everything uses Generics, just like normal Lists in Java 5+.</p>
<p>Any &#8220;normal&#8221; table in the UI-world will undoubtedly have Sorting and Filtering. With the code above we&#8217;re currently no different from any other plain old table implementation as we haven&#8217;t done sorting/filtering yet. So lets create the sorting/filtering layers that will be on top of the event list. We should think about order when it comes to our layers, as if we create a sorted list on top of a filtered list that&#8217;s not the same as creating a filtered list on top of a sorted list. Logically, we sort first, then apply a filter. To create our new lists we continue the code:</p>
<pre>
<pre class="syntax-highlight:java">
List&lt;?&gt; rootList = .... // this comes from you
EventList&lt;?&gt; eventList = GlazedLists.eventList(rootList);
SortedList sortedList = new SortedList(eventList, null);
FilterList(sortedList, new OurFilterMatcher());
</pre>
</pre>
<p>As you can see we keep using the last list as the input for the new list, as that&#8217;s our &#8220;chain&#8221;. The second parameter for the SortedList is the <code>Comparator</code> we want to use when sorting. I&#8217;m passing in <code>null</code> here to point out that it can be created when constructing the list (and not passing in null may actually throw an exception). Normally you don&#8217;t want the table sorted unless the user sorts it, so <code>null</code> makes sense. The <code>OurFilterMatcher</code> will be explained when we do the actual implementation, but just remember that it&#8217;s the &#8220;Matcher&#8221; that is used to filter a table (which is very much like a <code>ViewerFilter</code>).</p>
<p>There&#8217;s one more important aspect of Glazed Lists &#8211; locking. As lists can be updated on multiple threads (if you so wish) you need to tell the list when to &#8220;lock&#8221; and &#8220;unlock&#8221; so that only 1 thread is actively writing to the list at the same time. As such, it&#8217;s important we always do this when reading and writing to the list (and even if we don&#8217;t plan on multithread updates, it&#8217;s still good practice and takes little time to do). We could lock the lists when creating them even, but that&#8217;s a bit overkill, so keep en eye out for the locking code in the implementation part and in the framework code available for download. Glazed Lists even supports reading while writing, and their homepage has an example of an asynchronously updated list. Do check it out if you need advanced implementations. To give a quick example of locking/unlocking a list, here&#8217;s how it&#8217;s done:</p>
<pre>
<pre class="syntax-highlight:java">
_eventList.getReadWriteLock().writeLock().lock();
// ... modify list here ...
_eventList.getReadWriteLock().writeLock().unlock();
</pre>
</pre>
<h4><strong>The Callbacks</strong></h4>
<p>The whole idea of Glazed Lists is that when you modify the event list (and this is important: <strong>you <span style="text-decoration: underline;">always</span> modify the EventList</strong>!), it fires events to tell you what you need to change in your table implementation. Glazed has a lot of pre-built hooks for Swing, so there you don&#8217;t have to do much work, but as we use SWT we have to do pretty much all the work ourselves (great huh?). It&#8217;s not as bad as it sounds however. The callback interface that we care about here is <code>ListEventListener</code> which contains a method callback called <code>public void listChanged(ListEvent&lt;?&gt; listChanges)</code>. This list of changes <code>listChanges</code> tells us, <strong>in the correct order</strong>, what indexes and objects to update, delete and insert. It&#8217;s as easy as looping through that list, checking what the change is, and performing it. Here&#8217;s a barebones example of how it would look implemented:<a name="nutshell_code"></a></p>
<pre>
<pre class="syntax-highlight:java">
_filterList.addListEventListener(new ListEventListener&lt;?&gt;() {

	@Override
	public void listChanged(ListEvent&lt;?&gt; listChanges) {
		// get the list of changes (prior to looping, very important as it&#039;ll change as it&#039;s read)
		final List&lt;?&gt; changeList = listChanges.getSourceList();

		// go through all changes
		while (listChanges.next()) {
			// get the table index location of the change
			int sourceIndex = listChanges.getIndex();
			// get the change type
			int changeType = listChanges.getType();

			// update the table depending on the change type
			switch (changeType) {
				case ListEvent.DELETE:
					// delete row
					break;
				case ListEvent.INSERT:
					// insert row
					break;
				case ListEvent.UPDATE:
					// refresh row labels etc
					break;

			}
		}
	}

});
</pre>
</pre>
<p>That&#8217;s actually the most complex code we&#8217;ll be adding to make all of this work, and we can re-use that section of code for all our table widget implementations. </p>
<p>You might ask &#8220;why is the listener on the filter list if we were told to only modify the event list?&#8221;, which is a good and valid question. The answer is that if you were to listen on the event list instead, only base-level adds and deletes would be reported to you. As we also care about when stuff is re-ordered or moved in the list (which is what happens when users sort and filter) we want any list-changes to notify us. As our filtered list is the &#8220;topmost&#8221; or &#8220;last&#8221; list in the chain, it will get all notifications from all previous lists in the chain. Remember, all lists in the chain tell the next list down the line what changed (think of it as one-way gossip). By listening on the Filtered list we get all events, regardless on what list they happened. With that information we&#8217;re well on our way to putting Glazed into real use.</p>
<h2><strong>SWT TableViewer</strong></h2>
<p>Personally I was never a big fan of the SWT Table. A good thing about it is that it renders column headers natively, but it can be quite slow, sometimes buggy (first-column image support) and not very intuitive. It doesn&#8217;t support much customization and the second you need a table that can do a little more than a plain x/y grid, you soon hit those limitations head on. <a href="http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html" target="_new">Owner Draw</a> does help, but there&#8217;s still a lot of customization that is not possible. That said, as a &#8220;basic table&#8221; it does just fine and it&#8217;s getting better for each new SWT version. And of course, it&#8217;s native.</p>
<p>Normally when someone asks &#8220;can I sort a <code>SWT.VIRTUAL</code> table?&#8221; the answer is no or &#8220;not really&#8221;. The reason is that a virtual table only really knows what it shows in the current viewport (the visual area of the table), and how many items it has. Anything that you can&#8217;t see is not really &#8220;there&#8221;, except in memory. This obviously makes such a useful feature as <code>SWT.VIRTUAL</code> fairly useless in a sort scenario. The workaround in that case is to custom implement a sorter and replace the entire table content whenever the user sorts or filters. As we use Glazed Lists we don&#8217;t really care about this limitation, we&#8217;re working around it as we update the table with our changes whenever they occur thanks to the callback from Glazed Lists.</p>
<p>There&#8217;s a few special things we need apart from a &#8220;normal&#8221; Virtual TableViewer implementation. First, we&#8217;ll be using the <code>IStructuredContentProvider</code> for the Content provider. The <code>ILazyContentProvider</code> exists as well, but I had some odd issues with it in testing. If you want an example on how to create a virtual TableViewer, there&#8217;s a good snippet here: <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet029VirtualTableViewer.java?revision=1.1&amp;view=markup" target="_new">Snippet029VirtualTableViewer</a>. In the <code>IStructuredContentProvider</code> we simply return the topmost layer (which is the FilterList) as follows;</p>
<pre>
<pre class="syntax-highlight:java">
class GlazedContentProvider implements IStructuredContentProvider {

	@Override
	public Object[] getElements(Object inputElement) {
		return _filterList.toArray();
	}

	@Override
	public void dispose() {
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

}
</pre>
</pre>
<p>We also need to implement a Comparator which will be used by the SortedList, as well as a Matcher for the FilterList. The Comparator is a normal java Comparator whereas the Matcher is a Glazed Lists implementation. However, these are <a href="http://en.wikipedia.org/wiki/Immutable_object" target="_new">immutable</a> so they need to be new every time they are set on the respective lists. For the sake of simplicity, lets create an object called RowObject that represents one row in our table. All it does is keep track of the row number and it has a <code>getColumnText</code> method.</p>
<pre>
<pre class="syntax-highlight:java">
class RowObject {
	private int	_row;

	public RowObject(int row) {
		_row = row;
	}

	public int getRow() {
		return _row;
	}

	public String getColumnText(int col) {
		return &quot;&quot; + (_row + col);
	}
}
</pre>
</pre>
<p>With this all set up, we can now create the Comparator and Matcher. Lets start with the Comparator. As we&#8217;re basically returning an integer as display value for each cell in <code>getColumnText</code> we&#8217;ll use this as the basis for our comparison. Thus, our Comparator looks as follows;</p>
<pre>
<pre class="syntax-highlight:java">
class GlazedSortComparator implements Comparator&lt;RowObject&gt; {

	private int	_col;
	private int	_direction;

	public GlazedSortComparator(int col, int direction) {
		_col = col;
		_direction = direction;
	}

	@Override
	public int compare(RowObject o1, RowObject o2) {
		int ret = Integer.valueOf(o1.getColumnText(_col)).compareTo(Integer.valueOf(o2.getColumnText(_col)));

		if (_direction == SWT.DOWN)
			ret = -ret;

		return ret;
	}

}
</pre>
</pre>
<p>And now we create the Matcher which is used by the FilterList. The Matcher implementation in Glazed Lists asks us if a row object &#8220;matches&#8221; and not much more. This may seem confusing at first, but it&#8217;s quite similar to the <a href="http://help.eclipse.org/stable/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/viewers/ViewerFilter.html" target="_new">ViewerFilter</a>. In our code we will have a textbox and a button that represents a &#8220;search&#8221; box. When the user presses the button we set a String field called &#8220;_filterText&#8221; to whatever the user entered, and then we set a new matcher on the FilterList. As such, our code implementation looks like this:</p>
<pre>
<pre class="syntax-highlight:java">
class GlazedMatcher implements Matcher&lt;RowObject&gt; {
	@Override
	public boolean matches(RowObject row) {
		for (int i = 0; i &lt; _table.getColumnCount(); i++) {
			if (row.getColumnText(i).indexOf(_filterText) &gt; -1)
				return true;
		}

		return false;
	}
}
</pre>
</pre>
<p>Now we can add two methods that actually do the updating when we filter and when we sort. Like this:</p>
<pre>
<pre class="syntax-highlight:java">
// update the table filter
private void updateFilter() {
	// if the search text is blank, remove the matcher
	if (_filterText == null || _filterText.length() == 0) {
		_filterList.setMatcher(null);
	}
	else {
		_filterList.setMatcher(new GlazedMatcher());
	}
}

// sorts a table on the given column
private void sortColumn(int col) {
	// do some normal UP/DOWN checking and updating
	int dir = SWT.UP;
	int current = _table.getSortDirection();

	TableColumn tc = _table.getColumn(col);
	if (_table.getSortColumn() == tc) {
		dir = (current == SWT.UP ? SWT.DOWN : SWT.UP);
	}

	_table.setSortColumn(tc);
	_table.setSortDirection(dir);

	// now tell the sorted list we&#039;ve updated
	_sortedList.setComparator(new GlazedSortComparator(col, dir));
}
</pre>
</pre>
<p>At this point we&#8217;re basically there. All that&#8217;s missing is a proper <code>listChanged</code> that reflects how we update a TableViewer. So with a few modifications to the code I posted in the <a href="#nutshell_code">nutshell section</a>, we get code that looks like this:</p>
<pre>
<pre class="syntax-highlight:java">
_filterList.addListEventListener(new ListEventListener&lt;RowObject&gt;() {

	@Override
	public void listChanged(ListEvent&lt;RowObject&gt; listChanges) {
		try {
			_table.setRedraw(false);

			// get the list PRIOR to looping, otherwise it won&#039;t be the same list as it&#039;s modified continuously
			final List&lt;RowObject&gt; changeList = listChanges.getSourceList();

			while (listChanges.next()) {
				int sourceIndex = listChanges.getIndex();
				int changeType = listChanges.getType();

				switch (changeType) {
					case ListEvent.DELETE:
						// note the remove of the object fetched from the event list here, we need to remove by index which the viewer does not support
						// and we&#039;re removing the object index location from the event list, not the filtered list
						_viewer.remove(_eventList.get(sourceIndex));
						break;
					case ListEvent.INSERT:
						final RowObject obj = changeList.get(sourceIndex);
						_viewer.insert(obj, sourceIndex);
						_viewer.replace(_filterList.get(sourceIndex), sourceIndex);
						break;
					case ListEvent.UPDATE:
						_viewer.replace(_filterList.get(sourceIndex), sourceIndex);
						break;
				}
			}

			// most important, we update the table size after the update
			_viewer.setItemCount(_filterList.size());
		}
		catch (Exception err) {
			err.printStackTrace();
		}
		finally {
			_table.setRedraw(true);
		}
	}

});
</pre>
</pre>
<p>The result, when combined with the rest of the necessary boilerplate code, looks like this:</p>
<p><img title="TableViewer using Glazed Lists" src="http://hexapixel.com/wp-content/uploads/2009/01/tv_glazed.gif" alt="tv_glazed" width="646" height="400" /></p>
<h4><strong>Download Code</strong></h4>
<p><a href="/files/posts/TableViewerGlazedExample.java">TableViewerGlazedExample.java</a></p>
<h2><strong>Nebula Grid</strong></h2>
<p>Whereas the normal SWT table only supports single column sort (unless you <a href="http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html" target="_new">owner draw</a>) the Nebula grid can do more. It can do multiple column sorting (visually), grouped columns, and other custom features not available in the default grid. The Nebula Grid has a Viewer implementation, but unfortunately at the time of writing this article, it does not support <code>SWT.VIRTUAL</code> mode. As Nebula sticks very much to the SWT API in terms of naming conventions and implementation, it should be little trouble to convert it over when it does.</p>
<p>Depending on how you look at it, you are about to get quite lucky. For my job I wrote an entire framework implementation wrapping Glazed Lists in a Nebula Grid. It&#8217;s much more generically implemented (with interfaces etc) than our TableViewer example which was written purely for this article. Because of this I don&#8217;t feel like actually ripping it apart into something smaller, so I will use this Framework as the basis for the examples. Thus, this implementation comes with the option to use both the GridViewer implementation (which might be good if you are doing smaller tables that don&#8217;t need Virtual mode) or the grid in <code>SWT.VIRTUAL</code> mode.</p>
<p>One of the bigger differences when we&#8217;re not in viewer mode, is that on the <code>DELETE, INSERT</code> and <code>UPDATE</code> we need to create <code>GridItems</code> instead of just telling the viewer that it&#8217;s changed. It&#8217;s a bit more clunky and object-heavy of course, but it&#8217;s not as bad as it sounds. The switch statement that we used in the previous example becomes this:</p>
<pre>
<pre class="syntax-highlight:java">
switch (changeType) {
    case ListEvent.DELETE:
        removeItem(sourceIndex);
        break;
    case ListEvent.INSERT:
        final IDataObject insert = list.get(sourceIndex);
        addItem(insert, sourceIndex);
        break;
    case ListEvent.UPDATE:
        final IDataObject update = list.get(sourceIndex);
        removeItem(sourceIndex);
        addItem(update, sourceIndex);
        break;
}
</pre>
</pre>
<p>As you can see each one calls a sub-method of the class, and those methods look like this:</p>
<pre>
<pre class="syntax-highlight:java">
private void removeItem(int index) {
	_parent.getTableViewer().remove(_parent.getTableViewer().getElementAt(index));
}

private void addItem(IDataObject msg, int index) {
	GridItem gi = new GridItem(_parent.getGrid(), SWT.NONE, index);
	gi.setData(msg);

	try {
		for (int i = 0; i &lt; _parent.getGrid().getColumnCount(); i++) {
			Color fg = msg.getForeground(i, index % 2 == 0);
			Color bg = msg.getBackground(i, index % 2 == 0);
			Font f = msg.getFont(i);
			String txt = msg.getColumnText(i);
			Image image = msg.getColumnImage(i);

			if (fg != null) {
				gi.setForeground(i, fg);
			}
			if (bg != null) {
				gi.setBackground(i, bg);
			}
			if (f != null) {
				gi.setFont(i, f);
			}
			if (txt != null) {
				gi.setText(i, txt);
			}
			if (image != null) {
				gi.setImage(i, image);
			}
		}
	}
	catch (Exception err) {
		err.printStackTrace();
	}
}
</pre>
</pre>
<p>In this code we use an interface <code>IDataObject</code> that is basically our <code>RowObject</code> code from before. This object has various getter methods for returning column texts, images, fonts and so on.</p>
<p>So how do we go about creating a Nebula Grid based grid with this framework? Mostly it&#8217;s the same way it&#8217;s done with pretty much any SWT widget, along the lines of:</p>
<pre>
<pre class="syntax-highlight:java">
GlazedGridViewer viewer = new GlazedGridViewer(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.VIRTUAL, this, true, null, null, false);
viewer.setAllowMultiColumnSort(true);
viewer.getGrid().setCellSelectionEnabled(true);
</pre>
</pre>
<p>Some pointers: The <code>this</code> parameter is an interface implementation of <code>IGenericGridTable</code> which forces the implementor of the code to include methods for returning a Comparator and a Matcher, like we created before in the previous example. <code>true</code> is the option to create the grid as a plain grid, or to false to create a viewer. The last 3 parameters are a String id used for saving/loading the table state (column order, sizes etc), an <code>IPreferenceStore</code> where it would be saved on, and lastly a <code>true</code>/<code>false</code> whether the table should be saving the state at all.</p>
<p>The framework comes with a default implementation of a Label provider and a Content provider which hooks into the <code>IDataObject</code>. They are created automatically depending on the table type you picked. Column creation is also handled by the framework as the column wrapping implementation allows for individual setting of if a column is sortable, movable, hide-able etc. The API looks like:</p>
<pre>
<pre class="syntax-highlight:java">
public GridViewerColumn(IGlazedGridViewer parentTable, String name, boolean frozen, boolean moveable, boolean sortable, boolean hideable) {
     ...
}
</pre>
</pre>
<p>But when you create a column you do it via the framework as:</p>
<pre>
<pre class="syntax-highlight:java">
GridViewerColumn col = viewer.addColumn(str, false, true, true, true);
col.getGridColumn().setWidth(100);
</pre>
</pre>
<p>And finally, to add objects and set the table input, it&#8217;s again much like the TableViewer, for example:</p>
<pre>
<pre class="syntax-highlight:java">
List&lt;IDataObject&gt; input = new ArrayList&lt;IDataObject&gt;();
for (int i = 0; i &lt; 1000; i++) {
	input.add(new OurIDataObjectClass(i));
}
viewer.setInput(input);
</pre>
</pre>
<p>I don&#8217;t think much more needs to be said about the framework. It has a lot of API in it already and a test class as well. I suggest you download it if you want to give it a go. </p>
<p><strong>Important:</strong> as I currently am not using the framework due to the fact that the Nebula Grid was too slow and memory hungry for what we needed it for (40,000+ rows) I converted the entire framework over to use a NatTable and kept the work going from there. It should be fine, but there may be some minor issues here and there as it was never fully tested. If you find problems and fix them, do send me the code so that everyone can benefit. For the NatTable implementation of the framework, that&#8217;s the next section of this article.</p>
<p>All put together, the test code looks like this when run (with shameless self-promo selection):</p>
<p><img title="Glazed Nebula Grid" src="http://hexapixel.com/wp-content/uploads/2009/01/tv_nebula.gif" alt="tv_nebula" width="681" height="382" /></p>
<p>No filter box in this example, but I think the TableViewer one shows how it works so it&#8217;s not necessary. A point worth making though is that all filters are <code>DataFilter</code> based (framework class). That class extends <code>ViewerFilter</code> so that if you need more than one table implementation, you can re-use your filters should you so wish. That filter also comes with &#8220;whole word&#8221;, &#8220;regular expression&#8221; and &#8220;case sensitive&#8221; filter/search options in the filter itself.</p>
<h4><strong>Download Code</strong></h4>
<p><a href="/files/glazed_nebula_framework.zip">Glazed Lists &#8211; Nebula Grid Framework</a></p>
<h2><strong>Nat Table</strong></h2>
<p>The <a href="http://nattable.org/drupal/" target="_blank">NatTable</a> Widget (Nat stands for Natalie, the girlfriend of the original widget author) is a fairly new Grid widget and one that I personally was quite impressed with after having researched pretty much all SWT grid widgets out there. It&#8217;s still in active development, and not everything is working 100% yet (grouped columns are problematic, some random drawing issues, frozen columns behave oddly, listeners are quite messy), but overall it&#8217;s in a good enough shape that we decided to use it in production. The reason I decided we should use it was simple; We needed a table that could deal with 40,000+ rows in 20+ columns without any lag or any drawing slowdowns whatsoever. It needed to be customizable and I wanted Glazed Lists on it. NatTable is inherently virtual, so you don&#8217;t tell it that it&#8217;s virtual, it just is. It only draws what you see in the viewport which makes redraws near instant and the overall feel is &#8220;snappy&#8221;.</p>
<p>The NatTable API is also quite different from most SWT widgets. Pretty much every part of the table can be extended or implemented in a custom way, and there&#8217;s a lot of classes and places to do this in. At the same time, the API feels sometimes half-finished as simple methods such as <code>selectAll()</code> are nowhere to be found, and trying to figure out how to do it can be a bit of a head-scratcher.  So, It takes a while to wrap your head around the API, but with help from a few examples on their homepage it shouldn&#8217;t be too much of a deal (and most of those kinds of methods are in the framework). The table is model driven, which I suppose is the third type of table model I see used in tables (first being no model and row-item based, second being a viewer on top of a row-item based grid).</p>
<p>All that said, I truly hope the current development team keeps this widget going (and from the looks of it it&#8217;s very active), because it just might be the best SWT grid-table currently out there. The Nebula Grid had huge potential, but development on it is slow going. I know Tom Shindl is doing work on it, but I don&#8217;t know what the roadmap is.</p>
<p>Ok, lets get going with the framework I&#8217;ve built for NatTable. It&#8217;s not complete, just like the Nebula Grid framework which it is based on, but it will get you a very long way. As this framework has gotten further it has more utility methods and automation than it&#8217;s predecessor. It will even mimic the Windows column headers through a custom header drawer if so desired. It also has full color-customization support via interfaces and so on. It would be a long list if I wrote it all here, so I suggest you check out the API.</p>
<p>To create a table using the framework, you can use various constructors, of which the biggest is:</p>
<pre>
<pre class="syntax-highlight:java">
public GlazedNatGridViewer(Composite parent, int style, IGenericNatGridTable generic, String tableId, IPreferenceStore store, boolean allowStateSaving, AbstractGlazedNatVisualConfig config) {
       ...
}
</pre>
</pre>
<p>You will notice the <code>IGenericNatGridTable</code> is the same interface as in the Nebula Grid framework and works the same way (just different name). The last parameter, <code>AbstractGlazedNatVisualConfig</code> is if you want to pass in custom settings for colors, fonts, widths, heights etc. A whole load of things can be customized, or you can stick to the defaults (<code>DefaultGlazedNatVisualConfig</code>).</p>
<p>In our test class we will do the same as we&#8217;ve done in the previous examples, we&#8217;ll create some columns, some row data and do a sort. Using the framework we create a new table as follows:</p>
<pre>
<pre class="syntax-highlight:java">
GlazedNatGridViewer viewer = new GlazedNatGridViewer(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION, this, new DefaultGlazedNatVisualConfig());
viewer.setAllowMultiColumnSort(true);

for (int i = 0; i &lt; 10; i++) {
	NatGridViewerColumn col = viewer.addColumn(&quot;Column &quot;+i, false, true, true, true);
	col.setDefaultWidth(100);
}

List&lt;OneRow&gt; input = new ArrayList&lt;OneRow&gt;();
for (int i = 0; i &lt; 1000; i++) {
	input.add(new OneRow(i));
}

viewer.setInput(input);
</pre>
</pre>
<p>Our <code>OneRow</code> class implements <code>IDataObject</code> which contains getters for all things table related (column text etc). It also contains a getter specifically for returning a value that should be used when comparing column data when sorting. By default we simply return the same column text as the visible-to-user text:</p>
<pre>
<pre class="syntax-highlight:java">
class OneRow implements IDataObject {

	private int _row;

	public OneRow(int row) {
		_row = row;
	}

	@Override
	public String getColumnText(int column) {
		return &quot;&quot;+(_row+column);
	}

	@Override
	public String getSearchMatchText(int column) {
		return getColumnText(column);
	}

	...
}
</pre>
</pre>
<p>The multi column sort feature is already built in, and uses Glazed Lists ability to chain comparators. So whenever you sort more than one column, it will ask for a Comparator for each sorted column, and combine them in the order that you sorted the columns. You multi-sort by holding down the shift key to add more columns, and I&#8217;ve also added so it draws a number on the column header for displaying which order it was sorted in. The sort happens as soon as you let go of the shift key (and you can modify the columns by pressing the shift key again before clicking the column header).</p>
<p>Running the test class we get a table that looks like this:</p>
<p><img title="Glazed Nat Table Viewer" src="http://hexapixel.com/wp-content/uploads/2009/01/tv_nat.gif" alt="Glazed Nat Table Viewer" width="625" height="342" /></p>
<p>That pretty much sums it up for the Nat Table. Regardless of what table implementation you are going with or decide to go with, I&#8217;m sure you will agree that the power and simplicity of Glazed Lists is amazing. And we&#8217;ve only scratched the surface on it here. For much more information and examples with deeper complexity I suggest visiting the homepage and checking the Glazed Lists API. They have some built-in support for SWT as well (such as putting your updates on the UI thread), so they&#8217;re not 100% Swing-based even though most of their pre-implemented table-realted items are for Swing components. </p>
<h4><strong>Download Code</strong></h4>
<p><a href="/files/glazed_nat_framework.zip">Glazed Lists &#8211; Nat Table Framework</a></p>
<h2><strong>Closing words</strong></h2>
<p>This was a long article. It took me 2 full days to write and much longer if you count the hours put into creating both frameworks. My hopes is that you will find the article and Glazed Lists useful in your future table and grid implementations, and that this wonderful package becomes as part of your projects as <a href="http://www.miglayout.com/">MigLayout</a> or other &#8220;must have&#8221; API&#8217;s that make our lives coding UI&#8217;s much easier. Personally I cannot live without Glazed Lists, and I only wish I had found out about them sooner. </p>
<p>And again, I highly recommend the <a href="http://publicobject.com/glazedlistsdeveloper/">screen casts</a> for more information on each type of list you can create.</p>
<p>Do let me know if you spot errors in the article, it&#8217;s hard to cover everything on the first try. I will put these files + a link to this post into the Projects section later as well.</p>
<h2><strong>All Code</strong></h2>
<p>Here are download links to all code referenced in this article:</p>
<div id="bulletlist">
<ol>
<li><a href="/files/posts/TableViewerGlazedExample.java">TableViewer code</a></li>
<li><a href="/files/glazed_nebula_framework.zip">Glazed Lists &#8211; Nebula Grid Framework</a></li>
<li><a href="/files/glazed_nat_framework.zip">Glazed Lists &#8211; Nat Table Framework</a></li>
<li><a href="/files/glazed_nebula_and_nat_frameworks.zip">Glazed Lists &#8211; Both Frameworks Combined</a></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hexapixel.com/2009/01/02/glazed-lists-swt-tables-true/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>
