directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r478607 - in /directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller: AttributesViewController.java actions/AttributeEditAction.java actions/RenameAttributeAction.java
Date Thu, 23 Nov 2006 16:04:56 GMT
Author: pamarcelot
Date: Thu Nov 23 08:04:55 2006
New Revision: 478607

URL: http://svn.apache.org/viewvc?view=rev&rev=478607
Log:
Updating Browser. Adding the ability to change the value of an attribute.

Modified:
    directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/AttributesViewController.java
    directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/AttributeEditAction.java
    directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/RenameAttributeAction.java

Modified: directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/AttributesViewController.java
URL: http://svn.apache.org/viewvc/directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/AttributesViewController.java?view=diff&rev=478607&r1=478606&r2=478607
==============================================================================
--- directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/AttributesViewController.java
(original)
+++ directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/AttributesViewController.java
Thu Nov 23 08:04:55 2006
@@ -108,7 +108,7 @@
         {
             public void widgetDefaultSelected( SelectionEvent e )
             {
-                new RenameAttributeAction( view, view.getViewer().getTable(), "Rename" ).run();
+                new RenameAttributeAction( view, "Rename attribute" ).run();
             }
         } );
     }

Modified: directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/AttributeEditAction.java
URL: http://svn.apache.org/viewvc/directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/AttributeEditAction.java?view=diff&rev=478607&r1=478606&r2=478607
==============================================================================
--- directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/AttributeEditAction.java
(original)
+++ directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/AttributeEditAction.java
Thu Nov 23 08:04:55 2006
@@ -38,6 +38,11 @@
     private AttributesView view;
 
 
+    /**
+     * Creates a new instance of AttributeEditAction.
+     * @param view the associated view
+     * @param text the string used as the text for the action
+     */
     public AttributeEditAction( AttributesView view, String text )
     {
         super( text );
@@ -47,8 +52,11 @@
     }
 
 
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.action.Action#run()
+     */
     public void run()
     {
-        System.out.println( "Edit attribute" );
+        new RenameAttributeAction( view, "Rename attribute" ).run();
     }
 }

Modified: directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/RenameAttributeAction.java
URL: http://svn.apache.org/viewvc/directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/RenameAttributeAction.java?view=diff&rev=478607&r1=478606&r2=478607
==============================================================================
--- directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/RenameAttributeAction.java
(original)
+++ directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/controller/actions/RenameAttributeAction.java
Thu Nov 23 08:04:55 2006
@@ -22,18 +22,27 @@
 
 
 import org.apache.directory.ldapstudio.browser.view.views.AttributesView;
+import org.apache.directory.ldapstudio.browser.view.views.BrowserView;
+import org.apache.directory.ldapstudio.browser.view.views.wrappers.EntryWrapper;
+import org.apache.directory.ldapstudio.dsmlv2.Dsmlv2ResponseParser;
+import org.apache.directory.ldapstudio.dsmlv2.engine.Dsmlv2Engine;
+import org.apache.directory.ldapstudio.dsmlv2.reponse.ErrorResponse;
+import org.apache.directory.shared.ldap.codec.LdapResponse;
+import org.apache.directory.shared.ldap.codec.modify.ModifyResponse;
+import org.apache.directory.shared.ldap.codec.search.SearchResultEntry;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.TableEditor;
 import org.eclipse.swt.events.FocusAdapter;
 import org.eclipse.swt.events.FocusEvent;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.PlatformUI;
 
 
 /**
@@ -44,94 +53,68 @@
 public class RenameAttributeAction extends Action
 {
     private static final int COLUMN_TO_EDIT = 1;
-    private final AttributesView view;
-    private final Table table;
-    private final TableEditor tableEditor;
-    //	private final Text textActionHandler;
-    private Composite editorParent;
-    private Text editor;
-    private String originalText;
+    private AttributesView view;
+    private Table table;
+    private TableEditor tableEditor;
+    private Text textEditor;
+    private String originalValue;
 
 
-    public RenameAttributeAction( AttributesView view, Table table, String text )
+    /**
+     * Creates a new instance of RenameAttributeAction.
+     *
+     * @param view the associated view
+     * @param text the string used as the text for the action
+     */
+    public RenameAttributeAction( AttributesView view, String text )
     {
         super( text );
         this.view = view;
-        this.table = table;
-        tableEditor = new TableEditor( table );
+        this.table = view.getViewer().getTable();
     }
 
 
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.action.Action#run()
+     */
     public void run()
     {
-        originalText = getTextToEdit();
-        if ( originalText == null )
-        {
-            return;
-        }
-        if ( editor == null )
-        {
-            createEditor();
-        }
-        showEditor( originalText );
+        createEditor();
+        showEditor();
     }
 
 
