001    /*
002            Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005            it under the terms of the GNU General Public License as published by
006            the Free Software Foundation; either version 1, or (at your option)
007            any later version.
008    
009            This program is distributed in the hope that it will be useful,
010            but WITHOUT ANY WARRANTY; without even the implied warranty of
011            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012            GNU General Public License for more details.
013    
014            You should have received a copy of the GNU General Public License
015            along with this program; if not, write to the Free Software
016            Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.jbother.menus;
020    
021    import java.awt.event.*;
022    import java.awt.*;
023    import javax.swing.*;
024    import java.util.*;
025    
026    import com.valhalla.gui.*;
027    import com.valhalla.jbother.*;
028    import com.valhalla.settings.*;
029    import com.valhalla.jbother.groupchat.*;
030    
031    /**
032     * Buddies menu - contains all the other menus - excluding the help menu
033     * @author Adam Olsen
034     * @version 1.0
035    */
036    public class BuddyListBuddiesMenu extends JMenu
037    {
038            private BuddyList blist;
039            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
040            private JMenuItem addBuddyItem = new JMenuItem( resources.getString( "addBuddy" ) );
041            private JMenuItem quitItem = new JMenuItem( resources.getString( "quitButton" ) );
042            private JMenu servicesMenu = new JMenu( resources.getString( "jabberServices" ) );
043            private JMenuItem joinChatItem = new JMenuItem( resources.getString( "joinGroupChat" ) );
044            private JMenuItem discoItem = new JMenuItem( resources.getString( "serviceDiscovery" ) );
045            private JMenuItem blankItem = new JMenuItem( resources.getString( "blankMessage" ) );
046            private JCheckBoxMenuItem offlineBuddies = new JCheckBoxMenuItem( resources.getString( "showOfflineBuddies" ) );
047    
048            private JMenuItem registerItem = new JMenuItem( resources.getString( "registerForService" ) );
049            private BuddyListOptionsMenu optionsMenu = new BuddyListOptionsMenu();
050            private JMenuItem switchItem = new JMenuItem( resources.getString( "switchProfile" ) );
051            private JMenuItem editItem = new JMenuItem( resources.getString( "editAccount" ) );
052    
053            /**
054             * Creates the buddies menu
055             * @param blist the buddy list to attach this menu to
056            */
057            public BuddyListBuddiesMenu( BuddyList blist )
058            {
059                    super( "JBother" );
060    
061                    if( System.getProperty( "mrj.version" ) != null )
062                    {
063                            setText( resources.getString( "actions" ) );
064                            setMnemonic( KeyEvent.VK_A );
065                    }
066                    else {
067                            setMnemonic( KeyEvent.VK_J );
068                    }
069    
070                    this.blist = blist;
071    
072                    initComponents();
073            }
074    
075            /**
076             * Sets up the visual components
077            */
078            private void initComponents()
079            {
080                    joinChatItem.addActionListener( new MenuActionListener() );
081    
082                    MenuActionListener listener = new MenuActionListener();
083                    addBuddyItem.addActionListener( listener );
084                    blankItem.addActionListener( listener );
085                    quitItem.addActionListener( listener );
086                    registerItem.addActionListener( listener );
087                    discoItem.addActionListener( listener );
088                    switchItem.addActionListener( listener );
089                    offlineBuddies.addActionListener( listener );
090                    editItem.addActionListener( listener );
091                    offlineBuddies.setState( Settings.getInstance().getBoolean( "showOfflineBuddies" ) );
092    
093                    add( addBuddyItem );
094    
095                    addSeparator();
096                    servicesMenu.add( discoItem );
097                    add( servicesMenu );
098                    servicesMenu.add( registerItem );
099                    add( joinChatItem );
100                    add( blankItem );
101                    addSeparator();
102                    add( switchItem );
103                    add( editItem );
104                    add( offlineBuddies );
105                    add( optionsMenu );
106    
107                    addSeparator();
108                    add( quitItem );
109            }
110    
111            /**
112             * @param var true if this is OS X
113            */
114            public void setOSX( boolean var )
115            {
116                    if( var )
117                    {
118                            remove( quitItem );
119                    }
120                    else {
121                            if( !isMenuComponent( quitItem ) ) add( quitItem );
122                    }
123    
124                    optionsMenu.setOSX( var );
125            }
126    
127            /**
128             * Listens for an item to be clicked
129             * @author Adam Olsen
130             * @version 1.0
131            */
132            class MenuActionListener implements ActionListener
133            {
134                    public void actionPerformed( ActionEvent e )
135                    {
136                            if( e.getSource() == offlineBuddies ) BuddyList.getInstance().getBuddyListTree().setShowOfflineBuddies( offlineBuddies.getState() );
137                            else if( e.getSource() == quitItem ) blist.quitHandler();
138                            else if( e.getSource() == addBuddyItem )
139                            {
140                                    if( !BuddyList.getInstance().checkConnection() )
141                                    {
142                                            BuddyList.getInstance().connectionError();
143                                            return;
144                                    }
145    
146                                    new AddBuddyDialog().show();
147                            }
148                            else if( e.getSource() == blankItem ) MessageDelegator.getInstance().showPanel( new MessagePanel() );
149                            else if( e.getSource() == joinChatItem ) new GroupChatBookmarks().show();
150                            else if( e.getSource() == registerItem ) registrationHandler();
151                            else if( e.getSource() == discoItem ) new ServiceDiscoveryDialog( blist ).show();
152                            else if( e.getSource() == switchItem ) switchHandler();
153                            else if( e.getSource() == editItem ) editHandler();
154                    }
155            }
156    
157            private void editHandler()
158            {
159                    ProfileEditorDialog dialog = new ProfileEditorDialog( null, ProfileManager.getCurrentProfile() );
160                    dialog.setIsCurrentProfile( true );
161                    dialog.show();
162            }
163    
164            private void switchHandler()
165            {
166                    if( BuddyList.getInstance().checkConnection() )
167                    {
168                            Standard.warningMessage( null, resources.getString( "error" ), resources.getString( "stillConnected" ) );
169                    }
170                    else {
171                            BuddyList.getInstance().setVisible( false );
172                            new ProfileManager().show();
173                    }
174            }
175    
176            /**
177             * Registers for a server by displaying a RegistrationForm
178            */
179            private void registrationHandler()
180            {
181                    String result = (String)JOptionPane.showInputDialog( null,
182                            resources.getString( "pleaseEnterServer" ),
183                            resources.getString( "registerForService" ), JOptionPane.QUESTION_MESSAGE, null, null, "" );
184    
185                    if( result != null && !result.equals( "" ) )
186                    {
187                            RegistrationForm form = new RegistrationForm( result );
188                            form.getRegistrationInfo();
189                    }
190            }
191    }