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.util.*; 018 import javax.swing.*; 019 import java.awt.*; 020 import java.awt.event.*; 021 import javax.swing.event.*; 022 023 /** 024 * Tracks the different dialogs in JBother. 025 * Keeps track of dialogs that should only be opened one at a time 026 * or dialogs that should be killed when the 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 { 034 private static DialogTracker instance = null; 035 private HashMap kDialogs = null; 036 037 /** 038 * Default constructor - this is a singleton, so it's private 039 */ 040 private DialogTracker() 041 { 042 kDialogs = new HashMap(); 043 } 044 045 /** 046 * Make sure the singleton is active 047 */ 048 private static void checkInstance() 049 { 050 if( DialogTracker.instance == null ) 051 { 052 DialogTracker.instance = new DialogTracker(); 053 } 054 } 055 056 /** 057 * Returns the dialog tracker's instance 058 * 059 * @return The instance value 060 */ 061 public static DialogTracker getInstance() 062 { 063 checkInstance(); 064 return instance; 065 } 066 067 /** 068 * Checks to see if the tracker is tracking a specific dialog 069 * 070 * @param dialog the dialog class to check 071 * @return true if the dialog tracker is tracking the dialog 072 */ 073 public static boolean containsDialog( Class dialog ) 074 { 075 checkInstance(); 076 for( int i = 0; i < DialogTracker.instance.size(); i++ ) 077 { 078 Window check = (Window)DialogTracker.instance.get( i ); 079 if( check.getClass().getName().equals( dialog.getName() ) ) 080 { 081 check.toFront(); 082 return true; 083 } 084 } 085 086 return false; 087 } 088 089 /** 090 * Kills all the dialogs that are supposed to be killed when the connection 091 * is lost 092 */ 093 public static void kill() 094 { 095 checkInstance(); 096 for( int i = 0; i < DialogTracker.instance.size(); i++ ) 097 { 098 Window check = (Window)DialogTracker.instance.get( i ); 099 if( instance.kDialogs.get( check ) != null ) 100 { 101 check.setVisible( false ); 102 instance.removeDialog( check ); 103 i--; 104 check.dispose(); 105 } 106 } 107 } 108 109 /** 110 * Removes a dialog from the tracker, and calls it's dispose() method 111 * 112 * @param dialog the dialog to remove 113 */ 114 public static void removeDialog( Window dialog ) 115 { 116 checkInstance(); 117 118 for( int i = 0; i < DialogTracker.instance.size(); i++ ) 119 { 120 Window check = (Window)DialogTracker.instance.get( i ); 121 if( check == dialog ) 122 { 123 DialogTracker.instance.remove( i ); 124 instance.kDialogs.remove( check ); 125 check.setVisible( false ); 126 check.dispose(); 127 i--; 128 } 129 } 130 } 131 132 /** 133 * Adds a dialog to the tracker 134 * 135 * @param dialog the dialog to add 136 * @param signOffKill set to true if you want the dialog to be destroyed when the connection is lost 137 * @param addCloseHandler set to true if you want a default close handler that will remove the dialog to be added 138 */ 139 public static void addDialog( final Window dialog, boolean signOffKill, boolean addCloseHandler ) 140 { 141 checkInstance(); 142 if( addCloseHandler ) 143 { 144 dialog.addWindowListener( 145 new WindowAdapter() 146 { 147 public void windowClosing( WindowEvent e ) 148 { 149 DialogTracker.removeDialog( dialog ); 150 } 151 } ); 152 } 153 154 if( signOffKill ) 155 { 156 instance.kDialogs.put( dialog, "true" ); 157 } 158 159 addDialog( dialog ); 160 } 161 162 /** 163 * Adds a dialog 164 * 165 * @param dialog the dialog to add 166 */ 167 public static void addDialog( Window dialog ) 168 { 169 checkInstance(); 170 DialogTracker.instance.add( dialog ); 171 } 172 } 173