+    /**
+     * Creates the Editor cell and registers associated listeners
+     */
     private void createEditor()
     {
-        // Create the parent so that a simple border
-        // can be painted around the text editor
-        editorParent = new Composite( table, SWT.NONE );
-        TableItem[] tableItems = table.getSelection();
+        // Creating the Table Editor
+        tableEditor = new TableEditor( table );
         tableEditor.horizontalAlignment = SWT.LEFT;
         tableEditor.grabHorizontal = true;
-        tableEditor.setEditor( editorParent, tableItems[0], COLUMN_TO_EDIT );
-        editorParent.setVisible( false );
-        editorParent.addListener( SWT.Paint, new Listener()
-        {
-            public void handleEvent( Event e )
-            {
-                // Paint a simple border around the text editor
-                Point textSize = editor.getSize();
-                Point parentSize = editorParent.getSize();
-                int w = Math.min( textSize.x + 4, parentSize.x - 1 );
-                int h = parentSize.y - 1;
-                e.gc.drawRectangle( 0, 0, w, h );
-            }
-        } );
+        tableEditor.minimumWidth = 50;
 
-        // Create the editor itself
-        editor = new Text( editorParent, SWT.NONE );
-        editorParent.setBackground( editor.getBackground() );
-        editor.addListener( SWT.Modify, new Listener()
-        {
-            public void handleEvent( Event e )
-            {
-                Point textSize = editor.computeSize( SWT.DEFAULT, SWT.DEFAULT );
-                textSize.x += textSize.y;
-
-                // Add extra space for new characters
-                Point parentSize = editorParent.getSize();
-                int w = Math.min( textSize.x, textSize.y );
-                int h = parentSize.y - 2;
-                editor.setBounds( 2, 1, w, h );
-                editorParent.redraw();
-            }
-        } );
-        editor.addListener( SWT.Traverse, new Listener()
+        // Creating the Text Widget that will be used by the user 
+        // to enter the new value
+        textEditor = new Text( view.getViewer().getTable(), SWT.NONE );
+
+        // Adding Traverse Listener used to handle event when the 'return'
+        // or 'escape' key is pressed
+        textEditor.addListener( SWT.Traverse, new Listener()
         {
             public void handleEvent( Event event )
             {
                 // Workaround for bug 20214 due to extra traverse events
                 switch ( event.detail )
                 {
-                    case SWT.TRAVERSE_ESCAPE:
+                    case SWT.TRAVERSE_ESCAPE: // Escape Key
                         // Do nothing in this case
                         disposeEditor();
                         event.doit = true;
                         event.detail = SWT.TRAVERSE_NONE;
                         break;
-                    case SWT.TRAVERSE_RETURN:
+                    case SWT.TRAVERSE_RETURN: // Return Key
                         saveChangesAndDisposeEditor();
                         event.doit = true;
                         event.detail = SWT.TRAVERSE_NONE;
@@ -139,70 +122,148 @@
                 }
             }
         } );
-        editor.addFocusListener( new FocusAdapter()
+
+        // Adding Focus Listener used to handle event when the user
+        // clicks on the elsewhere
+        textEditor.addFocusListener( new FocusAdapter()
         {
             public void focusLost( FocusEvent fe )
             {
                 saveChangesAndDisposeEditor();
             }
         } );
-
-        // Add a handler to redirect global cut, copy, etc.
-        // textActionHandler.......
     }
 
 
-    private void showEditor( String name )
+    /**
+     * Shows the editor
+     */
+    private void showEditor()
     {
-        editor.setText( name );
-        editorParent.setVisible( true );
-        Point textSize = editor.computeSize( SWT.DEFAULT, SWT.DEFAULT );
-        textSize.x += textSize.y;
-        // Add extra space for new characters
-        Point parentSize = editorParent.getSize();
-        int w = Math.min( textSize.x, parentSize.x - 4 );
-        int h = parentSize.y - 2;
-        editor.setBounds( 2, 1, w, h );
-        editorParent.redraw();
-        editor.selectAll();
-        editor.setFocus();
+        // Backup of the original value
+        originalValue = getAttributeValue();
+
+        tableEditor.setEditor( textEditor, view.getViewer().getTable().getSelection()[0],
COLUMN_TO_EDIT );
+        textEditor.setText( getAttributeValue() );
+        textEditor.selectAll();
+        textEditor.setFocus();
     }
 
 
-    protected void saveChangesAndDisposeEditor()
+    /**
+     * Saves the changes made in the editor and disposes the editor
+     */
+    private void saveChangesAndDisposeEditor()
     {
-        String newText = editor.getText();
-        if ( !originalText.equals( newText ) )
+        String newText = textEditor.getText();
+        if ( !originalValue.equals( newText ) )
         {
-            saveChanges( newText );
+            saveChanges();
         }
         disposeEditor();
     }
 
 
-    protected void disposeEditor()
-    {
-        if ( editorParent != null )
-        {
-            editorParent.dispose();
-            editorParent = null;
-            editor = null;
-            tableEditor.setEditor( null, null, COLUMN_TO_EDIT );
-        }
+    /**
+     * Disposes the editor and refreshes the Atttributes View UI
+     */
+    private void disposeEditor()
+    {
+        textEditor.dispose();
+        textEditor = null;
+        tableEditor.setEditor( null, null, COLUMN_TO_EDIT );
+
+        // Resizing Columns and resetting the focus on the Table
+        view.resizeColumsToFit();
+        view.getViewer().getTable().setFocus();
     }
 
 
-    protected String getTextToEdit()
+    /**
+     * Gets the name of the selected attribute
+     * @return the name of the selected attribute
+     */
+    private String getAttributeName()
     {
         TableItem item = view.getSelectedAttributeTableItem();
-        return item.getText( 1 );
+        return item.getText( 0 );
     }
 
 
-    protected void saveChanges( String newText )
+    /**
+     * Gets the value of the selected attribute
+     * @return the value of the selected attribute
+     */
+    private String getAttributeValue()
     {
         TableItem item = view.getSelectedAttributeTableItem();
-        item.setText( 1, newText );
-        view.getViewer().refresh( item );
+        return item.getText( 1 );
+    }
+
+
+    /**
+     * Saves the changes made in the editor on the server
+     * @param newText
+     */
+    private void saveChanges()
+    {
+        try
+        {
+            // Getting the Browser View
+            BrowserView browserView = ( BrowserView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
+                .getActivePage().findView( BrowserView.ID );
+
+            EntryWrapper entryWrapper = ( EntryWrapper ) ( ( TreeSelection ) browserView.getViewer().getSelection()
)
+                .getFirstElement();
+            SearchResultEntry entry = entryWrapper.getEntry();
+
+            // Initialization of the DSML Engine and the DSML Response Parser
+            Dsmlv2Engine engine = entryWrapper.getDsmlv2Engine();
+            Dsmlv2ResponseParser parser = new Dsmlv2ResponseParser();
+
+            String request = "<batchRequest>" + "   <modifyRequest dn=\""
+                + entry.getObjectName().getNormName().toString() + "\">" + "      <modification
name=\""
+                + getAttributeName() + "\" operation=\"delete\">" + "         <value>"
+ getAttributeValue()
+                + "</value>" + "      </modification>" + "      <modification
name=\"" + getAttributeName()
+                + "\" operation=\"add\">" + "         <value>" + textEditor.getText()
+ "</value>"
+                + "      </modification>" + "   </modifyRequest>" + "</batchRequest>";
+
+            parser.setInput( engine.processDSML( request ) );
+            parser.parse();
+
+            LdapResponse ldapResponse = parser.getBatchResponse().getCurrentResponse();
+
+            if ( ldapResponse instanceof ModifyResponse )
+            {
+                ModifyResponse modifyResponse = ( ModifyResponse ) ldapResponse;
+
+                if ( modifyResponse.getLdapResult().getResultCode() == 0 )
+                {
+                    TableItem item = view.getSelectedAttributeTableItem();
+                    item.setText( 1, textEditor.getText() );
+                    view.getViewer().refresh( item );
+                }
+                else
+                {
+                    // Displaying an error
+                    MessageDialog.openError( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                        "Error !", "An error has ocurred.\n" + modifyResponse.getLdapResult().getErrorMessage()
);
+                }
+            }
+            else if ( ldapResponse instanceof ErrorResponse )
+            {
+                ErrorResponse errorResponse = ( ErrorResponse ) ldapResponse;
+
+                // Displaying an error
+                MessageDialog.openError( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
"Error !",
+                    "An error has ocurred.\n" + errorResponse.getMessage() );
+            }
+        }
+        catch ( Exception e )
+        {
+            // Displaying an error
+            MessageDialog.openError( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
"Error !",
+                "An error has ocurred.\n" + e.getMessage() );
+        }
     }
 }



Mime
View raw message