directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1693834 - in /directory/studio/trunk/plugins/connection.ui: ./ src/main/java/org/apache/directory/studio/connection/ui/widgets/ src/main/java/org/apache/directory/studio/utils/
Date Sun, 02 Aug 2015 22:34:16 GMT
Author: elecharny
Date: Sun Aug  2 22:34:15 2015
New Revision: 1693834

URL: http://svn.apache.org/r1693834
Log:
o Some PMD fixes
o Added a dependency on ldap API utils

Modified:
    directory/studio/trunk/plugins/connection.ui/pom-first.xml
    directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/CertificateListComposite.java
    directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/Messages.java
    directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/utils/ActionUtils.java

Modified: directory/studio/trunk/plugins/connection.ui/pom-first.xml
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/connection.ui/pom-first.xml?rev=1693834&r1=1693833&r2=1693834&view=diff
==============================================================================
--- directory/studio/trunk/plugins/connection.ui/pom-first.xml (original)
+++ directory/studio/trunk/plugins/connection.ui/pom-first.xml Sun Aug  2 22:34:15 2015
@@ -68,6 +68,7 @@
             
             <Require-Bundle>
  org.apache.directory.api.ldap.model;bundle-version="${org.apache.directory.api.bundleversion}",
+ org.apache.directory.api.util;bundle-version="${org.apache.directory.api.bundleversion}",
  org.apache.directory.studio.common.core,
  org.apache.directory.studio.common.ui,
  org.apache.directory.studio.connection.core,

Modified: directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/CertificateListComposite.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/CertificateListComposite.java?rev=1693834&r1=1693833&r2=1693834&view=diff
==============================================================================
--- directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/CertificateListComposite.java
(original)
+++ directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/CertificateListComposite.java
Sun Aug  2 22:34:15 2015
@@ -29,6 +29,7 @@ import java.security.cert.X509Certificat
 import java.util.Iterator;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.directory.api.util.Strings;
 import org.apache.directory.studio.common.ui.widgets.BaseWidgetUtils;
 import org.apache.directory.studio.connection.core.StudioKeyStoreManager;
 import org.apache.directory.studio.connection.ui.ConnectionUIConstants;
@@ -38,6 +39,7 @@ import org.apache.directory.studio.conne
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -61,26 +63,191 @@ import org.eclipse.swt.widgets.FileDialo
 
 /**
  * This composite displays a list of certificates and buttons
- * to add, delete and view certificates.
- *
+ * to add, delete and view certificates :
+ * 
+ * <pre>
+ * +-------------------------------------------------+
+ * | +------------------------------------+          |
+ * | | abc                                | (View)   |
+ * | | xyz                                | (Add)    |
+ * | |                                    | (Remove) |
+ * | |                                    | (Export) |
+ * | +------------------------------------+          |
+ * +-------------------------------------------------+
+ * </pre>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class CertificateListComposite extends Composite
 {
+    /** The KeyStore wrapper */
     private StudioKeyStoreManager keyStoreManager;
-    private Composite container;
+    
+    /** The table containing the KeyStore elements */
     private TableViewer tableViewer;
+    
+    /** The View action button */
     private Button viewButton;
+
+    /** The Add action button */
     private Button addButton;
+    
+    /** The Remove action button */
     private Button removeButton;
+    
+    /** The Export action button */
     private Button exportButton;
 
