directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1679435 - in /directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config: editor/dialogs/ServerIdDialog.java editor/pages/OverviewPage.java model/widgets/ServerIdTableWidget.java
Date Thu, 14 May 2015 19:15:46 GMT
Author: elecharny
Date: Thu May 14 19:15:46 2015
New Revision: 1679435

URL: http://svn.apache.org/r1679435
Log:
Added a widget and a dialog to handle the ServerIDs. As we may have many values, we need a
table for that value.

Added:
    directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/dialogs/ServerIdDialog.java
    directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/model/widgets/ServerIdTableWidget.java
Modified:
    directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/pages/OverviewPage.java

Added: directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/dialogs/ServerIdDialog.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/dialogs/ServerIdDialog.java?rev=1679435&view=auto
==============================================================================
--- directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/dialogs/ServerIdDialog.java
(added)
+++ directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/dialogs/ServerIdDialog.java
Thu May 14 19:15:46 2015
@@ -0,0 +1,375 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.studio.openldap.config.editor.dialogs;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
+import org.apache.directory.api.ldap.model.url.LdapUrl;
+import org.apache.directory.studio.common.ui.widgets.BaseWidgetUtils;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.apache.directory.studio.openldap.config.editor.pages.ServerIdWrapper;
+
+
+/**
+ * The ServerIdDialog is used to edit a ServerID, which can be an integer, an hexadicimal
number,
+ * optionally followed by an URL. The dialog overlay is like :
+ * 
+ * <pre>
+ * +---------------------------------------+
+ * |  ServerID                             |
+ * | .-----------------------------------. |
+ * | | ID  : [    ]                      | |
+ * | | URL : [                         ] | |
+ * | '-----------------------------------' |
+ * | .-----------------------------------. |
+ * | | ServerId : <////////////////////> | |
+ * | '-----------------------------------' |
+ * |                                       |
+ * |  (cancel)                       (OK)  |
+ * +---------------------------------------+
+ * 
+ * </pre>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ServerIdDialog extends Dialog
+{
+    /** The ServerId */
+    private ServerIdWrapper serverId;
+
+    /** The new serverId */
+    private ServerIdWrapper newServerId;
+
+    /** The list of existing ServerID */
+    List<ServerIdWrapper> serverIdList;
+    
+    // UI widgets
+    /** The ID Text */
+    private Text idText;
+    
+    /** The URL text */
+    private Text urlText;
+    
+    /** The resulting sevrerID Text, or an error message */
+    private Text serverIdText;
+
+
+    /**
+     * Create a new instance of the ServerIdDialog
+     * 
+     * @param parentShell The parent Shell
+     * @param serverId The instance containing the ServerID data
+     */
+    public ServerIdDialog( Shell parentShell, List<ServerIdWrapper> serverIdList, ServerIdWrapper
serverId )
+    {
+        super( parentShell );
+        super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
+        this.serverId = serverId;
+        this.serverIdList = serverIdList;
+        
+        if ( serverIdList == null )
+        {
+            this.serverIdList = new ArrayList<ServerIdWrapper>();
+        }
+    }
+
+
+    /**
+     * Create a new instance of the ServerIdDialog
+     * 
+     * @param parentShell The parent Shell
+     * @param serverIdStr : The string containing the serverID
+     */
+    public ServerIdDialog( Shell parentShell, String serverIdStr )
+    {
+        super( parentShell );
+        super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
+        this.serverId = new ServerIdWrapper( serverIdStr );
+    }
+    
+    
+    /**
+     * The A listener for the ID Text
+     */
+    private ModifyListener idTextListener = new ModifyListener()
+    {
+        public void modifyText( ModifyEvent e )
+        {
+            Display display = serverIdText.getDisplay();
+            Button okButton = getButton( IDialogConstants.OK_ID );
+            
+            // This button might be null when the dialog is called.
+            if ( okButton == null )
+            {
+                return;
+            }
+
+            try
+            {
+                int idValue = Integer.parseInt( idText.getText() );
+
+                // The value must be between 0 and 4095, and it must not already exists
+                if ( ( idValue < 0 ) || ( idValue > 4096 ) )
+                {
+                    System.out.println( "Wrong ID : it must be a value in [0..4095]" );
+                    serverIdText.setText( "Wrong ID : it must be a value in [0..4095]" );
+                    serverIdText.setForeground( display.getSystemColor( SWT.COLOR_RED ) );
+                    okButton.setEnabled( false );
+                    return;
+                }
+                else
+                {
+                    // Be sure the value is not already taken
+                    for ( ServerIdWrapper serverIdWrapper : serverIdList )
+                    {
+                        if ( serverIdWrapper.getServerId() == idValue )
+                        {
+                            System.out.println( "Wrong ServerID : already taken" );
+                            serverIdText.setText( "Wrong ID : it's already taken by another
Server ID" );
+                            serverIdText.setForeground( display.getSystemColor( SWT.COLOR_RED
) );
+                            okButton.setEnabled( false );
+                            return;
+                        }
+                    }
+                }
+                
+                serverIdText.setText( idText.getText() + ' ' + urlText.getText() );
+                serverIdText.setForeground( display.getSystemColor( SWT.COLOR_BLACK ) );
+                okButton.setEnabled( true );
+            }
+            catch ( NumberFormatException nfe )
+            {
+                // Not even a number
+                System.out.println( "Wrong ServerID : it must be an integer" );
+                serverIdText.setText( "Wrong ID : it must be an integer in [0..4095]" );
+                serverIdText.setForeground( display.getSystemColor( SWT.COLOR_RED ) );
+                okButton.setEnabled( false );
+            }
+        }
+    };
+    
+    
+    /**
+     * The A listener for the URL Text
+     */
+    private ModifyListener urlTextListener = new ModifyListener()
+    {
+        public void modifyText( ModifyEvent e )
+        {
+            Display display = serverIdText.getDisplay();
+            Button okButton = getButton( IDialogConstants.OK_ID );
+            
+            
+            // This button might be null when the dialog is called.
+            if ( okButton == null )
+            {
+                return;
+            }
+
+            try
+            {
+                new LdapUrl( urlText.getText() );
+                
+                serverIdText.setText( idText.getText() + ' ' + urlText.getText() );
+                serverIdText.setForeground( display.getSystemColor( SWT.COLOR_BLACK ) );
+                okButton.setEnabled( true );
+            }
+            catch ( LdapURLEncodingException luee )
+            {
+                System.out.println( "Wrong ServerID : the URL is invalid" );
+                serverIdText.setText( "Wrong ServerID : the URL is invalid" );
+                serverIdText.setForeground( display.getSystemColor( SWT.COLOR_RED ) );
+                okButton.setEnabled( false );
+            }
+
+        }
+    };
+            
+
+    /**
+     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
+     */
+    protected void configureShell( Shell shell )
+    {
+        super.configureShell( shell );
+        shell.setText( "ServerId" );
+    }
+
+
+    /**
+     * We have to check that the ID does not already exist.
+     * {@inheritDoc}
+     */
+    protected void okPressed()
+    {
+        // Creating the new index
+        String id = idText.getText();
+        String url = urlText.getText();
+        int idValue = Integer.valueOf( id );
+        
+        newServerId = new ServerIdWrapper( idValue , url );
+        super.okPressed();
+    }
+
+
+    /**
+     * Create the Dialog for ServerID :
+     * <pre>
+     * +---------------------------------------+
+     * |  ServerID                             |
+     * | .-----------------------------------. |
+     * | | ID  : [    ]                      | |
+     * | | URL : [                         ] | |
+     * | '-----------------------------------' |
+     * | .-----------------------------------. |
+     * | | ServerId : <////////////////////> | |
+     * | '-----------------------------------' |
+     * |                                       |
+     * |  (cancel)                       (OK)  |
+     * +---------------------------------------+
+     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
+     */
+    protected Control createDialogArea( Composite parent )
+    {
+        Composite composite = ( Composite ) super.createDialogArea( parent );
+        GridData gd = new GridData( GridData.FILL_BOTH );
+        composite.setLayoutData( gd );
+
+        createServerIdEditGroup( composite );
+        createServerIdShowGroup( composite );
+
+        initFromServerId();
+
+        applyDialogFont( composite );
+        
+        return composite;
+    }
+
+
+    /**
+     * Creates the ServerID input group. This is the part of the dialog
+     * where one can insert the ServerID values:
+     * 
+     * <pre>
+     * ServerID
+     * .-----------------------------------.
+     * | ID  : [    ]                      |
+     * | URL : [                         ] |
+     * '-----------------------------------'
+     * </pre>
+     * @param parent the parent composite
+     */
+    private void createServerIdEditGroup( Composite parent )
+    {
+        // Attributes Group
+        Group serverIdGroup = BaseWidgetUtils.createGroup( parent, "ServerID input", 1 );
+        GridLayout attributesGroupGridLayout = new GridLayout( 2, false );
+        serverIdGroup.setLayout( attributesGroupGridLayout );
+        serverIdGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+
+        // ServerID Text
+        BaseWidgetUtils.createLabel( serverIdGroup, "ID:", 1 );
+        idText = BaseWidgetUtils.createText( serverIdGroup, "", 1 );
+        idText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+        idText.addModifyListener( idTextListener );
+
+        // URL Text
+        BaseWidgetUtils.createLabel( serverIdGroup, "URL:", 1 );
+        urlText = BaseWidgetUtils.createText( serverIdGroup, "", 1 );
+        urlText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+        urlText.addModifyListener( urlTextListener );
+    }
+
+
+    /**
+     * Creates the ServerID show group. This is the part of the dialog
+     * where the real ServerID is shown, or an error message if the ServerID
+     * is invalid.
+     * 
+     * <pre>
+     * .-----------------------------------.
+     * | ServerID  : <///////////////////> |
+     * '-----------------------------------'
+     * </pre>
+     * @param parent the parent composite
+     */
+    private void createServerIdShowGroup( Composite parent )
+    {
+        // Attributes Group
+        Group serverIdGroup = BaseWidgetUtils.createGroup( parent, "", 1 );
+        GridLayout attributesGroupGridLayout = new GridLayout( 2, false );
+        serverIdGroup.setLayout( attributesGroupGridLayout );
+        serverIdGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+
+        // ServerID Text
+        serverIdText = BaseWidgetUtils.createText( serverIdGroup, "", 1 );
+        serverIdText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+    }
+
+
+    /**
+     * Initializes the UI from the ServerId
+     */
+    private void initFromServerId()
+    {
+        if ( serverId != null )
+        {
+            idText.setText( Integer.toString( serverId.getServerId() ) );
+            
+            String url = serverId.getUrl();
+            
+            if ( url == null )
+            {
+                urlText.setText( "" );
+            }
+            else
+            {
+                urlText.setText( serverId.getUrl() );
+            }
+        }
+    }
+
+
+    /**
+     * Gets the new ServerId.
+     *
+     * @return the new serverID
+     */
+    public ServerIdWrapper getNewServerId()
+    {
+        return newServerId;
+    }
+}

