directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r524445 - in /directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor: dialogs/ widgets/
Date Sat, 31 Mar 2007 16:42:21 GMT
Author: seelmann
Date: Sat Mar 31 09:42:19 2007
New Revision: 524445

URL: http://svn.apache.org/viewvc?view=rev&rev=524445
Log:
Added formatter in source editor

Added:
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIFormattingStrategy.java
Modified:
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/dialogs/ACIItemDialog.java
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACICodeScanner.java
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemSourceEditorComposite.java
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemTabFolderComposite.java
    directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACISourceViewerConfiguration.java

Modified: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/dialogs/ACIItemDialog.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/dialogs/ACIItemDialog.java?view=diff&rev=524445&r1=524444&r2=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/dialogs/ACIItemDialog.java
(original)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/dialogs/ACIItemDialog.java
Sat Mar 31 09:42:19 2007
@@ -31,6 +31,8 @@
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
@@ -93,6 +95,34 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     * 
+     * This implementation additionally adds the Format button.
+     */
+    protected Control createButtonBar( Composite parent )
+    {
+        Composite composite = ( Composite ) super.createButtonBar( parent );
+        super.createButton( composite, 987654321, "Format", false );
+        return composite;
+    }
+    
+    /**
+     * {@inheritDoc}
+     * 
+     * This implementation checks if the Format button was pressed.
+     */
+    protected void buttonPressed( int buttonId )
+    {
+        if ( buttonId == 987654321 )
+        {
+            tabFolderComposite.format();
+        }
+
+        // call super implementation
+        super.buttonPressed( buttonId );
+    }
+    
     /**
      * Reimplementation: Checks for valid syntax first and sets the return value.
      */

Modified: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACICodeScanner.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACICodeScanner.java?view=diff&rev=524445&r1=524444&r2=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACICodeScanner.java
(original)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACICodeScanner.java
Sat Mar 31 09:42:19 2007
@@ -61,7 +61,7 @@
 
     /** Keywords for AciItems values */
     public static final String[] aciKeywords = new String[]
