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.BorderLayout;
033: import java.awt.FlowLayout;
034: import java.awt.event.*;
035: import java.util.Vector;
036:
037: import javax.swing.*;
038:
039: import org.jvnet.substance.SubstanceLookAndFeel;
040: import org.jvnet.substance.skin.*;
041:
042: /**
043: * Test application that shows the use of the
044: * {@link SubstanceLookAndFeel#registerSkinChangeListener(SkinChangeListener)}
045: * API.
046: *
047: * @author Kirill Grouchnikov
048: * @see SubstanceLookAndFeel#registerSkinChangeListener(SkinChangeListener)
049: */
050: public class UnregisterSkinChangeListener extends JFrame {
051: /**
052: * Listener instance.
053: */
054: private SkinChangeListener listener;
055:
056: /**
057: * Creates the main frame for <code>this</code> sample.
058: */
059: public UnregisterSkinChangeListener() {
060: super ("Register skin change listener");
061:
062: this .setLayout(new BorderLayout());
063:
064: JPanel panel = new JPanel(new FlowLayout());
065:
066: // Get all skin display names and set the vector as a model
067: // for combobox.
068: final JComboBox cb = new JComboBox(new Vector<String>(
069: SubstanceLookAndFeel.getAllSkins().keySet()));
070: cb.setSelectedIndex(-1);
071:
072: cb.addItemListener(new ItemListener() {
073: public void itemStateChanged(ItemEvent evt) {
074: // Get the affected item
075: final Object item = evt.getItem();
076:
077: if (evt.getStateChange() == ItemEvent.SELECTED) {
078: SwingUtilities.invokeLater(new Runnable() {
079: public void run() {
080: try {
081: // Get the skin info object based on
082: // the selected skin display name
083: SkinInfo skinInfo = SubstanceLookAndFeel
084: .getAllSkins().get(
085: (String) item);
086: // Set the global skin based on the
087: // skin class name.
088: SubstanceLookAndFeel.setSkin(skinInfo
089: .getClassName());
090: SwingUtilities
091: .updateComponentTreeUI(UnregisterSkinChangeListener.this );
092: } catch (Exception exc) {
093: }
094: };
095: });
096: }
097: }
098: });
099:
100: panel.add(new JLabel("All skins:"));
101: panel.add(cb);
102:
103: JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
104: final JButton unregisterListener = new JButton(
105: "Unregister listener");
106: unregisterListener.addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent e) {
108: SwingUtilities.invokeLater(new Runnable() {
109: public void run() {
110: unregisterListener.setEnabled(false);
111: // unregister listener
112: SubstanceLookAndFeel
113: .unregisterSkinChangeListener(listener);
114: }
115: });
116: }
117: });
118: controls.add(unregisterListener);
119: this .add(controls, BorderLayout.SOUTH);
120:
121: this .add(panel, BorderLayout.CENTER);
122:
123: // register listener
124: SubstanceLookAndFeel
125: .registerSkinChangeListener(listener = new SkinChangeListener() {
126: public void skinChanged() {
127: // show dialog with skin changed message.
128: SwingUtilities.invokeLater(new Runnable() {
129: public void run() {
130: JOptionPane
131: .showMessageDialog(
132: UnregisterSkinChangeListener.this ,
133: "Skin changed");
134: }
135: });
136: }
137: });
138:
139: this .setSize(400, 200);
140: this .setLocationRelativeTo(null);
141: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
142: }
143:
144: /**
145: * The main method for <code>this</code> sample. The arguments are
146: * ignored.
147: *
148: * @param args
149: * Ignored.
150: * @throws Exception
151: * If some exception occured. Note that there is no special
152: * treatment of exception conditions in <code>this</code>
153: * sample code.
154: */
155: public static void main(String[] args) throws Exception {
156: UIManager
157: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
158: JFrame.setDefaultLookAndFeelDecorated(true);
159: JDialog.setDefaultLookAndFeelDecorated(true);
160: SwingUtilities.invokeLater(new Runnable() {
161: public void run() {
162: new UnregisterSkinChangeListener().setVisible(true);
163: }
164: });
165: }
166: }
|