001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.TreeMap;
024:
025: import javax.swing.JComboBox;
026: import javax.swing.UIManager;
027: import javax.swing.UIManager.LookAndFeelInfo;
028:
029: /**
030: * This <TT>JComboBox</TT> will display all the Look and Feels
031: * that have been registered with the <TT>UIManager</TT>.
032: *
033: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
034: */
035: public class LookAndFeelComboBox extends JComboBox {
036: private static final long serialVersionUID = 1L;
037:
038: /**
039: * <TT>LookAndFeelInfo</TT> objects keyed by the
040: * Look and Feel name.
041: */
042: private Map<String, LookAndFeelInfo> _lafsByName = new TreeMap<String, LookAndFeelInfo>();
043:
044: /**
045: * <TT>LookAndFeelInfo</TT> objects keyed by the
046: * Class name of the Look and Feel.
047: */
048: private Map<String, LookAndFeelInfo> _lafsByClassName = new TreeMap<String, LookAndFeelInfo>();
049:
050: /**
051: * Default ctor. Select the currently active L & F after
052: * building the combo box.
053: */
054: public LookAndFeelComboBox() {
055: this (null);
056: }
057:
058: /**
059: * Ctor specifying the L & F to select after building the combo box.
060: *
061: * @param Name of the L & F to be selected.
062: */
063: public LookAndFeelComboBox(String selectedLafName) {
064: super ();
065: generateLookAndFeelInfo();
066: if (selectedLafName == null) {
067: selectedLafName = UIManager.getLookAndFeel().getName();
068: }
069: setSelectedLookAndFeelName(selectedLafName);
070: }
071:
072: public LookAndFeelInfo getSelectedLookAndFeel() {
073: return _lafsByName.get(getSelectedItem());
074: }
075:
076: public void setSelectedLookAndFeelName(String selectedLafName) {
077: if (selectedLafName != null) {
078: getModel().setSelectedItem(selectedLafName);
079: }
080: }
081:
082: public void setSelectedLookAndFeelClassName(
083: String selectedLafClassName) {
084: if (selectedLafClassName != null) {
085: LookAndFeelInfo info = _lafsByClassName
086: .get(selectedLafClassName);
087: if (info != null) {
088: setSelectedLookAndFeelName(info.getName());
089: }
090: }
091: }
092:
093: /**
094: * Fill combo with the names of all the Look and Feels in
095: * alpabetical sequence.
096: */
097: private void generateLookAndFeelInfo() {
098: // Put all available "Look and Feel" objects into collections
099: // keyed by LAF name and by the class name.
100: LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
101: _lafsByName = new TreeMap<String, LookAndFeelInfo>();
102: for (int i = 0; i < info.length; ++i) {
103: _lafsByName.put(info[i].getName(), info[i]);
104: _lafsByClassName.put(info[i].getClassName(), info[i]);
105: }
106:
107: // Add the names of all LAF objects to control. By doing thru the Map
108: // these will be sorted.
109: for (Iterator<LookAndFeelInfo> it = _lafsByName.values()
110: .iterator(); it.hasNext();) {
111: addItem(it.next().getName());
112: }
113: }
114: }
|