Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [albireo-dev] AwtEnvironment vs. LookAndFeelHandler

Hi Gordon,

> > I propose to
> > 
> >   - Move the font propagation methods from AwtEnvironment to LookAndFeelHandler
> >     (but they will continue to be _invoked_ from AwtEnvironment),
> > 
> >   - Copy the setComponentForeground, setComponentBackground methods from
> >     SwingControl to LookAndFeelHandler, and change the implementation of
> >     these methods in SwingControl so that they reference the LookAndFeelHandler.
> >     This way, the user can customize it either globally (by customizing the
> >     LookAndFeelHandler) or locally (in the SwingControl subclass).
> > 
> > Does that sound okay?
> 
> Sounds fine to me.

Thanks for the approval. Committed as attached.

> The font/color code has some dependencies on a  
> Display instance. Will there now be one LookAndFeelHandler per Display 
> (like AwtEnvironment)? Or will a display instance be passed in as a 
> parameter, as necessary?

There were no such dependencies in the code I moved. If there had been,
I would simply have added the Display as parameter, in order to keep the
"customizable singleton" design pattern.

Bruno

Index: src/org/eclipse/albireo/core/LookAndFeelHandler.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/LookAndFeelHandler.java,v
retrieving revision 1.8
diff -c -3 -r1.8 LookAndFeelHandler.java
*** src/org/eclipse/albireo/core/LookAndFeelHandler.java	29 Apr 2008 18:33:36 -0000	1.8
--- src/org/eclipse/albireo/core/LookAndFeelHandler.java	29 Apr 2008 18:49:13 -0000
***************
*** 11,16 ****
--- 11,17 ----
   *******************************************************************************/
  package org.eclipse.albireo.core;
  
+ import java.awt.Component;
  import java.awt.EventQueue;
  import java.io.PrintStream;
  
***************
*** 19,24 ****
--- 20,26 ----
  import javax.swing.plaf.FontUIResource;
  
  import org.eclipse.albireo.internal.Platform;
+ import org.eclipse.swt.graphics.Color;
  import org.eclipse.swt.graphics.Font;
  
  /**
***************
*** 333,338 ****
--- 335,384 ----
          UIManager.put("ViewportFont.font", fontResource); //$NON-NLS-1$
      }
  
+     // --------------------------- Color Management ---------------------------
+ 
+     /**
+      * Propagates the foreground color from SWT to a given AWT/Swing component.
+      * @param component An AWT/Swing component.
+      * @param foreground The SWT foreground color.
+      * @param preserveDefaults If true, the color will not be set on the
+      *                         component if its foreground (whether specified
+      *                         or inherited) is already the same as the given
+      *                         foreground color.
+      */
+     public void propagateSwtForeground(Component component, Color foreground, boolean preserveDefaults) {
+         assert EventQueue.isDispatchThread();
+         assert component != null;
+ 
+         ResourceConverter converter = ResourceConverter.getInstance();
+         java.awt.Color fg = converter.convertColor(foreground);
+ 
+         if (!fg.equals(component.getForeground()) || !preserveDefaults) {
+             component.setForeground(fg);
+         }
+     }
+ 
+     /**
+      * Propagates the background color from SWT to a given AWT/Swing component.
+      * @param component An AWT/Swing component.
+      * @param background The SWT background color.
+      * @param preserveDefaults If true, the color will not be set on the
+      *                         component if its background (whether specified
+      *                         or inherited) is already the same as the given
+      *                         background color.
+      */
+     public void propagateSwtBackground(Component component, Color background, boolean preserveDefaults) {
+         assert EventQueue.isDispatchThread();
+         assert component != null;
+ 
+         ResourceConverter converter = ResourceConverter.getInstance();
+         java.awt.Color bg = converter.convertColor(background);
+ 
+         if (!bg.equals(component.getBackground()) || !preserveDefaults) {
+             component.setBackground(bg);
+         }
+     }
+ 
      // ========================================================================
      // Singleton design pattern
  
Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.73
diff -c -3 -r1.73 SwingControl.java
*** src/org/eclipse/albireo/core/SwingControl.java	29 Apr 2008 18:33:36 -0000	1.73
--- src/org/eclipse/albireo/core/SwingControl.java	29 Apr 2008 18:49:14 -0000
***************
*** 1089,1113 ****
          assert EventQueue.isDispatchThread();
          assert component != null;
  
!         ResourceConverter converter = ResourceConverter.getInstance();
!         java.awt.Color fg = converter.convertColor(foreground);
! 
!         if (!component.getForeground().equals(fg) || !preserveDefaults) {
!             component.setForeground(fg);
!         }
      }
