KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > ui > JTabContent


1 /*
2     =========================================================================
3     Package ui - Implements user interface components
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JTabContent.java,v 1.20 2007/02/13 04:46:28 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.20 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.ui;
40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.text.html.*;
45 import org.planetamessenger.mos.engine.*;
46 import org.planetamessenger.mos.forms.JMOSSendMessageDlg;
47 import org.planetamessenger.plugin.*;
48 import org.planetamessenger.util.JTextUtil;
49
50 /** The user interface for sending messages */
51 public class JTabContent extends java.awt.Container JavaDoc implements ChangeListener {
52
53   JMOSSendMessageDlg parent = null;
54   public JTextPane messageEdit = null;
55   public JScrollPane messagePane = null;
56   public JTextPane historyEdit = null;
57   public JScrollPane historyPane = null;
58   public JContactListItem ownerItem = null;
59   JSplitPane splitPane = null;
60   String JavaDoc strSelfAliasName = "";
61
62
63   /**
64    * Constructor. Initilizes all
65    * class data.
66    * @param parent Reference to the parent window;
67    * @param item The owner of this data class;
68    */

69   JTabContent( JMOSSendMessageDlg parent, JContactListItem ownerItem ) {
70     
71     java.awt.GridBagConstraints JavaDoc constraints = new java.awt.GridBagConstraints JavaDoc();
72
73
74     strSelfAliasName = JSharedObjects.getPluginEngine().get( ownerItem.getPluginId() ).getAliasName();
75     messageEdit = new JTextPane();
76     messagePane = new JScrollPane( messageEdit );
77     historyEdit = new JTextPane();
78     historyPane = new JScrollPane( historyEdit );
79     splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, historyPane, messagePane );
80     this.ownerItem = ownerItem;
81     this.parent = parent;
82     setLayout( new java.awt.GridBagLayout JavaDoc() );
83     
84     // History pane setup
85
constraints.gridx = 0;
86     constraints.gridy = 0;
87     constraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
88     constraints.gridheight = 1;
89     constraints.weightx = 1.0;
90     constraints.weighty = 1.0;
91     constraints.fill = java.awt.GridBagConstraints.BOTH;
92     constraints.insets = new java.awt.Insets JavaDoc( 1, 1, 1, 1 );
93     add( splitPane, constraints );
94
95     splitPane.setOneTouchExpandable( true );
96     
97     historyEdit.setFont( JSystemFonts.FONT );
98     historyEdit.setEditable( false );
99     historyEdit.setEditorKit( JEditorPane.createEditorKitForContentType( "text/html" ) );
100     //historyEdit.setContentType( "text/html" );
101
historyPane.setPreferredSize( new java.awt.Dimension JavaDoc( 350, 90 ) );
102     
103     // Message pane setup
104
messageEdit.setFont( JSystemFonts.FONT );
105     messageEdit.addKeyListener( new java.awt.event.KeyAdapter JavaDoc() {
106       public void keyReleased( java.awt.event.KeyEvent JavaDoc keyEvt ) {
107         processKey( keyEvt );
108       }
109     } );
110     messagePane.setPreferredSize( new java.awt.Dimension JavaDoc( 350, 75 ) );
111   }
112
113   /**
114    * Add the message text to the tab history box.
115    * This method is called when the PlanetaMessenger user
116    * send a message to a buddy.
117    */

118   public void addHistoryText() {
119   
120     if( messageEdit.getText().compareTo( "" ) != 0 ) {
121       JButton sendButton = this.parent.getSendButton();
122       HTMLDocument doc = ( HTMLDocument ) historyEdit.getDocument();
123       HTMLEditorKit html = ( HTMLEditorKit ) historyEdit.getEditorKit();
124
125       
126       try {
127         html.insertHTML(
128           doc, doc.getLength(),
129           "<b>" + strSelfAliasName + ": </b>" + JTextUtil.encodeHtml( messageEdit.getText() ),
130           0, 0, null
131         );
132         historyEdit.setCaretPosition( doc.getLength() );
133         messageEdit.setText( "" );
134         sendButton.setEnabled( false );
135         messageEdit.requestFocus();
136       } catch( BadLocationException e ) {
137         System.err.println( "JTabContent.addHistoryText() - " + e );
138       } catch( java.io.IOException JavaDoc ioe ) {
139         System.err.println( "JTabContent.addHistoryText() - " + ioe );
140       }
141     }
142   }
143   
144   /**
145    * Add a message text to the history box.
146    * This method is called when a buddy send a message to
147    * PlanetaMessenger user.
148    * @param strText The text to add;
149    */

150   public void addHistoryText( String JavaDoc strText ) {
151   
152     if( strText.compareTo( "" ) != 0 ) {
153       JButton sendButton = this.parent.getSendButton();
154       HTMLDocument doc = ( HTMLDocument ) historyEdit.getDocument();
155       HTMLEditorKit html = ( HTMLEditorKit ) historyEdit.getEditorKit();
156
157       try {
158         html.insertHTML( doc, doc.getLength(), strText, 0, 0, null );
159         historyEdit.setCaretPosition( doc.getLength() );
160       } catch( BadLocationException e ) {
161         System.err.println( "JTabContent.addHistoryText( String ) - " + e );
162       } catch( java.io.IOException JavaDoc ioe ) {
163         System.err.println( "JTabContent.addHistoryText( String ) - " + ioe );
164       }
165     }
166   }
167   
168   /**
169    * Process the key event of messageEdit
170    * component.
171    * @param keyEvt The key event object;
172    */

173   private void processKey( java.awt.event.KeyEvent JavaDoc keyEvt ) {
174     
175     JButton sendButton = this.parent.getSendButton();
176         
177     if( messageEdit.getText().length() > 0 )
178       sendButton.setEnabled( true );
179     else
180       sendButton.setEnabled( false );
181   }
182   
183   /**
184    * Implements the stateChanged method listener
185    * to receive Tab events.
186    * @param changeEvent The Tab event;
187    */

188   public void stateChanged( ChangeEvent changeEvent ) {
189    
190     JMessagePane messageTabs = this.parent.getTabbedMessagePane();
191     
192     if( !isShowing() )
193       processKey( null );
194     else
195       messageTabs.disableCurrentTabMessageIcon();
196   }
197 }
198
199 // JTabContent class
200
Popular Tags