KickJava   Java API By Example, From Geeks To Geeks.

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


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: JAvailablePluginsPanel.java,v 1.24 2007/02/23 21:54:23 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.24 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.forms.preferences;
40
41 import java.awt.*;
42 import javax.swing.*;
43 import javax.swing.tree.*;
44 import javax.swing.event.*;
45 import javax.swing.text.*;
46 import javax.swing.text.html.*;
47 import java.util.*;
48 import java.awt.Dimension JavaDoc;
49 import javax.swing.event.*;
50 import javax.swing.border.*;
51 import com.jgoodies.forms.builder.*;
52 import com.jgoodies.forms.layout.*;
53 import com.jgoodies.forms.debug.*;
54 import org.planetamessenger.mos.engine.*;
55 import org.planetamessenger.mos.forms.*;
56 import org.planetamessenger.mos.ui.*;
57 import org.planetamessenger.plugin.*;
58 import org.planetamessenger.ui.*;
59
60
61 public class JAvailablePluginsPanel extends JPluginPreferencesContainer implements TreeSelectionListener {
62
63   private final int NO_AVAILABLE_PLUGINS = -1000;
64   
65   private JPanel btnsPanel = null;
66   private JButton addButton = null;
67   private JButton removeButton = null;
68   private JProtocolTree pluginsTree = null;
69   private JScrollPane pluginsTreePane = null;
70   private JScrollPane pluginInfoPane = null;
71   private JTextPane pluginInfo = null;
72   private JSplitPane splitPane = null;
73   private JMOSPreferencesDlg parentDlg = null;
74   
75   
76   /**
77    * Constructor. Initialize all class data.
78    * @parentDlg The Options tree of preferences window;
79    */

80   public JAvailablePluginsPanel( JMOSPreferencesDlg parentDlg ) {
81
82     super();
83
84     FormLayout layout = new FormLayout( "fill:170dlu:grow, fill:80dlu:grow", "fill:180dlu" );
85     FormLayout layout2 = new FormLayout( "fill:250dlu:grow", "fill:160dlu" );
86     PanelBuilder builder = new PanelBuilder( layout );
87     PanelBuilder builder2 = new PanelBuilder( layout2 );
88     CellConstraints cellConst = new CellConstraints();
89     GridBagConstraints btnsConst = new java.awt.GridBagConstraints JavaDoc();
90     DefaultTreeCellRenderer renderer = null;
91
92     btnsPanel = new JPanel();
93     pluginInfo = new JTextPane();
94     pluginInfoPane = new JScrollPane( pluginInfo );
95     pluginsTree = new JProtocolTree();
96     pluginsTreePane = new JScrollPane( pluginsTree );
97     addButton = new JButton();
98     removeButton = new JButton();
99     splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, builder.getPanel(), builder2.getPanel() );
100     renderer = ( DefaultTreeCellRenderer ) pluginsTree.getCellRenderer();
101     this.parentDlg = parentDlg;
102     
103     builder.setDefaultDialogBorder();
104     builder2.setDefaultDialogBorder();
105
106     pluginsTree.putClientProperty( "JTree.lineStyle", "Angled" );
107     pluginsTree.addTreeSelectionListener( this );
108     pluginsTree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
109
110     renderer.setLeafIcon( null );
111     renderer.setOpenIcon( null );
112     renderer.setClosedIcon( null );
113
114     // Plugin Info pane setup
115
pluginInfo.setFont( JSystemFonts.FONT );
116     pluginInfo.setEditable( false );
117     pluginInfo.setEditorKit( JTextPane.createEditorKitForContentType( "text/html" ) );
118
119     // Add button setup
120
addButton.setFont( JSystemFonts.FONT );
121     addButton.setPreferredSize( new java.awt.Dimension JavaDoc( JResources.DEFAULT_BUTTON_WIDTH, JResources.DEFAULT_BUTTON_HEIGHT ) );
122     addButton.setMinimumSize( addButton.getPreferredSize() );
123     addButton.setIcon( JSharedObjects.getResources().getIcon( JResources.ADD_PLUGIN_ICON ) );
124     addButton.setEnabled( false );
125     addButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
126                                    public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
127                                      setInstallPlugin( true );
128                                    }
129                                  } );
130     
131     // Remove button setup
132
removeButton.setFont( JSystemFonts.FONT );
133     removeButton.setPreferredSize( new java.awt.Dimension JavaDoc( JResources.DEFAULT_BUTTON_WIDTH, JResources.DEFAULT_BUTTON_HEIGHT ) );
134     removeButton.setMinimumSize( removeButton.getPreferredSize() );
135     removeButton.setIcon( JSharedObjects.getResources().getIcon( JResources.REMOVE_PLUGIN_ICON ) );
136     removeButton.setEnabled( false );
137     removeButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
138                                       public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
139                                         setInstallPlugin( false );
140                                       }
141                                     } );
142
143     // Buttons panel setup
144
btnsPanel.setLayout( new java.awt.GridBagLayout JavaDoc() );
145     
146     btnsConst.gridx = 0;
147     btnsConst.gridy = 0;
148     btnsConst.gridwidth = GridBagConstraints.REMAINDER;
149     btnsConst.gridheight = 1;
150     btnsConst.weightx = 0.0001;
151     btnsConst.weighty = 0.0001;
152     btnsConst.insets = new Insets( 3, 3, 3, 3 );
153     btnsConst.fill = GridBagConstraints.HORIZONTAL;
154     btnsConst.anchor = GridBagConstraints.NORTH;
155     btnsPanel.add( addButton, btnsConst );
156     
157     btnsConst.gridy++;
158     btnsConst.gridwidth = java.awt.GridBagConstraints.REMAINDER;
159     btnsConst.weighty = 1.0;
160     btnsPanel.add( removeButton, btnsConst );
161     
162     builder.add( pluginsTreePane, cellConst.xy( 1, 1 ) );
163     builder.add( btnsPanel, cellConst.xy( 2, 1 ) );
164     builder2.add( pluginInfoPane, cellConst.xy( 1, 1 ) );
165   }
166
167 /**
168   * Initialize the component's panel to show on
169   * preferences window.
170   */

