Search in sources :

Example 31 with StructuredDocumentEvent

use of org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent in project webtools.sourceediting by eclipse.

the class ParserTest3 method testModel.

public void testModel() {
    IDOMModel model = createXMLModel();
    try {
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        Document document = model.getDocument();
        // StructuredDocumentEvent event =
        structuredDocument.replaceText(null, 0, 0, "<a b  >");
        // dont's print instance of event, or many tests will fail when
        // event class is changed (exactly what is not needed for unit
        // tests!)
        // fOutputWriter.writeln(event.toString());
        printSource(model);
        printTree(model);
        Element a = (Element) document.getFirstChild();
        NamedNodeMap attributes = a.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            String name = attr.getNodeName();
            String value = attr.getNodeValue();
            fOutputWriter.writeln(name + "=[" + value + "]");
        }
        StructuredDocumentEvent event2 = structuredDocument.replaceText(null, 4, 1, "");
        // I removed this part of output, since renaming class or package
        // will cause test to fail!
        fOutputWriter.writeln(event2.toString());
        printSource(model);
        printTree(model);
        a = (Element) document.getFirstChild();
        attributes = a.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            String name = attr.getNodeName();
            String value = attr.getNodeValue();
            fOutputWriter.writeln(name + "=[" + value + "]");
        }
        saveAndCompareTestResults();
    } finally {
        model.releaseFromEdit();
    }
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) NamedNodeMap(org.w3c.dom.NamedNodeMap) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 32 with StructuredDocumentEvent

use of org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent in project webtools.sourceediting by eclipse.

the class UnitTests method testPageDirective.

