001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: DynamicUIManager.java,v 1.11 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import javax.swing.*;
026: import java.awt.*;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.util.ArrayList;
030:
031: public class DynamicUIManager {
032: private static final DynamicUIManager instance = new DynamicUIManager();
033:
034: private ArrayList listeners = new ArrayList(2);
035: private ArrayList prioritizedListeners = new ArrayList(2);
036:
037: private String[] properties = { "win.3d.backgroundColor",
038: "win.3d.highlightColor", "win.3d.lightColor",
039: "win.3d.shadowColor", "win.frame.activeCaptionColor",
040: "win.frame.activeCaptionGradientColor",
041: "win.frame.captionTextColor",
042: "win.frame.activeBorderColor", "win.mdi.backgroundColor",
043: "win.desktop.backgroundColor",
044: "win.frame.inactiveCaptionColor",
045: "win.frame.inactiveCaptionGradientColor",
046: "win.frame.inactiveCaptionTextColor",
047: "win.frame.inactiveBorderColor",
048: "win.menu.backgroundColor", "win.menu.textColor",
049: "win.frame.textColor?????", "win.item.highlightColor",
050: "win.item.highlightTextColor",
051: "win.tooltip.backgroundColor", "win.tooltip.textColor",
052: "win.frame.backgroundColor", "win.frame.textColor",
053: "win.item.hotTrackedColor" };
054: private Toolkit currentToolkit;
055: private boolean propertyChangePending;
056:
057: public static DynamicUIManager getInstance() {
058: return instance;
059: }
060:
061: private DynamicUIManager() {
062: final PropertyChangeListener l = new PropertyChangeListener() {
063: public void propertyChange(PropertyChangeEvent event) {
064: handlePropertyChanges();
065: }
066: };
067:
068: UIManager
069: .addPropertyChangeListener(new PropertyChangeListener() {
070: public void propertyChange(PropertyChangeEvent event) {
071: if (event.getPropertyName().equals(
072: "lookAndFeel")) {
073: setupPropertyListener(l);
074: fireLookAndFeelChanging();
075: fireLookAndFeelChanged();
076: }
077: }
078: });
079: UIManager.getDefaults().addPropertyChangeListener(
080: new PropertyChangeListener() {
081: public void propertyChange(PropertyChangeEvent event) {
082: if (!(event.getNewValue() instanceof Class))
083: handlePropertyChanges();
084: }
085: });
086:
087: setupPropertyListener(l);
088: }
089:
090: private void setupPropertyListener(PropertyChangeListener l) {
091: if (currentToolkit != null)
092: for (int i = 0; i < properties.length; i++)
093: currentToolkit.removePropertyChangeListener(
094: properties[i], l);
095:
096: currentToolkit = Toolkit.getDefaultToolkit();
097: for (int i = 0; i < properties.length; i++) {
098: currentToolkit.addPropertyChangeListener(properties[i], l);
099: }
100: }
101:
102: public void addListener(DynamicUIManagerListener l) {
103: listeners.add(l);
104: }
105:
106: public void removeListener(DynamicUIManagerListener l) {
107: listeners.remove(l);
108: }
109:
110: public void addPrioritizedListener(DynamicUIManagerListener l) {
111: prioritizedListeners.add(l);
112: }
113:
114: public void removePrioritizedListener(DynamicUIManagerListener l) {
115: prioritizedListeners.remove(l);
116: }
117:
118: private void fireLookAndFeelChanging() {
119: Object l[] = prioritizedListeners.toArray();
120: Object l2[] = listeners.toArray();
121:
122: for (int i = 0; i < l.length; i++)
123: ((DynamicUIManagerListener) l[i]).lookAndFeelChanging();
124:
125: for (int i = 0; i < l2.length; i++)
126: ((DynamicUIManagerListener) l2[i]).lookAndFeelChanging();
127: }
128:
129: private void fireLookAndFeelChanged() {
130: Object l[] = prioritizedListeners.toArray();
131: Object l2[] = listeners.toArray();
132:
133: for (int i = 0; i < l.length; i++)
134: ((DynamicUIManagerListener) l[i]).lookAndFeelChanged();
135:
136: for (int i = 0; i < l2.length; i++)
137: ((DynamicUIManagerListener) l2[i]).lookAndFeelChanged();
138: }
139:
140: private void handlePropertyChanges() {
141: if (!propertyChangePending) {
142: propertyChangePending = true;
143:
144: SwingUtilities.invokeLater(new Runnable() {
145: public void run() {
146: propertyChangePending = false;
147:
148: firePropertyChanged();
149: }
150: });
151:
152: firePropertyChanging();
153: }
154: }
155:
156: private void firePropertyChanging() {
157: Object l[] = prioritizedListeners.toArray();
158: Object l2[] = listeners.toArray();
159:
160: for (int i = 0; i < l.length; i++)
161: ((DynamicUIManagerListener) l[i]).propertiesChanging();
162:
163: for (int i = 0; i < l2.length; i++)
164: ((DynamicUIManagerListener) l2[i]).propertiesChanging();
165: }
166:
167: private void firePropertyChanged() {
168: Object l[] = prioritizedListeners.toArray();
169: Object l2[] = listeners.toArray();
170:
171: for (int i = 0; i < l.length; i++)
172: ((DynamicUIManagerListener) l[i]).propertiesChanged();
173:
174: for (int i = 0; i < l2.length; i++)
175: ((DynamicUIManagerListener) l2[i]).propertiesChanged();
176: }
177: }
|