Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Façade clears complexity

Develop dialog boxes and Swing apps faster

  • Print
  • Feedback

Page 4 of 5

Finally, the preceding application uses the ApplicationSupport and JOptionPane façades to create a localized dialog box, shown in Figure 7.

Figure 7. An error dialog. Click on thumbnail to view full-size image.

Example 4 shows the code that displays Figure 7's dialog. Thanks to façades, two lines of code are all it takes to display a localized dialog.

Example 4. Use façades to create a localized dialog

   private void showErrorDialog(String missingFilename) {
      String msg = ApplicationSupport.formatMessage("error.missing.file",
                                                    new String[] {missingFilename});
      JOptionPane.showMessageDialog(LibraryViewer.this, msg,
               ApplicationSupport.getResource("error.missing.file.dialog.title"),
               JOptionPane.ERROR_MESSAGE);
   }


The ApplicationSupport.formatMessage() method formats a pattern (the method's first argument) given a set of arguments (the method's second argument) to format a localized message with the given filename. Because you probably don't format messages everyday, encapsulating that behavior in a façade is a great timesaver.

Example 5 lists the ApplicationSupport class.

Example 5. The ApplicationSupport façade

public final class ApplicationSupport {
   static private final String PREFS_BUNDLE_BASENAME = "prefs";
   static private final String BUNDLE_BASENAME = "app", PREFERRED_LOCALE_KEY = "locale";
   static private final JPanel statusArea = new JPanel();
   static private final JLabel status = new JLabel();
   static private ResourceBundle preferences, resources;
   static private Locale locale;
   static {
      try {
         preferences = ResourceBundle.getBundle(PREFS_BUNDLE_BASENAME);
         locale = new Locale(preferences.getString(PREFERRED_LOCALE_KEY));
      }
      catch(java.util.MissingResourceException ex) {
         System.err.println("ERROR: cannot find preferences properties file " + 
                            BUNDLE_BASENAME);
      }
      try {
         resources = ResourceBundle.getBundle(BUNDLE_BASENAME, locale);
      }
      catch(java.util.MissingResourceException ex) {
         System.err.println("ERROR: cannot find properties file for " + BUNDLE_BASENAME);
      }
   };
   // Disallow direct instantiation
   private ApplicationSupport() {}
   
   public static void launch(final JFrame f, String title,
                       final int x, final int y, 
                       final int w, int h) {
      f.setTitle(title);
      f.setBounds(x,y,w,h);
      f.setVisible(true);
      f.setResizable(true);
      status.setHorizontalAlignment(JLabel.LEFT);
      statusArea.setBorder(BorderFactory.createEtchedBorder());
      statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
      statusArea.add(status);
      f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      f.addWindowListener(new WindowAdapter() {
         public void windowClosed(WindowEvent e) {
            System.exit(0);
         }
      });
   }
   public static Locale getLocale() {
      return locale;
   }
   public static JMenu addMenu(final JFrame f, String titleKey,
                               String[] itemKeys) {
      JMenuBar mb = f.getJMenuBar();
      if(mb == null) {
         mb = new JMenuBar();
         f.setJMenuBar(mb);
      }
      JMenu menu = new JMenu(ApplicationSupport.getResource(titleKey));
      for(int i=0; i < itemKeys.length; ++i) {
         menu.add(new JMenuItem(ApplicationSupport.getResource(itemKeys[i])));
      }
      mb.add(menu);
      return menu;
   }
   public static JPanel getStatusArea() {
      return statusArea;
   }
   public static void showStatus(String s) {
      status.setText(s);
   }
   public static String getResource(String key) {
      return (resources == null) ? null : resources.getString(key);
   }
   public static String formatMessage(String patternKey, String[] params) {
      String pattern = ApplicationSupport.getResource(patternKey);
      MessageFormat fmt = new MessageFormat(pattern);
      return fmt.format(params);
   }
}


All ApplicationSupport methods are static, which is common for a façade because often those methods are unrelated. The ApplicationSupport class can launch a Swing application, localize text from a resource bundle, display information in a status panel, and create localized menus. The ApplicationSource class makes it significantly easier to implement Swing applications.

  • Print
  • Feedback

Resources