!     
      protected void setComponentBackground(Component component, Color background, boolean preserveDefaults) {
          assert EventQueue.isDispatchThread();
  
!         ResourceConverter converter = ResourceConverter.getInstance();
!         java.awt.Color bg = converter.convertColor(background);
! 
!         if (!component.getBackground().equals(bg) || !preserveDefaults) {
!             component.setBackground(bg);
!         }
      }
!     
      // ============================= Focus Management =============================
      private FocusHandler focusHandler;
      private boolean isSwtTabOrderExtended = true;
--- 1089,1104 ----
          assert EventQueue.isDispatchThread();
          assert component != null;
  
!         LookAndFeelHandler.getInstance().propagateSwtForeground(component, foreground, preserveDefaults);
      }
! 
      protected void setComponentBackground(Component component, Color background, boolean preserveDefaults) {
          assert EventQueue.isDispatchThread();
+         assert component != null;
  
!         LookAndFeelHandler.getInstance().propagateSwtBackground(component, background, preserveDefaults);
      }
! 
      // ============================= Focus Management =============================
      private FocusHandler focusHandler;
      private boolean isSwtTabOrderExtended = true;
Index: src/org/eclipse/albireo/core/AwtEnvironment.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/AwtEnvironment.java,v
retrieving revision 1.21
diff -c -3 -r1.21 AwtEnvironment.java
*** src/org/eclipse/albireo/core/AwtEnvironment.java	28 Apr 2008 19:40:11 -0000	1.21
--- src/org/eclipse/albireo/core/AwtEnvironment.java	29 Apr 2008 18:31:23 -0000
***************
*** 180,186 ****
              EventQueue.invokeAndWait(new Runnable() {
                  public void run() {
                      setLookAndFeel();
!                     propagateSwtFont(initialFont[0]);
                      if (FocusHandler.verboseKFHEvents)
                          FocusDebugging.enableKeyboardFocusManagerLogging();
                  }
--- 180,186 ----
              EventQueue.invokeAndWait(new Runnable() {
                  public void run() {
                      setLookAndFeel();
!                     LookAndFeelHandler.getInstance().propagateSwtFont(initialFont[0]);
                      if (FocusHandler.verboseKFHEvents)
                          FocusDebugging.enableKeyboardFocusManagerLogging();
                  }
***************
*** 322,413 ****
          }
      }
  
-     //==================== Font Management ================================
- 
-     private Font lastPropagatedSwtFont;
-     private static boolean isDefaultSwtFontPropagated = true;
- 
-     protected java.awt.Font propagateSwtFont(Font swtFont) {
-         assert EventQueue.isDispatchThread();    // On AWT event thread
- 
-         java.awt.Font awtFont = ResourceConverter.getInstance().convertFont(swtFont);
-         if (isDefaultSwtFontPropagated && (lastPropagatedSwtFont != swtFont) && !swtFont.getDevice().isDisposed()) {
-             lastPropagatedSwtFont = swtFont;
-             
-             // Update the look and feel defaults to use new font.
-             // Swing should take care of this on its own, but it does not seem to do it when mixed with SWT
-             updateLookAndFeelFonts(awtFont);
-         }
-         return awtFont;
-     }
- 
-     protected void updateLookAndFeelFonts(java.awt.Font awtFont) {
-         assert awtFont != null;
-         assert EventQueue.isDispatchThread();    // On AWT event thread
-         
-         // The FontUIResource class marks the font as replaceable by the look and feel
-         // implementation if font settings are later changed.
-         FontUIResource fontResource = new FontUIResource(awtFont);
- 
-         // Assign the new font to the relevant L&F font properties. These are
-         // the properties that are initially assigned to the system font
-         // under the Windows look and feel.
-         // TODO: It's possible that other platforms will need other assignments.
-         // TODO: This does not handle fonts other than the "system" font.
-         // TODO: Swing does not render the Vista default Segoe UI font well.
-         // Other fonts may change, and the Swing L&F may not be adjusting.
-         
-         UIManager.put("Button.font", fontResource); //$NON-NLS-1$
-         UIManager.put("CheckBox.font", fontResource); //$NON-NLS-1$
-         UIManager.put("ComboBox.font", fontResource); //$NON-NLS-1$
-         UIManager.put("EditorPane.font", fontResource); //$NON-NLS-1$
-         UIManager.put("Label.font", fontResource); //$NON-NLS-1$
-         UIManager.put("List.font", fontResource); //$NON-NLS-1$
-         UIManager.put("Panel.font", fontResource); //$NON-NLS-1$
-         UIManager.put("ProgressBar.font", fontResource); //$NON-NLS-1$
-         UIManager.put("RadioButton.font", fontResource); //$NON-NLS-1$
-         UIManager.put("ScrollPane.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TabbedPane.font", fontResource); //$NON-NLS-1$
-         UIManager.put("Table.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TableHeader.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TextField.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TextPane.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TitledBorder.font", fontResource); //$NON-NLS-1$
-         UIManager.put("ToggleButton.font", fontResource); //$NON-NLS-1$
-         UIManager.put("TreeFont.font", fontResource); //$NON-NLS-1$
-         UIManager.put("ViewportFont.font", fontResource); //$NON-NLS-1$
-     }
- 
-     /**
-      * Returns whether SWT default font changes are automatically propagated to Swing. 
-      * See {@link #setSwtDefaultFontPropagated(boolean)} for more information. 
-      *  
-      * @return boolean 
-      */
-     public static boolean isSwtDefaultFontPropagated() {
-         return isDefaultSwtFontPropagated;
-     }
- 
-     /**
-      * Configures automatic propagation of changes to the SWT default font to the 
-      * underlying Swing look and feel. 
-      * <p>
-      * The default value of this flag is <code>true</code>. 
-      * Normally, this ensures that Swing fonts will closely match the corresponding
-      * SWT fonts, and that changes to the system font settings will be obeyed by both
-      * Swing and SWT. However, Swing does not render certain fonts very well (e.g. the 
-      * default "Segoe UI" font in Windows Vista). Setting the flag to <code>false</code>
-      * will disable the propagation in cases where the Swing rendering is not acceptable, 
-      * and Swing will use its default fonts. Note, however, that if propagation is disabled, 
-      * changes to system font settings may not be detected by Swing.
-      * 
-      * @param val boolean flag. If <code>true</code>, default fonts will be propagated to 
-      * Swing.
-      */
-     public static void setSwtDefaultFontPropagated(boolean val) {
-         isDefaultSwtFontPropagated = val;
-     }
- 
      // =========================== Other useful API ===========================
  
      // -------------------------- Modal AWT Dialogs --------------------------
--- 322,327 ----
Index: src/org/eclipse/albireo/core/LookAndFeelHandler.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/LookAndFeelHandler.java,v
retrieving revision 1.7
diff -c -3 -r1.7 LookAndFeelHandler.java
*** src/org/eclipse/albireo/core/LookAndFeelHandler.java	16 Feb 2008 23:27:20 -0000	1.7
--- src/org/eclipse/albireo/core/LookAndFeelHandler.java	29 Apr 2008 18:31:23 -0000
***************
*** 16,23 ****
--- 16,25 ----
  
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
+ import javax.swing.plaf.FontUIResource;
  
  import org.eclipse.albireo.internal.Platform;
+ import org.eclipse.swt.graphics.Font;
  
  /**
   * This class deals with the customization of the look&amp;feel
***************
*** 114,119 ****
--- 116,157 ----
          lafChoice = choice;
      }
  
+     // ------------------------------------------------------------------------
+ 
+     private boolean isDefaultSwtFontPropagated = true;
+ 
+     /**
+      * Returns whether SWT default font changes are automatically propagated to
+      * Swing. See {@link #setSwtDefaultFontPropagated(boolean)} for more
+      * information. 
+      *  
+      * @return boolean 
+      */
+     public boolean isSwtDefaultFontPropagated() {
+         return isDefaultSwtFontPropagated;
+     }
+ 
+     /**
+      * Configures automatic propagation of changes to the SWT default font to
+      * the underlying Swing look and feel. 
+      * <p>
+      * The default value of this flag is <code>true</code>. 
+      * Normally, this ensures that Swing fonts will closely match the
+      * corresponding SWT fonts, and that changes to the system font settings
+      * will be obeyed by both Swing and SWT. However, Swing does not render
+      * certain fonts very well (e.g. the default "Segoe UI" font in Windows
+      * Vista). Setting the flag to <code>false</code> will disable the
+      * propagation in cases where the Swing rendering is not acceptable, and
+      * Swing will use its default fonts. Note, however, that if propagation is
+      * disabled, changes to system font settings may not be detected by Swing.
+      * 
+      * @param val boolean flag. If <code>true</code>, default fonts will be
+      *            propagated to Swing.
+      */
+     public void setSwtDefaultFontPropagated(boolean val) {
+         isDefaultSwtFontPropagated = val;
+     }
+ 
      // ========================================================================
      // Overridable API
  
***************
*** 222,227 ****
--- 260,338 ----
          }
      }
  
