KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > forms > preferences > JPrivacyControlPanel


1 /*
2     =========================================================================
3     Package preferences - Preferences management panels.
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: JPrivacyControlPanel.java,v 1.14 2007/01/28 17:39:20 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.14 $
35  * $State: Exp $
36  *
37  */

38 package org.planetamessenger.mos.forms.preferences;
39
40 import java.util.*;
41 import javax.swing.*;
42 import java.awt.*;
43 import java.awt.event.*;
44 import org.planetamessenger.plugin.*;
45 import org.planetamessenger.mos.engine.*;
46 import com.jgoodies.forms.builder.*;
47 import com.jgoodies.forms.layout.*;
48 import com.jgoodies.forms.debug.*;
49
50
51 public class JPrivacyControlPanel extends JPluginPreferencesContainer {
52   
53   private Object JavaDoc[] aContactList = null;
54   private JPlugin plugin = null;
55   private int nPluginId = -1;
56   private JButton addSelectedButton = null;
57   private JButton removeSelectedButton = null;
58   private JButton addAllButton = null;
59   private JButton removeAllButton = null;
60   private JLabel privacyLabel = null;
61   private JLabel availableLabel = null;
62   private JLabel selectedLabel = null;
63   private JComboBox privacyCombo = null;
64   private JList availableList = null;
65   private JList selectedList = null;
66   private JScrollPane selectedListPane = null;
67   private JScrollPane availableListPane = null;
68   private FormLayout layout = null;
69   private PanelBuilder builder = null;
70   private FormLayout operationsPanelLayout = null;
71   private PanelBuilder operationsBuilder = null;
72   
73   
74   public class ComboItem {
75     
76     static final int TYPE_VISIBLE_LIST = 0;
77     static final int TYPE_BLOCKED_LIST = 1;
78         
79     int nItemType;
80     String JavaDoc strItemText;
81
82     
83     /**
84      * Constructor. Initialize all class data.
85      * @param nItemType The type of creaed item;
86      * @param strItemText The item caption into ComboBox;
87      */

88     ComboItem( int nItemType, String JavaDoc strItemText ) {
89       
90       this.nItemType = nItemType;
91       this.strItemText = strItemText;
92     }
93     
94     /**
95      * Return the item type.
96      */

97     int getItemType() {
98       
99       return nItemType;
100     }
101     
102     /**
103      * Override toString() base class to return
104      * item caption string.
105      */

106     public String JavaDoc toString() {
107       
108       return strItemText;
109     }
110   }
111
112
113   
114   /**
115    * Constructor. Initializes all class data.
116    */

117   public JPrivacyControlPanel() {
118    
119     super();
120     
121     CellConstraints cellConst = new CellConstraints();
122
123
124     operationsPanelLayout = new FormLayout( "fill:25dlu", "fill:25dlu, 5dlu, fill:25dlu, 5dlu, fill:25dlu, 5dlu, fill:25dlu" );
125     operationsBuilder = new PanelBuilder( operationsPanelLayout );
126     layout = new FormLayout( "130dlu:grow, center:pref, 130dlu:grow", "fill:13dlu, fill:13dlu, 15dlu, fill:13dlu, fill:180dlu" );
127     builder = new PanelBuilder( layout );
128     addSelectedButton = new JButton();
129     removeSelectedButton = new JButton();
130     addAllButton = new JButton();
131     removeAllButton = new JButton();
132     privacyLabel = new JLabel();
133     availableLabel = new JLabel();
134     selectedLabel = new JLabel();
135     privacyCombo = new JComboBox();
136     availableList = new JList( new DefaultListModel() );
137     selectedList = new JList( new DefaultListModel() );
138     selectedListPane = new JScrollPane( selectedList );
139     availableListPane = new JScrollPane( availableList );
140
141     selectedList.setFont( JSystemFonts.FONT );
142     availableList.setFont( JSystemFonts.FONT );
143
144     operationsBuilder.setDefaultDialogBorder();
145     builder.setDefaultDialogBorder();
146     
147     // Privacy label initializations
148
privacyLabel.setFont( JSystemFonts.FONT );
149     privacyLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "PRIVACY_LEVEL" ) );
150
151     // Privacy Combo initializations
152
privacyCombo.setModel( new DefaultComboBoxModel() );
153     privacyCombo.setFont( JSystemFonts.FONT );
154     privacyCombo.addActionListener( new java.awt.event.ActionListener JavaDoc() {
155                                       public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
156                                         privacyComboActionPerformed( evt );
157                                       }
158                                     } );
159
160     // Available users label initializations
161
availableLabel.setFont( JSystemFonts.FONT );
162     availableLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "AVAILABLE_USERS" ) );
163
164     // Selected users label initializations
165
selectedLabel.setFont( JSystemFonts.FONT );
166     selectedLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "SELECTED_USERS" ) );
167
168     // Add Selected Button initialization
169
addSelectedButton.setText( ">" );
170     addSelectedButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "ADD_SELECTED_USERS" ) );
171     addSelectedButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
172                                            public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
173                                              operationsButtonActionPerformed( evt );
174                                            }
175                                          }
176                                        );
177
178     // Add All button initialization
179
addAllButton.setText( ">>" );
180     addAllButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "ADD_ALL_USERS" ) );
181     addAllButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
182                                       public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
183                                         operationsButtonActionPerformed( evt );
184                                       }
185                                     }
186                                   );
187     
188     // Remove selected button initialization
189
removeSelectedButton.setText( "<" );
190     removeSelectedButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE_SELECTED_USERS" ) );
191     removeSelectedButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
192                                               public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
193                                                 operationsButtonActionPerformed( evt );
194                                               }
195                                             }
196                                           );
197     
198     // Remove all button initialization
199
removeAllButton.setText( "<<" );
200     removeAllButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE_ALL_USERS" ) );
201     removeAllButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
202                                          public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
203                                            operationsButtonActionPerformed( evt );
204                                          }
205                                        }
206                                      );
207
208     // Add all buttons to button panel
209
operationsBuilder.add( addSelectedButton, cellConst.xy( 1, 1 ) );
210     operationsBuilder.add( addAllButton, cellConst.xy( 1, 3 ) );
211     operationsBuilder.add( removeSelectedButton, cellConst.xy( 1, 5 ) );
212     operationsBuilder.add( removeAllButton, cellConst.xy( 1, 7 ) );
213
214     // Add all components to layout manager
215
builder.add( privacyLabel, cellConst.xyw( 1, 1, 2 ) );
216     builder.add( privacyCombo, cellConst.xyw( 1, 2, 2 ) );
217     builder.add( availableLabel, cellConst.xy( 1, 4 ) );
218     builder.add( availableListPane, cellConst.xy( 1, 5 ) );
219     builder.add( selectedLabel, cellConst.xy( 3, 4 ) );
220     builder.add( selectedListPane, cellConst.xy( 3, 5 ) );
221     builder.add( operationsBuilder.getPanel(), cellConst.xy( 2, 5 ) );
222   }
223   
224   /**
225   * Initialize the component's panel to show on
226   * preferences window.
227   */