public void testPageDirective() {
    boolean verbose = false;
    setUpJSP();
    // GENERIC_NODES_REPLACED_EVENT_CASE;
    eventCase = 999;
    String startString = "<%@page lang=\"java\" <SCRIPT>var <% test %> String</SCRIPT>";
    String changes = "s";
    String expectedString = "<%@page lang=\"javas\" <SCRIPT>var <% test %> String</SCRIPT>";
    int startOfChanges = 18;
    int lengthToReplace = 0;
    fModel.setText(null, startString);
    if (verbose) {
        Debug.dump(fModel, true);
    }
    StructuredDocumentEvent structuredDocumentEvent = fModel.replaceText(null, startOfChanges, lengthToReplace, changes);
    String resultString = fModel.getText();
    boolean result = (expectedString.equals(resultString));
    // assertTrue(result); // && eventResult);
    assertTrue("text update", result);
    if (verbose) {
        System.out.println();
        System.out.println("structured document:");
        IStructuredDocument document = structuredDocumentEvent.getStructuredDocument();
        Debug.dump(document, true);
    }
    // now delete quote
    changes = "";
    expectedString = "<%@page lang=\"javas\" <SCRIPT>var <% test %> String</SCRIPT>";
    startOfChanges = 19;
    lengthToReplace = 1;
    fModel.replaceText(null, startOfChanges, lengthToReplace, changes);
    resultString = fModel.getText();
    assertTrue("text update", result);
    if (verbose) {
        System.out.println();
        System.out.println("structured document:");
        IStructuredDocument document = structuredDocumentEvent.getStructuredDocument();
        Debug.dump(document, true);
    }
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 33 with StructuredDocumentEvent

use of org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent in project webtools.sourceediting by eclipse.

the class JobSafeStructuredDocument method replaceText.

/* (non-Javadoc)
	 * @see org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument#replaceText(java.lang.Object, int, int, java.lang.String)
	 */
public StructuredDocumentEvent replaceText(final Object requester, final int start, final int replacementLength, final String changes) {
    StructuredDocumentEvent event = null;
    IExecutionDelegate delegate = getExecutionDelegate();
    if (delegate == null) {
        event = super.replaceText(requester, start, replacementLength, changes);
    } else {
        final Object[] resultSlot = new Object[1];
        JobSafeRunnable runnable = new JobSafeRunnable() {

            public void run() throws Exception {
                resultSlot[0] = JobSafeStructuredDocument.super.replaceText(requester, start, replacementLength, changes);
            }
        };
        delegate.execute(runnable);
        event = (StructuredDocumentEvent) resultSlot[0];
    }
    return event;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) IExecutionDelegate(org.eclipse.wst.sse.core.internal.IExecutionDelegate)

Example 34 with StructuredDocumentEvent

use of org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent in project webtools.sourceediting by eclipse.

the class BasicStructuredDocumentRegion method updateRegion.

public StructuredDocumentEvent updateRegion(Object requester, IStructuredDocumentRegion structuredDocumentRegion, String changes, int requestStart, int lengthToReplace) {
    StructuredDocumentEvent result = null;
    int lengthDifference = Utilities.calculateLengthDifference(changes, lengthToReplace);
    // Get the region pointed to by the requestStart postion, and give
    // that region a chance to effect
    // the update.
    ITextRegion region = getRegionAtCharacterOffset(requestStart);
    // the
    if ((region == null) && (lengthToReplace == 0)) {
        region = _getRegions().get(_getRegions().size() - 1);
        // make sure the region is contiguous
        if (getEndOffset(region) == requestStart) {
            result = region.updateRegion(requester, this, changes, requestStart, lengthToReplace);
        }
    } else {
        if (region != null) {
            // we don't give the region a chance to update.
            if ((containsOffset(region, requestStart)) && (containsOffset(region, requestStart + lengthToReplace))) {
                result = region.updateRegion(requester, this, changes, requestStart, lengthToReplace);
            }
        }
    }
    // (i.e. more than one region, but no change to the node itself)
    if (result != null) {
        // That is, a region decided it could handle the change and
        // created
        // a region changed event.
        // $NON-NLS-1$
        Assert.isTrue(result instanceof RegionChangedEvent, "Program Error");
        updateDownStreamRegions(((RegionChangedEvent) result).getRegion(), lengthDifference);
        // PLUS, we need to update our own node end point (length)
        setLength(getLength() + lengthDifference);
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 35 with StructuredDocumentEvent

use of org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent in project webtools.sourceediting by eclipse.

the class RegionChangedAdapterNotificationTests method testAppendWhitespaceToAttributeValue.

public void testAppendWhitespaceToAttributeValue() throws IOException {
    IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(getName() + ".xml", new NullInputStream(), null);
    try {
        Document document = model.getDocument();
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        structuredDocument.setText(this, "<a b= c></a>");
        Node before = document.getFirstChild();
        final int[] changed = new int[] { -1 };
        INodeAdapter adapter = new INodeAdapter() {

            public boolean isAdapterForType(Object type) {
                return type.equals(RegionChangedAdapterNotificationTests.class);
            }

            public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                changed[0] = eventType;
            }
        };
        ((INodeNotifier) before).addAdapter(adapter);
        Object[] originalRegions = structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray();
        StructuredDocumentEvent fmEvent = structuredDocument.replaceText(null, 7, 0, " ");
        assertTrue("Region instances changed", Arrays.equals(originalRegions, structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray()));
        assertTrue(fmEvent instanceof RegionChangedEvent);
        Node after = document.getFirstChild();
        assertEquals("Node replaced", before, after);
        assertEquals("unexpected adapter notification event sent " + structuredDocument.get(), -1, changed[0]);
        assertEquals("unexpected document content", "<a b= c ></a>", structuredDocument.get());
    } finally {
        model.releaseFromEdit();
    }
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Node(org.w3c.dom.Node) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Aggregations

StructuredDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)59 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)23 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)22 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 Document (org.w3c.dom.Document)20 Node (org.w3c.dom.Node)20 NullInputStream (org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)18 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)18 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)18 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)8 NoChangeEvent (org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)6 IExecutionDelegate (org.eclipse.wst.sse.core.internal.IExecutionDelegate)3 StructuredDocumentRegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentRegionsReplacedEvent)3 RegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionsReplacedEvent)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 List (java.util.List)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IRegion (org.eclipse.jface.text.IRegion)1