001: package net.sourceforge.squirrel_sql.client.gui.builders;
002:
003: /*
004: * Copyright (C) 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 javax.swing.JTabbedPane;
022: import javax.swing.event.EventListenerList;
023:
024: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
025: import net.sourceforge.squirrel_sql.client.IApplication;
026:
027: /**
028: * This singleton factory creates UI objects.
029: *
030: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
031: */
032: public class UIFactory {
033: /** Singleton instance. */
034: private static UIFactory s_instance;
035:
036: /** Application preferences. */
037: private SquirrelPreferences _prefs;
038:
039: /**
040: * Collection of listeners for events in this object.
041: */
042: private final EventListenerList _listenerList = new EventListenerList();
043: private IApplication _app;
044:
045: /**
046: * Retrieve the single instance of this class.
047: *
048: * @return the single instance of this class.
049: */
050: public static UIFactory getInstance() {
051: if (s_instance == null) {
052: throw new IllegalArgumentException(
053: "UIFactory has not been initialized");
054: }
055:
056: return s_instance;
057: }
058:
059: /**
060: * Initialize the single instance of this class.
061: *
062: * @param prefs Application preferences.
063: *
064: * @param app
065: * @throws IllegalArgumentException
066: * Thrown if <TT>null</TT> <TT>SquirrelPreferences</TT> passed.
067: *
068: * @throws IllegalStateException
069: * Thrown if initialization has already been done.
070: */
071: public synchronized static void initialize(
072: SquirrelPreferences prefs, IApplication app) {
073: if (s_instance != null) {
074: throw new IllegalStateException(
075: "UIFactory has alerady been initialized");
076: }
077: s_instance = new UIFactory(prefs, app);
078: }
079:
080: /**
081: * Default ctor. private as class is a singleton.
082: */
083: private UIFactory(SquirrelPreferences prefs, IApplication app) {
084: super ();
085: if (prefs == null) {
086: throw new IllegalArgumentException(
087: "SquirrelPreferences == null");
088: }
089: _prefs = prefs;
090: _app = app;
091: }
092:
093: /**
094: * Create a tabbed pane with a tab placement of <TT>JTabbedPane.TOP</TT>.
095: *
096: * @erturn The new tabbed pane.
097: */
098: public JTabbedPane createTabbedPane() {
099: return createTabbedPane(JTabbedPane.TOP);
100: }
101:
102: /**
103: * Create a tabbed pane specifying the tab placement.
104: *
105: * @param Tab Placement. See <TT>JTabbedPane</TT> javadoc for more
106: * information.
107: *
108: * @erturn The new tabbed pane.
109: */
110: public JTabbedPane createTabbedPane(int tabPlacement) {
111: final JTabbedPane pnl = new SquirrelTabbedPane(_prefs, _app);
112: pnl.setTabPlacement(tabPlacement);
113: fireTabbedPaneCreated(pnl);
114:
115: return pnl;
116: }
117:
118: /**
119: * Adds a listener to this object.
120: *
121: * @param lis Listener to be added.
122: */
123: public void addListener(IUIFactoryListener lis) {
124: _listenerList.add(IUIFactoryListener.class, lis);
125: }
126:
127: /**
128: * Removes a listener from this object.
129: *
130: * @param lis Listener to be removed.
131: */
132: public void removeListener(IUIFactoryListener lis) {
133: _listenerList.remove(IUIFactoryListener.class, lis);
134: }
135:
136: /**
137: * Fire a "tabbed pane created" event to all listeners.
138: */
139: private void fireTabbedPaneCreated(JTabbedPane tabPnl) {
140: // Guaranteed to be non-null.
141: Object[] listeners = _listenerList.getListenerList();
142: // Process the listeners last to first, notifying
143: // those that are interested in this event.
144: UIFactoryComponentCreatedEvent evt = null;
145: for (int i = listeners.length - 2; i >= 0; i -= 2) {
146: if (listeners[i] == IUIFactoryListener.class) {
147: // Lazily create the event.
148: if (evt == null) {
149: evt = new UIFactoryComponentCreatedEvent(this ,
150: tabPnl);
151: }
152: ((IUIFactoryListener) listeners[i + 1])
153: .tabbedPaneCreated(evt);
154: }
155: }
156: }
157: }
|