001: /*
002: * LnFManager.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.lnf;
013:
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.Comparator;
017: import java.util.List;
018: import javax.swing.LookAndFeel;
019: import javax.swing.UIManager;
020: import javax.swing.UIManager.LookAndFeelInfo;
021: import workbench.resource.Settings;
022: import workbench.util.StringUtil;
023:
024: /**
025: * Retrieve and store LnF definitions in the the Settings object
026: * @author support@sql-workbench.net
027: */
028: public class LnFManager {
029: private List<LnFDefinition> lnfList = new ArrayList<LnFDefinition>();
030:
031: public LnFManager() {
032: Settings set = Settings.getInstance();
033:
034: int count = set.getIntProperty("workbench.lnf.count", 0);
035: for (int i = 0; i < count; i++) {
036: String clz = set.getProperty("workbench.lnf." + i
037: + ".class", null);
038: String name = set.getProperty("workbench.lnf." + i
039: + ".name", clz);
040: String libs = set.getProperty("workbench.lnf." + i
041: + ".classpath", null);
042: libs = StringUtil.replace(libs,
043: LnFDefinition.LNF_PATH_SEPARATOR, StringUtil
044: .getPathSeparator());
045: if (clz != null && libs != null) {
046: LnFDefinition lnf = new LnFDefinition(name, clz, libs);
047: lnfList.add(lnf);
048: }
049: }
050:
051: // The Liquid Look & Feel "installs" itself as a System L&F and if
052: // activated is returned in getInstalledLookAndFeels(). To avoid
053: // a duplicate entry we check this before adding a "system" look and feel
054: LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
055:
056: for (int i = 0; i < info.length; i++) {
057: LnFDefinition lnf = new LnFDefinition(info[i].getName(),
058: info[i].getClassName());
059: if (!lnfList.contains(lnf)) {
060: lnfList.add(lnf);
061: }
062: }
063: Comparator<LnFDefinition> nameComp = new Comparator<LnFDefinition>() {
064: public int compare(LnFDefinition first, LnFDefinition second) {
065: return StringUtil.compareStrings(first.getName(),
066: second.getName(), true);
067: }
068: };
069: Collections.sort(lnfList, nameComp);
070: }
071:
072: public void removeDefinition(LnFDefinition lnf) {
073: if (lnf == null)
074: return;
075: if (!lnf.isBuiltInLnF()) {
076: this .lnfList.remove(lnf);
077: }
078: }
079:
080: public LnFDefinition getCurrentLnF() {
081: LookAndFeel lnf = UIManager.getLookAndFeel();
082: String lnfClass = lnf.getClass().getName();
083: return findLookAndFeel(lnfClass);
084: }
085:
086: public int addDefinition(LnFDefinition lnf) {
087: this .lnfList.add(lnf);
088: return lnfList.size() - 1;
089: }
090:
091: public void saveLookAndFeelDefinitions() {
092: Settings set = Settings.getInstance();
093: removeLnFEntries();
094: int count = this .lnfList.size();
095: int lnfCount = 0;
096: for (LnFDefinition lnf : lnfList) {
097: if (!lnf.isBuiltInLnF()) {
098: String libs = lnf.getLibrary();
099: libs = StringUtil.replace(libs, StringUtil
100: .getPathSeparator(),
101: LnFDefinition.LNF_PATH_SEPARATOR);
102: set.setProperty("workbench.lnf." + lnfCount
103: + ".classpath", libs);
104: set.setProperty("workbench.lnf." + lnfCount + ".class",
105: lnf.getClassName());
106: set.setProperty("workbench.lnf." + lnfCount + ".name",
107: lnf.getName());
108: lnfCount++;
109: }
110: }
111: set.setProperty("workbench.lnf.count", lnfCount);
112: }
113:
114: private void removeLnFEntries() {
115: Settings set = Settings.getInstance();
116: int count = set.getIntProperty("workbench.lnf.count", 0);
117: for (int i = 0; i < count; i++) {
118: set.removeProperty("workbench.lnf." + i + ".classpath");
119: set.removeProperty("workbench.lnf." + i + ".class");
120: set.removeProperty("workbench.lnf." + i + ".name");
121: }
122: }
123:
124: /**
125: * returns all LnFs defined in the system. This is
126: * a combined list of built-in LnFs and user-defined
127: * LnFs
128: *
129: * @return all available Look and Feels
130: * @see workbench.gui.lnf.LnFDefinition#isBuiltInLnF()
131: */
132: public List<LnFDefinition> getAvailableLookAndFeels() {
133: return Collections.unmodifiableList(lnfList);
134: }
135:
136: public LnFDefinition findLookAndFeel(String className) {
137: if (className == null)
138: return null;
139:
140: for (LnFDefinition lnf : lnfList) {
141: if (lnf.getClassName().equals(className)) {
142: return lnf;
143: }
144: }
145: return null;
146: }
147: }
|