228   public void onShow() {
229
230     if( getComponentCount() == 0 )
231       add( builder.getPanel() );
232   }
233
234   /**
235    * Called when Apply/Ok button is triggered.
236    * Store the new configuration to PM database.
237    */

238   public boolean onApply() {
239
240     if( plugin != null ) {
241       if( !plugin.isConnected() ) {
242         JOptionPane.showMessageDialog( this,
243                                        JSharedObjects.getLanguageManager().getStringEx( "DISCONNECTED_PLUGIN_WARNING" ),
244                                        JSharedObjects.getLanguageManager().getStringEx( "INFORMATION" ),
245                                        JOptionPane.INFORMATION_MESSAGE );
246         return false;
247       }
248       else
249         plugin.fireOnChangePrivacy( aContactList );
250     }
251     
252     return true;
253   }
254
255   /**
256    * Called when cancel button is triggered.
257    */

258   public void onCancel() {
259     
260     fillListBoxes();
261   }
262
263   /**
264    * Called by plugin manager when LAF is changed
265    * by user;
266    */

267   public void onLookAndFeelChanged() {
268
269     SwingUtilities.updateComponentTreeUI( this );
270   }
271
272   /**
273    * Called by plugin manager when LAF is changed
274    * by user;
275    */

276   public void onLanguageChanged( String JavaDoc strCountryCode ) {
277
278     privacyLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "PRIVACY_LEVEL" ) );
279     availableLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "AVAILABLE_USERS" ) );
280     selectedLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "SELECTED_USERS" ) );
281     addSelectedButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "ADD_SELECTED_USERS" ) );
282     addAllButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "ADD_ALL_USERS" ) );
283     removeSelectedButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE_SELECTED_USERS" ) );
284     removeAllButton.setToolTipText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE_ALL_USERS" ) );
285     fillPrivacyCombo();
286   }
287
288   /**
289    * Set the plugin id for selected Panel.
290    * @param nPluginId The new plugin id;
291    */

