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.jbother;
016    
017    import java.awt.Component;
018    import java.awt.event.ActionEvent;
019    import java.awt.event.ActionListener;
020    import java.util.Vector;
021    
022    import javax.swing.JFrame;
023    import javax.swing.JTabbedPane;
024    import javax.swing.SwingUtilities;
025    import net.infonode.tabbedpanel.*;
026    import net.infonode.tabbedpanel.titledtab.*;
027    import net.infonode.util.*;
028    
029    import com.valhalla.settings.Settings;
030    
031    /**
032     * Displays a ConversationPanel in the TabFrame or in a containing JFrame
033     * depending on whether or not the application is set to use a tabbed window
034     *
035     * @author Adam Olsen
036     * @created Oct 25, 2004
037     * @version 1.1
038     */
039    public class MessageDelegator {
040        private static MessageDelegator instance = null;
041    
042        private Vector panels = new Vector();
043    
044        private FocusTimer timer = new FocusTimer();
045    
046        private javax.swing.Timer t = new javax.swing.Timer(150, timer);
047    
048        private static ConversationPanel currentPanel;
049    
050        /**
051         * Default constructor... private for singleton
052         */
053        private MessageDelegator() {
054        }
055    
056        /**
057         * @return the MessageDelegator instance
058         */
059        public static MessageDelegator getInstance() {
060            if (instance == null) {
061                instance = new MessageDelegator();
062            }
063            return instance;
064        }
065    
066        /**
067         * Shows a panel using the TabFrame or a containing frame
068         *
069         * @param panel
070         *            the panel to show
071         */
072        public void showPanel(ConversationPanel panel) {
073            if (Settings.getInstance().getBoolean("useTabbedWindow")) {
074                BuddyList.getInstance().startTabFrame();
075                if (!BuddyList.getInstance().getTabFrame().contains(panel)) {
076                    BuddyList.getInstance().addTabPanel(panel);
077    
078                    if (panel instanceof ChatPanel) {
079                        ((ChatPanel) panel).setUpDivider();
080                    } else if (panel instanceof ConsolePanel) {
081                        ((ConsolePanel) panel).setUpDivider();
082                    }
083                }
084            } else {
085                if (panel.getContainingFrame() == null) {
086                    panel.createFrame();
087                }
088            }
089    
090            if (!panels.contains(panel)) {
091                panels.add(panel);
092            }
093        }
094    
095        /**
096         * If the panel is contained in a JFrame, this method brings that frame to
097         * the front of the screen
098         *
099         * @param panel
100         *            the panel containing the frame to bring to the front
101         */
102        public void frontFrame(final ConversationPanel panel) {
103            if (Settings.getInstance().getBoolean("useTabbedWindow")) {
104                TabbedPanel pane = BuddyList.getInstance().getTabFrame()
105                        .getTabPane();
106    
107    
108                pane.setSelectedTab(((TabFramePanel)panel).getTab());
109                currentPanel = panel;
110    
111                if (!t.isRunning())
112                    t.start();
113                else
114                    t.restart();
115    
116                return;
117            }
118    
119            JFrame frame = panel.getContainingFrame();
120    
121            if (frame != null) {
122                frame.setVisible(true);
123                frame.toFront();
124            }
125    
126            panel.getInputComponent().requestFocus();
127        }
128    
129        class FocusTimer implements ActionListener {
130            public void actionPerformed(ActionEvent e) {
131                t.stop();
132                SwingUtilities.invokeLater(new Runnable() {
133                    public void run() {
134                        currentPanel.getInputComponent().requestFocus();
135                    }
136                });
137            }
138        }
139    
140        /**
141         * Removes a panel from the panels Vector
142         *
143         * @param panel
144         *            the panel to remove
145         */
146        public void removePanel(ConversationPanel panel) {
147            if (panel instanceof ChatPanel) {
148                ((ChatPanel) panel).removeDividerListener();
149            } else if (panel instanceof ConsolePanel) {
150                ((ConsolePanel) panel).removeDividerListener();
151            }
152    
153            panels.remove(panel);
154            if(currentPanel == panel) currentPanel = null;
155        }
156    
157        /**
158         * @return the Vector containing a list of all the available
159         *         ConversationPanels
160         */
161        public Vector getPanels() {
162            return panels;
163        }
164    }
165