directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r491713 - in /directory/trunks/ldapstudio/ldapstudio-browser-ui: META-INF/ src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/
Date Tue, 02 Jan 2007 00:00:06 GMT
Author: seelmann
Date: Mon Jan  1 16:00:06 2007
New Revision: 491713

URL: http://svn.apache.org/viewvc?view=rev&rev=491713
Log:
Made LDIF editor ready to work in RCP

Added:
    directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/PathEditorInput.java
Modified:
    directory/trunks/ldapstudio/ldapstudio-browser-ui/META-INF/MANIFEST.MF
    directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifDocumentProvider.java
    directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifEditor.java
    directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/NonExistingLdifEditorInput.java

Modified: directory/trunks/ldapstudio/ldapstudio-browser-ui/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-ui/META-INF/MANIFEST.MF?view=diff&rev=491713&r1=491712&r2=491713
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-ui/META-INF/MANIFEST.MF (original)
+++ directory/trunks/ldapstudio/ldapstudio-browser-ui/META-INF/MANIFEST.MF Mon Jan  1 16:00:06
2007
@@ -8,10 +8,9 @@
 Bundle-Localization: plugin
 Require-Bundle: org.apache.directory.ldapstudio.browser.core,
  org.eclipse.core.runtime,
- org.eclipse.core.resources,
+ org.eclipse.core.filesystem,
  org.eclipse.search,
  org.eclipse.ui,
- org.eclipse.ui.ide,
  org.eclipse.ui.editors,
  org.eclipse.ui.workbench.texteditor,
  org.eclipse.ui.views,

Modified: directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifDocumentProvider.java
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifDocumentProvider.java?view=diff&rev=491713&r1=491712&r2=491713
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifDocumentProvider.java
(original)
+++ directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifDocumentProvider.java
Mon Jan  1 16:00:06 2007
@@ -21,6 +21,15 @@
 package org.apache.directory.ldapstudio.browser.ui.editors.ldif;
 
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -29,19 +38,27 @@
 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser;
+import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
 import org.apache.directory.ldapstudio.browser.ui.editors.ldif.text.LdifExternalAnnotationModel;
-
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.operation.IRunnableContext;
+import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.DocumentEvent;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentListener;
 import org.eclipse.jface.text.Region;
 import org.eclipse.jface.text.TextUtilities;
 import org.eclipse.jface.text.source.IAnnotationModel;
-import org.eclipse.ui.editors.text.TextFileDocumentProvider;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IPathEditorInput;
+import org.eclipse.ui.texteditor.AbstractDocumentProvider;
 
 
-public class LdifDocumentProvider extends TextFileDocumentProvider implements IDocumentListener
+public class LdifDocumentProvider extends AbstractDocumentProvider implements IDocumentListener
 {
 
     private final LdifParser ldifParser;
@@ -66,43 +83,6 @@
     }
 
 