Modified: directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/pages/OverviewPage.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/pages/OverviewPage.java?rev=1679435&r1=1679434&r2=1679435&view=diff
==============================================================================
--- directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/pages/OverviewPage.java
(original)
+++ directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/editor/pages/OverviewPage.java
Thu May 14 19:15:46 2015
@@ -36,15 +36,14 @@ import org.apache.directory.studio.openl
 import org.apache.directory.studio.openldap.config.editor.pages.OverlaysPage;
 import org.apache.directory.studio.openldap.config.model.OlcModuleList;
 import org.apache.directory.studio.openldap.config.model.database.OlcDatabaseConfig;
+import org.apache.directory.studio.openldap.config.model.widgets.ServerIdTableWidget;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.forms.events.HyperlinkAdapter;
@@ -74,7 +73,12 @@ import org.eclipse.ui.forms.widgets.Tabl
  * | .-------------------------------------------------------------------------------. |
  * | |V Global parameters                                                            | |
  * | +-------------------------------------------------------------------------------+ |
- * | | Server ID  : [   ]                                                            | |
+ * | | Server ID  :                                                                  | |
+ * | | +-----------------------------------------------------------------+           | |
+ * | | |                                                                 | (Add)     | |
+ * | | |                                                                 | (Edit)    | |
+ * | | |                                                                 | (Delete)  | |
+ * | | +-----------------------------------------------------------------+           | |
  * | |                                                                               | |
  * | | Configuration Dir : [                ]  Pid File  : [                ]        | |
  * | | Log File          : [                ]  Log Level : [                ]  (edit)| |
