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: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035:
036: import javax.swing.*;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039: import javax.swing.plaf.FontUIResource;
040:
041: import org.jvnet.substance.SubstanceLookAndFeel;
042: import org.jvnet.substance.fonts.FontPolicy;
043: import org.jvnet.substance.fonts.FontSet;
044: import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
045:
046: /**
047: * Test application that shows the use of the
048: * {@link SubstanceLookAndFeel#getFontPolicy()} API.
049: *
050: * @author Kirill Grouchnikov
051: * @see SubstanceLookAndFeel#getFontPolicy()
052: */
053: public class GetFontPolicy extends JFrame {
054: /**
055: * Wrapper around the base Substance font set. Is used to create larger /
056: * smaller font sets.
057: *
058: * @author Kirill Grouchnikov
059: */
060: private static class WrapperFontSet implements FontSet {
061: /**
062: * Extra size in pixels. Can be positive or negative.
063: */
064: private int extra;
065:
066: /**
067: * The base Substance font set.
068: */
069: private FontSet delegate;
070:
071: /**
072: * Creates a wrapper font set.
073: *
074: * @param delegate
075: * The base Substance font set.
076: * @param extra
077: * Extra size in pixels. Can be positive or negative.
078: */
079: public WrapperFontSet(FontSet delegate, int extra) {
080: super ();
081: this .delegate = delegate;
082: this .extra = extra;
083: }
084:
085: /**
086: * Returns the wrapped font.
087: *
088: * @param systemFont
089: * Original font.
090: * @return Wrapped font.
091: */
092: private FontUIResource getWrappedFont(FontUIResource systemFont) {
093: return new FontUIResource(systemFont.getFontName(),
094: systemFont.getStyle(), systemFont.getSize()
095: + this .extra);
096: }
097:
098: public FontUIResource getControlFont() {
099: return this .getWrappedFont(this .delegate.getControlFont());
100: }
101:
102: public FontUIResource getMenuFont() {
103: return this .getWrappedFont(this .delegate.getMenuFont());
104: }
105:
106: public FontUIResource getMessageFont() {
107: return this .getWrappedFont(this .delegate.getMessageFont());
108: }
109:
110: public FontUIResource getSmallFont() {
111: return this .getWrappedFont(this .delegate.getSmallFont());
112: }
113:
114: public FontUIResource getTitleFont() {
115: return this .getWrappedFont(this .delegate.getTitleFont());
116: }
117:
118: public FontUIResource getWindowTitleFont() {
119: return this .getWrappedFont(this .delegate
120: .getWindowTitleFont());
121: }
122: }
123:
124: /**
125: * Creates the main frame for <code>this</code> sample.
126: */
127: public GetFontPolicy() {
128: super ("Get font policy");
129:
130: this .setLayout(new BorderLayout());
131:
132: JPanel panel = new JPanel(new FlowLayout());
133:
134: // create a slider to control the font size.
135: final JSlider fontSizeSlider = new JSlider(-3, 3, 0);
136: fontSizeSlider.setPaintLabels(true);
137: fontSizeSlider.setMajorTickSpacing(1);
138: fontSizeSlider
139: .setToolTipText("Controls the global font set size");
140: fontSizeSlider.addChangeListener(new ChangeListener() {
141: public void stateChanged(ChangeEvent e) {
142: // if the value is adjusting - ignore. This is done
143: // to make CPU usage better.
144: if (!fontSizeSlider.getModel().getValueIsAdjusting()) {
145: final int newValue = fontSizeSlider.getValue();
146: SwingUtilities.invokeLater(new Runnable() {
147: public void run() {
148: // reset the base font policy to null - this
149: // restores the original font policy (default size).
150: SubstanceLookAndFeel.setFontPolicy(null);
151: // Get the default font set
152: final FontSet substanceCoreFontSet = SubstanceLookAndFeel
153: .getFontPolicy().getFontSet(
154: "Substance", null);
155: // Create the wrapper font set
156: FontPolicy newFontPolicy = new FontPolicy() {
157: public FontSet getFontSet(
158: String lafName, UIDefaults table) {
159: return new WrapperFontSet(
160: substanceCoreFontSet,
161: newValue);
162: }
163: };
164:
165: try {
166: GetFontPolicy.this
167: .setCursor(Cursor
168: .getPredefinedCursor(Cursor.WAIT_CURSOR));
169: // set the new font policy
170: SubstanceLookAndFeel
171: .setFontPolicy(newFontPolicy);
172: // reset the LAF to have the changes
173: UIManager
174: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
175: SwingUtilities
176: .updateComponentTreeUI(GetFontPolicy.this );
177: GetFontPolicy.this .setCursor(Cursor
178: .getDefaultCursor());
179: } catch (Exception exc) {
180: exc.printStackTrace();
181: }
182: }
183: });
184: }
185: }
186: });
187: panel.add(fontSizeSlider);
188:
189: JButton jb = new JButton("Show font info");
190: jb.addActionListener(new ActionListener() {
191: public void actionPerformed(ActionEvent e) {
192: FontPolicy fontPolicy = SubstanceLookAndFeel
193: .getFontPolicy();
194: FontSet fontSet = fontPolicy.getFontSet("Substance",
195: null);
196: String[] infoArray = new String[] {
197: "Control: " + fontSet.getControlFont(),
198: "Menu: " + fontSet.getMenuFont(),
199: "Message: " + fontSet.getMessageFont(),
200: "Small: " + fontSet.getSmallFont(),
201: "Title: " + fontSet.getTitleFont(),
202: "Window title: " + fontSet.getWindowTitleFont() };
203: JList infoList = new JList(infoArray);
204: JDialog infoDialog = new JDialog(GetFontPolicy.this ,
205: "Font set info", true);
206: infoDialog.setLayout(new BorderLayout());
207: infoDialog.add(infoList, BorderLayout.CENTER);
208: infoDialog.pack();
209: infoDialog.setLocationRelativeTo(GetFontPolicy.this );
210: infoDialog.setVisible(true);
211: }
212: });
213: panel.add(jb);
214:
215: this .add(panel, BorderLayout.CENTER);
216:
217: this .setSize(400, 200);
218: this .setLocationRelativeTo(null);
219: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
220: }
221:
222: /**
223: * The main method for <code>this</code> sample. The arguments are
224: * ignored.
225: *
226: * @param args
227: * Ignored.
228: * @throws Exception
229: * If some exception occured. Note that there is no special
230: * treatment of exception conditions in <code>this</code>
231: * sample code.
232: */
233: public static void main(String[] args) throws Exception {
234: UIManager
235: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
236: JFrame.setDefaultLookAndFeelDecorated(true);
237: JDialog.setDefaultLookAndFeelDecorated(true);
238: SwingUtilities.invokeLater(new Runnable() {
239: public void run() {
240: new GetFontPolicy().setVisible(true);
241: }
242: });
243: }
244: }
|