171   public void onShow() {
172
173     if( getComponentCount() == 0 )
174       add( splitPane );
175
176     fillAvailablePlugins();
177   }
178
179   /**
180    * Called when Apply/Ok button is triggered.
181    * Store the new configuration to PM database.
182    */

183   public boolean onApply() {
184
185     ArrayList<JProtocolTreeItem> aNodes = new ArrayList<JProtocolTreeItem>();
186     TreeModel treeModel = pluginsTree.getModel();
187     DefaultMutableTreeNode rootNode = ( DefaultMutableTreeNode ) treeModel.getRoot();
188     int nChildCount = treeModel.getChildCount( rootNode );
189
190
191     // Reset the plugin's install status of tree component
192
for( int nCount = 0; nCount < nChildCount; nCount++ ) {
193       DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) treeModel.getChild( rootNode, nCount );
194       JProtocolTreeItem item = ( JProtocolTreeItem ) node.getUserObject();
195
196       if( item.getInstall() ) {
197         item.setInstall( false );
198         aNodes.add( item );
199       }
200     }
201
202     nChildCount = aNodes.size();
203
204     // Update UI and add selected option to PM database
205
for( int nCount = 0; nCount < nChildCount; nCount++ ) {
206       JProtocolTreeItem item = ( JProtocolTreeItem ) aNodes.get( nCount );
207
208       pluginsTree.removeNode( item );
209       JSharedObjects.getProfileManager().addPlugin( item.getPluginId(), JSharedObjects.getProfileManager().getProfileId() );
210     }
211     
212     // Load all plugins to plugin engine but no start plugins
213
if( nChildCount > 0 )
214       JSharedObjects.getPluginEngine().loadAllPlugins( JSharedObjects.getProfileManager().getProfileId(), true );
215
216     // There's no available items ??
217
if( pluginsTree.getChildCount() == 0 ) {
218       JProtocolTreeItem item = new JProtocolTreeItem( JSharedObjects.getLanguageManager().getStringEx( "NO_AVAILABLE_PLUGINS" ), null, -1 );
219       pluginsTree.addNode( item );
220     }
221
222     pluginsTree.setSelectionRow( 0 );
223     addButton.setEnabled( false );
224     removeButton.setEnabled( false );
225     parentDlg.fillInstalledPluginsOptionsTree();
226     
227     return true;
228   }
229   
230   /**
231    * Called when cancel button is triggered.
232    */

