diff dwtx/jface/text/projection/ProjectionDocumentManager.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/projection/ProjectionDocumentManager.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,227 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.projection.ProjectionDocumentManager;
+
+import dwt.dwthelper.utils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import dwtx.jface.text.DocumentEvent;
+import dwtx.jface.text.IDocument;
+import dwtx.jface.text.IDocumentInformationMapping;
+import dwtx.jface.text.IDocumentListener;
+import dwtx.jface.text.ISlaveDocumentManager;
+import dwtx.jface.text.ISlaveDocumentManagerExtension;
+
+
+/**
+ * A <code>ProjectionDocumentManager</code> is one particular implementation
+ * of {@link dwtx.jface.text.ISlaveDocumentManager}. This manager
+ * creates so called projection documents (see
+ * {@link dwtx.jface.text.projection.ProjectionDocument}as slave
+ * documents for given master documents.
+ * <p>
+ * A projection document represents a particular projection of the master
+ * document and is accordingly adapted to changes of the master document. Vice
+ * versa, the master document is accordingly adapted to changes of its slave
+ * documents. The manager does not maintain any particular management structure
+ * but utilizes mechanisms given by {@link dwtx.jface.text.IDocument}
+ * such as position categories and position updaters.
+ * <p>
+ * Clients can instantiate this class. This class is not intended to be
+ * subclassed.</p>
+ *
+ * @since 3.0
+ * @noextend This class is not intended to be subclassed by clients.
+ */
+public class ProjectionDocumentManager : IDocumentListener, ISlaveDocumentManager, ISlaveDocumentManagerExtension {
+
+    /** Registry for master documents and their projection documents. */
+    private Map fProjectionRegistry= new HashMap();
+
+    /**
+     * Registers the given projection document for the given master document.
+     *
+     * @param master the master document
+     * @param projection the projection document
+     */
+    private void add(IDocument master, ProjectionDocument projection) {
+        List list= (List) fProjectionRegistry.get(master);
+        if (list is null) {
+            list= new ArrayList(1);
+            fProjectionRegistry.put(master, list);
+        }
+        list.add(projection);
+    }
+
+    /**
+     * Unregisters the given projection document from its master.
+     *
+     * @param master the master document
+     * @param projection the projection document
+     */
+    private void remove(IDocument master, ProjectionDocument projection) {
+        List list= (List) fProjectionRegistry.get(master);
+        if (list !is null) {
+            list.remove(projection);
+            if (list.size() is 0)
+                fProjectionRegistry.remove(master);
+        }
+    }
+
+    /**
+     * Returns whether the given document is a master document.
+     *
+     * @param master the document
+     * @return <code>true</code> if the given document is a master document known to this manager
+     */
+    private bool hasProjection(IDocument master) {
+        return (fProjectionRegistry.get(master) instanceof List);
+    }
+
+    /**
+     * Returns an iterator enumerating all projection documents registered for the given document or
+     * <code>null</code> if the document is not a known master document.
+     *
+     * @param master the document
+     * @return an iterator for all registered projection documents or <code>null</code>
+     */
+    private Iterator getProjectionsIterator(IDocument master) {
+        List list= (List) fProjectionRegistry.get(master);
+        if (list !is null)
+            return list.iterator();
+        return null;
+    }
+
+    /**
+     * Informs all projection documents of the master document that issued the given document event.
+     *
+     * @param about indicates whether the change is about to happen or happened already
+     * @param masterEvent the document event which will be processed to inform the projection documents
+     */
+    protected void fireDocumentEvent(bool about, DocumentEvent masterEvent) {
+        IDocument master= masterEvent.getDocument();
+        Iterator e= getProjectionsIterator(master);
+        if (e is null)
+            return;
+
+        while (e.hasNext()) {
+            ProjectionDocument document= (ProjectionDocument) e.next();
+            if (about)
+                document.masterDocumentAboutToBeChanged(masterEvent);
+            else
+                document.masterDocumentChanged(masterEvent);
+        }
+    }
+
+    /*
+     * @see dwtx.jface.text.IDocumentListener#documentChanged(dwtx.jface.text.DocumentEvent)
+     */
+    public void documentChanged(DocumentEvent event) {
+        fireDocumentEvent(false, event);
+    }
+
+    /*
+     * @see dwtx.jface.text.IDocumentListener#documentAboutToBeChanged(dwtx.jface.text.DocumentEvent)
+     */
+    public void documentAboutToBeChanged(DocumentEvent event) {
+        fireDocumentEvent(true, event);
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#createMasterSlaveMapping(dwtx.jface.text.IDocument)
+     */
+    public IDocumentInformationMapping createMasterSlaveMapping(IDocument slave) {
+        if (slave instanceof ProjectionDocument) {
+            ProjectionDocument projectionDocument= (ProjectionDocument) slave;
+            return projectionDocument.getDocumentInformationMapping();
+        }
+        return null;
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#createSlaveDocument(dwtx.jface.text.IDocument)
+     */
+    public IDocument createSlaveDocument(IDocument master) {
+        if (!hasProjection(master))
+            master.addDocumentListener(this);
+        ProjectionDocument slave= createProjectionDocument(master);
+        add(master, slave);
+        return slave;
+    }
+
+    /**
+     * Factory method for projection documents.
+     *
+     * @param master the master document
+     * @return the newly created projection document
+     */
+    protected ProjectionDocument createProjectionDocument(IDocument master) {
+        return new ProjectionDocument(master);
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#freeSlaveDocument(dwtx.jface.text.IDocument)
+     */
+    public void freeSlaveDocument(IDocument slave) {
+        if (slave instanceof ProjectionDocument) {
+            ProjectionDocument projectionDocument= (ProjectionDocument) slave;
+            IDocument master= projectionDocument.getMasterDocument();
+            remove(master, projectionDocument);
+            projectionDocument.dispose();
+            if (!hasProjection(master))
+                master.removeDocumentListener(this);
+        }
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#getMasterDocument(dwtx.jface.text.IDocument)
+     */
+    public IDocument getMasterDocument(IDocument slave) {
+        if (slave instanceof ProjectionDocument)
+            return ((ProjectionDocument) slave).getMasterDocument();
+        return null;
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#isSlaveDocument(dwtx.jface.text.IDocument)
+     */
+    public bool isSlaveDocument(IDocument document) {
+        return (document instanceof ProjectionDocument);
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManager#setAutoExpandMode(dwtx.jface.text.IDocument, bool)
+     */
+    public void setAutoExpandMode(IDocument slave, bool autoExpanding) {
+        if (slave instanceof ProjectionDocument)
+            ((ProjectionDocument) slave).setAutoExpandMode(autoExpanding);
+    }
+
+    /*
+     * @see dwtx.jface.text.ISlaveDocumentManagerExtension#getSlaveDocuments(dwtx.jface.text.IDocument)
+     */
+    public IDocument[] getSlaveDocuments(IDocument master) {
+        List list= (List) fProjectionRegistry.get(master);
+        if (list !is null) {
+            IDocument[] result= new IDocument[list.size()];
+            list.toArray(result);
+            return result;
+        }
+        return null;
+    }
+}