+    /**
+     * The listener called when the table selection has changed
+     */
+    private ISelectionChangedListener tableViewerSelectionListener = new ISelectionChangedListener()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void selectionChanged( SelectionChangedEvent event )
+        {
+            // Enable and disable the button accordingly to the selection :
+            // - 1 line selected : enable remove, view and export
+            // - N lines selected : enable remove, disable view and export
+            // - nothing selected, disable view, remove and exprt
+            viewButton.setEnabled( ( ( IStructuredSelection ) event.getSelection() ).size()
== 1 );
+            removeButton.setEnabled( !event.getSelection().isEmpty() );
+            exportButton.setEnabled( ( ( IStructuredSelection ) event.getSelection() ).size()
== 1 );
+        }
+    };
+    
+    
+    /**
+     * The listener called when a line is double-clicked in the table : we will
+     * open the Certificate dialog.
+     */
+    private IDoubleClickListener tableViewerDoubleClickListener = new IDoubleClickListener()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void doubleClick( DoubleClickEvent event )
+        {
+            openCertificate( event.getSelection() );
+        }
+    };
+    
+    
+    /**
+     * A selection listener on the View button : we will open the Certificate Dialog
+     */
+    private SelectionAdapter viewButtonSelectionListener = new SelectionAdapter()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void widgetSelected( SelectionEvent event )
+        {
+            openCertificate( tableViewer.getSelection() );
+        }
+    };
+    
+    
+    /**
+     * A selection listener on the Add button : we will open the File Dialog
+     * and let the user select the KeyStore location to add in the table
+     */
+    private SelectionAdapter addButtonSelectionListener = new SelectionAdapter()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void widgetSelected( SelectionEvent event )
+        {
+            // Asking the user for the certificate file
+            FileDialog dialog = new FileDialog( getShell(), SWT.OPEN );
+            dialog.setText( Messages.getString( "CertificateListComposite.LoadCertificate"
) ); //$NON-NLS-1$
+            String returnedFileName = dialog.open();
 
+            if ( returnedFileName != null )
+            {
+                try
+                {
+                    // Reading the certificate
+                    X509Certificate certificate = generateCertificate( FileUtils.readFileToByteArray(
new File(
+                        returnedFileName ) ) );
+
+                    // Adding the certificate
+                    keyStoreManager.addCertificate( certificate );
+
+                    // Refreshing the table viewer
+                    tableViewer.refresh();
+                    tableViewer.setSelection( new StructuredSelection( certificate ) );
+                }
+                catch ( Exception ex )
+                {
+                    MessageDialog.openError( addButton.getShell(),
+                        Messages.getString( "CertificateListComposite.ErrorDialogTitle" ),
//$NON-NLS-1$
+                        NLS.bind( Messages.getString( "CertificateListComposite.ErrorDialogMessage"
), //$NON-NLS-1$
+                            ex.getMessage() ) );
+                }
+            }
+        }
+    };
+    
+    
+    /**
+     * A selection listener on the Remove button.
+     */
+    private SelectionAdapter removeButtonSelectionListener = new SelectionAdapter()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void widgetSelected( SelectionEvent event )
+        {
+            IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
+            Iterator<?> iterator = selection.iterator();
+            
+            while ( iterator.hasNext() )
+            {
+                X509Certificate certificate = ( X509Certificate ) iterator.next();
+                
+                try
+                {
+                    keyStoreManager.removeCertificate( certificate );
+                }
+                catch ( CertificateException ce )
+                {
+                    throw new RuntimeException( ce );
+                }
+            }
+            
+            tableViewer.refresh();
+        }
+    };
+    
+    
+    /**
+     * A selection listener on the Export button. We will open the Export wizard.
+     */
+    private SelectionAdapter exportButtonSelectionListener = new SelectionAdapter()
+    {
+        /**
+         * {@inheritDoc}
+         */
+        public void widgetSelected( SelectionEvent event )
+        {
+            X509Certificate certificate = ( X509Certificate ) ( ( IStructuredSelection )
tableViewer.getSelection() )
+                .getFirstElement();
+
+            WizardDialog dialog = new WizardDialog( getShell(), new ExportCertificateWizard(
certificate ) );
+            dialog.open();
+        }
+    };
+
+    
     /**
      * Creates a new instance of CertificateInfoComposite.
      *
-     * @param parent
-     * @param style
+     * @param parent The paren'ts composite
+     * @param style The widget's style
      */
     public CertificateListComposite( Composite parent, int style )
     {
@@ -91,148 +258,75 @@ public class CertificateListComposite ex
         setLayout( layout );
         setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
 
-        container = new Composite( this, SWT.NONE );
+        Composite container = new Composite( this, SWT.NONE );
         layout = new GridLayout( 2, false );
         container.setLayout( layout );
         container.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, true )
);
 
