001: /*
002: * Copyright (c) 2005-2008 Flamingo / 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 Flamingo 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 org.jvnet.substance.flamingo.ribbon.gallery.oob;
031:
032: import java.awt.Frame;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.util.*;
036:
037: import javax.swing.SwingUtilities;
038:
039: import org.jvnet.flamingo.common.*;
040: import org.jvnet.flamingo.common.icon.ResizableIcon;
041: import org.jvnet.flamingo.ribbon.JRibbonBand;
042: import org.jvnet.flamingo.ribbon.RibbonElementPriority;
043: import org.jvnet.substance.SubstanceLookAndFeel;
044: import org.jvnet.substance.theme.SubstanceTheme;
045: import org.jvnet.substance.theme.ThemeInfo;
046:
047: /**
048: * In-ribbon gallery that contains all available <b>Substance</b> themes.
049: *
050: * @author Kirill Grouchnikov
051: */
052: public class SubstanceThemeRibbonGallery {
053: /**
054: * Adds an in-ribbon gallery that contains all available <b>Substance</b>
055: * themes to the specified ribbon band.
056: *
057: * @param ribbonBand
058: * Ribbon band that will contain the newly added theme gallery.
059: */
060: public static void addThemeGallery(JRibbonBand ribbonBand) {
061: Map<ElementState, Integer> prefWidths = new HashMap<ElementState, Integer>();
062: prefWidths.put(ElementState.SMALL, 1);
063: prefWidths.put(ElementState.MEDIUM, 3);
064: prefWidths.put(ElementState.BIG, 5);
065:
066: List<StringValuePair<List<JCommandToggleButton>>> themeGroups = new ArrayList<StringValuePair<List<JCommandToggleButton>>>();
067: List<JCommandToggleButton> coldButtons = new ArrayList<JCommandToggleButton>();
068: List<JCommandToggleButton> brightButtons = new ArrayList<JCommandToggleButton>();
069: List<JCommandToggleButton> darkButtons = new ArrayList<JCommandToggleButton>();
070:
071: Map<String, ThemeInfo> themes = SubstanceLookAndFeel
072: .getAllThemes();
073: for (Map.Entry<String, ThemeInfo> entry : themes.entrySet()) {
074: final String themeClassName = entry.getValue()
075: .getClassName();
076: try {
077: Class<?> themeClass = Class.forName(themeClassName);
078: final SubstanceTheme themeInstance = (SubstanceTheme) themeClass
079: .newInstance();
080:
081: ResizableIcon icon = new ThemeResizableIcon(
082: themeInstance, 60, 40);
083: final JCommandToggleButton themeButton = new JCommandToggleButton(
084: entry.getKey(), icon);
085:
086: switch (entry.getValue().getThemeKind()) {
087: case BRIGHT:
088: brightButtons.add(themeButton);
089: break;
090: case COLD:
091: coldButtons.add(themeButton);
092: break;
093: case DARK:
094: darkButtons.add(themeButton);
095: break;
096: }
097:
098: themeButton.addActionListener(new ActionListener() {
099: @Override
100: public void actionPerformed(ActionEvent e) {
101: SwingUtilities.invokeLater(new Runnable() {
102: public void run() {
103: SubstanceLookAndFeel
104: .setCurrentTheme(themeInstance);
105: for (Frame frame : Frame.getFrames())
106: SwingUtilities
107: .updateComponentTreeUI(frame);
108: }
109: });
110: }
111: });
112: } catch (Exception e) {
113: continue;
114: }
115: }
116:
117: themeGroups
118: .add(new StringValuePair<List<JCommandToggleButton>>(
119: "Bright themes", brightButtons));
120: themeGroups
121: .add(new StringValuePair<List<JCommandToggleButton>>(
122: "Cold themes", coldButtons));
123: themeGroups
124: .add(new StringValuePair<List<JCommandToggleButton>>(
125: "Dark themes", darkButtons));
126:
127: ribbonBand.addRibbonGallery(themeGroups, prefWidths, 5, 3,
128: RibbonElementPriority.TOP);
129: }
130: }
|