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.io.*;
023    import java.util.*;
024    
025    import javax.swing.JMenuItem;
026    import javax.swing.JPopupMenu;
027    
028    import com.valhalla.gui.*;
029    import com.valhalla.jbother.*;
030    import com.valhalla.settings.Settings;
031    
032    /**
033     * The menu that pops up when you right click on a ChatPanel
034     * @author Adam Olsen
035     * @version 1.0
036    */
037    public class ConversationPopupMenu extends JPopupMenu
038    {
039            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
040            private ConversationPanel window;
041            private ConversationArea area;
042            private JMenuItem log = new JMenuItem( resources.getString( "viewLog" ) );
043            private JMenuItem clear = new JMenuItem( resources.getString( "clearWindow" ) );
044            private JMenuItem block = new JMenuItem( resources.getString( "blockUser" ) );
045    
046            /**
047             * Sets up the menu
048             * @param window the window to attach this menu to
049             * @param area the conversation area to attach this menu to
050            */
051            public ConversationPopupMenu( final ConversationPanel window, final ConversationArea area )
052            {
053                    this.window = window;
054                    this.area = area;
055    
056                    initComponents();
057            }
058    
059            /**
060             * Sets up the visuals and event listeners
061            */
062            private void initComponents()
063            {
064                    add( log );
065                    add( clear );
066                    add( block );
067    
068                    log.addActionListener( new ActionListener()
069                    {
070                            public void actionPerformed( ActionEvent e ) { window.openLogWindow(); }
071                    } );
072    
073                    clear.addActionListener( new ActionListener()
074                    {
075                            public void actionPerformed( ActionEvent e ) { area.setText( "" ); }
076                    } );
077    
078                    block.addActionListener( new BlockActionListener() );
079            }
080    
081            /**
082             * Adds a user to the blocked users list
083             *
084             * @author Adam Olsen
085             * @version 1.0
086            */
087            class BlockActionListener implements ActionListener
088            {
089                    public void actionPerformed( ActionEvent e )
090                    {
091                            // just adds the user to the blocked list in buddy list
092                            File blockedFile = new File( JBother.profileDir + File.separator + "blocked" );
093                            BuddyList.getInstance().getBlockedUsers().put( window.getBuddy().getUser(), "blocked" );
094    
095                            // and then writes all of them to the blocked users file
096                            try {
097                                    FileWriter fw = new FileWriter( blockedFile );
098                                    PrintWriter out = new PrintWriter( fw );
099    
100                                    Iterator i = BuddyList.getInstance().getBlockedUsers().keySet().iterator();
101                                    while( i.hasNext() )
102                                    {
103                                            out.println( (String)i.next() );
104                                    }
105    
106                                    fw.close();
107                            }
108                            catch( IOException ex ) { Standard.warningMessage( window, resources.getString( "blockUser" ),
109                                    resources.getString( "problemWritingBlockedFile" ) ); }
110    
111                            Standard.noticeMessage( window, resources.getString( "blockUser" ), resources.getString( "userHasBeenBlocked" ) );
112                    }
113            }
114    }