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.check;
031:
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034:
035: import javax.swing.*;
036:
037: /**
038: * @author kirillg
039: * @author Daniel Stonier
040: * @author Keith Woodward
041: */
042: public class SubstanceLafChanger implements ActionListener {
043: private JFrame frame;
044:
045: private String lafClassName;
046:
047: private boolean wasOriginallyDecoratedByOS;
048:
049: public static JMenuItem getMenuItem(JFrame frame, String lafName,
050: String lafClassName) {
051: JMenuItem result = new JMenuItem(lafName);
052: result.addActionListener(new SubstanceLafChanger(frame,
053: lafClassName));
054: return result;
055: }
056:
057: public SubstanceLafChanger(JFrame frame, String lafClassName) {
058: super ();
059: this .frame = frame;
060: this .lafClassName = lafClassName;
061: this .wasOriginallyDecoratedByOS = !frame.isUndecorated();
062: }
063:
064: public void actionPerformed(ActionEvent e) {
065: SwingUtilities.invokeLater(new Runnable() {
066: public void run() {
067: boolean wasDecoratedByOS = !frame.isUndecorated();
068:
069: try {
070: UIManager.setLookAndFeel(lafClassName);
071: SwingUtilities.invokeLater(new Runnable() {
072: public void run() {
073: SwingUtilities.updateComponentTreeUI(frame);
074: }
075: });
076: } catch (ClassNotFoundException cnfe) {
077: out("LAF main class '" + lafClassName
078: + "' not found");
079: } catch (Exception exc) {
080: exc.printStackTrace();
081: }
082:
083: // if (!wasOriginallyDecoratedByOS)
084: // return;
085:
086: boolean canBeDecoratedByLAF = UIManager
087: .getLookAndFeel()
088: .getSupportsWindowDecorations();
089: if (canBeDecoratedByLAF == wasDecoratedByOS) {
090: boolean wasVisible = frame.isVisible();
091:
092: frame.setVisible(false);
093: frame.dispose();
094: if (!canBeDecoratedByLAF
095: || wasOriginallyDecoratedByOS) {
096: // see the java docs under the method
097: // JFrame.setDefaultLookAndFeelDecorated(boolean
098: // value) for description of these 2 lines:
099: frame.setUndecorated(false);
100: frame.getRootPane().setWindowDecorationStyle(
101: JRootPane.NONE);
102:
103: } else {
104: frame.setUndecorated(true);
105: frame.getRootPane().setWindowDecorationStyle(
106: JRootPane.FRAME);
107: }
108: frame.setVisible(wasVisible);
109: wasDecoratedByOS = !frame.isUndecorated();
110: }
111: }
112: });
113: }
114:
115: public static void out(Object obj) {
116: try {
117: System.out.println(obj);
118: } catch (Exception exc) {
119: // ignore - is thrown on Mac in WebStart (security access)
120: }
121: }
122: }
|