233   public void onCancel() {
234
235     TreeModel treeModel = pluginsTree.getModel();
236     DefaultMutableTreeNode rootNode = ( DefaultMutableTreeNode ) treeModel.getRoot();
237     int nChildCount = pluginsTree.getModel().getChildCount( rootNode );
238
239
240     // Reset the plugin's install status of tree component
241
for( int nCount = 0; nCount < nChildCount; nCount++ ) {
242       DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) treeModel.getChild( rootNode, nCount );
243       JProtocolTreeItem item = ( JProtocolTreeItem ) node.getUserObject();
244       
245       item.setInstall( false );
246     }
247
248     pluginsTree.setSelectionRow( 0 );
249     addButton.setEnabled( false );
250     removeButton.setEnabled( false );
251   }
252
253   /**
254    * Called by plugin manager when LAF is changed
255    * by user;
256    */

257   public void onLookAndFeelChanged() {
258
259     SwingUtilities.updateComponentTreeUI( this );
260     SwingUtilities.updateComponentTreeUI( pluginsTree );
261     SwingUtilities.updateComponentTreeUI( pluginInfo );
262     pluginsTree.renewRenderer();
263   }
264
265   /**
266    * Called by plugin manager when LAF is changed
267    * by user;
268    */

269   public void onLanguageChanged( String JavaDoc strCountryCode ) {
270
271     JProtocolTreeItem item = new JProtocolTreeItem( JSharedObjects.getLanguageManager().getStringEx( "NO_AVAILABLE_PLUGINS" ), null, NO_AVAILABLE_PLUGINS );
272     
273     if( pluginsTree.removeNode( item ) )
274       pluginsTree.addNode( item );
275     
276     removeButton.setText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE" ) );
277     addButton.setText( JSharedObjects.getLanguageManager().getStringEx( "ADD" ) );
278     pluginsTree.updateRootTreeLabel( JSharedObjects.getLanguageManager().getStringEx( "PLUGINS" ) );
279   }
280   
281   /**
282    * Implements the valueChanged of TreeSelectionListener
283    * interface.<br>
284    * Update the Plugin Info panel depending on selection;
285    * item selected;<br>
286    * @param See Jdk docs;
287    */

