1 29 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 ; 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 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 (); 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 pluginInfo.setFont( JSystemFonts.FONT ); 116 pluginInfo.setEditable( false ); 117 pluginInfo.setEditorKit( JTextPane.createEditorKitForContentType( "text/html" ) ); 118 119 addButton.setFont( JSystemFonts.FONT ); 121 addButton.setPreferredSize( new java.awt.Dimension ( 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 () { 126 public void actionPerformed( java.awt.event.ActionEvent evt ) { 127 setInstallPlugin( true ); 128 } 129 } ); 130 131 removeButton.setFont( JSystemFonts.FONT ); 133 removeButton.setPreferredSize( new java.awt.Dimension ( 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 () { 138 public void actionPerformed( java.awt.event.ActionEvent evt ) { 139 setInstallPlugin( false ); 140 } 141 } ); 142 143 btnsPanel.setLayout( new java.awt.GridBagLayout () ); 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 171 public void onShow() { 172 173 if( getComponentCount() == 0 ) 174 add( splitPane ); 175 176 fillAvailablePlugins(); 177 } 178 179 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 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 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 if( nChildCount > 0 ) 214 JSharedObjects.getPluginEngine().loadAllPlugins( JSharedObjects.getProfileManager().getProfileId(), true ); 215 216 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 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 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 257 public void onLookAndFeelChanged() { 258 259 SwingUtilities.updateComponentTreeUI( this ); 260 SwingUtilities.updateComponentTreeUI( pluginsTree ); 261 SwingUtilities.updateComponentTreeUI( pluginInfo ); 262 pluginsTree.renewRenderer(); 263 } 264 265 269 public void onLanguageChanged( String 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 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 ioe ) { 324 System.err.println( "JAvailablePluginsPanel.valueChanged() - " + ioe ); 325 } 326 } 327 else { 328 addButton.setEnabled( false ); 329 removeButton.setEnabled( false ); 330 } 331 } 332 333 337 private void fillAvailablePlugins() { 338 339 int nPluginId; 340 boolean bNoAvail = true; 341 long nProfileId = JSharedObjects.getProfileManager().getProfileId(); 342 java.sql.ResultSet rs = JSharedObjects.getProfileManager().getAllPlugins().getResultSet(); 343 344 345 JSharedObjects.getPluginEngine().loadAllPlugins( nProfileId, false ); 346 347 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 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 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 | Popular Tags |