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.util.*;
018    import javax.swing.*;
019    import java.awt.*;
020    import com.valhalla.settings.*;
021    
022    /**
023     * Displays a ConversationPanel in the TabFrame or in a containing JFrame
024     * depending on whether or not the application is set to use a tabbed window
025     *
026     * @author     Adam Olsen
027     * @created    Oct 25, 2004
028     * @version    1.1
029     */
030    public class MessageDelegator
031    {
032            private static MessageDelegator instance = null;
033            private Vector panels = new Vector();
034    
035            /**
036             * Default constructor... private for singleton
037             */
038            private MessageDelegator()
039            {
040            }
041    
042            /**
043             * @return    the MessageDelegator instance
044             */
045            public static MessageDelegator getInstance()
046            {
047                    if( instance == null )
048                    {
049                            instance = new MessageDelegator();
050                    }
051                    return instance;
052            }
053    
054            /**
055             * Shows a panel using the TabFrame or a containing frame
056             *
057             * @param  panel  the panel to show
058             */
059            public void showPanel( ConversationPanel panel )
060            {
061                    if( !( panel instanceof MessagePanel ) && Settings.getInstance().getBoolean( "useTabbedWindow" ) )
062                    {
063                            BuddyList.getInstance().startTabFrame();
064                            if( !BuddyList.getInstance().getTabFrame().contains( panel ) )
065                            {
066                                    BuddyList.getInstance().addTabPanel( panel );
067                                    if( panel instanceof ChatPanel )
068                                    {
069                                            ( (ChatPanel)panel ).setUpDivider();
070                                    }
071                            }
072                    }
073                    else
074                    {
075                            if( panel.getContainingFrame() == null )
076                            {
077                                    panel.createFrame();
078                            }
079                    }
080    
081                    if( !panels.contains( panel ) )
082                    {
083                            panels.add( panel );
084                    }
085            }
086    
087            /**
088             * If the panel is contained in a JFrame, this method brings that frame to the front
089             * of the screen
090             *
091             * @param  panel  the panel containing the frame to bring to the front
092             */
093            public void frontFrame( ConversationPanel panel )
094            {
095                    if( Settings.getInstance().getBoolean( "useTabbedWindow" ) )
096                    {
097                            JTabbedPane pane = BuddyList.getInstance().getTabFrame().getTabPane();
098                            pane.setSelectedComponent( (Component)panel );
099                            return;
100                    }
101    
102                    JFrame frame = panel.getContainingFrame();
103                    if( frame != null )
104                    {
105                            frame.setVisible( true );
106                            frame.toFront();
107                    }
108            }
109    
110            /**
111             * Removes a panel from the panels Vector
112             *
113             * @param  panel  the panel to remove
114             */
115            public void removePanel( ConversationPanel panel )
116            {
117                    panels.remove( panel );
118            }
119    
120            /**
121             * @return    the Vector containing a list of all the available ConversationPanels
122             */
123            public Vector getPanels()
124            {
125                    return panels;
126            }
127    }
128