Adopt Adapter

Understand how adapters let disparate systems work together

1 2 Page 2
Page 2 of 2

Example 4. Use the FileNode adapter

import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
abstract class TreePanel extends JPanel {
   abstract TreeModel createTreeModel();
   public void createTree() {
      final JTree tree = new JTree(createTreeModel());
      JScrollPane scrollPane = new JScrollPane(tree);
      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
      tree.addTreeExpansionListener(new TreeExpansionListener() {
         public void treeCollapsed(TreeExpansionEvent e) {
            // Don't care about collapse events.
         }
         public void treeExpanded(TreeExpansionEvent e) {
            ...
            TreePath path = e.getPath();
            ExplorableTreeNode node = (ExplorableTreeNode)
                           path.getLastPathComponent();
            if( ! node.isExplored()) {
               DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
               ...   
               node.explore();
               model.nodeStructureChanged(node);
            }
         }
         ...
      });
   }
}
public class Test extends JFrame {
   ...
   public Test() {
      ...
      updateButton.addActionListener(new ActionListener() {
         private boolean treeCreated = false;
         public void actionPerformed(ActionEvent event) {
            if(!treeCreated) {
               TreePanel treePanel = new TreePanel() {
                  public TreeModel createTreeModel() {
                     FileNode rootNode = new FileNode(new File("/"));
                     rootNode.explore();
                     return new DefaultTreeModel(rootNode);
                  }
               };
               ...
            }
         }
      });
   }
   public static void main(String args[]) {
      GJApp.launch(new Test(),"UI Builder Prototype", 300,300,600,175);
   }
}
// Class GJApp omitted for brevity.

The file adapter showcases the Adapter pattern's power. With very little effort, you can adapt any type of tree structure into objects JTree can manipulate.

Keep adapting

The Adapter pattern lets disparate object types work together. It allows Swing trees to manipulate and display any type of tree structure. The requisite adapters are simple and easy to plug into the tree's model.

David Geary is the author of Core JSTL Mastering the JSP Standard Tag Library (Prentice Hall, 2002; ISBN: 0131001531), Advanced JavaServer Pages (Prentice Hall, 2001; ISBN: 0130307041), and the Graphic Java series (Prentice Hall). David has been developing object-oriented software with numerous object-oriented languages for almost 20 years. Since reading the GOF Design Patterns book in 1994, David has been an active proponent of design patterns, and has used and implemented design patterns in Smalltalk, C++, and Java. In 1997, David began working full-time as an author and occasional speaker and consultant. David is a member of the expert groups defining the JSP Standard Tag Library and JavaServer Faces, and is a contributor to the Apache Struts JSP framework. David is currently working on Core JavaServer Faces, which will be published in the spring of 2004.

Learn more about this topic

1 2 Page 2
Page 2 of 2