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;
020    
021    import java.awt.*;
022    import java.awt.event.*;
023    
024    import javax.swing.*;
025    import java.util.*;
026    import com.valhalla.gui.*;
027    import com.valhalla.jbother.*;
028    import com.valhalla.jbother.jabber.BuddyStatus;
029    import com.valhalla.settings.Settings;
030    
031    /**
032     * Handles Headline messages (server announces, RSS, etc.)
033     *
034     * @author Yury Soldak (tail)
035     * @version 1.0
036     **/
037    public class HeadlinesPanel extends ConversationPanel
038    {
039            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
040    
041            private JScrollPane conversationScroll;
042            private JPopupMenu popMenu = new JPopupMenu();
043    
044    
045            /**
046             * Default constructor
047             * @param buddy the Buddy that this window corresponds to
048             * @param userId the user id of the buddy
049             * @param buddyName the buddy's alias
050            */
051            public HeadlinesPanel( BuddyStatus buddy )
052            {
053                    super( buddy );
054    
055                    conversationScroll = new JScrollPane( conversationArea );
056                    conversationScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
057                    JMenuItem viewLog = new JMenuItem( resources.getString( "viewLog" ) );
058                    popMenu.add( viewLog );
059    
060                    viewLog.addActionListener( new ActionListener()
061                    {
062                            public void actionPerformed( ActionEvent e ) { openLogWindow(); }
063                    } );
064    
065                    conversationArea.setScroll( conversationScroll );
066                    setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
067                    setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
068                    add( conversationScroll );
069                    addListeners();
070            }
071    
072            public void focusYourself() { }
073    
074            /**
075            **/
076            public void createFrame()
077            {
078                    frame = new JFrame();
079                    frame.setContentPane( this );
080                    frame.pack();
081                    frame.setSize( new Dimension( 400, 420 ) );
082                    ImageIcon icon = StatusIconCache.getStatusIcon( org.jivesoftware.smack.packet.Presence.Mode.AVAILABLE );
083                    if( icon != null )
084                    {
085                            frame.setIconImage( icon.getImage() );
086                    }
087    
088                    Standard.cascadePlacement( frame );
089    
090                    frame.addWindowListener(
091                            new WindowAdapter()
092                            {
093                                    public void windowClosing( WindowEvent e )
094                                    {
095                                            if( Settings.getInstance().getProperty( "preserveMessages" ) == null )
096                                            {
097                                                    closeHandler();
098                                            }
099                                            else
100                                            {
101                                                    startTimer();
102                                                    frame.setVisible( false );
103                                            }
104                                    }
105                            } );
106    
107                    frame.setTitle( buddy.getName() + " (" + buddy.getUser() + ")" );
108    
109                    if( !frame.isVisible() )
110                    {
111                            frame.setVisible( true );
112                    }
113    
114                    stopTimer();
115            }
116    
117            /**
118             * Adds the various event listeners for the components that are a part of this
119             * frame
120             **/
121            private void addListeners()
122            {
123                    conversationArea.addKeyListener( new CTRLWHandler() );
124                    conversationArea.addMouseListener( new RightClickListener( popMenu ) );
125            }
126    
127            /**
128             * Closes the Window if CTRL+W is pressed
129            */
130            class CTRLWHandler extends KeyAdapter
131            {
132                    public void keyTyped( KeyEvent e )
133                    {
134                            if( e.getKeyCode() == KeyEvent.VK_W && e.getModifiers() == Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() )
135                            {
136                                    checkCloseHandler();
137                            }
138    
139                            if( e.getKeyCode() == KeyEvent.VK_K && e.getModifiers() == Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() )
140                            {
141                                    closeHandler();
142                            }
143                    }
144            }
145    
146            /**
147             * Recieves a message
148             * @param sbj the subject
149             * @param body the message
150             * @param resource the resource of the buddy
151            */
152            public void recieveMessage( String sbj, String body, String resource )
153            {
154                    body = ConversationText.replaceText( body, false);
155                    String text = "<div style=\"margin:2px; background-color:#f0f0f0; padding:3px;\"><b>"+sbj+"</b><hr>"+body+"</div>";
156    
157                    conversationArea.append( text );
158    
159                    super.recieveMessage();
160    
161                    if( logOut != null )
162                    {
163                            logOut.println( conversationArea.getLastHTML() );
164                    }
165            }
166    
167    }