Search in sources :

Example 61 with IStructuredDocumentRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList in project webtools.sourceediting by eclipse.

the class JSONModelImpl method nodesReplaced.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.sse.core.internal.provisional.events.
	 * IStructuredDocumentListener
	 * #nodesReplaced(org.eclipse.wst.sse.core.internal
	 * .provisional.events.StructuredDocumentRegionsReplacedEvent)
	 */
@Override
public void nodesReplaced(StructuredDocumentRegionsReplacedEvent event) {
    if (event == null)
        return;
    IStructuredDocumentRegionList oldStructuredDocumentRegions = event.getOldStructuredDocumentRegions();
    IStructuredDocumentRegionList newStructuredDocumentRegions = event.getNewStructuredDocumentRegions();
    JSONModelUpdater updater = getActiveUpdater();
    if (updater != null) {
        // being updated
        try {
            updater.replaceStructuredDocumentRegions(newStructuredDocumentRegions, oldStructuredDocumentRegions);
        } catch (Exception ex) {
            Logger.logException(ex);
            this.refresh = true;
            handleRefresh();
        } finally {
            setActive(null);
        }
        // checkForReinit();
        return;
    }
    JSONModelNotifier notifier = getModelNotifier();
    boolean isChanging = notifier.isChanging();
    if (!isChanging)
        notifier.beginChanging();
    JSONModelParser parser = getModelParser();
    setActive(parser);
    try {
        /* workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=486860 */
        // this.refresh = true;
        // handleRefresh();
        boolean reloadModel = false;
        // the model is reloaded completely.
        if (newStructuredDocumentRegions != null) {
            int newCount = newStructuredDocumentRegions.getLength();
            for (int i = 0; i < newCount - 1; i++) {
                if (newStructuredDocumentRegions.item(i).getType().equals(JSONRegionContexts.JSON_COMMA)) {
                    IStructuredDocumentRegion nextNode = newStructuredDocumentRegions.item(i).getNext();
                    while (nextNode != null) {
                        if (nextNode.getType().equals(JSONRegionContexts.JSON_OBJECT_KEY) || nextNode.getType().equals(JSONRegionContexts.JSON_OBJECT_OPEN) || nextNode.getType().equals(JSONRegionContexts.JSON_ARRAY_OPEN)) {
                            reloadModel = true;
                            break;
                        }
                        nextNode = nextNode.getNext();
                    }
                }
            }
        }
        if (!reloadModel && oldStructuredDocumentRegions != null && oldStructuredDocumentRegions.getLength() > 0) {
            if (oldStructuredDocumentRegions.getLength() > 3 || oldStructuredDocumentRegions.item(0).getType().equals(JSONRegionContexts.JSON_OBJECT_OPEN)) {
                // Reload all the model when the first region that will be
                // replaced is a JSONObject or if more than 3 regions are
                // replaced in the model (a JSONPair is composed by 3 regions,
                // this means more that one JSON Pair are replaced in the model)
                reloadModel = true;
            } else {
                // also, always reload the model in case of removing at least one UNDEFINED region
                for (int i = 0; !reloadModel && i < oldStructuredDocumentRegions.getLength(); i++) {
                    if (oldStructuredDocumentRegions.item(i).getType().equals(JSONRegionContexts.UNDEFINED)) {
                        reloadModel = true;
                    }
                }
            }
        }
        if (reloadModel) {
            this.refresh = true;
            handleRefresh();
        } else {
            parser.replaceStructuredDocumentRegions(newStructuredDocumentRegions, oldStructuredDocumentRegions);
        }
    } catch (Exception ex) {
        if (ex.getClass().equals(StructuredDocumentRegionManagementException.class)) {
            Logger.traceException(TRACE_PARSER_MANAGEMENT_EXCEPTION, ex);
        } else {
            Logger.logException(ex);
        }
        this.refresh = true;
        handleRefresh();
    } finally {
        setActive(null);
        if (!isChanging) {
            notifier.endChanging();
            handleRefresh();
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList)

Example 62 with IStructuredDocumentRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList in project webtools.sourceediting by eclipse.

the class JSONModelImpl method internalSetNewDocument.

private void internalSetNewDocument(IStructuredDocument structuredDocument) {
    if (structuredDocument == null)
        return;
    IStructuredDocumentRegionList flatNodes = structuredDocument.getRegionList();
    if ((flatNodes == null) || (flatNodes.getLength() == 0)) {
        return;
    }
    if (this.document == null)
        // being constructed
        return;
    JSONModelUpdater updater = getActiveUpdater();
    if (updater != null) {
        // being updated
        try {
            updater.replaceStructuredDocumentRegions(flatNodes, null);
        } catch (Exception ex) {
            Logger.logException(ex);
            this.refresh = true;
            handleRefresh();
        } finally {
            setActive(null);
        }
        // checkForReinit();
        return;
    }
    JSONModelNotifier notifier = getModelNotifier();
    boolean isChanging = notifier.isChanging();
    // call even if changing to notify doing new model
    getModelNotifier().beginChanging(true);
    JSONModelParser parser = getModelParser();
    setActive(parser);
    this.document.removeChildNodes();
    try {
        parser.replaceStructuredDocumentRegions(flatNodes, null);
    } catch (Exception ex) {
        Logger.logException(ex);
    // meaningless to refresh, because the result might be the same
    } finally {
        setActive(null);
        if (!isChanging) {
            getModelNotifier().endChanging();
        }
        // ignore refresh
        this.refresh = false;
    }
}
Also used : IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList)

Example 63 with IStructuredDocumentRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList in project webtools.sourceediting by eclipse.

the class CSSModelImpl method nodesReplaced.

public void nodesReplaced(StructuredDocumentRegionsReplacedEvent structuredDocumentEvent) {
    if (structuredDocumentEvent == null) {
        return;
    }
    IStructuredDocumentRegionList oldStructuredDocumentRegions = structuredDocumentEvent.getOldStructuredDocumentRegions();
    IStructuredDocumentRegionList newStructuredDocumentRegions = structuredDocumentEvent.getNewStructuredDocumentRegions();
    if (oldStructuredDocumentRegions == null && newStructuredDocumentRegions == null) {
        return;
    }
    fStructuredDocumentUpdate = true;
    CSSModelParser parser = getParser();
    parser.setStructuredDocumentEvent(structuredDocumentEvent);
    if (structuredDocumentEvent.isEntireDocumentReplaced())
        parser.replaceDocument(newStructuredDocumentRegions);
    else
        parser.replaceStructuredDocumentRegions(newStructuredDocumentRegions, oldStructuredDocumentRegions);
    fStructuredDocumentUpdate = false;
}
Also used : IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList)

Example 64 with IStructuredDocumentRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList in project webtools.sourceediting by eclipse.

the class CSSModelImpl method newModel.

public void newModel(NewDocumentEvent structuredDocumentEvent) {
    if (structuredDocumentEvent == null)
        return;
    IStructuredDocument structuredDocument = structuredDocumentEvent.getStructuredDocument();
    if (structuredDocument == null)
        return;
    // this should not happen, but for the case
    if (structuredDocument != getStructuredDocument())
        setStructuredDocument(structuredDocument);
    IStructuredDocumentRegionList flatNodes = structuredDocument.getRegionList();
    if (flatNodes == null)
        return;
    if (getDocument() == null)
        return;
    fStructuredDocumentUpdate = true;
    ((CSSStructuredDocumentRegionContainer) getDocument()).removeChildNodes();
    CSSModelParser parser = getParser();
    parser.setStructuredDocumentEvent(structuredDocumentEvent);
    parser.replaceStructuredDocumentRegions(flatNodes, null);
    fStructuredDocumentUpdate = false;
}
Also used : IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 65 with IStructuredDocumentRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList in project webtools.sourceediting by eclipse.

the class TestStructuredDocument method testGetRegionList.

public void testGetRegionList() throws IOException, CoreException {
    IStructuredModel model = getTestModel();
    try {
        IStructuredDocument sDoc = model.getStructuredDocument();
        IStructuredDocumentRegionList regions = sDoc.getRegionList();
        assertNotNull("couldn't get region list", regions);
        assertEquals(17, regions.getLength());
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Aggregations

IStructuredDocumentRegionList (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList)68 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)3 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)3 Enumeration (java.util.Enumeration)2 Iterator (java.util.Iterator)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2 DOMException (org.w3c.dom.DOMException)2 Element (org.w3c.dom.Element)2 NodesEvent (org.eclipse.wst.dtd.core.internal.event.NodesEvent)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1 NewDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.NewDocumentEvent)1 NoChangeEvent (org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)1 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)1 RegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionsReplacedEvent)1 StructuredDocumentRegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentRegionsReplacedEvent)1 CoreNodeList (org.eclipse.wst.sse.core.internal.text.CoreNodeList)1