From: Kimmo R. <ki...@us...> - 2012-12-01 07:58:47
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/client/gui In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv17588/src/games/stendhal/client/gui Modified Files: WindowUtils.java Log Message: Added a utility method for making a component tree listen default font size changes Index: WindowUtils.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/client/gui/WindowUtils.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WindowUtils.java 29 Aug 2011 18:28:22 -0000 1.1 --- WindowUtils.java 1 Dec 2012 07:58:43 -0000 1.2 *************** *** 1,4 **** /*************************************************************************** ! * (C) Copyright 2003-2011 - Stendhal * *************************************************************************** *************************************************************************** --- 1,4 ---- /*************************************************************************** ! * (C) Copyright 2003-2012 - Stendhal * *************************************************************************** *************************************************************************** *************** *** 12,18 **** --- 12,27 ---- package games.stendhal.client.gui; + import games.stendhal.client.gui.wt.core.SettingChangeAdapter; + import games.stendhal.client.gui.wt.core.SettingChangeListener; + import games.stendhal.client.gui.wt.core.WtWindowManager; + import games.stendhal.common.MathHelper; + + import java.awt.Component; + import java.awt.Container; + import java.awt.Font; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; + import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; *************** *** 30,35 **** */ public class WindowUtils { ! /** A key that should not get mixed with anything else */ ! private static final String WINDOW_CLOSE = "org.stendhalgame:window_closing"; // getRootPane() in JDialog and JFrame has no common ancestor --- 39,48 ---- */ public class WindowUtils { ! /** A key that should not get mixed with anything else. */ ! private static final String WINDOW_CLOSE = "org.stendhalgame:window_closing"; ! /** Font size property name. */ ! private static final String FONT_SIZE_PROPERTY = "ui.font_size"; ! /** Default font point size. */ ! private static final int DEFAULT_FONT_SIZE = 12; // getRootPane() in JDialog and JFrame has no common ancestor *************** *** 64,75 **** private static void closeOnEscape(final Window window, final JRootPane root) { InputMap map = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ! map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WINDOW_CLOSE); ! ! Action dispatchClosing = new AbstractAction() { ! public void actionPerformed(ActionEvent event) { ! window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); ! } ! }; ! root.getActionMap().put(WINDOW_CLOSE, dispatchClosing); } } --- 77,140 ---- private static void closeOnEscape(final Window window, final JRootPane root) { InputMap map = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ! map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WINDOW_CLOSE); ! Action dispatchClosing = new AbstractAction() { ! @Override ! public void actionPerformed(ActionEvent event) { ! window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); ! } ! }; ! root.getActionMap().put(WINDOW_CLOSE, dispatchClosing); ! } ! ! /** ! * Register a component that should watch for default font size changes. ! * Typically the component should be the top level window. ! * ! * @param component root component for the tree whose font size shall be ! * changed at default font size changes ! */ ! public static void watchFontSize(final Component component) { ! final SettingChangeListener listener = new SettingChangeAdapter(FONT_SIZE_PROPERTY, ! Integer.toString(DEFAULT_FONT_SIZE)) { ! @Override ! public void changed(String newValue) { ! int size = MathHelper.parseIntDefault(newValue, DEFAULT_FONT_SIZE); ! scaleComponentFonts(component, size); ! component.validate(); ! component.setSize(component.getPreferredSize()); ! } ! }; ! ! WtWindowManager.getInstance().registerSettingChangeListener(FONT_SIZE_PROPERTY, listener); ! ! /* ! * Dialogs typically get disposed when they are closed. Remove the ! * listener so that the the dialog and its subcomponents can be ! * reclaimed by the garbage collector. ! */ ! if (component instanceof Window) { ! ((Window) component).addWindowListener(new WindowAdapter() { ! @Override ! public void windowClosed(WindowEvent e) { ! WtWindowManager.getInstance().deregisterSettingChangeListener(FONT_SIZE_PROPERTY, listener); ! } ! }); ! } ! } ! ! /** ! * Scale the font of a component and its subcomponents. ! * ! * @param component root component ! * @param size new font size ! */ ! private static void scaleComponentFonts(Component component, float size) { ! Font f = component.getFont().deriveFont(size); ! component.setFont(f); ! if (component instanceof Container) { ! for (Component child : ((Container) component).getComponents()) { ! scaleComponentFonts(child, size); ! } ! } } } |