/* Utilities for managing the background color. Copyright (c) 2009-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.gui; import java.awt.Color; import javax.swing.UIManager; import javax.swing.text.AttributeSet; import javax.swing.text.Style; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.StyleSheet; /////////////////////////////////////////////////////////////////// //// JFileChooserBugFix /** A workaround for a JFileChooser bug.

This class is necessary to work around a bug under Windows where the "common places" portion of the JFileChooser dialog is affected by the background color of a component. Sun has acknowledged the bug as #6817933. See also "open dialog, common places pane has white box instead of text."

Every time JFileChooser is instantiated, saveBackground() should be called so that the background is properly set. Then, in a finally clause, restoreBackground() should be called. For example:

  // Swap backgrounds and avoid white boxes in "common places" dialog
  JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
  Color background = null;
  try {
      background = jFileChooserBugFix.saveBackground();
      PtFileChooser ptFileChooser = new PtFileChooser();
      // Do the usual JFileChooser song and dance . . .
  } finally {
      jFileChooserBugFix.restoreBackground(background);
  }
 

Note that Java under Mac OS X has a much better implementation of the file chooser that uses java.awt.FileDialog instead of javax.swing.JFileChooser. Thus, under Mac OS X, this class is typically not invoked. Under Mac OS X, to invoke this class, either {@link ptolemy.gui.PtGUIUtilities#useFileDialog()} or {@link ptolemy.gui.PtGUIUtilities#macOSLookAndFeel()} should return false. To do this, set either the ptolemy.ptII.useFileDialog property to false or set the ptolemy.ptII.MacOS property to false. See {@link ptolemy.gui.PtGUIUtilities} for how to set these properties.

@author Christopher Brooks @version $Id: JFileChooserBugFix.java 70402 2014-10-23 00:52:20Z cxh $ @since Ptolemy II 8.0 @Pt.ProposedRating Red (cxh) @Pt.AcceptedRating Red (cxh) */ public class JFileChooserBugFix { /** Instantiate a JFileChooserBugFix object. */ public JFileChooserBugFix() { _HTMLEditorKit = new HTMLEditorKit(); } /** Restore the background. *

This method is not typically called on the Mac, see * the class comment.

* @param background The background to be restored. * @see #saveBackground() */ public void restoreBackground(Color background) { try { if (background != null) { // Restore the background color. String rgb = Integer.toHexString(background.getRGB()); String rule = "body {background: #" + rgb.substring(2, rgb.length()) + ";}"; StyleSheet styleSheet = _HTMLEditorKit.getStyleSheet(); styleSheet.addRule(rule); _HTMLEditorKit.setStyleSheet(styleSheet); } } catch (Exception ex) { System.out.println("Problem restoring background color."); ex.printStackTrace(); } } /** Set the background to the value of the ToolBar.shadow property * and return the previous background. *

Avoid a problem under Windows where the common places pane * on the left of the file browser dialog has white boxes * because the background is set to white. * http://bugzilla.ecoinformatics.org/show_bug.cgi?id=3801 *

Call this method before instantiating a JFileChooser. *

This method is not typically called on the Mac, see * the class comment.

* @return the value of the previous background. */ public Color saveBackground() { if (_HTMLEditorKit == null) { _HTMLEditorKit = new HTMLEditorKit(); } StyleSheet styleSheet = _HTMLEditorKit.getStyleSheet(); Color background = null; try { // Get the background color of the HTML widget. Style style = styleSheet.getStyle("body"); if (style != null) { AttributeSet bodyAttribute = (AttributeSet) style .getAttribute(javax.swing.text.StyleConstants.ResolveAttribute); if (bodyAttribute != null) { background = styleSheet.getBackground(bodyAttribute); } } } catch (Exception ex) { if (!_printedMessage) { _printedMessage = true; System.out .println("Failed to set the background of the file dialog:" + ex); } } try { // Get the color of the ToolBar shadow and use it. Color shadow = UIManager.getColor("ToolBar.shadow"); String rgb = Integer.toHexString(shadow.getRGB()); String rule = "body {background: #" + rgb.substring(2, rgb.length()) + ";}"; styleSheet.addRule(rule); _HTMLEditorKit.setStyleSheet(styleSheet); } catch (Exception ex) { System.err.println("Problem setting background color"); ex.printStackTrace(); } return background; } /////////////////////////////////////////////////////////////////// //// private variables //// /** The HTMLEditorKit*/ private HTMLEditorKit _HTMLEditorKit; /** True if we have printed the message about failing to set the * background. */ private boolean _printedMessage; }