Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-ui-dev] More from the colors and fonts front


Theme support in eclipse moves ever forward.  On the extension point side, definitions for colors, fonts and gradients have been moved under the org.eclipse.ui.themes point.  The old font extension point still exists for backwards compatibility and the fonts it describes are first class citizens in the themes world.  The themes point also allows for theme elements.  A theme is an identifiable grouping of font, color and gradient overrides that is used to provide alternate schemes for workbench usage.  In addition to these named themes, there is also the default theme which contains all of the base definitions.

The workbench has started using themes for all of its color, font and gradient resolution.  We have introduced getThemeManager() into IWorkbench.  IThemeManager allows you to get and set  the currently active theme, get an arbitrary theme, and add theme change listeners.  Clients who introduce their own definitions or rely on the definitions of others should use the color, font and gradient registries provided by the ITheme interface rather than the those supplied by JFaceResources.  By doing so they can better integrate with a users currently active theme.  You can change the currently active theme by going to the appearance page and modifying the theme combo.  When the current theme changes, the values in the Colors and Fonts preference page change to reflect the new theme.  There are still some implementation details to work out here (the workbench doesn't yet respect theme changes in all cases and the preference page doesn't default to the correct values for anything but the default theme) but it's something to play with.  These issues should be addressed shortly.

Here is an example of a simple view that responds to theme changes. Please note that the color IDs used here don't actually exist in the workbench.  If you want to give this a try you'll need to add some colorDefinitions and possibly a theme with colorOverrides.

public class ThemeDemo extends ViewPart {

    static final String MYCOLORID1 = "scratch1";
    static final String MYCOLORID2 = "scratch2";

    private Label control1;
    private Label control2;

    private IPropertyChangeListener valueChangeListener = new IPropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(IThemeManager.CHANGE_CURRENT_THEME)) {
                // the current enabled theme has changed. refresh all colors
                // and listeners
                ((ITheme) event.getOldValue()).getColorRegistry().removeListener(this);
                ((ITheme) event.getNewValue()).getColorRegistry().addListener(this);
                        setControlColor(control1, MYCOLORID1);
                        setControlColor(control2, MYCOLORID2);
            } else if (event.getProperty().equals(MYCOLORID1)) {
                    // the value of mycolor1 has changed in the current theme
                setControlColor(control1, MYCOLORID1);
            } else if (event.getProperty().equals(MYCOLORID2)) {
                    // the value of mycolor2 has changed in the current theme
                setControlColor(control2, MYCOLORID2);
            }
        }
    };

    /**
     * @see ViewPart#createPartControl
     */
    public void createPartControl(Composite parent) {
        control1 = new Label(parent, SWT.NONE);
        control1.setText("Some text");
        control2 = new Label(parent, SWT.NONE);
        control2.setText("Some more text");

        IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
        // listen for the default workbench theme to change.
        themeManager.addPropertyChangeListener(valueChangeListener);
        // listen for changes to the current theme values
        themeManager.getCurrentTheme().getColorRegistry().addListener(valueChangeListener);
        setControlColor(control1, MYCOLORID1);
        setControlColor(control2, MYCOLORID2);
    }

    private void setControlColor(Control control, String colorKey) {
               if (control != null && !control.isDisposed())
                control.setForeground(PlatformUI.getWorkbench().getThemeManager()
                        .getCurrentTheme().getColorRegistry().get(colorKey));
    }
   
    public void setFocus() {}

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IWorkbenchPart#dispose()
     */
    public void dispose() {
        PlatformUI.getWorkbench().getThemeManager()
                .removePropertyChangeListener(valueChangeListener);
        PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
                .removePropertyChangeListener(valueChangeListener);
        super.dispose();
    }
}

Back to the top