001 /*
002 * Copyright (C) 2003 Adam Olsen
003 * This program is free software; you can redistribute it and/or modify
004 * it under the terms of the GNU General Public License as published by
005 * the Free Software Foundation; either version 1, or (at your option)
006 * any later version.
007 * This program is distributed in the hope that it will be useful,
008 * but WITHOUT ANY WARRANTY; without even the implied warranty of
009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010 * GNU General Public License for more details.
011 * You should have received a copy of the GNU General Public License
012 * along with this program; if not, write to the Free Software
013 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014 */
015 package com.valhalla.gui;
016
017 import java.awt.Window;
018 import java.awt.event.WindowAdapter;
019 import java.awt.event.WindowEvent;
020 import java.util.ArrayList;
021 import java.util.HashMap;
022
023 /**
024 * Tracks the different dialogs in JBother. Keeps track of dialogs that should
025 * only be opened one at a time or dialogs that should be killed when the
026 * connection is lost
027 *
028 * @author Adam Olsen
029 * @created October 22, 2004
030 * @version 1.0
031 */
032 public class DialogTracker extends ArrayList {
033 private static DialogTracker instance = null;
034
035 private HashMap kDialogs = null;
036
037 /**
038 * Default constructor - this is a singleton, so it's private
039 */
040 private DialogTracker() {
041 kDialogs = new HashMap();
042 }
043
044 /**
045 * Make sure the singleton is active
046 */
047 private static void checkInstance() {
048 if (DialogTracker.instance == null) {
049 DialogTracker.instance = new DialogTracker();
050 }
051 }
052
053 /**
054 * Returns the dialog tracker's instance
055 *
056 * @return The instance value
057 */
058 public static DialogTracker getInstance() {
059 checkInstance();
060 return instance;
061 }
062
063 /**
064 * Checks to see if the tracker is tracking a specific dialog
065 *
066 * @param dialog
067 * the dialog class to check
068 * @return true if the dialog tracker is tracking the dialog
069 */
070 public static boolean containsDialog(Class dialog) {
071 checkInstance();
072 for (int i = 0; i < DialogTracker.instance.size(); i++) {
073 Window check = (Window) DialogTracker.instance.get(i);
074 if (check.getClass().getName().equals(dialog.getName())) {
075 check.toFront();
076 return true;
077 }
078 }
079
080 return false;
081 }
082
083 /**
084 * Kills all the dialogs that are supposed to be killed when the connection
085 * is lost
086 */
087 public static void kill() {
088 checkInstance();
089 for (int i = 0; i < DialogTracker.instance.size(); i++) {
090 Window check = (Window) DialogTracker.instance.get(i);
091 if (instance.kDialogs.get(check) != null) {
092 check.setVisible(false);
093 DialogTracker.removeDialog(check);
094 i--;
095 check.dispose();
096 }
097 }
098 }
099
100 /**
101 * Removes a dialog from the tracker, and calls it's dispose() method
102 *
103 * @param dialog
104 * the dialog to remove
105 */
106 public static void removeDialog(Window dialog) {
107 checkInstance();
108
109 for (int i = 0; i < DialogTracker.instance.size(); i++) {
110 Window check = (Window) DialogTracker.instance.get(i);
111 if (check == dialog) {
112 DialogTracker.instance.remove(i);
113 instance.kDialogs.remove(check);
114 check.setVisible(false);
115 check.dispose();
116 i--;
117 }
118 }
119 }
120
121 /**
122 * Adds a dialog to the tracker
123 *
124 * @param dialog
125 * the dialog to add
126 * @param signOffKill
127 * set to true if you want the dialog to be destroyed when the
128 * connection is lost
129 * @param addCloseHandler
130 * set to true if you want a default close handler that will
131 * remove the dialog to be added
132 */
133 public static void addDialog(final Window dialog, boolean signOffKill,
134 boolean addCloseHandler) {
135 checkInstance();
136 if (addCloseHandler) {
137 dialog.addWindowListener(new WindowAdapter() {
138 public void windowClosing(WindowEvent e) {
139 DialogTracker.removeDialog(dialog);
140 }
141 });
142 }
143
144 if (signOffKill) {
145 instance.kDialogs.put(dialog, "true");
146 }
147
148 addDialog(dialog);
149 }
150
151 /**
152 * Adds a dialog
153 *
154 * @param dialog
155 * the dialog to add
156 */
157 public static void addDialog(Window dialog) {
158 checkInstance();
159 DialogTracker.instance.add(dialog);
160 }
161 }
162