+     // --------------------------- Font Management ---------------------------
+ 
+     private Font lastPropagatedSwtFont;
+ 
+     /**
+      * Propagates the default SWT font to the Swing look&feel,
+      * if allowed by {@link #isSwtDefaultFontPropagated()}.
+      * In this implementation, this method calls the
+      * {@link #updateLookAndFeelFonts(java.awt.Font)} method.
+      * @param swtFont The default SWT font.
+      * @return The corresponding AWT font.
+      * @see #isSwtDefaultFontPropagated()
+      * @see #updateLookAndFeelFonts(java.awt.Font)
+      */
+     public java.awt.Font propagateSwtFont(Font swtFont) {
+         assert EventQueue.isDispatchThread();    // On AWT event thread
+ 
+         java.awt.Font awtFont = ResourceConverter.getInstance().convertFont(swtFont);
+         if (isSwtDefaultFontPropagated()
+             && !swtFont.getDevice().isDisposed()
+             && (lastPropagatedSwtFont != swtFont)) {
+             lastPropagatedSwtFont = swtFont;
+ 
+             // Update the look and feel defaults to use new font.
+             // Swing should take care of this on its own, but it does not seem
+             // to do it when mixed with SWT.
+             updateLookAndFeelFonts(awtFont);
+         }
+         return awtFont;
+     }
+ 
+     /**
+      * Changes the currently active Swing look&feel to use the given font
+      * as primary font for everything.
+      * @param awtFont A font.
+      */
+     protected void updateLookAndFeelFonts(java.awt.Font awtFont) {
+         assert awtFont != null;
+         assert EventQueue.isDispatchThread();    // On AWT event thread
+         
+         // The FontUIResource class marks the font as replaceable by the look and feel
+         // implementation if font settings are later changed.
+         FontUIResource fontResource = new FontUIResource(awtFont);
+ 
+         // Assign the new font to the relevant L&F font properties. These are
+         // the properties that are initially assigned to the system font
+         // under the Windows look and feel.
+         // TODO: It's possible that other platforms will need other assignments.
+         // TODO: This does not handle fonts other than the "system" font.
+         // TODO: Swing does not render the Vista default Segoe UI font well.
+         // Other fonts may change, and the Swing L&F may not be adjusting.
+         
+         UIManager.put("Button.font", fontResource); //$NON-NLS-1$
+         UIManager.put("CheckBox.font", fontResource); //$NON-NLS-1$
+         UIManager.put("ComboBox.font", fontResource); //$NON-NLS-1$
+         UIManager.put("EditorPane.font", fontResource); //$NON-NLS-1$
+         UIManager.put("Label.font", fontResource); //$NON-NLS-1$
+         UIManager.put("List.font", fontResource); //$NON-NLS-1$
+         UIManager.put("Panel.font", fontResource); //$NON-NLS-1$
+         UIManager.put("ProgressBar.font", fontResource); //$NON-NLS-1$
+         UIManager.put("RadioButton.font", fontResource); //$NON-NLS-1$
+         UIManager.put("ScrollPane.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TabbedPane.font", fontResource); //$NON-NLS-1$
+         UIManager.put("Table.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TableHeader.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TextField.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TextPane.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TitledBorder.font", fontResource); //$NON-NLS-1$
+         UIManager.put("ToggleButton.font", fontResource); //$NON-NLS-1$
+         UIManager.put("TreeFont.font", fontResource); //$NON-NLS-1$
+         UIManager.put("ViewportFont.font", fontResource); //$NON-NLS-1$
+     }
+ 
      // ========================================================================
      // Singleton design pattern
  
Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.72
diff -c -3 -r1.72 SwingControl.java
*** src/org/eclipse/albireo/core/SwingControl.java	28 Apr 2008 20:28:37 -0000	1.72
--- src/org/eclipse/albireo/core/SwingControl.java	29 Apr 2008 18:31:24 -0000
***************
*** 977,988 ****
          });
      }
  
- 
- 
      private void updateDefaultFont(Font swtFont) {
          assert EventQueue.isDispatchThread();    // On AWT event thread
  
!         java.awt.Font awtFont = AwtEnvironment.getInstance(display).propagateSwtFont(swtFont);
          if (awtFont == null) {
              return;
          }
--- 977,986 ----
          });
      }
  
      private void updateDefaultFont(Font swtFont) {
          assert EventQueue.isDispatchThread();    // On AWT event thread
  
!         java.awt.Font awtFont = LookAndFeelHandler.getInstance().propagateSwtFont(swtFont);
          if (awtFont == null) {
              return;
          }

Back to the top