KickJava   Java API By Example, From Geeks To Geeks.

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


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: JMessagePane.java,v 1.21 2007/01/28 17:39:20 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.21 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.ui;
40
41 import java.util.*;
42 import org.planetamessenger.mos.engine.*;
43 import org.planetamessenger.mos.forms.JMOSSendMessageDlg;
44
45
46
47 public class JMessagePane extends javax.swing.JTabbedPane JavaDoc {
48   
49   HashMap<String JavaDoc, JTabContent> hTabs;
50   JMOSSendMessageDlg parent;
51
52   
53   /**
54    * Create and initializes the message
55    * pane.
56    * @param parent Reference to parent window;
57    */

58   public JMessagePane( JMOSSendMessageDlg parent ) {
59     
60     super( javax.swing.JTabbedPane.TOP, javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT );
61     this.parent = parent;
62     hTabs = new HashMap<String JavaDoc, JTabContent>();
63     setAutoscrolls( true );
64   }
65   
66   /**
67    * Add a new tab to this pane.
68    * @param item The contact item to add;
69    */

70   public void addTab( org.planetamessenger.plugin.JContactListItem item ) {
71     
72     if( hTabs.get( item.getUserId() ) == null ) {
73       JTabContent tabContent = new JTabContent( parent, item );
74
75       addTab( item.getNickName() + ":" + item.getUserId(), null, tabContent );
76       addChangeListener( tabContent );
77       setSelectedIndex( this.getTabCount() - 1 );
78       hTabs.put( item.getUserId(), tabContent );
79     }
80     else {
81       JTabContent tabContent = ( JTabContent ) hTabs.get( item.getUserId() );
82       int nTabIndex = indexOfComponent( tabContent );
83
84       this.setIconAt( nTabIndex, JSharedObjects.getResources().getIcon( JResources.MESSAGE_BLINK ) );
85     }
86   }
87
88   /**
89    * Set the text to a tab item.
90    * @param item Item to add the text;
91    * @param strText The text to add;
92    */

93   public void setText( org.planetamessenger.plugin.JContactListItem item, String JavaDoc strText ) {
94     
95     JTabContent tabContent = ( JTabContent ) hTabs.get( item.getUserId() );
96     
97     
98     if( tabContent != null ) {
99       String JavaDoc strHistory = strText;
100       
101       if( strText.compareTo( "" ) == 0 )
102         setSelectedComponent( tabContent );
103       else
104         tabContent.addHistoryText( strHistory );
105     }
106   }
107   
108   /**
109    * Check if Tab contain a specified item.
110    * @param item The item to check;
111    */

112   public boolean contains( org.planetamessenger.plugin.JContactListItem item ) {
113     
114     if( hTabs.get( ( item == null ? "" : item.getUserId() ) ) == null )
115       return false;
116     else
117       return true;
118   }
119   
120   /**
121    * Check if item is the active tab
122    * @param item The item to check;
123    */

124   public boolean isItemActive( org.planetamessenger.plugin.JContactListItem item ) {
125     
126     if( item != null ) {
127       JTabContent tabContent = ( JTabContent ) hTabs.get( item.getUserId() );
128     
129     
130       if( tabContent != null )
131         return getSelectedComponent().equals( tabContent );
132     }
133
134     return false;
135   }
136    
137   /**
138    * Return the active contact item.
139    */

140   public JTabContent getActiveItem() {
141      
142     Iterator<JTabContent> itItems = hTabs.values().iterator();
143     JTabContent tabContent;
144      
145      
146     while( itItems.hasNext() ) {
147       tabContent = itItems.next();
148     
149       if( tabContent != null ) {
150         if( isItemActive( tabContent.ownerItem ) )
151           return tabContent;
152       }
153     }
154
155     return null;
156   }
157   
158   /**
159    * Disable the icon message on current active tab.
160    */

161   public void disableCurrentTabMessageIcon() {
162     
163     JTabContent tabContent = getActiveItem();
164     
165     if( tabContent != null ) {
166       int nIndex = this.indexOfComponent( tabContent );
167
168       setIconAt( nIndex, null );
169     }
170   }
171   
172   /**
173    * Override the removeAll method to perform
174    * correct clean at JMessagePane class object.
175    */

176   public void removeAll() {
177    
178     super.removeAll();
179     hTabs.clear();
180   }
181
182   /**
183    * Remove the selected Tab.
184    */

185   public void removeSelectedTab() {
186     
187     int nTabNumber = this.getSelectedIndex();
188
189     
190     if( nTabNumber < 0 )
191       return;
192     
193     JTabContent tabContent = ( JTabContent ) getComponentAt( nTabNumber );
194       
195   
196     removeChangeListener( tabContent );
197     hTabs.remove( tabContent.ownerItem.getUserId() );
198     removeTabAt( nTabNumber );
199       
200     if( getTabCount() == 0 )
201       parent.dispose();
202     else
203       if( getTabCount() == 1 )
204         this.disableCurrentTabMessageIcon();
205   }
206
207   // JCloseTabIcon class implementation
208
/**
209    * The class which generates the 'X' icon for the tabs. The constructor
210    * accepts an icon which is extra to the 'X' icon, so you can have tabs
211    * like in NetBeans and JBuilder. This value is null if no extra icon is required.
212    * Thanks to Mr_Silly at Sun java forum groups (http://forum.java.sun.com/profile.jsp?user=157799);
213    */

214   class JCloseTabIcon implements javax.swing.Icon JavaDoc {
215     
216     static final int CLOSE_ICON = 0;
217     static final int MESSAGE_ICON = 1;
218     private final int CLOSE_BTN_WIDTH = 16;
219     private final int CLOSE_BTN_HEIGHT = 16;
220
221     private int nCoordX;
222     private int nCoordY;
223     private int nWidth;
224     private int nHeight;
225     private int nIconType;
226     private javax.swing.Icon JavaDoc fileIcon;
227
228
229     /**
230      * Constructor. Creates a JCloseTabIcon.
231      * @param fileIcon The icon to show in tab;<br>
232      * @param nIconType The icon type;<br>
233      */

234     public JCloseTabIcon( javax.swing.Icon JavaDoc fileIcon, int nIconType ) {
235       
236       this.fileIcon = fileIcon;
237       this.nIconType = nIconType;
238
239       if( fileIcon == null ) {
240         nWidth = CLOSE_BTN_WIDTH;
241         nHeight = CLOSE_BTN_HEIGHT;
242       }
243       else {
244         nWidth = fileIcon.getIconWidth();
245         nHeight = fileIcon.getIconHeight();
246       }
247     }
248
249     /**
250      * Override the default paintIcon method, to
251      * enable a new behaviour to this component.
252      * @param c The component that will be painted;
253      * @param g The current paint device;
254      * @x The X coordinate to paint icon;
255      * @y The Y coordinate to paint icon;
256      */

257     public void paintIcon( java.awt.Component JavaDoc c, java.awt.Graphics JavaDoc g, int x, int y ) {
258       
259       int nTmpY = y + 2;
260
261       this.nCoordX = x;
262       this.nCoordY = y;
263
264       if( fileIcon != null ) {
265         fileIcon.paintIcon( c, g, x, nTmpY - 1 );
266       }
267       else {
268         java.awt.Color JavaDoc color = g.getColor();
269               
270         g.setColor( java.awt.Color.black );
271         g.drawLine( x + 1, nTmpY, x + 12, nTmpY );
272         g.drawLine( x + 1, nTmpY + 13, x + 12, nTmpY + 13 );
273         g.drawLine( x, nTmpY + 1, x, nTmpY + 12 );
274         g.drawLine( x + 13, nTmpY + 1, x + 13, nTmpY + 12 );
275         g.drawLine( x + 3, nTmpY + 3, x + 10, nTmpY + 10 );
276         g.drawLine( x + 3, nTmpY + 4, x + 9, nTmpY + 10 );
277         g.drawLine( x + 4, nTmpY + 3, x + 10, nTmpY + 9 );
278         g.drawLine( x + 10, nTmpY + 3, x + 3, nTmpY + 10 );
279         g.drawLine( x + 10, nTmpY + 4, x + 4, nTmpY + 10 );
280         g.drawLine( x + 9, nTmpY + 3, x + 3, nTmpY + 9 );
281         g.setColor( color );
282       }
283     }
284
285     /**
286      * Override the default getIconWidth method.
287      * Return the icon width;
288      */

289     public int getIconWidth() {
290
291       return nWidth;
292     }
293
294     /**
295      * Override the default getIconHeight method.
296      * Return the icon height;
297      */

298     public int getIconHeight() {
299       
300       return nHeight;
301     }
302     
303     /**
304      * Get the icon type.
305      */

306     public int getIconType() {
307
308       return nIconType;
309     }
310
311     /**
312      * Override the default getBounds method.
313      * Return the boundary of component;
314      */

315     public java.awt.Rectangle JavaDoc getBounds() {
316       
317       return new java.awt.Rectangle JavaDoc( nCoordX, nCoordY, nWidth, nHeight );
318     }
319   }
320   // JCloseTabIcon class
321
}
322 // JMessagePane class
Popular Tags