288    public void valueChanged( TreeSelectionEvent treeSelectionEvent ) {
289
290      TreePath path = treeSelectionEvent.getPath();
291      DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) path.getLastPathComponent();
292      JProtocolTreeItem item = ( JProtocolTreeItem ) node.getUserObject();
293
294      pluginInfo.setText( "" );
295
296      if( item.getPluginId() != -1 ) {
297        JPluginProperties properties = JSharedObjects.getPluginEngine().getPluginPropertiesByClassName( item.getClassName() );
298        HTMLDocument doc = ( HTMLDocument ) pluginInfo.getDocument();
299        HTMLEditorKit html = ( HTMLEditorKit ) pluginInfo.getEditorKit();
300
301
302        if( properties == null )
303          return;
304        
305        if( item.getInstall() ) {
306          addButton.setEnabled( false );
307          removeButton.setEnabled( true );
308        }
309        else {
310          addButton.setEnabled( true );
311          removeButton.setEnabled( false );
312        }
313
314        try {
315          html.insertHTML( doc, doc.getLength(), "<strong>" + properties.getName() + "</strong><p>" , 0, 0, null );
316          html.insertHTML( doc, doc.getLength(), "<b>" + JSharedObjects.getLanguageManager().getStringEx( "VERSION_FULL" ) + " : </b>" + properties.getVersion(), 0, 0, null );
317          html.insertHTML( doc, doc.getLength(), "<b>" + JSharedObjects.getLanguageManager().getStringEx( "OWNER" ) + " : </b>" + properties.getPluginOwner(), 0, 0, null );
318          html.insertHTML( doc, doc.getLength(), "<b>" + JSharedObjects.getLanguageManager().getStringEx( "LICENSE" ) + " : </b>" + properties.getLicense(), 0, 0, null );
319          html.insertHTML( doc, doc.getLength(), "<b>" + JSharedObjects.getLanguageManager().getStringEx( "HOME_PAGE" ) + " : </b>" + properties.getHomePage(), 0, 0, null );
320          html.insertHTML( doc, doc.getLength(), "<b>" + JSharedObjects.getLanguageManager().getStringEx( "DESCRIPTION" ) + " : </b>" + properties.getDescription(), 0, 0, null );
321        } catch( BadLocationException e ) {
322          System.err.println( "JAvailablePluginsPanel.valueChanged() - " + e );
323        } catch( java.io.IOException JavaDoc ioe ) {
324          System.err.println( "JAvailablePluginsPanel.valueChanged() - " + ioe );
325        }
326      }
327      else {
328        addButton.setEnabled( false );
329        removeButton.setEnabled( false );
330      }
331   }
332
333   /**
334    * Fill all available plugins into protocol
335    * tree view.
336    */

337   private void fillAvailablePlugins() {
338     
339     int nPluginId;
340     boolean bNoAvail = true;
341     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
342     java.sql.ResultSet JavaDoc rs = JSharedObjects.getProfileManager().getAllPlugins().getResultSet();
343     
344
345     JSharedObjects.getPluginEngine().loadAllPlugins( nProfileId, false );
346
347     /*
348      * Search for all uninstalled plugins
349      * for this profile and put it on the Tree
350      * list.
351      */

352     try {
353       while( rs.next() ) {
354
355         nPluginId = rs.getInt( "plugin_id" );
356
357         if( !JSharedObjects.getProfileManager().checkPluginInstall( nPluginId, nProfileId ) ) {
358           JPluginProperties properties = JSharedObjects.getPluginEngine().getPluginPropertiesByClassName( rs.getString( "class_name" ) );
359           JProtocolTreeItem item = new JProtocolTreeItem( properties.getName(), rs.getString( "class_name" ), nPluginId );
360
361           pluginsTree.removeNode( item );
362           pluginsTree.addNode( item );
363           bNoAvail = false;
364         }
365       }
366     } catch( java.sql.SQLException JavaDoc e ) {
367       System.out.println( "JAvailablePluginsPanel.fillAvailablePlugins() - " + e );
368     }
369
370     if( bNoAvail ) {
371       JProtocolTreeItem item = new JProtocolTreeItem( JSharedObjects.getLanguageManager().getStringEx( "NO_AVAILABLE_PLUGINS" ), null, NO_AVAILABLE_PLUGINS );
372       pluginsTree.removeNode( item );
373       pluginsTree.addNode( item );
374     }
375     
376     pluginsTree.repaint();
377     pluginsTree.expandRow( 0 );
378   }
379
380   /**
381    * Set the selected plugin to be installed.
382    * @param bInstall The new install flag to selected plugin item;
383    */

384   private void setInstallPlugin( boolean bInstall ) {
385     
386     if( !pluginsTree.isSelectionEmpty() ) {
387       
388       TreePath path = pluginsTree.getSelectionPath();
389       
390       if( path != null ) {
391         DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) path.getLastPathComponent();
392         JProtocolTreeItem item = ( JProtocolTreeItem ) node.getUserObject();
393         
394         item.setInstall( bInstall );
395         pluginsTree.repaint();
396         addButton.setEnabled( !bInstall );
397         removeButton.setEnabled( bInstall );
398         JSharedObjects.getKernelManager().getPreferencesManager().EnableApplyPreferencesManager( true );
399       }
400     }
401   }
402 }
403
404 // JAvailablePluginsPanel class
405
Popular Tags