-        createTreeViewer();
-        createButtons();
-    }
-
-
-    private void createTreeViewer()
-    {
+        // Create the Table Viewer
         tableViewer = new TableViewer( container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.BORDER );
-        GridData gd = new GridData( GridData.FILL, GridData.FILL, true, true );
-        gd.widthHint = 360;
-        gd.heightHint = 10;
-        tableViewer.getTable().setLayoutData( gd );
+        GridData gridData = new GridData( GridData.FILL, GridData.FILL, true, true );
+        gridData.widthHint = 360;
+        gridData.heightHint = 10;
+        tableViewer.getTable().setLayoutData( gridData );
         tableViewer.setContentProvider( new KeyStoreContentProvider() );
         tableViewer.setLabelProvider( new KeyStoreLabelProvider() );
-        tableViewer.addSelectionChangedListener( new ISelectionChangedListener()
-        {
-            public void selectionChanged( SelectionChangedEvent event )
-            {
-                viewButton.setEnabled( ( ( IStructuredSelection ) event.getSelection() ).size()
== 1 );
-                removeButton.setEnabled( !event.getSelection().isEmpty() );
-                exportButton.setEnabled( ( ( IStructuredSelection ) event.getSelection()
).size() == 1 );
-            }
-        } );
-        tableViewer.addDoubleClickListener( new IDoubleClickListener()
-        {
-            public void doubleClick( DoubleClickEvent event )
-            {
-                IStructuredSelection selection = ( IStructuredSelection ) event.getSelection();
-                X509Certificate certificate = ( X509Certificate ) selection.getFirstElement();
-                new CertificateInfoDialog( getShell(), new X509Certificate[]
-                    { certificate } ).open();
-            }
-        } );
+        tableViewer.addSelectionChangedListener( tableViewerSelectionListener );
+        tableViewer.addDoubleClickListener( tableViewerDoubleClickListener );
+
+        createButtons( container );
     }
 
 