292   public void setPluginId( int nPluginId ) {
293
294     if( this.nPluginId != nPluginId ) {
295       this.nPluginId = nPluginId;
296       plugin = JSharedObjects.getPluginEngine().get( nPluginId );
297
298       if( plugin != null ) {
299         fillPrivacyCombo();
300         fillListBoxes();
301       }
302     }
303   }
304   
305   /**
306    * Fill the privacy combo box.
307    */

308   private void fillPrivacyCombo() {
309
310     boolean bEnableControls = true;
311
312
313     ( ( DefaultComboBoxModel ) privacyCombo.getModel() ).removeAllElements();
314     
315     if( plugin == null )
316       return;
317
318     if( plugin.getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_BLOCK_CONTACT ) )
319       ( ( DefaultComboBoxModel ) privacyCombo.getModel() ).addElement( new ComboItem( ComboItem.TYPE_BLOCKED_LIST, JSharedObjects.getLanguageManager().getStringEx( "BLOCKED_LIST" ) ) );
320     //( ( DefaultComboBoxModel ) privacyCombo.getModel() ).addElement( new ComboItem( ComboItem.TYPE_VISIBLE_LIST, JSharedObjects.getLanguageManager().getStringEx( "VISIBLE_LIST" ) ) );
321
}
322   
323   /**
324    * Fill all privacy lists.
325    * This is a variable list and depends of
326    * privacy combo selection.
327    */

328   private void fillListBoxes() {
329
330     ComboItem comboItem = ( ComboItem ) privacyCombo.getModel().getSelectedItem();
331
332
333     ( ( DefaultListModel ) availableList.getModel() ).removeAllElements();
334     ( ( DefaultListModel ) selectedList.getModel() ).removeAllElements();
335         
336     if( comboItem != null ) {
337
338       Hashtable hContactList = ( ( Hashtable )( JSharedObjects.getContactListManager().getContactList( plugin ) ) );
339
340       if( hContactList == null )
341         return;
342
343       aContactList = hContactList.values().toArray();
344
345       if( aContactList != null ) {
346         JContactListItem item;
347         boolean bAvailable;
348
349         for( int nCount = 0; nCount < aContactList.length; nCount++ ) {
350
351           item = ( JContactListItem ) aContactList[nCount];
352           bAvailable = false;
353
354           switch( comboItem.getItemType() ) {
355
356             case ComboItem.TYPE_VISIBLE_LIST : {
357                                                   if( !item.getVisible() )
358                                                     bAvailable = true;
359                                                 } break;
360
361             case ComboItem.TYPE_BLOCKED_LIST : {
362                                                   if( !item.getBlocked() )
363                                                     bAvailable = true;
364                                                 } break;
365           }
366
367           if( bAvailable )
368             ( ( DefaultListModel ) availableList.getModel() ).addElement( item );
369           else
370             ( ( DefaultListModel ) selectedList.getModel() ).addElement( item );
371         }
372       }
373     }
374   }
375   
376   /**
377    * Implements the onClick event for all operations
378    * buttons (add select, add all, remove selected, remove all).
379    * @param evt The button event object;
380    */

