KickJava   Java API By Example, From Geeks To Geeks.

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


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

38
39 package org.planetamessenger.mos.ui;
40
41 import org.planetamessenger.mos.engine.*;
42
43
44 public class JAccountsTable extends javax.swing.JTable JavaDoc implements java.awt.event.ActionListener JavaDoc {
45   
46   private static final int DEFAULT_CHECKBOX_WIDTH = 20;
47   static final int STATUS_INDEX = 0;
48   static final int CONNECTION_INDEX = 1;
49   static final int AUTOCONNECT_INDEX = 2;
50   static final int PROTOCOL_INDEX = 3;
51   
52   private int nTimerCounter = 0;
53   private javax.swing.Timer JavaDoc updateTimer = null;
54   JAccountsTableModel model = null;
55   
56   
57   
58   /**
59    * Start the update timer.
60    */

61   synchronized void startUpdateUI() {
62     
63     if( nTimerCounter == 0 )
64       updateTimer.start();
65
66     nTimerCounter++;
67   }
68   
69   /**
70    * Stop the update timer.
71    */

72   synchronized void stopUpdateUI() {
73
74     nTimerCounter--;
75     
76     if( nTimerCounter == 0 )
77       updateTimer.stop();
78   }
79   
80   /**
81    * Constructor. Creates and initializes a
82    * JAccountsTable.
83    */

84   public JAccountsTable() {
85     
86     super();
87
88     nTimerCounter = 0;
89     updateTimer = new javax.swing.Timer JavaDoc( 60, this );
90     updateTimer.stop();
91
92     model = new JAccountsTableModel( this );
93     /*
94      * If the column order change, please see the #BeCarefull 001 remark
95      * at JAccountsTableModel class.
96      */

97     model.addColumn( JSharedObjects.getLanguageManager().getStringEx( "STATUS" ) );
98     model.addColumn( JSharedObjects.getLanguageManager().getStringEx( "CONNECT" ) );
99     model.addColumn( JSharedObjects.getLanguageManager().getStringEx( "AUTO" ) );
100     model.addColumn( JSharedObjects.getLanguageManager().getStringEx( "PLUGIN" ) );
101     setModel( model );
102     
103     getColumnModel().getColumn( CONNECTION_INDEX ).setPreferredWidth( DEFAULT_CHECKBOX_WIDTH );
104     getColumnModel().getColumn( CONNECTION_INDEX ).setMinWidth( DEFAULT_CHECKBOX_WIDTH );
105     getColumnModel().getColumn( CONNECTION_INDEX ).setMaxWidth( DEFAULT_CHECKBOX_WIDTH );
106     getColumnModel().getColumn( AUTOCONNECT_INDEX ).setPreferredWidth( DEFAULT_CHECKBOX_WIDTH );
107     getColumnModel().getColumn( AUTOCONNECT_INDEX ).setMinWidth( DEFAULT_CHECKBOX_WIDTH );
108     getColumnModel().getColumn( AUTOCONNECT_INDEX ).setMaxWidth( DEFAULT_CHECKBOX_WIDTH );
109     getColumnModel().getColumn( AUTOCONNECT_INDEX ).setResizable( false );
110     
111     getTableHeader().setResizingAllowed( false );
112     getTableHeader().setReorderingAllowed( false );
113     
114     setDefaultRenderer( JAccountsTableModel.JPluginComboBox.class, new JAccountsRenderer() );
115     setDefaultEditor( JAccountsTableModel.JPluginComboBox.class, new JAccountsCellEditor() );
116     
117     setDefaultRenderer( JAccountsTableModel.JPluginCheckBox.class, new JAccountsRenderer() );
118     setDefaultEditor( JAccountsTableModel.JPluginCheckBox.class, new JAccountsCellEditor() );
119     
120     setDefaultRenderer( javax.swing.JLabel JavaDoc.class, new JAccountsRenderer() );
121     setDefaultEditor( javax.swing.JLabel JavaDoc.class, new JAccountsCellEditor() );
122   }
123   
124   /**
125    * Add a table row based on
126    * parameter plugin.
127    * @param plugin The plugin to add;
128    */

129   public void addRow( org.planetamessenger.plugin.JPlugin plugin ) {
130     
131     model.addRow( plugin );
132   }
133   
134   /**
135    * Remove a table row based from
136    * parameter plugin.
137    * @param plugin The plugin to remove;
138    */

139   public void removeRow( org.planetamessenger.plugin.JPlugin plugin ) {
140     
141     model.removeRow( plugin );
142   }
143   
144   /**
145    * Change the plugin status in plugin status
146    * table.
147    * @param plugin The plugin that status will be
148    * changed;
149    * @param nStatus The new plugin status;
150    */

151   public void changeStatus( org.planetamessenger.plugin.JPlugin plugin, int nStatus ) {
152     
153     model.changeStatus( plugin, nStatus );
154   }
155   
156   /**
157    * Change the plugin connection status in plugin connection status
158    * table.
159    * @param plugin The plugin that status will be changed;
160    * @param nStatus The new plugin connection status.
161    * The valid values:
162    * Connecting - JPluginEngineListener.CONNECTING;
163    * Connected - JPluginEngineListener.CONNECTED;
164    * attempting connection - JPluginEngineListener.CONNECTING
165    *
166    */

167   public void changeConnectionStatus( org.planetamessenger.plugin.JPlugin plugin, int nStatus ) {
168     
169     model.changeConnectionStatus( plugin, nStatus );
170     repaint();
171   }
172   
173   /**
174     * Implements a timer to update the table UI
175     * because a animated icon don't works correctly
176     * and need update (forced) 60 millisecond update.
177     * @param see jdk docs.
178     */

179   public void actionPerformed( java.awt.event.ActionEvent JavaDoc actionEvent ) {
180
181     repaint();
182   }
183   
184   /**
185    * Replace the updateUI method to perform
186    * correct update to table component,
187    * updating all components into table.
188    */

189   public void updateUI() {
190
191     super.updateUI();
192     
193     if( model != null ) {
194       java.util.Vector JavaDoc vData = model.getDataVector();
195
196       for( int nCount = 0; nCount < vData.size(); nCount++ ) {
197         java.util.Vector JavaDoc vItems = ( java.util.Vector JavaDoc ) vData.get( nCount );
198       
199         for( int nItemsCount = 0; nItemsCount < vItems.size(); nItemsCount++ ) {
200           javax.swing.JComponent JavaDoc itemCell = ( ( javax.swing.JComponent JavaDoc ) vItems.get( nItemsCount ) );
201
202           javax.swing.SwingUtilities.updateComponentTreeUI( itemCell );
203           itemCell.invalidate();
204           itemCell.validate();
205           itemCell.repaint();
206         }
207       }
208     }
209   }
210   
211   /**
212    * Update the language for all table items.
213    */

214   public void updateLanguage() {
215
216     // Update column title
217
getColumnModel().getColumn( STATUS_INDEX ).setHeaderValue( JSharedObjects.getLanguageManager().getStringEx( "STATUS" ) );
218     getColumnModel().getColumn( CONNECTION_INDEX ).setHeaderValue( JSharedObjects.getLanguageManager().getStringEx( "CONNECT" ) );
219     getColumnModel().getColumn( AUTOCONNECT_INDEX ).setHeaderValue( JSharedObjects.getLanguageManager().getStringEx( "AUTO" ) );
220     getColumnModel().getColumn( PROTOCOL_INDEX ).setHeaderValue( JSharedObjects.getLanguageManager().getStringEx( "PLUGIN" ) );
221     model.updateLanguage();
222   }
223
224
225   /**
226    *=========================================================================
227    * JAccountsRenderer class implementation
228    * This is a new table renderer implementation for the accounts table,
229    * that will manage the entire redrawing for labels, icons, system labels, ...
230    *=========================================================================
231    */

232   
233   class JAccountsRenderer extends javax.swing.JComponent JavaDoc implements javax.swing.table.TableCellRenderer JavaDoc {
234
235
236     /**
237      * Constructor. Initializes all
238      * class data;
239      */

240     public JAccountsRenderer() {
241       
242       super();
243       setOpaque( true );
244     }
245     
246     /**
247      * getTableCellRendererComponent implementation.
248      * @param see javax.swing.table.TableCellRenderer for
249      * parameters description.
250      */

251     public java.awt.Component JavaDoc getTableCellRendererComponent( javax.swing.JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int nRow, int nCol ) {
252
253       if( value instanceof JAccountsTableModel.JPluginComboBox )
254         return ( JAccountsTableModel.JPluginComboBox ) value;
255       
256       if( value instanceof JAccountsTableModel.JPluginCheckBox )
257           return ( JAccountsTableModel.JPluginCheckBox ) value;
258
259       //if( value instanceof JAccountsCellEditor )
260
// return ( JAccountsCellEditor ) value;
261

262       return ( javax.swing.JLabel JavaDoc ) value;
263     }
264     
265     //public java.awt.Component getTableCellRendererComponent( javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int nRow, int nCol ) {
266

267     // if( isSelected ) {
268
// setForeground( table.getSelectionForeground() );
269
// setBackground( table.getSelectionBackground() );
270
// } else {
271
// setForeground( table.getForeground() );
272
// setBackground( table.getBackground() );
273
// }
274

275     // System.err.println( "JAccountsRenderer.getTableCellRendererComponent() - " + value );
276

277     // if( !( ( javax.swing.JCheckBox ) value ).isEnabled() )
278
// setIcon( ( ( javax.swing.JCheckBox ) value ).getIcon() );
279
// else
280
// setSelected( ( value != null && ( ( javax.swing.JCheckBox ) value ).isSelected() ) );
281

282     // return this;
283
//}
284

285   } // JAccountsRenderer class
286

287   
288   /**
289    *=========================================================================
290    * JAccountsCellEditor class implementation
291    * This is a new cell edit implementation for the accounts table,
292    * that will manage the managment of data changing on the component.
293    *=========================================================================
294    */

295   
296   class JAccountsCellEditor extends javax.swing.JComponent JavaDoc implements javax.swing.table.TableCellEditor JavaDoc {
297
298     protected javax.swing.event.EventListenerList JavaDoc listenerList = null;
299     protected javax.swing.event.ChangeEvent JavaDoc changeEvent = null;
300     
301
302     
303     /**
304      * Constructor. Initializes all
305      * class data.
306      */

307     public JAccountsCellEditor() {
308       
309       super();
310       listenerList = new javax.swing.event.EventListenerList JavaDoc();
311       changeEvent = new javax.swing.event.ChangeEvent JavaDoc( this );
312       setOpaque( true );
313     }
314     
315     /**
316      * Implements the javax.swing.table.TableCellEditor
317      * method.
318      * @param see jdk docs;
319      */

320     public void addCellEditorListener( javax.swing.event.CellEditorListener JavaDoc cellEditorListener ) {
321     
322       listenerList.add( javax.swing.event.CellEditorListener JavaDoc.class, cellEditorListener );
323     }
324     
325     /**
326      * Implements the javax.swing.table.TableCellEditor
327      * method.
328      * @param see jdk docs;
329      */

330     public void removeCellEditorListener( javax.swing.event.CellEditorListener JavaDoc cellEditorListener ) {
331
332       listenerList.remove( javax.swing.event.CellEditorListener JavaDoc.class, cellEditorListener );
333     }
334     
335     /**
336      * Implements the javax.swing.table.TableCellEditor
337      * method.
338      * @param see jdk docs;
339      */

340     public Object JavaDoc getCellEditorValue() {
341       
342       return this;
343     }
344     
345     /**
346      * Implements the javax.swing.table.TableCellEditor
347      * method.
348      * @param see jdk docs;
349      */

350     public java.awt.Component JavaDoc getTableCellEditorComponent( javax.swing.JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int nRow, int nCol ) {
351       
352       if( value instanceof JAccountsTableModel.JPluginComboBox )
353         return ( JAccountsTableModel.JPluginComboBox ) value;
354       
355       if( value instanceof JAccountsTableModel.JPluginCheckBox )
356         return ( JAccountsTableModel.JPluginCheckBox ) value;
357       
358       return ( javax.swing.JLabel JavaDoc ) value;
359     }
360     
361     /**
362      * Implements the javax.swing.table.TableCellEditor
363      * method.
364      * @param see jdk docs;
365      */

366     public boolean isCellEditable( java.util.EventObject JavaDoc eventObject ) {
367       
368       return true;
369     }
370     
371     /**
372      * Implements the javax.swing.table.TableCellEditor
373      * method.
374      * @param see jdk docs;
375      */

376     public boolean shouldSelectCell( java.util.EventObject JavaDoc eventObject ) {
377       
378       return true;
379     }
380     
381     /**
382      * Implements the javax.swing.table.TableCellEditor
383      * method.
384      * @param see jdk docs;
385      */

386     public void cancelCellEditing() {
387       
388       fireEditingCanceled();
389     }
390     
391     /**
392      * Implements the javax.swing.table.TableCellEditor
393      * method.
394      * @param see jdk docs;
395      */

396     public boolean stopCellEditing() {
397       
398       //fireEditingStopped();
399
return true;
400     }
401
402     /**
403      * Dispatch Editing stopped event
404      * through listener
405      */

406     protected void fireEditingStopped() {
407       
408       javax.swing.event.CellEditorListener JavaDoc listener;
409       java.lang.Object JavaDoc[] listeners = listenerList.getListenerList();
410       
411       
412       for( int i = 0; i < listeners.length; i++ ) {
413         if( listeners[i] == javax.swing.event.CellEditorListener JavaDoc.class ) {
414           listener = ( javax.swing.event.CellEditorListener JavaDoc ) listeners[i + 1];
415           listener.editingStopped( changeEvent );
416         }
417       }
418     }
419
420     /**
421      * Dispatch Editing canceled event
422      * through listener
423      */

424     protected void fireEditingCanceled() {
425       javax.swing.event.CellEditorListener JavaDoc listener;
426       java.lang.Object JavaDoc[] listeners = listenerList.getListenerList();
427       
428       
429       for( int i = 0; i < listeners.length; i++ ) {
430         if( listeners[i] == javax.swing.event.CellEditorListener JavaDoc.class ) {
431           listener = ( javax.swing.event.CellEditorListener JavaDoc ) listeners[i + 1];
432           listener.editingCanceled( changeEvent );
433         }
434       }
435     }
436   } // JAccountsCellEditor class
437

438 }
439 // JAccountsTable class
Popular Tags