-        { "protectedItems", "itemPermissions", "entry", "allowUserAttributeTypes", "attributeType",
+        { "protectedItems", "itemPermissions", "entry", "allUserAttributeTypes", "attributeType",
             "allAttributeValues", "allUserAttributeTypesAndValues", "attributeValue", "selfValue",
"rangeOfValues",
             "maxValueCount", "maxImmSub", "restrictedBy", "classes", "grantsAndDenials",
"allUsers", "thisEntry",
             "name", "userGroup", "subtree", "type", "valuesIn", "none", "simple", "strong"
};
@@ -73,7 +73,7 @@
 
     /** Keywords for deny values */
     public static final String[] aciDenyValues = new String[]
-        { "denyAdd", "denyDiscloseOnError", "denyRead", "denyRemove", "denyBrowse", "denyExport",
" denyImport",
+        { "denyAdd", "denyDiscloseOnError", "denyRead", "denyRemove", "denyBrowse", "denyExport",
"denyImport",
             "denyModify", "denyRename", "denyReturnDN", "denyCompare", "denyFilterMatch",
"denyInvoke" };
 
 

Added: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIFormattingStrategy.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIFormattingStrategy.java?view=auto&rev=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIFormattingStrategy.java
(added)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIFormattingStrategy.java
Sat Mar 31 09:42:19 2007
@@ -0,0 +1,290 @@
+
+package org.apache.directory.ldapstudio.aciitemeditor.widgets;
+
+import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
+import org.eclipse.jface.text.formatter.IFormattingStrategy;
+import org.eclipse.jface.text.source.ISourceViewer;
+
+/**
+ * This class implements the formatting strategy for the ACI Item Editor.
+ * <ul>
+ * <li>New line after a comma
+ * <li>New line after a opened left curly
+ * </ul>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ACIFormattingStrategy implements IFormattingStrategy
+{
+
+    /** The Constant INDENT_STRING. */
+    public static final String INDENT_STRING = "    ";
+    
+    /** The Constant NEWLINE. */
+    public static final String NEWLINE = BrowserCoreConstants.LINE_SEPARATOR;
+    
+    /** The source viewer. */
+    private ISourceViewer sourceViewer;
+    
+    
+    /**
+     * Creates a new instance of ACIFormattingStrategy.
+     *
+     * @param sourceViewer the source viewer
+     */
+    public ACIFormattingStrategy( ISourceViewer sourceViewer )
+    {
+        this.sourceViewer = sourceViewer;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String format( String content, boolean isLineStart, String indentation, int[]
positions )
+    {
+        String oldContent = sourceViewer.getDocument().get();
+        String newContent = internFormat ( oldContent );
+        sourceViewer.getDocument().set( newContent );
+        
+        return null;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void formatterStarts( String initialIndentation )
+    {
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void formatterStops()
+    {
+    }
+
+    private String internFormat( String content )
+    {
+        StringBuffer sb = new StringBuffer();
+
+        // flag to track if a new line was started
+        boolean newLineStarted = true;
+        
+        // flag to track if we are within a quoted string
+        boolean inQuotedString = false;
+        
+        // flag to track if the current expression is appended in one-line mode
+        boolean oneLineMode = false;
+        
+        // the current indent
+        int indent = 0;
+
+        int contentLength = content.length();
+        for (int i=0; i<contentLength; i++)
+        {
+            char c = content.charAt( i );
+            
+            // track quotes
+            if( c == '"')
+            {
+                inQuotedString = !inQuotedString;
+            }
+            
+            if( c == '{' && !inQuotedString )
+            {
+                // check one-line mode
+                oneLineMode = checkInOneLine(i, content);
+                
+                if(oneLineMode)
+                {
+                    // no new line in one-line mode
+                    sb.append( c );
+                    newLineStarted = false;
+                }
+                else
+                {
+                    // start a new line, but avoid blank lines if there are multiple opened
curlies
+                    if( !newLineStarted )
+                    {
+                        sb.append( NEWLINE );
+                        for ( int x = 0; x < indent; x++ )
+                        {
+                            sb.append( INDENT_STRING );
+                        }
+                    }
+                    
+                    // append the curly
+                    sb.append( c );
+                    
+                    // start a new line and increment indent
+                    sb.append( NEWLINE );
+                    newLineStarted = true;
+                    indent++;
+                    for ( int x = 0; x < indent; x++ )
+                    {
+                        sb.append( INDENT_STRING );
+                    }
+                }
+            }
+            else if (c == '}' && !inQuotedString )
+            {
+                if(oneLineMode)
+                {
+                    // no new line in one-line mode
+                    sb.append( c );
+                    newLineStarted = false;
+                    
+                    // closed curly indicates end of one-line mode
+                    oneLineMode = false;
+                }
+                else
+                {
+                    // decrement indent
+                    indent--;
+    
+                    // start a new line, but avoid blank lines if there are multiple closed
curlies
+                    if( newLineStarted )
+                    {
+                        // delete one indent 
+                        sb.delete( sb.length()-INDENT_STRING.length(), sb.length() );
+                    }
+                    else
+                    {
+                        sb.append( NEWLINE );
+                        for ( int x = 0; x < indent; x++ )
+                        {
+                            sb.append( INDENT_STRING );
+                        }
+                    }
+                    
+                    // append the curly
+                    sb.append( c );
+                    
+                    // start a new line 
+                    sb.append( NEWLINE );
+                    newLineStarted = true;
+                    for ( int x = 0; x < indent; x++ )
+                    {
+                        sb.append( INDENT_STRING );
+                    }
+                }
+            }
+            else if (c == ',' && !inQuotedString )
+            {
+                // start new line on comma
+                if(oneLineMode)
+                {
+                    sb.append( c );
+                    newLineStarted = false;
+                }
+                else
+                {
+                    sb.append( c );
+                    
+                    sb.append( NEWLINE );
+                    newLineStarted = true;
+                    
+                    for ( int x = 0; x < indent; x++ )
+                    {
+                        sb.append( INDENT_STRING );
+                    }
+                }
+            }
+            else if ( Character.isWhitespace( c ) )
+            {
+                char c1 = 'A';
+                if(i+1 < contentLength )
+                {
+                    c1 = content.charAt( i+1 );
+                }
+                
+                if( newLineStarted )
+                {
+                    // ignore space after starting a new line
+                }
+                else if (c == '\n' || c == '\r' )
+                {
+                    // ignore new lines
+                }
+                else if( Character.isWhitespace( c1 ) || c1 == '\n' || c1 == '\r' )
+                {
+                    // compress whitespaces
+                }
+                else
+                {
+                    sb.append( c );
+                }
+            }
+            
+            else
+            {
+                // default case: append the char
+                sb.append( c );
+                newLineStarted = false;
+            }
+        }
+
+        return sb.toString();
+    }
+
+
+    /**
+     * Checks if an expression could be appended in one line. That is 
+     * if the expression starting at index i doesn't contain nested
+     * expressions and only one comma.
+     * 
+     * @param i the starting index of the expression
+     * @param content the content
+     * 
+     * @return true, if the expression could be appended in one line
+     */
+    private boolean checkInOneLine( int i, String content )
+    {
+        // flag to track if we are within a quoted string
+        boolean inQuote = false;
+        
+        // counter for commas
+        int commaCounter = 0;
+
+        int contentLength = content.length();
+        for ( int k=i+1; k<contentLength; k++)
+        {
+            char c = content.charAt( k );
+
+            // track quotes
+            if( c == '"')
+            {
+                inQuote = !inQuote;
+            }
+            
+            // open curly indicates nested expression
+            if( ( c == '{'  )  && !inQuote )
+            {
+                return false;
+            }
+            
+            // closing curly indicates end of expression
+            if ( c == '}' && !inQuote ) 
+            {
+                return true;
+            }
+            
+            // allow only single comma in an expression in one line
+            if (c == ',' && !inQuote )
+            {
+                commaCounter++;
+                if(commaCounter > 1)
+                {
+                    return false;
+                }
+            }
+        }
+        
+        return false;
+    }
+    
+}

Modified: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemSourceEditorComposite.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemSourceEditorComposite.java?view=diff&rev=524445&r1=524444&r2=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemSourceEditorComposite.java
(original)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemSourceEditorComposite.java
Sat Mar 31 09:42:19 2007
@@ -24,15 +24,24 @@
 
 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
+import org.apache.directory.shared.ldap.aci.ACIItem;
 import org.apache.directory.shared.ldap.aci.ACIItemParser;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
 import org.eclipse.jface.text.source.SourceViewer;
 import org.eclipse.jface.text.source.SourceViewerConfiguration;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 
 
@@ -47,6 +56,9 @@
 
     /** The source editor */
     private SourceViewer sourceEditor;
+    
+    /** The source editor configuration. */
+    private SourceViewerConfiguration configuration;
 
 
     /**
@@ -72,9 +84,9 @@
     {
         // create source editor
         sourceEditor = new SourceViewer( this, null, null, false, SWT.BORDER | SWT.V_SCROLL
| SWT.H_SCROLL );
-
+        
         // setup basic configuration
-        SourceViewerConfiguration configuration = new ACISourceViewerConfiguration();
+        configuration = new ACISourceViewerConfiguration();
         sourceEditor.configure( configuration );
 
         // set text font
@@ -84,7 +96,6 @@
         // setup document
         IDocument document = new Document();
         sourceEditor.setDocument( document );
-
     }
 
 
@@ -113,6 +124,11 @@
     public void forceSetInput( String input )
     {
         sourceEditor.getDocument().set( input );
+        
+        // format
+        IRegion region = new Region( 0, sourceEditor.getDocument().getLength() );
+        configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(),
region );
+
     }
 
 
@@ -129,9 +145,14 @@
         String input = forceGetInput();
 
         ACIItemParser parser = Activator.getDefault().getACIItemParser();
-        parser.parse( input );
+        ACIItem aciItem = parser.parse( input );
 
-        return input;
+        StringBuffer buffer = new StringBuffer();
+        if(aciItem != null)
+        {
+            aciItem.printToBuffer( buffer );
+        }
+        return buffer.toString();
     }
 
 
@@ -155,6 +176,16 @@
     public void setContext( ACIItemValueWithContext context )
     {
         
+    }
+
+
+    /**
+     * Formats the content.
+     */
+    public void format()
+    {
+      IRegion region = new Region( 0, sourceEditor.getDocument().getLength() );
+      configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(),
region );
     }
     
 }

Modified: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemTabFolderComposite.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemTabFolderComposite.java?view=diff&rev=524445&r1=524444&r2=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemTabFolderComposite.java
(original)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACIItemTabFolderComposite.java
Sat Mar 31 09:42:19 2007
@@ -284,4 +284,14 @@
         visualComposite.setContext( context );
     }
 
+
+    /**
+     * Formats the content.
+     */
+    public void format()
+    {
+        sourceComposite.format();
+        //visualComposite.format();
+    }
+
 }

