Hi there, I added a theme switcher menu into our Swing demo. I also moved the GNULookAndFeel into the gnu.javax.swing... namespace and added it as an 'installed' L&F to the UIManager.
2006-03-16 Roman Kennke <[EMAIL PROTECTED]> * gnu/javax/swing/plaf/gnu/GNULookAndFeel.java Moved from examples into gnu.javax.swing.. namespace. * examples/gnu/classpath/examples/swing/GNULookAndFeel.java Moved to gnu.javax.swing.. namespace. * examples/gnu/classpath/examples/swing/Demo.java: (themesMenu): New field. Used to disable theme switch in non-Metal-L&Fs. (mkMenuBar): Added L&F menu. (ChangeThemeAction.actionPerformed): Only switch theme when in Metal L&F. (ChangeLAFAction): New class used for changing themes. * javax/swing/UIManager.java (installed): Added GNU L&F as installed L&F. /Roman -- “Improvement makes straight roads, but the crooked roads, without Improvement, are roads of Genius.” - William Blake
Index: javax/swing/UIManager.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/UIManager.java,v retrieving revision 1.30 diff -u -r1.30 UIManager.java --- javax/swing/UIManager.java 7 Feb 2006 15:42:15 -0000 1.30 +++ javax/swing/UIManager.java 16 Mar 2006 12:00:33 -0000 @@ -121,7 +121,8 @@ /** The installed look and feel(s). */ static LookAndFeelInfo [] installed = { - new LookAndFeelInfo("Metal", "javax.swing.plaf.metal.MetalLookAndFeel") + new LookAndFeelInfo("Metal", "javax.swing.plaf.metal.MetalLookAndFeel"), + new LookAndFeelInfo("GNU", "gnu.javax.swing.plaf.gnu.GNULookAndFeel") }; /** The installed auxiliary look and feels. */ Index: examples/gnu/classpath/examples/swing/Demo.java =================================================================== RCS file: /cvsroot/classpath/classpath/examples/gnu/classpath/examples/swing/Demo.java,v retrieving revision 1.38 diff -u -r1.38 Demo.java --- examples/gnu/classpath/examples/swing/Demo.java 15 Mar 2006 16:41:02 -0000 1.38 +++ examples/gnu/classpath/examples/swing/Demo.java 16 Mar 2006 12:00:33 -0000 @@ -45,6 +45,12 @@ */ JDesktopPane desktop; + /** + * The themes menu. This is implemented as a field so that the L&F switcher + * can disable the menu when a non-Metal L&F is selected. + */ + JMenu themesMenu; + static Color blueGray = new Color(0xdc, 0xda, 0xd5); private static Icon stockIcon(String s) @@ -171,25 +177,41 @@ } }); + // Create L&F menu. + JMenu lafMenu = new JMenu("Look and Feel"); + ButtonGroup lafGroup = new ButtonGroup(); + UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); + String currentLaf = UIManager.getLookAndFeel().getClass().getName(); + for (int i = 0; i < lafs.length; ++i) + { + UIManager.LookAndFeelInfo laf = lafs[i]; + ChangeLAFAction action = new ChangeLAFAction(laf); + JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(action); + boolean selected = laf.getClassName().equals(currentLaf); + lafItem.setSelected(selected); + lafMenu.add(lafItem); + } + // Create themes menu. - JMenu themes = new JMenu("Themes"); + themesMenu = new JMenu("Themes"); ButtonGroup themesGroup = new ButtonGroup(); JRadioButtonMenuItem ocean = new JRadioButtonMenuItem(new ChangeThemeAction(new OceanTheme())); ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme); - themes.add(ocean); + themesMenu.add(ocean); themesGroup.add(ocean); JRadioButtonMenuItem steel = new JRadioButtonMenuItem(new ChangeThemeAction(new DefaultMetalTheme())); ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof DefaultMetalTheme); - themes.add(steel); + themesMenu.add(steel); themesGroup.add(steel); bar.add(file); bar.add(edit); bar.add(examples); - bar.add(themes); + bar.add(lafMenu); + bar.add(themesMenu); bar.add(help); return bar; } @@ -539,7 +561,10 @@ MetalLookAndFeel.setCurrentTheme(theme); try { - UIManager.setLookAndFeel(new MetalLookAndFeel()); + // Only switch theme if we have a metal L&F. It is still necessary + // to install a new MetalLookAndFeel instance. + if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel) + UIManager.setLookAndFeel(new MetalLookAndFeel()); } catch (UnsupportedLookAndFeelException ex) { @@ -549,4 +574,47 @@ } } + + /** + * This Action is used to switch Metal themes. + */ + class ChangeLAFAction extends AbstractAction + { + /** + * The theme to switch to. + */ + private UIManager.LookAndFeelInfo laf; + + /** + * Creates a new ChangeLAFAction for the specified L&F. + * + * @param l the L&F to switch to + */ + ChangeLAFAction(UIManager.LookAndFeelInfo l) + { + laf = l; + putValue(NAME, laf.getName()); + } + + /** + * Changes the theme to the one specified in the constructor. + * + * @param event the action event that triggered this action + */ + public void actionPerformed(ActionEvent event) + { + try + { + UIManager.setLookAndFeel(laf.getClassName()); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + SwingUtilities.updateComponentTreeUI(frame); + themesMenu.setEnabled(laf.getClassName() + .equals("javax.swing.plaf.metal.MetalLookAndFeel")); + } + + } } Index: examples/gnu/classpath/examples/swing/GNULookAndFeel.java =================================================================== RCS file: examples/gnu/classpath/examples/swing/GNULookAndFeel.java diff -N examples/gnu/classpath/examples/swing/GNULookAndFeel.java --- examples/gnu/classpath/examples/swing/GNULookAndFeel.java 8 Nov 2005 22:42:45 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,265 +0,0 @@ -/* GNULookAndFeel.java -- An example of using the javax.swing UI. - Copyright (C) 2005 Free Software Foundation, Inc. - -This file is part of GNU Classpath examples. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. -*/ - -package gnu.classpath.examples.swing; - -import java.awt.Color; -import java.awt.Component; -import java.awt.Graphics; - -import javax.swing.Icon; -import javax.swing.ImageIcon; -import javax.swing.JCheckBox; -import javax.swing.JRadioButton; -import javax.swing.UIDefaults; -import javax.swing.plaf.ColorUIResource; -import javax.swing.plaf.IconUIResource; -import javax.swing.plaf.basic.BasicLookAndFeel; - -public class GNULookAndFeel extends BasicLookAndFeel -{ - - static Color blueGray = new Color(0xdc, 0xda, 0xd5); - - public boolean isNativeLookAndFeel() { return true; } - public boolean isSupportedLookAndFeel() { return true; } - public String getDescription() { return "GNU Look and Feel"; } - public String getID() { return "GNULookAndFeel"; } - public String getName() { return "GNU"; } - - static UIDefaults LAF_defaults; - - private final static String iconspath = "/gnu/javax/swing/plaf/gtk/icons/"; - - public UIDefaults getDefaults() - { - if (LAF_defaults == null) - { - LAF_defaults = super.getDefaults(); - Object[] myDefaults = new Object[] { - "Button.background", new ColorUIResource(blueGray), - "CheckBox.background", new ColorUIResource(blueGray), - "CheckBoxMenuItem.background", new ColorUIResource(blueGray), - "ToolBar.background", new ColorUIResource(blueGray), - "Panel.background", new ColorUIResource(blueGray), - "Slider.background", new ColorUIResource(blueGray), - "OptionPane.background", new ColorUIResource(blueGray), - "ProgressBar.background", new ColorUIResource(blueGray), - "TabbedPane.background", new ColorUIResource(blueGray), - "Label.background", new ColorUIResource(blueGray), - "Menu.background", new ColorUIResource(blueGray), - "MenuBar.background", new ColorUIResource(blueGray), - "MenuItem.background", new ColorUIResource(blueGray), - "ScrollBar.background", new ColorUIResource(blueGray), - "CheckBox.icon", new CheckBoxIcon(), - "RadioButton.icon", new RadioButtonIcon(), - - "Tree.closedIcon", - new IconUIResource(new ImageIcon - (getClass().getResource - (iconspath + "TreeClosed.png"))), - "Tree.leafIcon", - new IconUIResource(new ImageIcon - (getClass().getResource - (iconspath + "TreeLeaf.png"))), - "Tree.openIcon", - new IconUIResource(new ImageIcon - (getClass().getResource - (iconspath + "TreeOpen.png"))), - }; - LAF_defaults.putDefaults(myDefaults); - } - return LAF_defaults; - } - - /** - * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty - * icon with a size of 13x13 pixels. - */ - static class CheckBoxIcon - implements Icon - { - /** - * Returns the height of the icon. The BasicLookAndFeel CheckBox icon - * has a height of 13 pixels. - * - * @return the height of the icon - */ - public int getIconHeight() - { - return 13; - } - - /** - * Returns the width of the icon. The BasicLookAndFeel CheckBox icon - * has a width of 13 pixels. - * - * @return the height of the icon - */ - public int getIconWidth() - { - return 13; - } - - /** - * Paints the icon. The BasicLookAndFeel CheckBox icon is empty and does - * not need to be painted. - * - * @param c the component to be painted - * @param g the Graphics context to be painted with - * @param x the x position of the icon - * @param y the y position of the icon - */ - public void paintIcon(Component c, Graphics g, int x, int y) - { - Color save = g.getColor(); - g.setColor(c.getForeground()); - g.drawRect(x, y, getIconWidth(), getIconHeight()); - - JCheckBox item = (JCheckBox) c; - if (item.isSelected()) - { - g.drawLine(3 + x, 5 + y, 3 + x, 9 + y); - g.drawLine(4 + x, 5 + y, 4 + x, 9 + y); - g.drawLine(5 + x, 7 + y, 9 + x, 3 + y); - g.drawLine(5 + x, 8 + y, 9 + x, 4 + y); - } - - g.setColor(save); - } - } - - /** - * The icon used for RadioButtons in the GNULookAndFeel. This is an empty - * icon with a size of 13x13 pixels. - */ - static class RadioButtonIcon - implements Icon - { - /** - * Returns the height of the icon. The GNULookAndFeel RadioButton icon - * has a height of 13 pixels. - * - * @return the height of the icon - */ - public int getIconHeight() - { - return 13; - } - - /** - * Returns the width of the icon. The GNULookAndFeel RadioButton icon - * has a width of 13 pixels. - * - * @return the height of the icon - */ - public int getIconWidth() - { - return 13; - } - - /** - * Paints the icon. The GNULookAndFeel RadioButton icon is empty and does - * not need to be painted. - * - * @param c the component to be painted - * @param g the Graphics context to be painted with - * @param x the x position of the icon - * @param y the y position of the icon - */ - public void paintIcon(Component c, Graphics g, int x, int y) - { - Color savedColor = g.getColor(); - JRadioButton b = (JRadioButton) c; - - // draw outer circle - if (b.isEnabled()) - g.setColor(Color.GRAY); - else - g.setColor(Color.GRAY); - g.drawLine(x + 2, y + 1, x + 3, y + 1); - g.drawLine(x + 4, y, x + 7, y); - g.drawLine(x + 8, y + 1, x + 9, y + 1); - g.drawLine(x + 10, y + 2, x + 10, y + 3); - g.drawLine(x + 11, y + 4, x + 11, y + 7); - g.drawLine(x + 10, y + 8, x + 10, y + 9); - g.drawLine(x + 8, y + 10, x + 9, y + 10); - g.drawLine(x + 4, y + 11, x + 7, y + 11); - g.drawLine(x + 2, y + 10, x + 3, y + 10); - g.drawLine(x + 1, y + 9, x + 1, y + 8); - g.drawLine(x, y + 7, x, y + 4); - g.drawLine(x + 1, y + 2, x + 1, y + 3); - - if (b.getModel().isArmed()) - { - g.setColor(Color.GRAY); - g.drawLine(x + 4, y + 1, x + 7, y + 1); - g.drawLine(x + 4, y + 10, x + 7, y + 10); - g.drawLine(x + 1, y + 4, x + 1, y + 7); - g.drawLine(x + 10, y + 4, x + 10, y + 7); - g.fillRect(x + 2, y + 2, 8, 8); - } - else - { - // only draw inner highlight if not filled - if (b.isEnabled()) - { - g.setColor(Color.WHITE); - - g.drawLine(x + 2, y + 8, x + 2, y + 9); - g.drawLine(x + 1, y + 4, x + 1, y + 7); - g.drawLine(x + 2, y + 2, x + 2, y + 3); - g.drawLine(x + 3, y + 2, x + 3, y + 2); - g.drawLine(x + 4, y + 1, x + 7, y + 1); - g.drawLine(x + 8, y + 2, x + 9, y + 2); - } - } - - // draw outer highlight - if (b.isEnabled()) - { - g.setColor(Color.WHITE); - - // outer - g.drawLine(x + 10, y + 1, x + 10, y + 1); - g.drawLine(x + 11, y + 2, x + 11, y + 3); - g.drawLine(x + 12, y + 4, x + 12, y + 7); - g.drawLine(x + 11, y + 8, x + 11, y + 9); - g.drawLine(x + 10, y + 10, x + 10, y + 10); - g.drawLine(x + 8, y + 11, x + 9, y + 11); - g.drawLine(x + 4, y + 12, x + 7, y + 12); - g.drawLine(x + 2, y + 11, x + 3, y + 11); - } - - if (b.isSelected()) - { - if (b.isEnabled()) - g.setColor(Color.BLACK); - else - g.setColor(Color.GRAY); - g.drawLine(x + 4, y + 3, x + 7, y + 3); - g.fillRect(x + 3, y + 4, 6, 4); - g.drawLine(x + 4, y + 8, x + 7, y + 8); - } - g.setColor(savedColor); - } - } -} Index: gnu/javax/swing/plaf/gnu/GNULookAndFeel.java =================================================================== RCS file: gnu/javax/swing/plaf/gnu/GNULookAndFeel.java diff -N gnu/javax/swing/plaf/gnu/GNULookAndFeel.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ gnu/javax/swing/plaf/gnu/GNULookAndFeel.java 16 Mar 2006 12:00:33 -0000 @@ -0,0 +1,265 @@ +/* GNULookAndFeel.java -- An example of using the javax.swing UI. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath examples. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. +*/ + +package gnu.javax.swing.plaf.gnu; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; + +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.JCheckBox; +import javax.swing.JRadioButton; +import javax.swing.UIDefaults; +import javax.swing.plaf.ColorUIResource; +import javax.swing.plaf.IconUIResource; +import javax.swing.plaf.basic.BasicLookAndFeel; + +public class GNULookAndFeel extends BasicLookAndFeel +{ + + static Color blueGray = new Color(0xdc, 0xda, 0xd5); + + public boolean isNativeLookAndFeel() { return true; } + public boolean isSupportedLookAndFeel() { return true; } + public String getDescription() { return "GNU Look and Feel"; } + public String getID() { return "GNULookAndFeel"; } + public String getName() { return "GNU"; } + + static UIDefaults LAF_defaults; + + private final static String iconspath = "/gnu/javax/swing/plaf/gtk/icons/"; + + public UIDefaults getDefaults() + { + if (LAF_defaults == null) + { + LAF_defaults = super.getDefaults(); + Object[] myDefaults = new Object[] { + "Button.background", new ColorUIResource(blueGray), + "CheckBox.background", new ColorUIResource(blueGray), + "CheckBoxMenuItem.background", new ColorUIResource(blueGray), + "ToolBar.background", new ColorUIResource(blueGray), + "Panel.background", new ColorUIResource(blueGray), + "Slider.background", new ColorUIResource(blueGray), + "OptionPane.background", new ColorUIResource(blueGray), + "ProgressBar.background", new ColorUIResource(blueGray), + "TabbedPane.background", new ColorUIResource(blueGray), + "Label.background", new ColorUIResource(blueGray), + "Menu.background", new ColorUIResource(blueGray), + "MenuBar.background", new ColorUIResource(blueGray), + "MenuItem.background", new ColorUIResource(blueGray), + "ScrollBar.background", new ColorUIResource(blueGray), + "CheckBox.icon", new CheckBoxIcon(), + "RadioButton.icon", new RadioButtonIcon(), + + "Tree.closedIcon", + new IconUIResource(new ImageIcon + (getClass().getResource + (iconspath + "TreeClosed.png"))), + "Tree.leafIcon", + new IconUIResource(new ImageIcon + (getClass().getResource + (iconspath + "TreeLeaf.png"))), + "Tree.openIcon", + new IconUIResource(new ImageIcon + (getClass().getResource + (iconspath + "TreeOpen.png"))), + }; + LAF_defaults.putDefaults(myDefaults); + } + return LAF_defaults; + } + + /** + * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty + * icon with a size of 13x13 pixels. + */ + static class CheckBoxIcon + implements Icon + { + /** + * Returns the height of the icon. The BasicLookAndFeel CheckBox icon + * has a height of 13 pixels. + * + * @return the height of the icon + */ + public int getIconHeight() + { + return 13; + } + + /** + * Returns the width of the icon. The BasicLookAndFeel CheckBox icon + * has a width of 13 pixels. + * + * @return the height of the icon + */ + public int getIconWidth() + { + return 13; + } + + /** + * Paints the icon. The BasicLookAndFeel CheckBox icon is empty and does + * not need to be painted. + * + * @param c the component to be painted + * @param g the Graphics context to be painted with + * @param x the x position of the icon + * @param y the y position of the icon + */ + public void paintIcon(Component c, Graphics g, int x, int y) + { + Color save = g.getColor(); + g.setColor(c.getForeground()); + g.drawRect(x, y, getIconWidth(), getIconHeight()); + + JCheckBox item = (JCheckBox) c; + if (item.isSelected()) + { + g.drawLine(3 + x, 5 + y, 3 + x, 9 + y); + g.drawLine(4 + x, 5 + y, 4 + x, 9 + y); + g.drawLine(5 + x, 7 + y, 9 + x, 3 + y); + g.drawLine(5 + x, 8 + y, 9 + x, 4 + y); + } + + g.setColor(save); + } + } + + /** + * The icon used for RadioButtons in the GNULookAndFeel. This is an empty + * icon with a size of 13x13 pixels. + */ + static class RadioButtonIcon + implements Icon + { + /** + * Returns the height of the icon. The GNULookAndFeel RadioButton icon + * has a height of 13 pixels. + * + * @return the height of the icon + */ + public int getIconHeight() + { + return 13; + } + + /** + * Returns the width of the icon. The GNULookAndFeel RadioButton icon + * has a width of 13 pixels. + * + * @return the height of the icon + */ + public int getIconWidth() + { + return 13; + } + + /** + * Paints the icon. The GNULookAndFeel RadioButton icon is empty and does + * not need to be painted. + * + * @param c the component to be painted + * @param g the Graphics context to be painted with + * @param x the x position of the icon + * @param y the y position of the icon + */ + public void paintIcon(Component c, Graphics g, int x, int y) + { + Color savedColor = g.getColor(); + JRadioButton b = (JRadioButton) c; + + // draw outer circle + if (b.isEnabled()) + g.setColor(Color.GRAY); + else + g.setColor(Color.GRAY); + g.drawLine(x + 2, y + 1, x + 3, y + 1); + g.drawLine(x + 4, y, x + 7, y); + g.drawLine(x + 8, y + 1, x + 9, y + 1); + g.drawLine(x + 10, y + 2, x + 10, y + 3); + g.drawLine(x + 11, y + 4, x + 11, y + 7); + g.drawLine(x + 10, y + 8, x + 10, y + 9); + g.drawLine(x + 8, y + 10, x + 9, y + 10); + g.drawLine(x + 4, y + 11, x + 7, y + 11); + g.drawLine(x + 2, y + 10, x + 3, y + 10); + g.drawLine(x + 1, y + 9, x + 1, y + 8); + g.drawLine(x, y + 7, x, y + 4); + g.drawLine(x + 1, y + 2, x + 1, y + 3); + + if (b.getModel().isArmed()) + { + g.setColor(Color.GRAY); + g.drawLine(x + 4, y + 1, x + 7, y + 1); + g.drawLine(x + 4, y + 10, x + 7, y + 10); + g.drawLine(x + 1, y + 4, x + 1, y + 7); + g.drawLine(x + 10, y + 4, x + 10, y + 7); + g.fillRect(x + 2, y + 2, 8, 8); + } + else + { + // only draw inner highlight if not filled + if (b.isEnabled()) + { + g.setColor(Color.WHITE); + + g.drawLine(x + 2, y + 8, x + 2, y + 9); + g.drawLine(x + 1, y + 4, x + 1, y + 7); + g.drawLine(x + 2, y + 2, x + 2, y + 3); + g.drawLine(x + 3, y + 2, x + 3, y + 2); + g.drawLine(x + 4, y + 1, x + 7, y + 1); + g.drawLine(x + 8, y + 2, x + 9, y + 2); + } + } + + // draw outer highlight + if (b.isEnabled()) + { + g.setColor(Color.WHITE); + + // outer + g.drawLine(x + 10, y + 1, x + 10, y + 1); + g.drawLine(x + 11, y + 2, x + 11, y + 3); + g.drawLine(x + 12, y + 4, x + 12, y + 7); + g.drawLine(x + 11, y + 8, x + 11, y + 9); + g.drawLine(x + 10, y + 10, x + 10, y + 10); + g.drawLine(x + 8, y + 11, x + 9, y + 11); + g.drawLine(x + 4, y + 12, x + 7, y + 12); + g.drawLine(x + 2, y + 11, x + 3, y + 11); + } + + if (b.isSelected()) + { + if (b.isEnabled()) + g.setColor(Color.BLACK); + else + g.setColor(Color.GRAY); + g.drawLine(x + 4, y + 3, x + 7, y + 3); + g.fillRect(x + 3, y + 4, 6, 4); + g.drawLine(x + 4, y + 8, x + 7, y + 8); + } + g.setColor(savedColor); + } + } +}
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil