diff options
Diffstat (limited to 'bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection')
5 files changed, 0 insertions, 1027 deletions
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionAnnotationModelChanges.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionAnnotationModelChanges.java deleted file mode 100644 index 7d415f9558..0000000000 --- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionAnnotationModelChanges.java +++ /dev/null @@ -1,54 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006 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 - *******************************************************************************/ -package org.eclipse.wst.xml.ui.internal.projection; - -import java.util.Map; - -import org.eclipse.jface.text.source.Annotation; -import org.w3c.dom.Node; - -/** - * Contains a set of projection model additions/deletions/modifications - */ -class ProjectionAnnotationModelChanges { - // copies of this class located in: - // org.eclipse.wst.xml.ui.internal.projection - // org.eclipse.wst.css.ui.internal.projection - // org.eclipse.wst.html.ui.internal.projection - // org.eclipse.jst.jsp.ui.internal.projection - private Node fNode; - private Annotation[] fDeletions; - private Map fAdditions; - private Annotation[] fModifications; - - public ProjectionAnnotationModelChanges(Node node, Annotation[] deletions, Map additions, Annotation[] modifications) { - fNode = node; - fDeletions = deletions; - fAdditions = additions; - fModifications = modifications; - } - - public Map getAdditions() { - return fAdditions; - } - - public Annotation[] getDeletions() { - return fDeletions; - } - - public Annotation[] getModifications() { - return fModifications; - } - - public Node getNode() { - return fNode; - } -} diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterFactoryXML.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterFactoryXML.java deleted file mode 100644 index 96cba42537..0000000000 --- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterFactoryXML.java +++ /dev/null @@ -1,157 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2005 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 - * - *******************************************************************************/ -package org.eclipse.wst.xml.ui.internal.projection; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import org.eclipse.jface.text.source.Annotation; -import org.eclipse.jface.text.source.projection.ProjectionViewer; -import org.eclipse.wst.sse.core.internal.provisional.AbstractAdapterFactory; -import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; -import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; -import org.w3c.dom.Node; - -public class ProjectionModelNodeAdapterFactoryXML extends AbstractAdapterFactory { - /** - * List of projection viewers currently associated with this projection - * model node adapter factory. - */ - private HashMap fProjectionViewers; - - public ProjectionModelNodeAdapterFactoryXML() { - super(ProjectionModelNodeAdapterXML.class, true); - } - - protected INodeAdapter createAdapter(INodeNotifier target) { - ProjectionModelNodeAdapterXML adapter = null; - - // create adapter for every element tag - if ((isActive()) && (target instanceof Node) && ((Node) target).getNodeType() == Node.ELEMENT_NODE) { - adapter = new ProjectionModelNodeAdapterXML(this); - adapter.updateAdapter((Node) target); - } - - return adapter; - } - - /** - * Return true if this factory is currently actively managing projection - * - * @return - */ - boolean isActive() { - return (fProjectionViewers != null && !fProjectionViewers.isEmpty()); - } - - /** - * Updates projection annotation model if document is not in flux. - * Otherwise, queues up the changes to be applied when document is ready. - * - * @param node - * @param deletions - * @param additions - * @param modifications - */ - void queueAnnotationModelChanges(Node node, Annotation[] deletions, Map additions, Annotation[] modifications) { - queueAnnotationModelChanges(node, deletions, additions, modifications, null); - } - - /** - * Updates projection annotation model for a specific projection viewer if - * document is not in flux. Otherwise, queues up the changes to be applied - * when document is ready. - * - * @param node - * @param deletions - * @param additions - * @param modifications - * @param viewer - */ - void queueAnnotationModelChanges(Node node, Annotation[] deletions, Map additions, Annotation[] modifications, ProjectionViewer viewer) { - // create a change object for latest change and add to queue - ProjectionAnnotationModelChanges newChange = new ProjectionAnnotationModelChanges(node, deletions, additions, modifications); - if (fProjectionViewers != null) { - if (viewer != null) { - ProjectionViewerInformation info = (ProjectionViewerInformation) fProjectionViewers.get(viewer); - if (info != null) { - info.queueAnnotationModelChanges(newChange); - } - } - else { - Iterator infos = fProjectionViewers.values().iterator(); - while (infos.hasNext()) { - ProjectionViewerInformation info = (ProjectionViewerInformation) infos.next(); - info.queueAnnotationModelChanges(newChange); - } - } - } - } - - public void release() { - // go through every projectionviewer and call - // removeProjectionViewer(viewer); - if (fProjectionViewers != null) { - Iterator infos = fProjectionViewers.values().iterator(); - while (infos.hasNext()) { - ProjectionViewerInformation info = (ProjectionViewerInformation) infos.next(); - info.dispose(); - infos.remove(); - } - fProjectionViewers = null; - } - super.release(); - } - - /** - * Adds viewer to list of projection viewers this factory is associated - * with - * - * @param viewer - - * assumes viewer's document and projection annotation model - * are not null - */ - void addProjectionViewer(ProjectionViewer viewer) { - // remove old entry if it exists - removeProjectionViewer(viewer); - - if (fProjectionViewers == null) { - fProjectionViewers = new HashMap(); - } - - // create new object containing projection viewer and its info - ProjectionViewerInformation info = new ProjectionViewerInformation(viewer); - fProjectionViewers.put(viewer, info); - info.initialize(); - } - - /** - * Removes the given viewer from the list of projection viewers this - * factor is associated with - * - * @param viewer - */ - void removeProjectionViewer(ProjectionViewer viewer) { - if (fProjectionViewers != null) { - // remove entry from list of viewers - ProjectionViewerInformation info = (ProjectionViewerInformation) fProjectionViewers.remove(viewer); - if (info != null) { - info.dispose(); - } - // if removing last projection viewer, clear out everything - if (fProjectionViewers.isEmpty()) { - fProjectionViewers = null; - } - } - } -} diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterXML.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterXML.java deleted file mode 100644 index 62026bcd6b..0000000000 --- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionModelNodeAdapterXML.java +++ /dev/null @@ -1,296 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2005 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 - * - *******************************************************************************/ -package org.eclipse.wst.xml.ui.internal.projection; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import org.eclipse.core.runtime.Platform; -import org.eclipse.jface.text.Position; -import org.eclipse.jface.text.source.projection.ProjectionAnnotation; -import org.eclipse.jface.text.source.projection.ProjectionViewer; -import org.eclipse.swt.graphics.FontMetrics; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.graphics.Rectangle; -import org.eclipse.swt.widgets.Canvas; -import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; -import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; -import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; -import org.w3c.dom.Node; - -/** - * Updates projection annotation model with projection annotations for this - * adapter node's children - */ -public class ProjectionModelNodeAdapterXML implements INodeAdapter { - private final static boolean debugProjectionPerf = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.xml.ui/projectionperf")); //$NON-NLS-1$ //$NON-NLS-2$ - - private class TagProjectionAnnotation extends ProjectionAnnotation { - private boolean fIsVisible = false; /* workaround for BUG85874 */ - private Node fNode; - - public TagProjectionAnnotation(Node node, boolean isCollapsed) { - super(isCollapsed); - fNode = node; - } - - public Node getNode() { - return fNode; - } - - public void setNode(Node node) { - fNode = node; - } - - /** - * Does not paint hidden annotations. Annotations are hidden when they - * only span one line. - * - * @see ProjectionAnnotation#paint(org.eclipse.swt.graphics.GC, - * org.eclipse.swt.widgets.Canvas, - * org.eclipse.swt.graphics.Rectangle) - */ - public void paint(GC gc, Canvas canvas, Rectangle rectangle) { - /* workaround for BUG85874 */ - /* - * only need to check annotations that are expanded because hidden - * annotations should never have been given the chance to - * collapse. - */ - if (!isCollapsed()) { - // working with rectangle, so line height - FontMetrics metrics = gc.getFontMetrics(); - if (metrics != null) { - // do not draw annotations that only span one line and - // mark them as not visible - if ((rectangle.height / metrics.getHeight()) <= 1) { - fIsVisible = false; - return; - } - } - } - fIsVisible = true; - super.paint(gc, canvas, rectangle); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.jface.text.source.projection.ProjectionAnnotation#markCollapsed() - */ - public void markCollapsed() { - /* workaround for BUG85874 */ - // do not mark collapsed if annotation is not visible - if (fIsVisible) - super.markCollapsed(); - } - } - - ProjectionModelNodeAdapterFactoryXML fAdapterFactory; - private Map fTagAnnotations = new HashMap(); - - public ProjectionModelNodeAdapterXML(ProjectionModelNodeAdapterFactoryXML factory) { - fAdapterFactory = factory; - } - - /** - * Create a projection position from the given node. Able to get - * projection position if node isNodeProjectable. - * - * @param node - * @return null if no projection position possible, a Position otherwise - */ - private Position createProjectionPosition(Node node) { - Position pos = null; - if (isNodeProjectable(node) && node instanceof IndexedRegion) { - // IDocument document = - // fAdapterFactory.getProjectionViewer().getDocument(); - // if (document != null) { - IndexedRegion inode = (IndexedRegion) node; - int start = inode.getStartOffset(); - int end = inode.getEndOffset(); - if (start >= 0 && start < end) { - // region-based - // extra line when collapsed, but no region increase when add - // newline - pos = new Position(start, end - start); - // try { - // // line-based - // // extra line when collapsed, but no region increase when - // add newline - // IRegion startLineRegion = - // document.getLineInformationOfOffset(start); - // IRegion endLineRegion = - // document.getLineInformationOfOffset(end); - // int startOffset = startLineRegion.getOffset(); - // int endOffset = endLineRegion.getOffset() + - // endLineRegion.getLength(); - // if (endOffset > startOffset) { - // pos = new Position(startOffset, endOffset - startOffset); - // } - - // // line-based - // // no extra line when collapsed, but region increase when - // add newline - // int startLine = document.getLineOfOffset(start); - // int endLine = document.getLineOfOffset(end); - // if (endLine + 1 < document.getNumberOfLines()) { - // int offset = document.getLineOffset(startLine); - // int endOffset = document.getLineOffset(endLine + 1); - // pos = new Position(offset, endOffset - offset); - // } - // } - // catch (BadLocationException x) { - // Logger.log(Logger.WARNING_DEBUG, null, x); - // } - } - // } - } - return pos; - } - - /** - * Find TagProjectionAnnotation for node in the current list of projection - * annotations for this adapter - * - * @param node - * @return TagProjectionAnnotation - */ - private TagProjectionAnnotation getExistingAnnotation(Node node) { - TagProjectionAnnotation anno = null; - - if ((node != null) && (!fTagAnnotations.isEmpty())) { - Iterator it = fTagAnnotations.keySet().iterator(); - while (it.hasNext() && anno == null) { - TagProjectionAnnotation a = (TagProjectionAnnotation) it.next(); - Node n = a.getNode(); - if (node.equals(n)) { - anno = a; - } - } - } - return anno; - } - - public boolean isAdapterForType(Object type) { - return type == ProjectionModelNodeAdapterXML.class; - } - - /** - * Returns true if node is a node type able to fold - * - * @param node - * @return boolean true if node is projectable, false otherwise - */ - private boolean isNodeProjectable(Node node) { - if (node.getNodeType() == Node.ELEMENT_NODE) { - return true; - } - return false; - } - - public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) { - // check if folding is even enabled, if not, just ignore notifyChanged - // events - if (!fAdapterFactory.isActive()) { - return; - } - - if ((eventType == INodeNotifier.STRUCTURE_CHANGED) && (notifier instanceof Node)) { - updateAdapter((Node) notifier); - } - } - - /** - * Update the projection annotation of all the nodes that are children of - * node - * - * @param node - */ - void updateAdapter(Node node) { - updateAdapter(node, null); - } - - /** - * Update the projection annotation of all the nodes that are children of - * node and adds all projection annotations to viewer (for newly added - * viewers) - * - * @param node - * @param viewer - */ - void updateAdapter(Node node, ProjectionViewer viewer) { - long start = System.currentTimeMillis(); - - Map additions = new HashMap(); - Map projectionAnnotations = new HashMap(); - - // go through immediate child nodes and figure out projection - // model annotations - if (node != null) { - Node childNode = node.getFirstChild(); - while (childNode != null) { - Position newPos = createProjectionPosition(childNode); - if (newPos != null) { - TagProjectionAnnotation newAnnotation = new TagProjectionAnnotation(childNode, false); - TagProjectionAnnotation existing = getExistingAnnotation(childNode); - if (existing == null) { - // add to map containing all annotations for this - // adapter - projectionAnnotations.put(newAnnotation, newPos); - // add to map containing annotations to add - additions.put(newAnnotation, newPos); - } - else { - // add to map containing all annotations for this - // adapter - projectionAnnotations.put(existing, newPos); - // remove from map containing annotations to delete - fTagAnnotations.remove(existing); - } - } - childNode = childNode.getNextSibling(); - } - - // in the end, want to delete anything leftover in old list, add - // everything in additions, and update everything in - // projectionAnnotations - ProjectionAnnotation[] oldList = null; - if (!fTagAnnotations.isEmpty()) { - oldList = (ProjectionAnnotation[]) fTagAnnotations.keySet().toArray(new ProjectionAnnotation[0]); - } - ProjectionAnnotation[] modifyList = null; - if (!projectionAnnotations.isEmpty()) { - modifyList = (ProjectionAnnotation[]) projectionAnnotations.keySet().toArray(new ProjectionAnnotation[0]); - } - - // specifically add all annotations to viewer - if (viewer != null && !projectionAnnotations.isEmpty()) { - fAdapterFactory.queueAnnotationModelChanges(node, null, projectionAnnotations, null, viewer); - } - - // only update when there is something to update - if ((oldList != null && oldList.length > 0) || (!additions.isEmpty()) || (modifyList != null && modifyList.length > 0)) - fAdapterFactory.queueAnnotationModelChanges(node, oldList, additions, modifyList); - } - - // save new list of annotations - fTagAnnotations = projectionAnnotations; - - if (debugProjectionPerf) { - long end = System.currentTimeMillis(); - String nodeName = node != null ? node.getNodeName() : "null"; //$NON-NLS-1$ - System.out.println("ProjectionModelNodeAdapterXML.updateAdapter (" + nodeName + "):" + (end - start)); //$NON-NLS-1$ //$NON-NLS-2$ - } - } -} diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionViewerInformation.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionViewerInformation.java deleted file mode 100644 index c20c119591..0000000000 --- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/ProjectionViewerInformation.java +++ /dev/null @@ -1,196 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006 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 - *******************************************************************************/ -package org.eclipse.wst.xml.ui.internal.projection; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.jface.text.DocumentEvent; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IDocumentExtension; -import org.eclipse.jface.text.IDocumentListener; -import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; -import org.eclipse.jface.text.source.projection.ProjectionViewer; -import org.eclipse.wst.xml.ui.internal.Logger; - -/** - * Contains information about a projection viewer and also manages updating - * the viewer's projection annotation model - */ -class ProjectionViewerInformation { - // copies of this class located in: - // org.eclipse.wst.xml.ui.internal.projection - // org.eclipse.wst.css.ui.internal.projection - // org.eclipse.wst.html.ui.internal.projection - // org.eclipse.jst.jsp.ui.internal.projection - - /** - * Listens to document to be aware of when to update the projection - * annotation model. - */ - private class DocumentListener implements IDocumentListener { - private ProjectionViewerInformation fInfo; - - public DocumentListener(ProjectionViewerInformation info) { - fInfo = info; - } - - public void documentAboutToBeChanged(DocumentEvent event) { - IDocument document = event.getDocument(); - if (fInfo.getDocument() == document) { - fInfo.setIsDocumentChanging(true); - } - } - - public void documentChanged(DocumentEvent event) { - // register a post notification replace so that projection - // annotation model will be updated after all documentChanged - // listeners have been notified - IDocument document = event.getDocument(); - if (document instanceof IDocumentExtension && fInfo.getDocument() == document) { - if (fInfo.hasChangesQueued()) - ((IDocumentExtension) document).registerPostNotificationReplace(this, new PostDocumentChangedListener(fInfo)); - } - } - } - - /** - * Essentially a post document changed listener because it is called after - * documentchanged has been fired. - */ - private class PostDocumentChangedListener implements IDocumentExtension.IReplace { - private ProjectionViewerInformation fInfo; - - public PostDocumentChangedListener(ProjectionViewerInformation info) { - fInfo = info; - } - - public void perform(IDocument document, IDocumentListener owner) { - fInfo.applyAnnotationModelChanges(); - fInfo.setIsDocumentChanging(false); - } - } - - /** - * Projection annotation model current associated with this projection - * viewer - */ - private ProjectionAnnotationModel fProjectionAnnotationModel; - /** - * Document currently associated with this projection viewer - */ - private IDocument fDocument; - /** - * Listener to fProjectionViewer's document - */ - private IDocumentListener fDocumentListener; - /** - * Indicates whether or not document is in the middle of changing - */ - private boolean fIsDocumentChanging = false; - /** - * List of projection annotation model changes that need to be applied - */ - private List fQueuedAnnotationChanges; - - public ProjectionViewerInformation(ProjectionViewer viewer) { - fDocument = viewer.getDocument(); - fProjectionAnnotationModel = viewer.getProjectionAnnotationModel(); - } - - IDocument getDocument() { - return fDocument; - } - - private List getQueuedAnnotationChanges() { - if (fQueuedAnnotationChanges == null) { - fQueuedAnnotationChanges = new ArrayList(); - } - return fQueuedAnnotationChanges; - } - - void setIsDocumentChanging(boolean changing) { - fIsDocumentChanging = changing; - } - - private boolean isDocumentChanging() { - return fIsDocumentChanging; - } - - /** - * Applies the pending projection annotation model changes to the - * projection annotation model. - */ - void applyAnnotationModelChanges() { - List queuedChanges = getQueuedAnnotationChanges(); - // go through all the pending annotation changes and apply - // them to - // the projection annotation model - while (!queuedChanges.isEmpty()) { - ProjectionAnnotationModelChanges changes = (ProjectionAnnotationModelChanges) queuedChanges.remove(0); - try { - fProjectionAnnotationModel.modifyAnnotations(changes.getDeletions(), changes.getAdditions(), changes.getModifications()); - } - catch (Exception e) { - // if anything goes wrong, log it be continue - Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); - } - } - } - - /** - * Returns true if there are annotation changes queued up, false otherwise - * - * @return boolean - */ - boolean hasChangesQueued() { - return !getQueuedAnnotationChanges().isEmpty(); - } - - /** - * Updates projection annotation model if document is not in flux. - * Otherwise, queues up the changes to be applied when document is ready. - */ - public void queueAnnotationModelChanges(ProjectionAnnotationModelChanges newChange) { - /* - * future_TODO: maybe improve by checking if annotation projection - * model change already exists for node. if so, throw out old change. - */ - getQueuedAnnotationChanges().add(newChange); - - // if document isn't changing, go ahead and apply it - if (!isDocumentChanging()) { - applyAnnotationModelChanges(); - } - } - - public void initialize() { - // add document listener - if (fDocumentListener == null) { - fDocumentListener = new DocumentListener(this); - } - getDocument().addDocumentListener(fDocumentListener); - } - - public void dispose() { - // remove document listener - if (fDocumentListener != null) { - getDocument().removeDocumentListener(fDocumentListener); - } - - // clear out list of queued changes since it may no longer - // be accurate - if (fQueuedAnnotationChanges != null) { - fQueuedAnnotationChanges.clear(); - fQueuedAnnotationChanges = null; - } - } -} diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/StructuredTextFoldingProviderXML.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/StructuredTextFoldingProviderXML.java deleted file mode 100644 index 4ac69d088b..0000000000 --- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/projection/StructuredTextFoldingProviderXML.java +++ /dev/null @@ -1,324 +0,0 @@ -package org.eclipse.wst.xml.ui.internal.projection; - -import org.eclipse.core.runtime.Platform; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.ITextInputListener; -import org.eclipse.jface.text.source.projection.IProjectionListener; -import org.eclipse.jface.text.source.projection.ProjectionViewer; -import org.eclipse.wst.sse.core.StructuredModelManager; -import org.eclipse.wst.sse.core.internal.PropagatingAdapter; -import org.eclipse.wst.sse.core.internal.model.FactoryRegistry; -import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; -import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; -import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; -import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; -import org.eclipse.wst.sse.ui.internal.projection.IStructuredTextFoldingProvider; -import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; -import org.w3c.dom.Document; -import org.w3c.dom.Node; - -/** - * Updates the projection model of a structured model for XML. - */ -public class StructuredTextFoldingProviderXML implements IStructuredTextFoldingProvider, IProjectionListener, ITextInputListener { - private final static boolean debugProjectionPerf = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.xml.ui/projectionperf")); //$NON-NLS-1$ //$NON-NLS-2$ - - private IDocument fDocument; - private ProjectionViewer fViewer; - private boolean fProjectionNeedsToBeEnabled = false; - /** - * Maximum number of child nodes to add adapters to (limit for performance - * sake) - */ - private final int MAX_CHILDREN = 10; - /** - * Maximum number of sibling nodes to add adapters to (limit for - * performance sake) - */ - private final int MAX_SIBLINGS = 1000; - - /** - * Adds an adapter to node and its children - * - * @param node - * @param childLevel - */ - private void addAdapterToNodeAndChildren(Node node, int childLevel) { - // stop adding initial adapters MAX_CHILDREN levels deep for - // performance sake - if (node instanceof INodeNotifier && childLevel < MAX_CHILDREN) { - INodeNotifier notifier = (INodeNotifier) node; - - // try and get the adapter for the current node and update the - // adapter with projection information - ProjectionModelNodeAdapterXML adapter = (ProjectionModelNodeAdapterXML) notifier.getExistingAdapter(ProjectionModelNodeAdapterXML.class); - if (adapter != null) { - adapter.updateAdapter(node, fViewer); - } - else { - // just call getadapter so the adapter is created and - // automatically initialized - notifier.getAdapterFor(ProjectionModelNodeAdapterXML.class); - } - int siblingLevel = 0; - Node nextChild = node.getFirstChild(); - while (nextChild != null && siblingLevel < MAX_SIBLINGS) { - Node childNode = nextChild; - nextChild = childNode.getNextSibling(); - - addAdapterToNodeAndChildren(childNode, childLevel + 1); - } - } - } - - /** - * Goes through every node and adds an adapter onto each for tracking - * purposes - */ - private void addAllAdapters() { - long start = System.currentTimeMillis(); - - if (fDocument != null) { - IStructuredModel sModel = null; - try { - sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); - if (sModel != null) { - int startOffset = 0; - IndexedRegion startNode = sModel.getIndexedRegion(startOffset); - if (startNode instanceof Node) { - int siblingLevel = 0; - Node nextSibling = (Node) startNode; - while (nextSibling != null && siblingLevel < MAX_SIBLINGS) { - Node currentNode = nextSibling; - nextSibling = currentNode.getNextSibling(); - - addAdapterToNodeAndChildren(currentNode, 0); - ++siblingLevel; - } - } - } - } - finally { - if (sModel != null) { - sModel.releaseFromRead(); - } - } - } - if (debugProjectionPerf) { - long end = System.currentTimeMillis(); - System.out.println("StructuredTextFoldingProviderXML.addAllAdapters: " + (end - start)); //$NON-NLS-1$ - } - } - - /** - * Get the ProjectionModelNodeAdapterFactoryXML to use with this provider. - * - * @return ProjectionModelNodeAdapterFactoryXML - */ - private ProjectionModelNodeAdapterFactoryXML getAdapterFactory(boolean createIfNeeded) { - long start = System.currentTimeMillis(); - - ProjectionModelNodeAdapterFactoryXML factory = null; - if (fDocument != null) { - IStructuredModel sModel = null; - try { - sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); - if (sModel != null) { - FactoryRegistry factoryRegistry = sModel.getFactoryRegistry(); - - // getting the projectionmodelnodeadapter for the first - // time - // so do some initializing - if (!factoryRegistry.contains(ProjectionModelNodeAdapterXML.class) && createIfNeeded) { - ProjectionModelNodeAdapterFactoryXML newFactory = new ProjectionModelNodeAdapterFactoryXML(); - - // add factory to factory registry - factoryRegistry.addFactory(newFactory); - - // add factory to propogating adapter - IDOMModel domModel = (IDOMModel) sModel; - Document document = domModel.getDocument(); - PropagatingAdapter propagatingAdapter = (PropagatingAdapter) ((INodeNotifier) document).getAdapterFor(PropagatingAdapter.class); - if (propagatingAdapter != null) { - propagatingAdapter.addAdaptOnCreateFactory(newFactory); - } - } - - // try and get the factory - factory = (ProjectionModelNodeAdapterFactoryXML) factoryRegistry.getFactoryFor(ProjectionModelNodeAdapterXML.class); - } - } - finally { - if (sModel != null) - sModel.releaseFromRead(); - } - } - - if (debugProjectionPerf) { - long end = System.currentTimeMillis(); - System.out.println("StructuredTextFoldingProviderXML.getAdapterFactory: " + (end - start)); //$NON-NLS-1$ - } - return factory; - } - - /** - * Initialize this provider with the correct document. Assumes projection - * is enabled. (otherwise, only install would have been called) - */ - public void initialize() { - if (!isInstalled()) - return; - - // clear out old info - projectionDisabled(); - - fDocument = fViewer.getDocument(); - - // set projection viewer on new document's adapter factory - if (fViewer.getProjectionAnnotationModel() != null) { - ProjectionModelNodeAdapterFactoryXML factory = getAdapterFactory(true); - if (factory != null) { - factory.addProjectionViewer(fViewer); - } - - addAllAdapters(); - } - fProjectionNeedsToBeEnabled = false; - } - - /** - * Associate a ProjectionViewer with this IStructuredTextFoldingProvider - * - * @param viewer - - * assumes not null - */ - public void install(ProjectionViewer viewer) { - // uninstall before trying to install new viewer - if (isInstalled()) { - uninstall(); - } - fViewer = viewer; - fViewer.addProjectionListener(this); - fViewer.addTextInputListener(this); - } - - private boolean isInstalled() { - return fViewer != null; - } - - public void projectionDisabled() { - ProjectionModelNodeAdapterFactoryXML factory = getAdapterFactory(false); - if (factory != null) { - factory.removeProjectionViewer(fViewer); - } - - // clear out all annotations - if (fViewer.getProjectionAnnotationModel() != null) - fViewer.getProjectionAnnotationModel().removeAllAnnotations(); - - removeAllAdapters(); - - fDocument = null; - fProjectionNeedsToBeEnabled = false; - } - - public void projectionEnabled() { - initialize(); - } - - /** - * Removes an adapter from node and its children - * - * @param node - * @param level - */ - private void removeAdapterFromNodeAndChildren(Node node, int level) { - if (node instanceof INodeNotifier) { - INodeNotifier notifier = (INodeNotifier) node; - - // try and get the adapter for the current node and remove it - INodeAdapter adapter = notifier.getExistingAdapter(ProjectionModelNodeAdapterXML.class); - if (adapter != null) { - notifier.removeAdapter(adapter); - } - - Node nextChild = node.getFirstChild(); - while (nextChild != null) { - Node childNode = nextChild; - nextChild = childNode.getNextSibling(); - - removeAdapterFromNodeAndChildren(childNode, level + 1); - } - } - } - - /** - * Goes through every node and removes adapter from each for cleanup - * purposes - */ - private void removeAllAdapters() { - long start = System.currentTimeMillis(); - - if (fDocument != null) { - IStructuredModel sModel = null; - try { - sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); - if (sModel != null) { - int startOffset = 0; - IndexedRegion startNode = sModel.getIndexedRegion(startOffset); - if (startNode instanceof Node) { - Node nextSibling = (Node) startNode; - while (nextSibling != null) { - Node currentNode = nextSibling; - nextSibling = currentNode.getNextSibling(); - - removeAdapterFromNodeAndChildren(currentNode, 0); - } - } - } - } - finally { - if (sModel != null) { - sModel.releaseFromRead(); - } - } - } - - if (debugProjectionPerf) { - long end = System.currentTimeMillis(); - System.out.println("StructuredTextFoldingProviderXML.removeAllAdapters: " + (end - start)); //$NON-NLS-1$ - } - } - - public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { - // if folding is enabled and new document is going to be a totally - // different document, disable projection - if (fDocument != null && fDocument != newInput) { - // disable projection and disconnect everything - projectionDisabled(); - fProjectionNeedsToBeEnabled = true; - } - } - - public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { - // if projection was previously enabled before input document changed - // and new document is different than old document - if (fProjectionNeedsToBeEnabled && fDocument == null && newInput != null) { - projectionEnabled(); - fProjectionNeedsToBeEnabled = false; - } - } - - /** - * Disconnect this IStructuredTextFoldingProvider from projection viewer - */ - public void uninstall() { - if (isInstalled()) { - projectionDisabled(); - - fViewer.removeProjectionListener(this); - fViewer.removeTextInputListener(this); - fViewer = null; - } - } -} |