381   private void operationsButtonActionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
382
383     Object JavaDoc obj = privacyCombo.getModel().getSelectedItem();
384     int nSelectedIndex = ( ( DefaultComboBoxModel ) privacyCombo.getModel() ).getIndexOf( obj );
385
386
387     JSharedObjects.getKernelManager().getPreferencesManager().EnableApplyPreferencesManager( true );
388
389     // Processing Add selected button
390
if( evt.getSource() == addSelectedButton ) {
391
392       DefaultListModel selectedModel = ( ( DefaultListModel ) selectedList.getModel() );
393       DefaultListModel availableModel = ( ( DefaultListModel ) availableList.getModel() );
394
395
396       if( availableList.getSelectedValues().length > 0 ) {
397         Object JavaDoc selectedValues[] = availableList.getSelectedValues();
398
399         for( int nCount = 0; nCount < selectedValues.length; nCount++ ) {
400           selectedModel.addElement( selectedValues[nCount] );
401           availableModel.removeElement( selectedValues[nCount] );
402
403           if( nSelectedIndex == 0 )
404             ( ( JContactListItem ) selectedValues[nCount] ).setVisible( true );
405           else
406             ( ( JContactListItem ) selectedValues[nCount] ).setBlocked( true );
407         }
408       }
409
410       return;
411     }
412
413     // Processing Add all button
414
if( evt.getSource() == addAllButton ) {
415
416       DefaultListModel availableModel = ( ( DefaultListModel ) availableList.getModel() );
417       DefaultListModel selectedModel = ( ( DefaultListModel ) selectedList.getModel() );
418
419
420       if( availableModel.size() > 0 ) {
421
422         Object JavaDoc item;
423
424         for( int nCount = 0; nCount < availableModel.size(); nCount++ ) {
425           item = availableModel.get( nCount );
426           selectedModel.addElement( item );
427          
428           if( nSelectedIndex == 0 )
429             ( ( JContactListItem ) item ).setVisible( true );
430           else
431             ( ( JContactListItem ) item ).setBlocked( true );
432         }
433
434         availableModel.removeAllElements();
435       }
436       
437       return;
438     }
439
440     // Processing Remove selected button
441
if( evt.getSource() == removeSelectedButton ) {
442
443       DefaultListModel availableModel = ( ( DefaultListModel ) availableList.getModel() );
444       DefaultListModel selectedModel = ( ( DefaultListModel ) selectedList.getModel() );
445
446       
447       if( selectedList.getSelectedValues().length > 0 ) {
448         Object JavaDoc selectedValues[] = selectedList.getSelectedValues();
449         
450         for( int nCount = 0; nCount < selectedValues.length; nCount++ ) {
451           availableModel.addElement( selectedValues[nCount] );
452           selectedModel.removeElement( selectedValues[nCount] );
453           
454           if( nSelectedIndex == 0 )
455             ( ( JContactListItem ) selectedValues[nCount] ).setVisible( false );
456           else
457             ( ( JContactListItem ) selectedValues[nCount] ).setBlocked( false );
458         }
459       }
460       
461       return;
462     }
463
464     // Processing Remove all button
465
if( evt.getSource() == removeAllButton ) {
466
467       DefaultListModel availableModel = ( ( DefaultListModel ) availableList.getModel() );
468       DefaultListModel selectedModel = ( ( DefaultListModel ) selectedList.getModel() );
469
470
471       if( selectedModel.size() > 0 ) {
472         
473         Object JavaDoc item;
474         
475         for( int nCount = 0; nCount < selectedModel.size(); nCount++ ) {
476           item = selectedModel.get( nCount );
477           availableModel.addElement( item );
478           
479           if( nSelectedIndex == 0 )
480             ( ( JContactListItem ) item ).setVisible( false );
481           else
482             ( ( JContactListItem ) item ).setBlocked( false );
483         }
484
485         selectedModel.removeAllElements();
486       }
487       
488       return;
489     }
490   }
491
492   /**
493    * Implements the ActionPerformed event of
494    * ok provacy combobox
495    * @param evt The combo event object;
496    */

497   private void privacyComboActionPerformed( ActionEvent evt ) {
498     
499     fillListBoxes();
500   }
501 }
502
503 // JPrivacyControlPanel class
Popular Tags