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 com.valhalla.jbother.*;
022    import com.valhalla.gui.*;
023    import javax.swing.*;
024    import java.awt.*;
025    import java.awt.event.*;
026    import java.util.*;
027    
028    import org.jivesoftware.smack.packet.Message;
029    
030    /**
031     * A blank message window - to send "NORMAL" type messages
032     *
033     * @author Adam Olsen
034     * @version 1.0
035    */
036    public class MessagePanel extends ConversationPanel implements TabFramePanel
037    {
038            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
039            private JLabel toLabel = new JLabel( resources.getString( "to" ) + ": " );
040            private JLabel subjectLabel = new JLabel( resources.getString( "subject" ) + ": " );
041            private JButton sendButton = new JButton( resources.getString( "send" ) );
042            private JButton replyButton = new JButton( resources.getString( "reply" ) );
043            private JTextField toField = new JTextField();
044            private JTextField subjectField = new JTextField();
045            private JTextArea textEntryArea = new JTextArea();
046            private JPanel buttonPanel = new JPanel();
047            private JScrollPane scroll = new JScrollPane();
048            private MessagePanel thisPointer = this;
049    
050            /**
051             * Default constructor
052            */
053            public MessagePanel()
054            {
055                    super( null );
056    
057                    setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
058                    setLayout( new BorderLayout( 5, 5 ) );
059    
060                    JPanel topPanel = new JPanel();
061                    topPanel.setLayout( new BoxLayout( topPanel, BoxLayout.Y_AXIS ) );
062    
063                    JPanel toPanel = new JPanel( new BorderLayout( 5, 5 ) );
064                    toPanel.add( toLabel, BorderLayout.WEST );
065                    toPanel.add( toField, BorderLayout.CENTER );
066                    toPanel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 0 ) );
067                    topPanel.add( toPanel );
068    
069                    JPanel subjectPanel = new JPanel( new BorderLayout( 5, 5 ) );
070                    subjectPanel.add( subjectLabel, BorderLayout.WEST );
071                    subjectPanel.add( subjectField, BorderLayout.CENTER );
072                    topPanel.add( subjectPanel );
073    
074                    add( topPanel, BorderLayout.NORTH );
075                    scroll.setViewportView( textEntryArea );
076                    conversationArea.setScroll( scroll );
077                    add( scroll, BorderLayout.CENTER );
078    
079                    buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) );
080                    buttonPanel.add( Box.createHorizontalGlue() );
081                    buttonPanel.add( sendButton );
082    
083                    addListeners();
084    
085                    add( buttonPanel, BorderLayout.SOUTH );
086    
087                    toField.grabFocus();
088            }
089    
090            /**
091            **/
092            public void createFrame()
093            {
094                    frame = new JFrame();
095                    frame.setContentPane( this );
096    
097                    frame.pack();
098                    frame.setSize( new Dimension( 450, 370 ) );
099    
100                    ImageIcon icon = StatusIconCache.getStatusIcon( org.jivesoftware.smack.packet.Presence.Mode.AVAILABLE );
101                    if( icon != null )
102                    {
103                            frame.setIconImage( icon.getImage() );
104                    }
105    
106                    Standard.cascadePlacement( frame );
107    
108                    frame.addWindowListener(
109                            new WindowAdapter()
110                            {
111                                    public void windowClosing( WindowEvent e )
112                                    {
113                                            closeHandler();
114                                    }
115                            } );
116    
117                    if( !frame.isVisible() )
118                    {
119                            frame.setVisible( true );
120                    }
121    
122            }
123    
124            /**
125             * Focuses when a Tab in the TabFrame is clicked
126            */
127            public void focusYourself()
128            {
129                    if( toField.isEnabled() )
130                    {
131                            toField.grabFocus();
132                    }
133                    else {
134                            conversationArea.grabFocus();
135                    }
136            }
137    
138            /**
139             * Recieves a message and displays it in the Dialog
140             * @param message the message packet to recieve
141            */
142            public void recieveMessage( Message message )
143            {
144                    super.recieveMessage();
145                    toLabel.setText( resources.getString( "from" ) + ": " );
146                    toField.setText( message.getFrom() );
147                    toField.setEnabled( false ); // to and subject fields are disabled in the
148                                                                             // receiving dialog
149                    subjectField.setText( message.getSubject() );
150                    subjectField.setEnabled( false );
151    
152                    scroll.setViewportView( conversationArea );
153                    String body = ConversationText.replaceText( message.getBody(), false );
154    
155                    conversationArea.append( body );
156                    buttonPanel.remove( sendButton );
157                    buttonPanel.add( replyButton );
158                    buttonPanel.repaint();
159    
160                    validate();
161                    MessageDelegator.getInstance().showPanel( this );
162                    MessageDelegator.getInstance().frontFrame( this );
163            }
164    
165            /**
166             * @return the name of the tab
167            */
168            public String getPanelName()
169            {
170                    if( !toField.getText().equals( "" ) )
171                    {
172                            return toField.getText();
173                    }
174                    else {
175                            return "Blank Message";
176                    }
177            }
178    
179            /**
180             * @return the name of the tab
181            */
182            public String getWindowTitle()
183            {
184                    if( !toField.getText().equals( "" ) )
185                    {
186                            return toField.getText();
187                    }
188                    else {
189                            return "Blank Message";
190                    }
191            }
192    
193            /**
194             * Sets the text in the "To: " field
195             * @param to the string to set the text to
196            */
197            public void setTo( String to )
198            {
199                    toField.setText( to );
200            }
201    
202            /**
203             * Sets the text in the "Subject:" field
204             * @param subject the string to set the subject text to
205            */
206            public void setSubject( String subject )
207            {
208                    subjectField.setText( subject );
209            }
210    
211            /**
212             * Returns the TextArea widget
213             * @return The main text area
214            */
215            public JTextArea getTextEntryArea() { return textEntryArea; }
216    
217            /**
218             * Called by the reply button to create a reply window
219            */
220            private void replyHandler()
221            {
222                    MessagePanel window = new MessagePanel();
223                    window.setTo( toField.getText() );
224                    window.setSubject( "Re: " + subjectField.getText() );
225                    window.getTextEntryArea().grabFocus();
226                    window.validate();
227                    MessageDelegator.getInstance().showPanel( window );
228                    closeHandler();
229            }
230    
231            /**
232             * Adds the listeners to the various event emitting widgets
233            */
234            private void addListeners()
235            {
236                    final MessagePanel thisPointer = this;
237    
238                    MessageActionListener listener = new MessageActionListener();
239                    sendButton.addActionListener( listener );
240                    replyButton.addActionListener( listener );
241    
242                    Action closeAction = new AbstractAction()
243                    {
244                            public void actionPerformed( ActionEvent e )
245                            {
246                                    closeHandler();
247                            }
248                    };
249    
250                    toField.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ), closeAction );
251                    subjectField.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ), closeAction );
252                    textEntryArea.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ), closeAction );
253                    conversationArea.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ), closeAction );
254            }
255    
256            /**
257             * Sends the message
258            */
259            private void sendHandler()
260            {
261                    if( toField.getText().equals( "" ) || textEntryArea.getText().equals( "" ) )
262                    {
263                            Standard.warningMessage( this, "messageWindow",
264                                    resources.getString( "mustSpecifyToAndBody" ) );
265                            return;
266                    }
267    
268                    Message message = new Message();
269    
270                    // sets up the message
271                    message.setBody( textEntryArea.getText() );
272                    message.setType( Message.Type.NORMAL );
273                    message.setTo( toField.getText() );
274                    message.setSubject( subjectField.getText() );
275                    message.setFrom( BuddyList.getInstance().getConnection().getUser() );
276    
277                    BuddyList.getInstance().getConnection().sendPacket( message );
278    
279                    closeHandler();
280            }
281    
282            /**
283             * Handles the events in the MessageDialog
284             * @author Adam Olsen
285             * @version 1.0
286            */
287            class MessageActionListener implements ActionListener
288            {
289                    /**
290                     * called by the button widgets
291                    */
292                    public void actionPerformed( ActionEvent e )
293                    {
294                            if( e.getSource() == sendButton ) sendHandler();
295                            if( e.getSource() == replyButton ) replyHandler();
296                    }
297            }
298    }