001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.samples.substance.api;
031:
032: import java.awt.*;
033:
034: import javax.swing.*;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037: import javax.swing.plaf.FontUIResource;
038:
039: import org.jvnet.substance.SubstanceLookAndFeel;
040: import org.jvnet.substance.fonts.FontPolicy;
041: import org.jvnet.substance.fonts.FontSet;
042: import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
043:
044: /**
045: * Test application that shows the use of the
046: * {@link SubstanceLookAndFeel#setFontPolicy(FontPolicy)} API.
047: *
048: * @author Kirill Grouchnikov
049: * @see SubstanceLookAndFeel#setFontPolicy(FontPolicy)
050: */
051: public class SetFontPolicy extends JFrame {
052: /**
053: * Wrapper around the base Substance font set. Is used to create larger /
054: * smaller font sets.
055: *
056: * @author Kirill Grouchnikov
057: */
058: private static class WrapperFontSet implements FontSet {
059: /**
060: * Extra size in pixels. Can be positive or negative.
061: */
062: private int extra;
063:
064: /**
065: * The base Substance font set.
066: */
067: private FontSet delegate;
068:
069: /**
070: * Creates a wrapper font set.
071: *
072: * @param delegate
073: * The base Substance font set.
074: * @param extra
075: * Extra size in pixels. Can be positive or negative.
076: */
077: public WrapperFontSet(FontSet delegate, int extra) {
078: super ();
079: this .delegate = delegate;
080: this .extra = extra;
081: }
082:
083: /**
084: * Returns the wrapped font.
085: *
086: * @param systemFont
087: * Original font.
088: * @return Wrapped font.
089: */
090: private FontUIResource getWrappedFont(FontUIResource systemFont) {
091: return new FontUIResource(systemFont.getFontName(),
092: systemFont.getStyle(), systemFont.getSize()
093: + this .extra);
094: }
095:
096: public FontUIResource getControlFont() {
097: return this .getWrappedFont(this .delegate.getControlFont());
098: }
099:
100: public FontUIResource getMenuFont() {
101: return this .getWrappedFont(this .delegate.getMenuFont());
102: }
103:
104: public FontUIResource getMessageFont() {
105: return this .getWrappedFont(this .delegate.getMessageFont());
106: }
107:
108: public FontUIResource getSmallFont() {
109: return this .getWrappedFont(this .delegate.getSmallFont());
110: }
111:
112: public FontUIResource getTitleFont() {
113: return this .getWrappedFont(this .delegate.getTitleFont());
114: }
115:
116: public FontUIResource getWindowTitleFont() {
117: return this .getWrappedFont(this .delegate
118: .getWindowTitleFont());
119: }
120: }
121:
122: /**
123: * Creates the main frame for <code>this</code> sample.
124: */
125: public SetFontPolicy() {
126: super ("Set font policy");
127:
128: this .setLayout(new BorderLayout());
129:
130: JPanel panel = new JPanel(new FlowLayout());
131:
132: // create a slider to control the font size.
133: final JSlider fontSizeSlider = new JSlider(-3, 3, 0);
134: fontSizeSlider.setPaintLabels(true);
135: fontSizeSlider.setMajorTickSpacing(1);
136: fontSizeSlider
137: .setToolTipText("Controls the global font set size");
138: fontSizeSlider.addChangeListener(new ChangeListener() {
139: public void stateChanged(ChangeEvent e) {
140: // if the value is adjusting - ignore. This is done
141: // to make CPU usage better.
142: if (!fontSizeSlider.getModel().getValueIsAdjusting()) {
143: final int newValue = fontSizeSlider.getValue();
144: SwingUtilities.invokeLater(new Runnable() {
145: public void run() {
146: // reset the base font policy to null - this
147: // restores the original font policy (default size).
148: SubstanceLookAndFeel.setFontPolicy(null);
149: // Get the default font set
150: final FontSet substanceCoreFontSet = SubstanceLookAndFeel
151: .getFontPolicy().getFontSet(
152: "Substance", null);
153: // Create the wrapper font set
154: FontPolicy newFontPolicy = new FontPolicy() {
155: public FontSet getFontSet(
156: String lafName, UIDefaults table) {
157: return new WrapperFontSet(
158: substanceCoreFontSet,
159: newValue);
160: }
161: };
162:
163: try {
164: SetFontPolicy.this
165: .setCursor(Cursor
166: .getPredefinedCursor(Cursor.WAIT_CURSOR));
167: // set the new font policy
168: SubstanceLookAndFeel
169: .setFontPolicy(newFontPolicy);
170: // reset the LAF to have the changes
171: UIManager
172: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
173: SwingUtilities
174: .updateComponentTreeUI(SetFontPolicy.this );
175: SetFontPolicy.this .setCursor(Cursor
176: .getDefaultCursor());
177: } catch (Exception exc) {
178: exc.printStackTrace();
179: }
180: }
181: });
182: }
183: }
184: });
185: panel.add(fontSizeSlider);
186:
187: panel.add(new JButton("button"));
188: panel.add(new JComboBox(new Object[] { "item1", "item2" }));
189: //JSlider slider = new JSlider();
190: //slider.setEnabled(false);
191: //panel.add(slider);
192:
193: this .add(panel, BorderLayout.CENTER);
194:
195: this .setSize(400, 200);
196: this .setLocationRelativeTo(null);
197: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
198: }
199:
200: /**
201: * The main method for <code>this</code> sample. The arguments are
202: * ignored.
203: *
204: * @param args
205: * Ignored.
206: * @throws Exception
207: * If some exception occured. Note that there is no special
208: * treatment of exception conditions in <code>this</code>
209: * sample code.
210: */
211: public static void main(String[] args) throws Exception {
212: UIManager
213: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
214: JFrame.setDefaultLookAndFeelDecorated(true);
215: JDialog.setDefaultLookAndFeelDecorated(true);
216: SwingUtilities.invokeLater(new Runnable() {
217: public void run() {
218: new SetFontPolicy().setVisible(true);
219: }
220: });
221: }
222: }
|