<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Depth-First: Tag painter</title>
    <link>http://depth-first.com/articles/tag/painter</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>Building a Molecule Preview with Firefly: The Joy of Swing</title>
      <description>&lt;p&gt;Previous articles have discussed &lt;a href="http://depth-first.com/articles/tag/firefly"&gt;Firefly&lt;/a&gt;, the codename for a new 2D structure editor for the Web. Although it can be deployed as a self-contained applet on Web pages, Firefly is composed of modules that are readily re-used. By taking advantage of this design and Java's native UI toolkit Swing, new UI elements can be built with relatively little effort. This article outlines one such use - the creation of a file dialog that contains a molecule preview.&lt;/p&gt;

&lt;p&gt;Many image processing applications such as Photoshop or GIMP provide an image preview that appears in file browser dialogs. Wouldn't it be nice if applications that process molecular structure files came with a similar feature? The screenshot below shows a file chooser with an embedded molecule preview based of Firefly's Painter component:&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20070718/dialog1.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;When a new molfile is highlighted, a new preview is automatically generated:&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20070718/dialog2.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;The molecule preview is capable of all of the customizations available in Firefly including background, bond, and atom colors, borders, and atom label fonts. No matter how large or small the molecule, and regardless of its starting coordinates, it will always be exactly scaled to fit the available space and precisely centered.&lt;/p&gt;

&lt;p&gt;This dialog was rapidly implemented using the accessory capability provided by Swing's &lt;tt&gt;JFileChooser&lt;/tt&gt;:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;JFileChooser chooser = new JFileChooser();
PreviewAccessory accessory = new PreviewAccessory(chooser);

accessory.setPreferredSize(new Dimension(150, 150));

chooser.setAccessory(accessory);
chooser.addPropertyChangeListener(accessory);

// ...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Defining a JComponent implmenting the &lt;tt&gt;PropertyChangeListener&lt;/tt&gt; interface is all that's needed to get a working molecule preview:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;class PreviewAccessory extends DefaultPainter implements PropertyChangeListener
{
  // implementation
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Swing has come a long way since the dark days of JDK 1.2. What started out as the dog-slow ugly duckling of user interface toolkits has developed into one of the best platforms for building desktop applications out there. Advanced tools such as the WYSIWYG interface builder &lt;a href="http://www.netbeans.org/kb/articles/matisse.html"&gt;Matisse&lt;/a&gt; and the polished components offered by &lt;a href="http://www.jidesoft.com/"&gt;JIDE&lt;/a&gt; make Swing an even more attractive option. The example described here is just one instance of how Swing's well-conceived design simplifies the job of building rich user interfaces.&lt;/p&gt;</description>
      <pubDate>Wed, 18 Jul 2007 06:52:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:33a3bc07-253a-4221-a8ae-46a77a47076a</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2007/07/18/building-a-molecule-preview-with-firefly-the-joy-of-swing</link>
      <category>Tools</category>
      <category>firefly</category>
      <category>filechooser</category>
      <category>swing</category>
      <category>painter</category>
      <category>preview</category>
    </item>
  </channel>
</rss>