@@ -109,9 +113,12 @@ public class OverviewPage extends OpenLD
     private static final String TITLE = Messages.getString( "OpenLDAPOverviewPage.Title"
); //$NON-NLS-1$"Overview";
     
     // UI Controls
-    /** olcServerID */
-    private Text serverIdText;
+    /** The serverID wrapper */
+    private List<ServerIdWrapper> serverIdWrappers = new ArrayList<ServerIdWrapper>();
     
+    /** The Widget used to manage ServerID */
+    private ServerIdTableWidget serverIdWidget;
+
     /** olcConfigDir */
     private Text configDirText;
     
@@ -318,12 +325,18 @@ public class OverviewPage extends OpenLD
      * .-------------------------------------------------------------------------------.
      * |V Global parameters                                                            |
      * +-------------------------------------------------------------------------------+
-     * | Server ID  : [   ]                                                            |
+     * | Server ID  :                                                                  |
+     * | +-----------------------------------------------------------------+           |
+     * | |                                                                 | (Add)     |
+     * | |                                                                 | (Edit)    |
+     * | |                                                                 | (Delete)  |
+     * | +-----------------------------------------------------------------+           |
      * |                                                                               |
      * | Configuration Dir : [                ]  Pid File  : [                ]        |
-     * | Log File          : [                ]  Log Level : [                ] (Edit) |
+     * | Log File          : [                ]  Log Level : [                ]  (edit)|
      * +-------------------------------------------------------------------------------+
      * </pre>
