001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *
004     *  This program is free software; you can redistribute it and/or modify it under
005     *  the terms of the GNU General Public License as published by the Free Software
006     *  Foundation; either version 1, or (at your option) any later version.
007     *
008     *  This program is distributed in the hope that it will be useful, but WITHOUT
009     *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010     *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
011     *  details.
012     *
013     *  You should have received a copy of the GNU General Public License along with
014     *  this program; if not, write to the Free Software Foundation, Inc., 675 Mass
015     *  Ave, Cambridge, MA 02139, USA.
016     */
017    package com.valhalla.jbother;
018    
019    import java.awt.*;
020    import java.awt.event.ActionEvent;
021    import java.awt.event.ActionListener;
022    import java.awt.event.MouseAdapter;
023    import java.awt.event.MouseEvent;
024    import java.net.URL;
025    
026    import com.valhalla.jbother.groupchat.*;
027    
028    import javax.swing.BorderFactory;
029    import javax.swing.JFrame;
030    import javax.swing.JLabel;
031    import javax.swing.JPanel;
032    import javax.swing.JWindow;
033    import javax.swing.Timer;
034    
035    import com.valhalla.settings.Settings;
036    
037    /**
038     *  Description of the Class
039     *
040     *@author     synic
041     *@created    May 18, 2005
042     */
043    public class NotificationPopup extends JWindow {
044        private Timer timer = new Timer(3000, new DestroyListener());
045    
046        private JLabel messageLabel = new JLabel();
047    
048        private static NotificationPopup instance;
049    
050        private Window focusWindow;
051        private Container focusComponent;
052    
053    
054        /**
055         *  Constructor for the NotificationPopup object
056         */
057        private NotificationPopup() {
058            super((JFrame) null);
059            Toolkit toolkit = Toolkit.getDefaultToolkit();
060            Dimension size = toolkit.getScreenSize();
061    
062            JPanel panel = (JPanel) getContentPane();
063            panel.setLayout(new BorderLayout());
064    
065            messageLabel.addMouseListener(
066                new MouseAdapter() {
067                    public void mouseClicked(MouseEvent e) {
068                        if (focusWindow != null) {
069                            focusWindow.setVisible(false);
070                            focusWindow.setVisible(true);
071                            if(focusWindow instanceof JFrame) ((JFrame)focusWindow).setExtendedState(JFrame.NORMAL);
072    
073                            focusWindow.toFront();
074                        }
075    
076                        if( focusComponent instanceof ChatRoomPanel )
077                        {
078                            BuddyList.getInstance().getTabFrame().getTabPane().setSelectedTab(((ChatRoomPanel)focusComponent).getTab());
079                        }
080    
081                        setVisible(false);
082                    }
083                });
084    
085            setFocusableWindowState(false);
086            panel.add(messageLabel, BorderLayout.CENTER);
087            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
088            panel.setBackground(new Color(247, 255, 117));
089            pack();
090            setSize(new Dimension(200, 60));
091        }
092    
093    
094        /**
095         *  Description of the Method
096         *
097         *@param  focusWindow     Description of the Parameter
098         *@param  title           Description of the Parameter
099         *@param  message         Description of the Parameter
100         *@param  focusComponent  Description of the Parameter
101         */
102        public static void showSingleton(Window focusWindow, String title,
103                String message, Container focusComponent) {
104            if(!Settings.getInstance().getBoolean("usePopup")) return;
105            if (instance == null) {
106                instance = new NotificationPopup();
107            }
108    
109            if (focusComponent != null && checkFocus(focusComponent)) {
110                return;
111            }
112            if (BuddyList.getInstance().getCurrentPresenceMode() == org.jivesoftware.smack.packet.Presence.Mode.DO_NOT_DISTURB) {
113                return;
114            }
115    
116            instance.setLocation();
117            instance.focusWindow = focusWindow;
118            instance.focusComponent = focusComponent;
119    
120            StringBuffer mess = new StringBuffer();
121    
122            URL light = instance.getClass().getClassLoader().getResource(
123                    "images/lightbulb.png");
124    
125            mess.append("<html><table><tr><td valign='top' width='2%'>").append(
126                    "<img src='").append(light.toString()).append("'></td>")
127                    .append("<td valign='top'><b>").append(title)
128                    .append("</b><br>").append(message).append(
129                    "</td></tr></table></html>");
130    
131            instance.messageLabel.setText(mess.toString());
132            instance.setVisible(true);
133            if (instance.timer.isRunning()) {
134                instance.timer.restart();
135            } else {
136                instance.timer.start();
137            }
138        }
139    
140    
141        /**
142         *  Description of the Method
143         *
144         *@param  container  Description of the Parameter
145         *@return            Description of the Return Value
146         */
147        private static boolean checkFocus(Container container) {
148            Component[] components = container.getComponents();
149            for (int i = 0; i < components.length; i++) {
150                if (components[i] instanceof Container) {
151                    if (checkFocus((Container) components[i])) {
152                        return true;
153                    }
154                    if (components[i].hasFocus()) {
155                        return true;
156                    }
157                }
158            }
159    
160            return false;
161        }
162    
163    
164        /**
165         *  Sets the location attribute of the NotificationPopup object
166         */
167        private void setLocation() {
168            int npopupx = 100;
169            int npopupy = 100;
170            int npopupw = 200;
171            int npopuph = 60;
172    
173            try {
174                npopupx = Integer.parseInt(Settings.getInstance().getProperty(
175                        "NPopupX"));
176                npopupy = Integer.parseInt(Settings.getInstance().getProperty(
177                        "NPopupY"));
178                npopupw = Integer.parseInt(Settings.getInstance().getProperty(
179                        "NPopupW"));
180                npopuph = Integer.parseInt(Settings.getInstance().getProperty(
181                        "NPopupH")) - 20;
182            } catch (Exception e) {
183            }
184    
185            setLocation(npopupx, npopupy + 20);
186            setSize(npopupw, npopuph);
187        }
188    
189    
190        /**
191         *  Description of the Class
192         *
193         *@author     synic
194         *@created    May 18, 2005
195         */
196        private class DestroyListener implements ActionListener {
197            /**
198             *  Description of the Method
199             *
200             *@param  e  Description of the Parameter
201             */
202            public void actionPerformed(ActionEvent e) {
203                focusComponent = null;
204                dispose();
205            }
206        }
207    }