-    private void createButtons()
+    /**
+     * Creates the 4 buttons for this widget :
+     * 
+     * <pre>
+     * (view)
+     * (Add)
+     * (Remove)
+     * (Export)
+     * </pre>
+     */
+    private void createButtons( Composite container )
     {
         Composite buttonContainer = BaseWidgetUtils.createColumnContainer( container, 1,
1 );
         buttonContainer.setLayoutData( new GridData( GridData.FILL, GridData.FILL, false,
false ) );
-        viewButton = BaseWidgetUtils.createButton( buttonContainer, Messages
+
+        // The View Button 
+        Button viewButton = BaseWidgetUtils.createButton( buttonContainer, Messages
             .getString( "CertificateListComposite.ViewButton" ), 1 );//$NON-NLS-1$
+        
         viewButton.setEnabled( false );
-        viewButton.addSelectionListener( new SelectionAdapter()
-        {
-            public void widgetSelected( SelectionEvent e )
-            {
-                IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
-                X509Certificate certificate = ( X509Certificate ) selection.getFirstElement();
-                new CertificateInfoDialog( getShell(), new X509Certificate[]
-                    { certificate } ).open();
-            }
-        } );
+        viewButton.addSelectionListener( viewButtonSelectionListener );
 
+        // The Add button
         addButton = BaseWidgetUtils.createButton( buttonContainer, Messages
             .getString( "CertificateListComposite.AddButton" ), 1 ); //$NON-NLS-1$
-        addButton.addSelectionListener( new SelectionAdapter()
-        {
-            public void widgetSelected( SelectionEvent e )
-            {
-                // Asking the user for the certificate file
-                FileDialog dialog = new FileDialog( getShell(), SWT.OPEN );
-                dialog.setText( Messages.getString( "CertificateListComposite.LoadCertificate"
) ); //$NON-NLS-1$
-                String returnedFileName = dialog.open();
-
-                if ( returnedFileName != null )
-                {
-                    try
-                    {
-                        // Reading the certificate
-                        X509Certificate certificate = generateCertificate( FileUtils.readFileToByteArray(
new File(
-                            returnedFileName ) ) );
-
-                        // Adding the certificate
-                        keyStoreManager.addCertificate( certificate );
-
-                        // Refreshing the table viewer
-                        tableViewer.refresh();
-                        tableViewer.setSelection( new StructuredSelection( certificate )
);
-                    }
-                    catch ( Exception ex )
-                    {
-                        MessageDialog.openError( addButton.getShell(),
-                            Messages.getString( "CertificateListComposite.ErrorDialogTitle"
), //$NON-NLS-1$
-                            NLS.bind( Messages.getString( "CertificateListComposite.ErrorDialogMessage"
), //$NON-NLS-1$
-                                ex.getMessage() ) );
-                    }
-                }
-            };
-        } );
+        addButton.addSelectionListener( addButtonSelectionListener );
 
+        // The remove button
         removeButton = BaseWidgetUtils.createButton( buttonContainer, Messages
             .getString( "CertificateListComposite.RemoveButton" ), 1 ); //$NON-NLS-1$
         removeButton.setEnabled( false );
-        removeButton.addSelectionListener( new SelectionAdapter()
-        {
-            public void widgetSelected( SelectionEvent e )
-            {
-                IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
-                Iterator<?> iterator = selection.iterator();
-                while ( iterator.hasNext() )
-                {
-                    X509Certificate certificate = ( X509Certificate ) iterator.next();
-                    try
-                    {
-                        keyStoreManager.removeCertificate( certificate );
-                    }
-                    catch ( CertificateException ce )
-                    {
-                        throw new RuntimeException( ce );
-                    }
-                }
-                tableViewer.refresh();
-            }
-        } );
-
+        removeButton.addSelectionListener( removeButtonSelectionListener );
+        
+        // The export button
         exportButton = BaseWidgetUtils.createButton( buttonContainer, Messages
             .getString( "CertificateListComposite.ExportButton" ), 1 ); //$NON-NLS-1$
         exportButton.setEnabled( false );
-        exportButton.addSelectionListener( new SelectionAdapter()
-        {
-            public void widgetSelected( SelectionEvent e )
-            {
-                X509Certificate certificate = ( X509Certificate ) ( ( IStructuredSelection
) tableViewer.getSelection() )
-                    .getFirstElement();
-
-                WizardDialog dialog = new WizardDialog( getShell(), new ExportCertificateWizard(
certificate ) );
-                dialog.open();
-            }
-        } );
+        exportButton.addSelectionListener( exportButtonSelectionListener );
     }
 
 
+    /**
+     * Creates a certificate from a byte[].
+     */
     private static X509Certificate generateCertificate( byte[] data ) throws CertificateException
     {
-        CertificateFactory cf = CertificateFactory.getInstance( "X.509" ); //$NON-NLS-1$
-        Certificate certificate = cf.generateCertificate( new ByteArrayInputStream( data
) );
+        CertificateFactory certificateFactory = CertificateFactory.getInstance( "X.509" );
//$NON-NLS-1$
+        Certificate certificate = certificateFactory.generateCertificate( new ByteArrayInputStream(
data ) );
+        
         if ( certificate instanceof X509Certificate )
         {
             return ( X509Certificate ) certificate;
@@ -253,10 +347,14 @@ public class CertificateListComposite ex
         tableViewer.setInput( keyStoreManager );
     }
 
-    class KeyStoreContentProvider implements IStructuredContentProvider
-    {
 
+    /**
+     * This class is used to give back the content of teh Table viewer
+     */
+    private class KeyStoreContentProvider implements IStructuredContentProvider
+    {
         /**
+         * Gets the list of Certificates stored into the selected KeyStore
          * {@inheritDoc}
          */
         public Object[] getElements( Object inputElement )
@@ -264,6 +362,7 @@ public class CertificateListComposite ex
             if ( inputElement instanceof StudioKeyStoreManager )
             {
                 StudioKeyStoreManager keyStoreManager = ( StudioKeyStoreManager ) inputElement;
+                
                 try
                 {
                     return keyStoreManager.getCertificates();
@@ -274,7 +373,7 @@ public class CertificateListComposite ex
                 }
             }
 
-            return null;
+            return new Object[]{};
         }
 
 
@@ -295,8 +394,17 @@ public class CertificateListComposite ex
 
     }
 
+    
+    /**
+     * This helper class is used to decorate the elements in the table : we use the certificate
name if any.
+     * @author elecharny
+     *
+     */
     class KeyStoreLabelProvider extends LabelProvider
     {
+        /**
+         * {@inheritDoc}
+         */
         public String getText( Object element )
         {
             if ( element instanceof X509Certificate )
@@ -304,13 +412,14 @@ public class CertificateListComposite ex
                 X509Certificate certificate = ( X509Certificate ) element;
 
                 String certificateName = certificate.getSubjectX500Principal().getName();
-                if ( ( certificateName != null ) && ( !"".equals( certificateName
) ) ) //$NON-NLS-1$
+                
+                if ( Strings.isEmpty( certificateName  ) )
                 {
-                    return certificateName;
+                    return Messages.getString( "CertificateListComposite.UntitledCertificate"
); //$NON-NLS-1$
                 }
                 else
                 {
-                    return Messages.getString( "CertificateListComposite.UntitledCertificate"
); //$NON-NLS-1$
+                    return certificateName;
                 }
             }
 
@@ -318,6 +427,9 @@ public class CertificateListComposite ex
         }
 
 
+        /**
+         * {@inheritDoc}
+         */
         public Image getImage( Object element )
         {
             if ( element instanceof X509Certificate )
@@ -328,4 +440,17 @@ public class CertificateListComposite ex
             return super.getImage( element );
         }
     }
-}
+    
+    
+    /**
+     * A private method that opens the Certificate Dialog
+     * @param selection
+     */
+    private void openCertificate( ISelection selection )
+    {
+        IStructuredSelection structuredSelection = ( IStructuredSelection ) selection;
+        X509Certificate certificate = ( X509Certificate ) structuredSelection.getFirstElement();
+        new CertificateInfoDialog( getShell(), new X509Certificate[]
+            { certificate } ).open();
+    }
+}
\ No newline at end of file