-    protected FileInfo createFileInfo( Object element ) throws CoreException
-    {
-        FileInfo info = super.createFileInfo( element );
-
-        if ( info != null )
-        {
-            // setup document partitioning
-            IDocument document = info.fTextFileBuffer.getDocument();
-            ldifDocumentSetupParticipant.setup( document );
-
-            // initial parsing of whole document
-            this.ldifModel = this.ldifParser.parse( document.get() );
-
-            // add listener for incremental parsing
-            document.addDocumentListener( this );
-
-            IAnnotationModel annotationModel = info.fTextFileBuffer.getAnnotationModel();
-            if ( annotationModel == null )
-            {
-                annotationModel = new LdifExternalAnnotationModel();
-                info.fModel = annotationModel;
-            }
-        }
-
-        return info;
-    }
-
-
-    protected void disposeFileInfo( Object element, FileInfo info )
-    {
-        IDocument document = info.fTextFileBuffer.getDocument();
-        document.removeDocumentListener( this );
-
-        super.disposeFileInfo( element, info );
-    }
-
-
     public LdifFile getLdifModel()
     {
         return ldifModel;
@@ -119,10 +99,8 @@
      */
     public void documentChanged( DocumentEvent event )
     {
-
         try
         {
-
             int changeOffset = event.getOffset();
             int replacedTextLength = event.getLength();
             int insertedTextLength = event.getText() != null ? event.getText().length() :
0;
@@ -134,7 +112,7 @@
 
             // get containers to replace (from changeOffset till
             // changeOffset+replacedTextLength, check end of record)
-            List oldContainerList = new ArrayList();
+            List<LdifContainer> oldContainerList = new ArrayList<LdifContainer>();
             LdifContainer[] containers = this.ldifModel.getContainers();
             for ( int i = 0; i < containers.length; i++ )
             {
@@ -220,6 +198,251 @@
             e.printStackTrace();
         }
 
+    }
+
+
+    /**
+     * Creates an LDIF annotation model.
+     */
+    protected IAnnotationModel createAnnotationModel( Object element ) throws CoreException
+    {
+        return new LdifExternalAnnotationModel();
+    }
+
+
+    /**
+     * Tries to read the file pointed at by <code>input</code> if it is an
+     * <code>IPathEditorInput</code>. If the file does not exist, <code>true</code>
+     * is returned.
+     *  
+     * @param document the document to fill with the contents of <code>input</code>
+     * @param input the editor input
+     * @return <code>true</code> if setting the content was successful or no
file exists, <code>false</code> otherwise
+     * @throws CoreException if reading the file fails
+     */
+    private boolean setDocumentContent( IDocument document, IEditorInput input ) throws CoreException
+    {
+        // TODO: handle encoding
+        Reader reader;
+        try
+        {
+            if ( input instanceof IPathEditorInput )
+            {
+                reader = new FileReader( ( ( IPathEditorInput ) input ).getPath().toFile()
);
+            }
+            else
+            {
+                return false;
+            }
+        }
+        catch ( FileNotFoundException e )
+        {
+            // return empty document and save later
+            return true;
+        }
+
+        try
+        {
+            setDocumentContent( document, reader );
+            return true;
+        }
+        catch ( IOException e )
+        {
+            throw new CoreException( new Status( IStatus.ERROR, BrowserUIPlugin.PLUGIN_ID,
IStatus.OK,
+                "error reading file", e ) ); //$NON-NLS-1$
+        }
+    }
+
+
+    /**
+     * Reads in document content from a reader and fills <code>document</code>
+     * 
+     * @param document the document to fill
+     * @param reader the source
+     * @throws IOException if reading fails
+     */
+    private void setDocumentContent( IDocument document, Reader reader ) throws IOException
+    {
+        Reader in = new BufferedReader( reader );
+        try
+        {
+            StringBuffer buffer = new StringBuffer( 512 );
+            char[] readBuffer = new char[512];
+            int n = in.read( readBuffer );
+            while ( n > 0 )
+            {
+                buffer.append( readBuffer, 0, n );
+                n = in.read( readBuffer );
+            }
+
+            document.set( buffer.toString() );
+
+        }
+        finally
+        {
+            in.close();
+        }
+    }
+
+
+    /**
+     * Set up the document: partitioning and incremental parser
+     * 
+     * @param document the new document
+     */
+    private void setupDocument( IDocument document )
+    {
+
+        // setup document partitioning
+        ldifDocumentSetupParticipant.setup( document );
+
+        // initial parsing of whole document
+        this.ldifModel = this.ldifParser.parse( document.get() );
+
+        // add listener for incremental parsing
+        document.addDocumentListener( this );
+
+    }
+
+
+    /**
+     * Remove document listener.
+     */
+    protected void disposeElementInfo( Object element, ElementInfo info )
+    {
+        IDocument document = info.fDocument;
+        document.removeDocumentListener( this );
+
+        super.disposeElementInfo( element, info );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    protected IDocument createDocument( Object element ) throws CoreException
+    {
+        if ( element instanceof IEditorInput )
+        {
+            IDocument document = new Document();
+            if ( setDocumentContent( document, ( IEditorInput ) element ) )
+            {
+                setupDocument( document );
+            }
+            return document;
+        }
+
+        return null;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void doSaveDocument( IProgressMonitor monitor, Object element, IDocument document,
boolean overwrite )
+        throws CoreException
+    {
+
+        if ( element instanceof IPathEditorInput )
+        {
+            IPathEditorInput pei = ( IPathEditorInput ) element;
+            IPath path = pei.getPath();
+            File file = path.toFile();
+
+            try
+            {
+                file.createNewFile();
+
+                if ( file.exists() )
+                {
+                    if ( file.canWrite() )
+                    {
+                        Writer writer = new FileWriter( file );
+                        writeDocumentContent( document, writer, monitor );
+                    }
+                    else
+                    {
+                        throw new CoreException( new Status( IStatus.ERROR,
+                            "org.eclipse.ui.examples.rcp.texteditor", IStatus.OK, "file is
read-only", null ) ); //$NON-NLS-1$ //$NON-NLS-2$
+                    }
+                }
+                else
+                {
+                    throw new CoreException( new Status( IStatus.ERROR,
+                        "org.eclipse.ui.examples.rcp.texteditor", IStatus.OK, "error creating
file", null ) ); //$NON-NLS-1$ //$NON-NLS-2$
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new CoreException( new Status( IStatus.ERROR,
+                    "org.eclipse.ui.examples.rcp.texteditor", IStatus.OK, "error when saving
file", e ) ); //$NON-NLS-1$ //$NON-NLS-2$
+            }
+
+        }
+    }
+
+
+    /**
+     * Saves the document contents to a stream.
+     * 
+     * @param document the document to save
+     * @param writer the stream to save it to
+     * @param monitor a progress monitor to report progress
+     * @throws IOException if writing fails
+     */
+    private void writeDocumentContent( IDocument document, Writer writer, IProgressMonitor
monitor ) throws IOException
+    {
+        Writer out = new BufferedWriter( writer );
+        try
+        {
+            out.write( document.get() );
+        }
+        finally
+        {
+            out.close();
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    protected IRunnableContext getOperationRunner( IProgressMonitor monitor )
+    {
+        return null;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isModifiable( Object element )
+    {
+        if ( element instanceof IPathEditorInput )
+        {
+            IPathEditorInput pei = ( IPathEditorInput ) element;
+            File file = pei.getPath().toFile();
+            return file.canWrite() || !file.exists(); // Allow to edit new files
+        }
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isReadOnly( Object element )
+    {
+        return !isModifiable( element );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isStateValidated( Object element )
+    {
+        return true;
     }
 
 }

Modified: directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifEditor.java
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifEditor.java?view=diff&rev=491713&r1=491712&r2=491713
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifEditor.java
(original)
+++ directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/LdifEditor.java
Mon Jan  1 16:00:06 2007
@@ -46,11 +46,11 @@
 import org.apache.directory.ldapstudio.browser.ui.valueeditors.IValueEditor;
 import org.apache.directory.ldapstudio.browser.ui.valueeditors.internal.ValueEditorManager;
 import org.apache.directory.ldapstudio.browser.ui.widgets.BaseWidgetUtils;
-
-import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IContributionItem;
@@ -60,6 +60,7 @@
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.action.ToolBarManager;
 import org.eclipse.jface.commands.ActionHandler;
+import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.text.ITextHover;
@@ -70,6 +71,7 @@
 import org.eclipse.jface.text.source.projection.ProjectionSupport;
 import org.eclipse.jface.text.source.projection.ProjectionViewer;
 import org.eclipse.jface.util.PropertyChangeEvent;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.ViewForm;
 import org.eclipse.swt.events.ModifyEvent;
@@ -79,11 +81,14 @@
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IPageLayout;
 import org.eclipse.ui.IPartListener2;
+import org.eclipse.ui.IPathEditorInput;
 import org.eclipse.ui.ISharedImages;
 import org.eclipse.ui.IWorkbenchPartReference;
 import org.eclipse.ui.PartInitException;
@@ -93,7 +98,6 @@
 import org.eclipse.ui.contexts.IContextService;
 import org.eclipse.ui.editors.text.EditorsUI;
 import org.eclipse.ui.editors.text.TextEditor;
-import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.part.IShowInTargetList;
 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
 import org.eclipse.ui.texteditor.ContentAssistAction;
@@ -154,10 +158,9 @@
 
             ISourceViewer sourceViewer = getSourceViewer();
             if ( sourceViewer == null )
+            {
                 return;
-
-            // ((LdifSourceViewerConfiguration)
-            // getSourceViewerConfiguration()).handlePropertyChangeEvent(event);
+            }
 
             int topIndex = getSourceViewer().getTopIndex();
             getSourceViewer().getDocument().set( getSourceViewer().getDocument().get() );
@@ -192,12 +195,10 @@
 
     public void init( IEditorSite site, IEditorInput input ) throws PartInitException
     {
-
-        if ( input instanceof FileEditorInput )
+        if ( input instanceof IPathEditorInput )
         {
-            FileEditorInput fei = ( FileEditorInput ) input;
-            IFile file = fei.getFile();
-            IPath path = file.getLocation();
+            IPathEditorInput pei = ( IPathEditorInput ) input;
+            IPath path = pei.getPath();
             File javaIoFile = path.toFile();
             long fileLength = javaIoFile.length();
             if ( fileLength > ( 1 * 1024 * 1024 ) )
@@ -290,7 +291,6 @@
         IContributionItem[] items = menu.getItems();
         for ( int i = 0; i < items.length; i++ )
         {
-
             if ( items[i] instanceof ActionContributionItem )
             {
                 ActionContributionItem aci = ( ActionContributionItem ) items[i];
@@ -302,15 +302,6 @@
                 {
                     menu.remove( items[i] );
                 }
-                // if(aci.getAction() instanceof TextOperationAction) {
-                // TextOperationAction toa =
-                // (TextOperationAction)aci.getAction();
-                // String id = toa.getActionDefinitionId();
-                // IAction referenceAction =
-                // getAction(ITextEditorActionConstants.CUT);
-                // boolean equal = toa==referenceAction;
-                // System.out.println(equal);
-                // }
             }
         }
 
@@ -503,38 +494,6 @@
     }
 
 
-    protected void configureSourceViewerDecorationSupport()
-    {
-
-        // Iterator e=
-        // fAnnotationPreferences.getAnnotationPreferences().iterator();
-        // while (e.hasNext())
-        // fSourceViewerDecorationSupport.setAnnotationPreference((AnnotationPreference)
-        // e.next());
-        // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(DefaultMarkerAnnotationAccess.UNKNOWN,
-        // UNKNOWN_INDICATION_COLOR, UNKNOWN_INDICATION,
-        // UNKNOWN_INDICATION_IN_OVERVIEW_RULER, 0);
-        //			
-        // fSourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(CURRENT_LINE,
-        // CURRENT_LINE_COLOR);
-        // fSourceViewerDecorationSupport.setMarginPainterPreferenceKeys(PRINT_MARGIN,
-        // PRINT_MARGIN_COLOR, PRINT_MARGIN_COLUMN);
-        // fSourceViewerDecorationSupport.setSymbolicFontName(getFontPropertyPreferenceKey());
-    }
-
-
-    protected void doSetInput( IEditorInput input ) throws CoreException
-    {
-        // this.ldifModel = null;
-        super.doSetInput( input );
-
-        // setOutlinePageInput();
-        // if (fFoldingStructureProvider != null) {
-        // fFoldingStructureProvider.setDocument(getDocumentProvider().getDocument(input));
-        // }
-    }
-
-
     public LdifFile getLdifModel()
     {
         IDocumentProvider provider = getDocumentProvider();
@@ -588,13 +547,123 @@
     }
 
 
+    /**
+     * This implementation checks if the input is of type
+     * NonExistingLdifEditorInput. In that case doSaveAs() is 
+     * called to prompt for a new file name and location.
+     */
+    public void doSave( IProgressMonitor progressMonitor )
+    {
+        final IEditorInput input = getEditorInput();
+        if ( input instanceof NonExistingLdifEditorInput )
+        {
+            super.doSaveAs();
+            return;
+        }
+
+        super.doSave( progressMonitor );
+    }
+
+
+    /**
+     * The input could be one of the following types:
+     * - NonExistingLdifEditorInput: New file, not yet saved
+     * - PathEditorInput: file opened with our internal "Open File.." action
+     * - FileEditorInput: file is within workspace
+     * - JavaFileEditorInput: file opend with "Open File..." action from org.eclipse.ui.editor

+     * 
+     * In RCP the FileDialog appears.
+     * In IDE the super implementation is called.
+     * To detect if this plugin runs in IDE the org.eclipse.ui.ide extension point is checked.
+     * 
+     */
     protected void performSaveAs( IProgressMonitor progressMonitor )
     {
+        // detect IDE or RCP: 
+        // check if perspective org.eclipse.ui.resourcePerspective is available
+        boolean isIDE = BrowserUIPlugin.isIDEEnvironment();
+
+        if ( isIDE )
+        {
+            // Just call super implementation for now
+            IPreferenceStore store = EditorsUI.getPreferenceStore();
+            String key = getEditorSite().getId() + ".internal.delegateSaveAs"; // $NON-NLS-1$
+            store.setValue( key, true );
+            super.performSaveAs( progressMonitor );
+        }
+        else
+        {
+            // Open FileDialog
+            Shell shell = getSite().getShell();
+            final IEditorInput input = getEditorInput();
+
+            IDocumentProvider provider = getDocumentProvider();
+            final IEditorInput newInput;
+
+            FileDialog dialog = new FileDialog( shell, SWT.SAVE );
+
+            String path = dialog.open();
+            if ( path == null )
+            {
+                if ( progressMonitor != null )
+                {
+                    progressMonitor.setCanceled( true );
+                }
+                return;
+            }
+
+            // Check whether file exists and if so, confirm overwrite
+            final File externalFile = new File( path );
+            if ( externalFile.exists() )
+            {
+                MessageDialog overwriteDialog = new MessageDialog( shell, "Overwrite", null,
"Overwrite?",
+                    MessageDialog.WARNING, new String[]
+                        { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 1 ); //
'No' is the default
+                if ( overwriteDialog.open() != Window.OK )
+                {
+                    if ( progressMonitor != null )
+                    {
+                        progressMonitor.setCanceled( true );
+                        return;
+                    }
+                }
+            }
+
+            IPath iPath = new Path( path );
+            newInput = new PathEditorInput( iPath );
+
+            boolean success = false;
+            try
+            {
+                provider.aboutToChange( newInput );
+                provider.saveDocument( progressMonitor, newInput, provider.getDocument( input
), true );
+                success = true;
+            }
+            catch ( CoreException x )
+            {
+                final IStatus status = x.getStatus();
+                if ( status == null || status.getSeverity() != IStatus.CANCEL )
+                {
+                    String title = "Error in Save As...";
+                    String msg = "Error in Save As... " + x.getMessage();
+                    MessageDialog.openError( shell, title, msg );
+                }
+            }
+            finally
+            {
+                provider.changed( newInput );
+                if ( success )
+                {
+                    setInput( newInput );
+                }
+            }
+
+            if ( progressMonitor != null )
+            {
+                progressMonitor.setCanceled( !success );
+            }
+        }
 
-        IPreferenceStore store = EditorsUI.getPreferenceStore();
-        String key = getEditorSite().getId() + ".internal.delegateSaveAs"; // $NON-NLS-1$
-        store.setValue( key, true );
-        super.performSaveAs( progressMonitor );
     }
 
     private IContextActivation contextActivation;

Modified: directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/NonExistingLdifEditorInput.java
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/NonExistingLdifEditorInput.java?view=diff&rev=491713&r1=491712&r2=491713
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/NonExistingLdifEditorInput.java
(original)
+++ directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/NonExistingLdifEditorInput.java
Mon Jan  1 16:00:06 2007
@@ -21,78 +21,133 @@
 package org.apache.directory.ldapstudio.browser.ui.editors.ldif;
 
 
+import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.resource.ImageDescriptor;
-import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IPathEditorInput;
 import org.eclipse.ui.IPersistableElement;
 import org.eclipse.ui.editors.text.ILocationProvider;
 
 
-public class NonExistingLdifEditorInput implements IEditorInput, ILocationProvider
+/**
+ * This EditorInput is used to create a LDIF file that isn't saved yet.
+ * It is used from File->New, but also from the embedded LDIF editors
+ * in modification view, in batch operation wizard and the LDIF preference page.
+ * 
+ * Inspired from org.eclipse.ui.internal.editors.text.NonExistingFileEditorInput.java
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class NonExistingLdifEditorInput implements IPathEditorInput, ILocationProvider
 {
-
+    /** The counter to create unique names */
     private static int counter = 0;
 
+    /** The name, displayed in Editor tab */
     private String name;
 
 
+    /**
+     * Creates a new instance of NonExistingLdifEditorInput.
+     */
     public NonExistingLdifEditorInput()
     {
-        ++counter;
+        counter++;
         name = "LDIF " + counter; //$NON-NLS-1$
     }
 
 
+    /**
+     * As the name says, this implementations always returns false.
+     */
     public boolean exists()
     {
         return false;
     }
 
 
+    /**
+     * Returns the LDIF file image.
+     */
     public ImageDescriptor getImageDescriptor()
     {
-        return null;
+        return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_BROWSER_LDIFEDITOR
);
     }
 
 
+    /**
+     * Returns the name.
+     */
     public String getName()
     {
         return name;
     }
 
 
+    /**
+     * As the name says, this implementations always returns false.
+     */
     public IPersistableElement getPersistable()
     {
         return null;
     }
 
 
+    /**
+     * Returns the name.
+     */
     public String getToolTipText()
     {
         return name;
     }
 
 
+    /**
+     * An EditorInput must return a good ILocationProvider, otherwise
+     * the editor is not editable.
+     */
     public Object getAdapter( Class adapter )
     {
         if ( ILocationProvider.class.equals( adapter ) )
+        {
             return this;
+        }
+        
         return Platform.getAdapterManager().getAdapter( this, adapter );
     }
 
 
+    /**
+     * This implementation returns a path that point to the plugin's
+     * state location. 
+     * 
+     * A valid, writeable path must be returned, otherwise the editor
+     * is not editable.
+     */
     public IPath getPath( Object element )
     {
-        return BrowserUIPlugin.getDefault().getStateLocation().append( name + ".ldif" );
+        if ( element instanceof NonExistingLdifEditorInput )
+        {
+            NonExistingLdifEditorInput input = ( NonExistingLdifEditorInput ) element;
+            return input.getPath();
+        }
+        
+        return null;
     }
 
 
+    /** 
+     * This implemention just compares the names
+     */
     public boolean equals( Object o )
     {
         if ( o == this )
+        {
             return true;
+        }
 
         if ( o instanceof NonExistingLdifEditorInput )
         {
@@ -104,8 +159,26 @@
     }
 
 
+    /**
+     * Returns hash code of the name string.
+     */
     public int hashCode()
     {
         return name.hashCode();
     }
+
+
+    /**
+     * This implementation returns a path that point to the plugin's
+     * state location. The state location is a platform indepentend 
+     * location that is writeable.
+     * 
+     * A valid, writeable path must be returned, otherwise the editor
+     * is not editable.
+     */
+    public IPath getPath()
+    {
+        return BrowserUIPlugin.getDefault().getStateLocation().append( name + ".ldif" );
+    }
+
 }

Added: directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/PathEditorInput.java
URL: http://svn.apache.org/viewvc/directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/PathEditorInput.java?view=auto&rev=491713
==============================================================================
--- directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/PathEditorInput.java
(added)
+++ directory/trunks/ldapstudio/ldapstudio-browser-ui/src/main/java/org/apache/directory/ldapstudio/browser/ui/editors/ldif/PathEditorInput.java
Mon Jan  1 16:00:06 2007
@@ -0,0 +1,171 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.apache.directory.ldapstudio.browser.ui.editors.ldif;
+
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.IPathEditorInput;
+import org.eclipse.ui.IPersistableElement;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.editors.text.ILocationProvider;
+
+
+/**
+ * EditorInput that stores a path.
+ */
+
+/**
+ * This EditorInput is used to open LDIF files that are located in the local file system.
+ * 
+ * Inspired from org.eclipse.ui.internal.editors.text.NonExistingFileEditorInput.java
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class PathEditorInput implements IPathEditorInput, ILocationProvider
+{
+
+    /** The absolute path in local file system */
+    private IPath path;
+
+
+    /**
+     * 
+     * Creates a new instance of PathEditorInput.
+     *
+     * @param path the absolute path
+     */
+    public PathEditorInput( IPath path )
+    {
+        if ( path == null )
+        {
+            throw new IllegalArgumentException();
+        }
+
+        this.path = path;
+    }
+
+
+    /**
+     * Returns hash code of the path.
+     */
+    public int hashCode()
+    {
+        return path.hashCode();
+    }
+
+
+    /** 
+     * This implemention just compares the names
+     */
+    public boolean equals( Object o )
+    {
+        if ( this == o )
+        {
+            return true;
+        }
+
+        if ( o instanceof PathEditorInput )
+        {
+            PathEditorInput input = ( PathEditorInput ) o;
+            return path.equals( input.path );
+        }
+
+        return false;
+    }
+
+
+    /*
+     * @see org.eclipse.ui.IEditorInput#exists()
+     */
+    public boolean exists()
+    {
+        return path.toFile().exists();
+    }
+
+
+    /**
+     * Returns the LDIF file image.
+     */
+    public ImageDescriptor getImageDescriptor()
+    {
+        return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor( path.toString()
);
+    }
+
+
+    /**
+     * Returns the file name only.
+     */
+    public String getName()
+    {
+        return path.toFile().getName();
+        //return path.toString();
+    }
+
+
+    /**
+     * Returns the complete path. 
+     */
+    public String getToolTipText()
+    {
+        return path.makeRelative().toOSString();
+    }
+
+
+    /**
+     * Returns the path. 
+     */
+    public IPath getPath()
+    {
+        return path;
+    }
+
+
+    /**
+     * An EditorInput must return a good ILocationProvider, otherwise
+     * the editor is not editable.
+     */
+    public Object getAdapter( Class adapter )
+    {
+        if ( ILocationProvider.class.equals( adapter ) )
+        {
+            return this;
+        }
+
+        return Platform.getAdapterManager().getAdapter( this, adapter );
+    }
+
+
+    /*
+     * @see org.eclipse.ui.IEditorInput#getPersistable()
+     */
+    public IPersistableElement getPersistable()
+    {
+        return null;
+    }
+
+
+    /**
+     * Returns the path.
+     */
+    public IPath getPath( Object element )
+    {
+        if ( element instanceof PathEditorInput )
+        {
+            PathEditorInput input = ( PathEditorInput ) element;
+            return input.getPath();
+        }
+
+        return null;
+    }
+}



Mime
View raw message