001: package net.sourceforge.squirrel_sql.client.gui;
002:
003: /*
004: * Copyright (C) 2002-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.awt.event.WindowAdapter;
022: import java.awt.event.WindowEvent;
023: import java.net.URL;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
029:
030: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
031:
032: /**
033: * This factory stores instances of <TT>HtmlViewerSheet</TT> (viewer) objects
034: * keyed by the URL of the file they are displaying. When a viewer is requested
035: * the collection of existing viewers is checked and if one exists it is
036: * returned, otherwise a new one is created and returned. When a viewer is
037: * closed iy is removed from the collection.
038: *
039: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
040: */
041: public class FileViewerFactory {
042: /** Single instance of this class. */
043: private static final FileViewerFactory s_instance = new FileViewerFactory();
044:
045: /**
046: * Collection of <TT>HtmlViewer</TT> objects keyed by the URL they are
047: * displaying.
048: */
049: private final HashMap<String, HtmlViewerSheet> _sheets = new HashMap<String, HtmlViewerSheet>();
050:
051: /** Listener used to cleanup instances of viewers after they are closed. */
052: private MyInternalFrameListener _lis = new MyInternalFrameListener();
053:
054: /**
055: * private default ctor as class is a singleton.
056: */
057: private FileViewerFactory() {
058: super ();
059: }
060:
061: /**
062: * Return the single instance of this class.
063: *
064: * @return The singleton instance of this class.
065: */
066: public static FileViewerFactory getInstance() {
067: return s_instance;
068: }
069:
070: /**
071: * Return the viewer for the passed URL.
072: *
073: * @param parent MDI parent frame to hold the new viewer.
074: * @param url The URL to be displayed.
075: *
076: * @return the viewer for the passed URL.
077: *
078: * @throws IllegalArgumentException
079: * Thrown if null MainFrame or URL passed.
080: */
081: public synchronized HtmlViewerSheet getViewer(MainFrame parent,
082: URL url) {
083: if (parent == null) {
084: throw new IllegalArgumentException("MainFrame == null");
085: }
086: if (url == null) {
087: throw new IllegalArgumentException("URL == null");
088: }
089:
090: HtmlViewerSheet viewer = _sheets.get(url.toString());
091: if (viewer == null) {
092: viewer = new HtmlViewerSheet(parent.getApplication(), url
093: .toString(), url);
094: // viewer.addInternalFrameListener(_lis);
095: viewer.addWindowListener(_lis);
096: viewer.setSize(600, 400);
097: // parent.addInternalFrame(viewer, true, null);
098: // GUIUtils.centerWithinDesktop(viewer);
099: GUIUtils.centerWithinParent(viewer);
100: _sheets.put(url.toString(), viewer);
101: }
102: return viewer;
103: }
104:
105: public synchronized void closeAllViewers() {
106: final Map<String, HtmlViewerSheet> viewers = new HashMap<String, HtmlViewerSheet>(
107: _sheets);
108: final Iterator<HtmlViewerSheet> it = viewers.values()
109: .iterator();
110: while (it.hasNext()) {
111: final HtmlViewerSheet v = it.next();
112: removeViewer(v);
113: v.dispose();
114: }
115: }
116:
117: private synchronized void removeViewer(HtmlViewerSheet viewer) {
118: //viewer.removeInternalFrameListener(_lis);
119: viewer.removeWindowListener(_lis);
120: _sheets.remove(viewer.getURL().toString());
121: }
122:
123: // private final class MyInternalFrameListener extends InternalFrameAdapter
124: private final class MyInternalFrameListener extends WindowAdapter {
125: /**
126: * Viewer has been closed so allow it to be garbage collected.
127: */
128: // public void internalFrameClosed(InternalFrameEvent evt)
129: public void windowClosed(WindowEvent evt) {
130: // removeViewer((HtmlViewerSheet)evt.getInternalFrame());
131: removeViewer((HtmlViewerSheet) evt.getWindow());
132: // super.internalFrameClosed(evt);
133: }
134: }
135:
136: }
|