Modified: directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/Messages.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/Messages.java?rev=1693834&r1=1693833&r2=1693834&view=diff
==============================================================================
--- directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/Messages.java
(original)
+++ directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/connection/ui/widgets/Messages.java
Sun Aug  2 22:34:15 2015
@@ -29,8 +29,16 @@ import java.util.ResourceBundle;
  *  
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class Messages
+public final class Messages
 {
+    /**
+     * Make the constructor private to make this class an utility class
+     */
+    private Messages()
+    {
+    }
+    
+    
     /** The resource name */
     private static final ResourceBundle RESOURCE_BUNDLE = 
         ResourceBundle.getBundle( Messages.class.getPackage().getName() + ".messages" );

Modified: directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/utils/ActionUtils.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/utils/ActionUtils.java?rev=1693834&r1=1693833&r2=1693834&view=diff
==============================================================================
--- directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/utils/ActionUtils.java
(original)
+++ directory/studio/trunk/plugins/connection.ui/src/main/java/org/apache/directory/studio/utils/ActionUtils.java
Sun Aug  2 22:34:15 2015
@@ -21,6 +21,7 @@
 package org.apache.directory.studio.utils;
 
 
+import org.eclipse.core.commands.Command;
 import org.eclipse.core.commands.IHandler;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.commands.ActionHandler;
@@ -33,9 +34,16 @@ import org.eclipse.ui.commands.ICommandS
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class ActionUtils
+public final class ActionUtils
 {
-
+    /**
+     * A private constructor to make this class an utility class we can't instanciate.
+     **/
+    private ActionUtils()
+    {
+    }
+    
+    
     /**
      * Deactivates the action handler, if the handler's action is equal to 
      * the given action.
@@ -46,20 +54,24 @@ public class ActionUtils
     {
         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
             ICommandService.class );
+        
         if ( commandService != null )
         {
-            IHandler handler = commandService.getCommand( action.getActionDefinitionId()
).getHandler();
+            Command command = commandService.getCommand( action.getActionDefinitionId() );
+            IHandler handler = command.getHandler();
+            
             if ( handler instanceof ActionHandler )
             {
                 ActionHandler actionHandler = ( ActionHandler ) handler;
-                if ( actionHandler != null && actionHandler.getAction() == action
)
+                
+                if ( ( actionHandler != null ) && ( actionHandler.getAction() ==
action ) )
                 {
-                    commandService.getCommand( action.getActionDefinitionId() ).setHandler(
null );
+                    command.setHandler( null );
                 }
             }
             else if ( handler != null )
             {
-                commandService.getCommand( action.getActionDefinitionId() ).setHandler( null
);
+                command.setHandler( null );
             }
         }
     }
@@ -74,11 +86,11 @@ public class ActionUtils
     {
         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
             ICommandService.class );
+        
         if ( commandService != null )
         {
             ActionHandler actionHandler = new ActionHandler( action );
             commandService.getCommand( action.getActionDefinitionId() ).setHandler( actionHandler
);
         }
     }
-
 }



Mime
View raw message