+     * 
      *
      * @param toolkit the toolkit
      * @param parent the parent composite
@@ -334,42 +347,45 @@ public class OverviewPage extends OpenLD
         Section section = createSection( toolkit, parent, Messages.getString( "OpenLDAPOverviewPage.GlobalSection"
) );
 
         // The content
-        Composite composite = toolkit.createComposite( section );
-        toolkit.paintBordersFor( composite );
+        Composite globalSectionComposite = toolkit.createComposite( section );
+        toolkit.paintBordersFor( globalSectionComposite );
         GridLayout gridLayout = new GridLayout( 4, false );
         gridLayout.marginHeight = gridLayout.marginWidth = 0;
-        composite.setLayout( gridLayout );
-        section.setClient( composite );
+        globalSectionComposite.setLayout( gridLayout );
+        section.setClient( globalSectionComposite );
 
-        // The ServerID parameter
-        toolkit.createLabel( composite, Messages.getString( "OpenLDAPOverviewPage.ServerID"
) ); //$NON-NLS-1$
-        serverIdText = createServerIdText( toolkit, composite );
-        toolkit.createLabel( composite, TABULATION );
-        toolkit.createLabel( composite, TABULATION );
+        // The ServerID parameter.
+        Label serverIdLabel = toolkit.createLabel( globalSectionComposite, Messages.getString(
"OpenLDAPOverviewPage.ServerID" ) ); //$NON-NLS-1$
+        serverIdLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, false, false, 4, 1
) );
+
+        // The ServerID widget
+        serverIdWidget = new ServerIdTableWidget();
+        serverIdWidget.createWidget( globalSectionComposite );
+        serverIdWidget.getControl().setLayoutData( new GridData( SWT.FILL, SWT.NONE, true,
false, 4, 1 ) );
         
         // One blank line
         for ( int i = 0; i < gridLayout.numColumns; i++ )
         {
-            toolkit.createLabel( composite, TABULATION );
+            toolkit.createLabel( globalSectionComposite, TABULATION );
         }
         
         // The ConfigDir parameter
-        toolkit.createLabel( composite, Messages.getString( "OpenLDAPOverviewPage.ConfigDir"
) ); //$NON-NLS-1$
-        configDirText = createConfigDirText( toolkit, composite );
+        toolkit.createLabel( globalSectionComposite, Messages.getString( "OpenLDAPOverviewPage.ConfigDir"
) ); //$NON-NLS-1$
+        configDirText = createConfigDirText( toolkit, globalSectionComposite );
         
         // The PidFile parameter
-        toolkit.createLabel( composite, Messages.getString( "OpenLDAPOverviewPage.PidFile"
) ); //$NON-NLS-1$
-        pidFileText = createPidFileText( toolkit, composite );
+        toolkit.createLabel( globalSectionComposite, Messages.getString( "OpenLDAPOverviewPage.PidFile"
) ); //$NON-NLS-1$
+        pidFileText = createPidFileText( toolkit, globalSectionComposite );
         pidFileText.setText( getConfiguration().getGlobal().getOlcPidFile() );
         
         // The LogFile parameter
-        toolkit.createLabel( composite, Messages.getString( "OpenLDAPOverviewPage.LogFile"
) ); //$NON-NLS-1$
-        logFileText = createLogFileText( toolkit, composite );
+        toolkit.createLabel( globalSectionComposite, Messages.getString( "OpenLDAPOverviewPage.LogFile"
) ); //$NON-NLS-1$
+        logFileText = createLogFileText( toolkit, globalSectionComposite );
         
         // The LogLevel parameter
-        toolkit.createLabel( composite, Messages.getString( "OpenLDAPOverviewPage.LogLevel"
) );
+        toolkit.createLabel( globalSectionComposite, Messages.getString( "OpenLDAPOverviewPage.LogLevel"
) );
         logLevelWidget = new LogLevelWidget();
-        logLevelWidget.create( composite, toolkit );
+        logLevelWidget.create( globalSectionComposite, toolkit );
         logLevelWidget.getControl().setLayoutData( new GridData( SWT.FILL, SWT.NONE, true,
false ) );
     }
     
@@ -549,49 +565,6 @@ public class OverviewPage extends OpenLD
 
     
     /**
-     * Creates a Text that can be used to enter a serverID. If the serverID is incorrect,

-     * it will be in red while typing until it gets correct.
-     *
-     * @param toolkit the toolkit
-     * @param parent the parent
-     * @return a Text that can be used to enter a ServerID
-     */
-    private Text createServerIdText( FormToolkit toolkit, Composite parent )
-    {
-        final Text serverIdText = toolkit.createText( parent, "" ); //$NON-NLS-1$
-        GridData gd = new GridData( SWT.NONE, SWT.NONE, false, false );
-        gd.widthHint = 200;
-        serverIdText.setLayoutData( gd );
-        
-        serverIdText.addModifyListener( new ModifyListener()
-        {
-            Display display = serverIdText.getDisplay();
-
-            // Check that the ServerID is valid
-            public void modifyText( ModifyEvent e )
-            {
-                Text serverIdText = (Text)e.widget;
-                String serverId = serverIdText.getText();
-                
-                try
-                {
-                    Integer.parseInt( serverId );
-                }
-                catch ( NumberFormatException nfe )
-                {
-                    serverIdText.setForeground( display.getSystemColor( SWT.COLOR_RED ) );
-                }
-            }
-        } );
-        
-        // No more than 3 digits
-        serverIdText.setTextLimit( 3 );
-
-        return serverIdText;
-    }
-
-
-    /**
      * Creates a Text that can be used to enter an ConfigDir.
      *
      * @param toolkit the toolkit
@@ -655,85 +628,6 @@ public class OverviewPage extends OpenLD
 
 
     /**
-     * Creates a Text that can be used to enter the Log level.
-     *
-     * @param toolkit the toolkit
-     * @param parent the parent
-     * @return a Text that can be used to enter the Log level
-     */
-    private Text createLogLevelText( FormToolkit toolkit, Composite parent )
-    {
-        final Text logLevelText = toolkit.createText( parent, "" ); //$NON-NLS-1$
-        GridData gd = new GridData( SWT.NONE, SWT.NONE, false, false );
-        gd.widthHint = 200;
-        logLevelText.setLayoutData( gd );
-        
-        // No more than 512 digits
-        logLevelText.setTextLimit( 512 );
-
-        logLevelText.addModifyListener( new ModifyListener()
-        {
-            Display display = logLevelText.getDisplay();
-
-            // Check that the LogLevel is valid
-            public void modifyText( ModifyEvent e )
-            {
-                Text logLevelText = (Text)e.widget;
-                String logLevel = logLevelText.getText();
-                
-                try
-                {
-                    Integer.parseInt( logLevel );
-                    logLevelText.setForeground( display.getSystemColor( SWT.COLOR_BLACK )
);
-                }
-                catch ( NumberFormatException nfe )
-                {
-                    logLevelText.setForeground( display.getSystemColor( SWT.COLOR_RED ) );
-                }
-            }
-        } );
-        
-        // No more than 6 digits
-        logLevelText.setTextLimit( 6 );
-
-        return logLevelText;
-    }
-
-    
-    /**
-     * Get the ServerID
-     */
-    private String getServerId()
-    {
-        List<String> serverIdList = getConfiguration().getGlobal().getOlcServerID();
-        
-        if ( serverIdList == null )
-        {
-            return "";
-        }
-        
-        boolean isFirst = true;
-        StringBuilder sb = new StringBuilder();
-        
-        for ( String serverId : serverIdList )
-        {
-            if ( isFirst )
-            {
-                isFirst = false;
-            }
-            else
-            {
-                sb.append( ", " );
-            }
-            
-            sb.append( serverId );
-        }
-        
-        return sb.toString();
-    }
-
-    
-    /**
      * Get the various LogLevel values, and concatenate them in a String
      */
     private String getLogLevel()
