KickJava   Java API By Example, From Geeks To Geeks.

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


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: JProtocolTree.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.lang.*;
42 import javax.swing.*;
43 import javax.swing.tree.*;
44 import org.planetamessenger.mos.engine.*;
45
46
47 public class JProtocolTree extends JTree {
48   
49   DefaultTreeModel treeModel = null;
50   DefaultMutableTreeNode rootNode = null;
51   
52   
53
54   /**
55    * Constructor. Creates and initializes a JTree component.
56    * @param strRootLabel The Root label of this Tree component;
57    */

58   public JProtocolTree( java.lang.String JavaDoc strRootLabel ) {
59     
60     super();
61     putClientProperty( "JTree.lineStyle", "Angled" );
62     renewRenderer();
63     rootNode = new DefaultMutableTreeNode( new JProtocolTreeItem( strRootLabel, null, -1 ) );
64     treeModel = new DefaultTreeModel( rootNode );
65     setModel( treeModel );
66   }
67   
68   /**
69    * Constructor. Initialize all class data.
70    */

71   public JProtocolTree() {
72     
73     this( "" );
74   }
75
76   /**
77    * Renew the object renderer. Call this method
78    * is nice when user changes current look-and-feel
79    * to avoid non-consistent tree item updates, because
80    * all items was draw with old look-and-feel scheme.
81    */

82   public void renewRenderer() {
83
84     DefaultTreeCellRenderer renderer;
85
86     setCellRenderer( new JTreeCellRenderer() );
87     renderer = ( DefaultTreeCellRenderer ) getCellRenderer();
88     renderer.setLeafIcon( null );
89     renderer.setOpenIcon( null );
90     renderer.setClosedIcon( null );
91   }
92
93   /**
94    * Add a new node to this Tree component.
95    * @param item The item that will be added to tree.
96    */

97   public void addNode( JProtocolTreeItem item ) {
98     
99     DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( item );
100     rootNode.add( newNode );
101     reload( rootNode );
102   }
103   
104   /**
105    * Remove the specified node of tree component.
106    * @param item The item that will be removed to tree.
107    */

108   public boolean removeNode( JProtocolTreeItem item ) {
109
110     int nChildCount = treeModel.getChildCount( rootNode );
111     int nCount = 0;
112     boolean bRemoved = false;
113
114     while( nCount < treeModel.getChildCount( rootNode ) ) {
115       DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) treeModel.getChild( rootNode, nCount );
116       JProtocolTreeItem tmpItem = ( JProtocolTreeItem ) node.getUserObject();
117
118       if( tmpItem.getPluginId() == item.getPluginId() ) {
119         rootNode.remove( node );
120         bRemoved = true;
121         nCount = 0;
122       }
123       else
124         nCount++;
125     }
126
127     reload( rootNode );
128     
129     return bRemoved;
130   }
131   
132   /**
133    * Return the number of Tree child items.
134    */

135   public int getChildCount() {
136     
137     return treeModel.getChildCount( rootNode );
138   }
139
140   /**
141    * Update the root item label tree component.
142    * @param strRootLabel The new root item label;
143    */

144   public void updateRootTreeLabel( String JavaDoc strRootLabel ) {
145
146     JProtocolTreeItem item = ( JProtocolTreeItem ) rootNode.getUserObject();
147
148     item.setText( strRootLabel );
149     reload();
150   }
151
152   /**
153    * Reload complete tree node and it's childs from root node.
154    * @param The node to reload;
155    */

156   public void reload() {
157     
158     reload( rootNode );
159   }
160
161   /**
162    * Reload complete tree node and it's childs.
163    * @param The node to reload;
164    */

165   public void reload( TreeNode node ) {
166     
167     treeModel.reload( node );
168   }
169
170   /**
171    * Update complete tree node and it's childs.
172    */

173   public void refresh() {
174
175     refresh( rootNode );
176   }
177
178   /**
179    * Update a tree node and it's childs.
180    * @param node The node to refresh;
181    */

182   private void refresh( TreeNode node ) {
183     
184     treeModel.nodeChanged( node );
185     
186     for( int nCount = 0; nCount < node.getChildCount(); nCount++ )
187       refresh( node.getChildAt( nCount ) );
188   }
189
190   /**
191    *=========================================================================
192    * JTreeCellRenderer class implementation
193    * This is a new cell renderer implementation Protocol tree component.
194    *=========================================================================
195    */

196   private class JTreeCellRenderer extends DefaultTreeCellRenderer {
197
198     /**
199      * Overrides the Cell renderer method of this JTreeCellRenderer.<br>
200      * Show the check icon (disable option) when plugin is selected to be installed;
201      * See JDK API for the parameters.
202      */

203     public java.awt.Component JavaDoc getTreeCellRendererComponent( javax.swing.JTree JavaDoc tree, java.lang.Object JavaDoc objValue, boolean bSelected, boolean bExpanded, boolean bIsLeaf, int nRow, boolean bHasFocus ) {
204
205       super.getTreeCellRendererComponent( tree, objValue, bSelected, bExpanded, bIsLeaf, nRow, bHasFocus );
206       
207       try {
208
209         DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) objValue;
210         JProtocolTreeItem item = ( JProtocolTreeItem ) node.getUserObject();
211
212         // Add/remove the checked icon
213
if( item.getPluginId() >= 0 ) {
214           if( item.getInstall() )
215             setIcon( JSharedObjects.getResources().getIcon( JResources.INSTALL_PLUGIN_ICON ) );
216           else
217             setIcon( JSharedObjects.getResources().getIcon( JResources.UNINSTALL_PLUGIN_ICON ) );
218         }
219       } catch( Exception JavaDoc e ) {
220         System.err.println( "JProtocolTree.JTreeCellRenderer.getTreeCellRendererComponent() - Invalid item object. Ignoring event" );
221       }
222
223       return this;
224     }
225   } // JTreeCellRenderer Class
226
}
227
228 // JProtocolTree Class
Popular Tags