Modified: directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACISourceViewerConfiguration.java
URL: http://svn.apache.org/viewvc/directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACISourceViewerConfiguration.java?view=diff&rev=524445&r1=524444&r2=524445
==============================================================================
--- directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACISourceViewerConfiguration.java
(original)
+++ directory/ldapstudio/trunk/ldapstudio-aciitemeditor/src/main/java/org/apache/directory/ldapstudio/aciitemeditor/widgets/ACISourceViewerConfiguration.java
Sat Mar 31 09:42:19 2007
@@ -22,10 +22,14 @@
 
 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
 import org.apache.directory.ldapstudio.aciitemeditor.dialogs.DialogContentAssistant;
+import org.eclipse.jface.text.IAutoEditStrategy;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.contentassist.ContentAssistant;
 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 import org.eclipse.jface.text.contentassist.IContentAssistant;
+import org.eclipse.jface.text.formatter.ContentFormatter;
+import org.eclipse.jface.text.formatter.IContentFormatter;
+import org.eclipse.jface.text.formatter.IFormattingStrategy;
 import org.eclipse.jface.text.presentation.IPresentationReconciler;
 import org.eclipse.jface.text.presentation.PresentationReconciler;
 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
@@ -75,5 +79,22 @@
         assistant.setContextInformationPopupOrientation( IContentAssistant.CONTEXT_INFO_ABOVE
);
 
         return assistant;
+    }
+    
+    @Override
+    public IContentFormatter getContentFormatter( ISourceViewer sourceViewer )
+    {
+        ContentFormatter formatter = new ContentFormatter();
+        IFormattingStrategy formattingStrategy = new ACIFormattingStrategy( sourceViewer
);
+        formatter.enablePartitionAwareFormatting( false );
+        formatter.setFormattingStrategy( formattingStrategy, IDocument.DEFAULT_CONTENT_TYPE
);
+        return formatter;
+    }
+    
+    @Override
+    public IAutoEditStrategy[] getAutoEditStrategies( ISourceViewer sourceViewer, String
contentType )
+    {
+        // TODO Auto-generated method stub
+        return super.getAutoEditStrategies( sourceViewer, contentType );
     }
 }



Mime
View raw message