@@ -776,7 +670,15 @@ public class OverviewPage extends OpenLD
             removeListeners();
 
             // Update the ServerIDText
-            serverIdText.setText( getServerId() );
+            // Update the DatabaseTableViewer
+            serverIdWrappers.clear();
+
+            for ( String serverIdWrapper : getConfiguration().getGlobal().getOlcServerID()
)
+            {
+                serverIdWrappers.add( new ServerIdWrapper( serverIdWrapper ) );
+            }
+
+            serverIdWidget.setElements( serverIdWrappers );
             
             // Update the ConfigDirText
             configDirText.setText( getConfiguration().getGlobal().getOlcConfigDir() );
@@ -841,7 +743,7 @@ public class OverviewPage extends OpenLD
     private void removeListeners()
     {
         // The serverID Text 
-        removeDirtyListener( serverIdText );
+        //removeDirtyListener( serverIdText );
 
         // The configDir Text 
         removeDirtyListener( configDirText );
@@ -863,7 +765,7 @@ public class OverviewPage extends OpenLD
     private void addListeners()
     {
         // The serverID Text 
-        addDirtyListener( serverIdText );
+        //addDirtyListener( serverIdText );
 
         // The configDir Text 
         addDirtyListener( configDirText );

Added: directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/model/widgets/ServerIdTableWidget.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/model/widgets/ServerIdTableWidget.java?rev=1679435&view=auto
==============================================================================
--- directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/model/widgets/ServerIdTableWidget.java
(added)
+++ directory/studio/trunk/plugins/openldap.config.editor/src/main/java/org/apache/directory/studio/openldap/config/model/widgets/ServerIdTableWidget.java
Thu May 14 19:15:46 2015
@@ -0,0 +1,413 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.studio.openldap.config.model.widgets;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.studio.common.ui.widgets.BaseWidgetUtils;
+import org.apache.directory.studio.ldapbrowser.common.widgets.BrowserWidget;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.apache.directory.studio.openldap.config.OpenLdapConfigurationPlugin;
+import org.apache.directory.studio.openldap.config.OpenLdapConfigurationPluginConstants;
+import org.apache.directory.studio.openldap.config.editor.dialogs.ServerIdDialog;
+import org.apache.directory.studio.openldap.config.editor.pages.ServerIdWrapper;
+
+
+/**
+ * The ServerIdTable Widget provides a table viewer to add/edit/remove ServerId from
+ * a list of ServerId.
+ * <pre>
+ * +--------------------------------------+
+ * | ServerId 1                           | (Add... )
+ * | ServerId 2                           | (Edit...)
+ * |                                      | (Delete )
+ * +--------------------------------------+
+ * </pre>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ServerIdTableWidget extends BrowserWidget
+{
+    /** The ServerId list */
+    private List<ServerIdWrapper> serverIds = new ArrayList<ServerIdWrapper>();
+
+    // UI widgets
+    private Composite composite;
+    private Table table;
+    private TableViewer tableViewer;
+    private Button addButton;
+    private Button editButton;
+    private Button deleteButton;
+    
+    /**
+     *  A listener on the ServerIds table, that modifies the button when an ServerId is selected
+     */
+    private ISelectionChangedListener tableViewerSelectionChangedListener = new ISelectionChangedListener()
+    {
+        public void selectionChanged( SelectionChangedEvent event )
+        {
+            StructuredSelection selection = ( StructuredSelection ) tableViewer.getSelection();
+
+            editButton.setEnabled( !selection.isEmpty() );
+            deleteButton.setEnabled( !selection.isEmpty() );
+        }
+    };
+    
+    /**
+     * A listener on the ServerIds table, that reacts to a doubleClick : it's opening the
ServerId editor
+     */
+    private IDoubleClickListener tableViewerDoubleClickListener = new IDoubleClickListener()
+    {
+        public void doubleClick( DoubleClickEvent event )
+        {
+            editServerId();
+        }
+    };
+    
+    
+    /**
+     *  A listener on the Add button, which opens the ServerId addition editor
+     */
+    private SelectionListener addButtonListener = new SelectionAdapter()
+    {
+        public void widgetSelected( SelectionEvent e )
+        {
+            addServerId();
+        }
+    };
+    
+    /**
+     *  A listener on the Edit button, that open the ServerId editor
+     */
+    private SelectionListener editButtonListener = new SelectionAdapter()
+    {
+        public void widgetSelected( SelectionEvent e )
+        {
+            editServerId();
+        }
+    };
+    
+    /**
+     *  A listener on the Delete button, which delete the selected ServerId
+     */
+    private SelectionListener deleteButtonListener = new SelectionAdapter()
+    {
+        public void widgetSelected( SelectionEvent e )
+        {
+            deleteServerId();
+        }
+    };
+
+
+    /**
+     * Creates a new instance of ServerIdTableWidget.
+     */
+    public ServerIdTableWidget()
+    {
+    }
+
+
+    /**
+     * Creates the widget.
+     * 
+     * @param parent the parent
+     */
+    public void createWidget( Composite parent )
+    {
+        createWidget( parent, null );
+    }
+
+
+    /**
+     * Creates the ServerId widget. It's a Table and three button :
+     * <pre>
+     * +--------------------------------------+
+     * | ServerId 1                           | (Add... )
+     * | ServerId 2                           | (Edit...)
+     * |                                      | (Delete )
+     * +--------------------------------------+
+     * </pre>
+     * 
+     * @param parent the parent
+     * @param toolkit the toolkit
+     */
+    public void createWidget( Composite parent, FormToolkit toolkit )
+    {
+        // Compositea
+        if ( toolkit != null )
+        {
+            composite = toolkit.createComposite( parent );
+        }
+        else
+        {
+            composite = new Composite( parent, SWT.NONE );
+        }
+        
+        // First, define a grid of 2 columns
+        GridLayout compositeGridLayout = new GridLayout( 2, false );
+        compositeGridLayout.marginHeight = compositeGridLayout.marginWidth = 0;
+        composite.setLayout( compositeGridLayout );
+
+        // Create the Element Table and Table Viewer
+        if ( toolkit != null )
+        {
+            table = toolkit.createTable( composite, SWT.BORDER );
+        }
+        else
+        {
+            table = new Table( composite, SWT.BORDER );
+        }
+        
+        // Define the table size and height. It will span on 3 lines.
+        GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true, 1, 3 );
+        gd.heightHint = 20;
+        gd.widthHint = 100;
+        table.setLayoutData( gd );
+        
+        // Create the Elements TableViewer
+        tableViewer = new TableViewer( table );
+        tableViewer.setContentProvider( new ArrayContentProvider() );
+        
+        // The LabelProvider
+        tableViewer.setLabelProvider( new LabelProvider()
+        {
+            public Image getImage( Object element )
+            {
+                return OpenLdapConfigurationPlugin.getDefault().getImage(
+                    OpenLdapConfigurationPluginConstants.IMG_LDAP_SERVER );
+            }
+        } );
+        
+        // Listeners : we want to catch changes and double clicks
+        tableViewer.addSelectionChangedListener( tableViewerSelectionChangedListener );
+        tableViewer.addDoubleClickListener( tableViewerDoubleClickListener );
+        
+        // Inject the existing indices
+        tableViewer.setInput( serverIds );
+
+        // Create the Add Button and its listener
+        if ( toolkit != null )
+        {
+            addButton = toolkit.createButton( composite, "Add...", SWT.PUSH );
+        }
+        else
+        {
+            addButton = BaseWidgetUtils.createButton( composite, "Add...", 1 );
+        }
+        
+        addButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
+        addButton.addSelectionListener( addButtonListener );
+
+        // Create the Edit Button and its listener
+        if ( toolkit != null )
+        {
+            editButton = toolkit.createButton( composite, "Edit...", SWT.PUSH );
+        }
+        else
+        {
+            editButton = BaseWidgetUtils.createButton( composite, "Edit...", SWT.PUSH );
+        }
+        
+        // It's not enabled unless we have selected a serverId
+        editButton.setEnabled( false );
+        editButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
+        editButton.addSelectionListener( editButtonListener );
+
+        // Create the Delete Button and its listener
+        if ( toolkit != null )
+        {
+            deleteButton = toolkit.createButton( composite, "Delete", SWT.PUSH );
+        }
+        else
+        {
+            deleteButton = BaseWidgetUtils.createButton( composite, "Delete", SWT.PUSH );
+        }
+        
+        // It's not selected unless we have selected a ServerId
+        deleteButton.setEnabled( false );
+        deleteButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false )
);
+        deleteButton.addSelectionListener( deleteButtonListener );
+    }
+
+
+    /**
+     * Returns the primary control associated with this widget.
+     *
+     * @return the primary control associated with this widget.
+     */
+    public Control getControl()
+    {
+        return composite;
+    }
+
+
+    /**
+     * Sets the ServerIds.
+     *
+     * @param serverIds the ServerIds
+     */
+    public void setElements( List<ServerIdWrapper> serverIds )
+    {
+        if ( ( serverIds != null ) && ( serverIds.size() > 0 ) )
+        {
+            this.serverIds.addAll( serverIds );
+        }
+
+        tableViewer.refresh();
+    }
+
+
+    /**
+     * Gets the ServerIds.
+     *
+     * @return the ServerIds
+     */
+    public List<ServerIdWrapper> getServerIds()
+    {
+        return serverIds;
+    }
+    
+    
+    /**
+     * Insert the modified or added ServerID at the right place in the ServerID table
+     * @param newServerId
+     */
+    private void insertServerId( ServerIdWrapper newServerId )
+    {
+        // Search for the inclusion position
+        int pos = 0;
+
+        for ( ServerIdWrapper serverIdWrapper : serverIds )
+        {
+            if ( serverIdWrapper.getServerId() > newServerId.getServerId() )
+            {
+                serverIds.add( pos, newServerId );
+                break;
+            }
+            else
+            {
+                pos++;
+            }
+        }
+        
+        // Special case : the value has to be added at the end
+        if ( pos == serverIds.size() )
+        {
+            serverIds.add( newServerId );
+        }
+
+    }
+
+
+    /**
+     * This method is called when the 'Add...' button is clicked.
+     */
+    private void addServerId()
+    {
+        ServerIdDialog dialog = new ServerIdDialog( addButton.getShell(), serverIds, (ServerIdWrapper)null
);
+        
+        if ( dialog.open() == ServerIdDialog.OK )
+        {
+            ServerIdWrapper newServerId = dialog.getNewServerId();
+            
+            // If the user clicked on OK but the value was invalid, we will receive a Null
value
+            if ( newServerId != null )
+            {
+                String serverIdString = newServerId.toString();
+                
+                insertServerId( newServerId );
+                tableViewer.refresh();
+                tableViewer.setSelection( new StructuredSelection( serverIdString ) );
+                notifyListeners();
+            }
+        }
+    }
+
+
+    /**
+     * This method is called when the 'Edit...' button is clicked
+     * or the table viewer is double-clicked.
+     */
+    private void editServerId()
+    {
+        StructuredSelection selection = ( StructuredSelection ) tableViewer.getSelection();
+
+        if ( !selection.isEmpty() )
+        {
+            ServerIdWrapper selectedServerId = ( ServerIdWrapper ) selection.getFirstElement();
+
+            // Open the ServerID dialog, with the selected serverId
+            ServerIdDialog dialog = new ServerIdDialog( addButton.getShell(), serverIds,
selectedServerId );
+            
+            if ( dialog.open() == ServerIdDialog.OK )
+            {
+                ServerIdWrapper newServerId = dialog.getNewServerId();
+                
+                // We will remove the modifies serverId, and replace it with the new serverId,
at the right position
+                serverIds.remove( selectedServerId );
+                
+                insertServerId( newServerId );
+                tableViewer.refresh();
+                tableViewer.setSelection( new StructuredSelection( newServerId.toString()
) );
+                notifyListeners();
+            }
+        }
+    }
+
+
+    /**
+     * This method is called when the 'Delete' button is clicked. It removes the selected
+     * ServerId from the list.
+     */
+    private void deleteServerId()
+    {
+        StructuredSelection selection = ( StructuredSelection ) tableViewer.getSelection();
+
+        if ( !selection.isEmpty() )
+        {
+            ServerIdWrapper selectedIndex = ( ServerIdWrapper ) selection.getFirstElement();
+
+            serverIds.remove( selectedIndex );
+            tableViewer.refresh();
+            notifyListeners();
+        }
+    }
+}



Mime
View raw message