use of org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent in project webtools.sourceediting by eclipse.
the class RegionChangedAdapterNotificationTests method testChangeQuotedAttributeValue.
public void testChangeQuotedAttributeValue() 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, "d");
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("Property Changed Adapter Notification event not sent " + changed[0] + " " + structuredDocument.get(), INodeNotifier.CHANGE, changed[0]);
assertEquals("unexpected document content", "<a b= \"dc\"></a>", structuredDocument.get());
} finally {
model.releaseFromEdit();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent in project webtools.sourceediting by eclipse.
the class RegionChangedAdapterNotificationTests method testRemoveTrailingSpaceFromTagName.
public void testRemoveTrailingSpaceFromTagName() 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);
((INodeNotifier) document).addAdapter(adapter);
Object[] originalRegions = structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray();
StructuredDocumentEvent fmEvent = structuredDocument.replaceText(null, 2, 1, "");
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();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent in project webtools.sourceediting by eclipse.
the class RegionChangedAdapterNotificationTests method testAppendWhitespaceToTagName.
public void testAppendWhitespaceToTagName() 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);
((INodeNotifier) document).addAdapter(adapter);
Object[] originalRegions = structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray();
StructuredDocumentEvent fmEvent = structuredDocument.replaceText(null, 2, 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();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent in project webtools.sourceediting by eclipse.
the class StructuredDocumentReParser method regionCheck.
/**
* If only one node is involved, sees how many regions are changed. If
* only one, then its a 'regionChanged' event ... if more than one, its a
* 'regionsReplaced' event.
*/
protected StructuredDocumentEvent regionCheck(IStructuredDocumentRegion oldNode, IStructuredDocumentRegion newNode) {
//
StructuredDocumentEvent result = null;
ITextRegionList oldRegions = oldNode.getRegions();
ITextRegionList newRegions = newNode.getRegions();
ITextRegion[] oldRegionsArray = oldRegions.toArray();
ITextRegion[] newRegionsArray = newRegions.toArray();
//
// for the 'regionsReplaced' event, we don't care if
// the regions changed due to type, or text,
// we'll just collect all those that are not equal
// into the old and new region lists.
// Note: we, of course, assume that old and new regions
// are basically contiguous -- and we force it to be so,
// even if not literally so, by starting at beginning to
// find first difference, and then starting at end to find
// last difference. Everything in between we assume is different.
//
// going up is easy, we start at zero in each, and continue
// till regions are not the same.
int startOfDifferences = _computeStartOfDifferences(oldNode, oldRegions, newNode, newRegions);
int endOfDifferencesOld = -1;
int endOfDifferencesNew = -1;
// then some portion of the lists are identical
if ((startOfDifferences >= oldRegions.size()) || (startOfDifferences >= newRegions.size())) {
if (oldRegions.size() < newRegions.size()) {
// INSERT CASE
// then there are new regions to add
// these lengths will cause the vector of old ones to not
// have any elements, and the vector of new regions to have
// just the new ones.
startOfDifferences = oldRegionsArray.length;
endOfDifferencesOld = oldRegionsArray.length - 1;
endOfDifferencesNew = newRegionsArray.length - 1;
} else {
if (oldRegions.size() > newRegions.size()) {
// DELETE CASE
// then there are old regions to delete
// these lengths will cause the vector of old regions to
// contain the ones to delete, and the vector of new
// regions
// not have any elements
startOfDifferences = newRegionsArray.length;
endOfDifferencesOld = oldRegionsArray.length - 1;
endOfDifferencesNew = newRegionsArray.length - 1;
} else {
// else the lists are identical!
// unlikely event, probably error in current design, since
// we check for identity at the very beginning of
// reparsing.
result = new NoChangeEvent(fStructuredDocument, fRequester, fChanges, fStart, fLengthToReplace);
}
}
} else {
if ((startOfDifferences > -1) && (endOfDifferencesOld < 0) && (endOfDifferencesNew < 0)) {
// We found a normal startOfDiffernces, but have not yet found
// the ends.
// We'll look for the end of differences by going backwards
// down the two lists.
// Here we need a seperate index for each array, since they
// may
// be (and
// probably are) of different lengths.
int indexOld = oldRegionsArray.length - 1;
int indexNew = newRegionsArray.length - 1;
while ((indexOld >= startOfDifferences) && (_greaterThanEffectedRegion(oldNode, oldRegionsArray[indexOld]))) {
if ((!(oldNode.sameAs(oldRegionsArray[indexOld], newNode, newRegionsArray[indexNew], fLengthDifference)))) {
// endOfDifferencesNew = indexTwo;
break;
}
indexOld--;
indexNew--;
}
endOfDifferencesOld = indexOld;
endOfDifferencesNew = indexNew;
}
}
// result != null means the impossible case above occurred
if (result == null) {
// Now form the two vectors of different regions
ITextRegionList holdOldRegions = new TextRegionListImpl();
ITextRegionList holdNewRegions = new TextRegionListImpl();
if (startOfDifferences > -1 && endOfDifferencesOld > -1) {
for (int i = startOfDifferences; i <= endOfDifferencesOld; i++) {
holdOldRegions.add(oldRegionsArray[i]);
}
}
if (startOfDifferences > -1 && endOfDifferencesNew > -1) {
for (int i = startOfDifferences; i <= endOfDifferencesNew; i++) {
holdNewRegions.add(newRegionsArray[i]);
}
}
if (holdOldRegions.size() == 0 && holdNewRegions.size() == 0) {
// then this means the regions were identical, which means
// someone
// pasted exactly the same thing they had selected, or !!!
// someone deleted the end bracket of the tag. !!!?
result = new NoChangeEvent(fStructuredDocument, fRequester, fChanges, fStart, fLengthToReplace);
} else {
// old instance of old node
if ((holdOldRegions.size() == 1) && (holdNewRegions.size() == 1) && _regionsSameKind((holdNewRegions.get(0)), (holdOldRegions.get(0)))) {
ITextRegion newOldRegion = swapNewForOldRegion(oldNode, holdOldRegions.get(0), newNode, holdNewRegions.get(0));
// -- need to update any down stream regions, within this
// 'oldNode'
updateDownStreamRegions(oldNode, newOldRegion);
result = new RegionChangedEvent(fStructuredDocument, fRequester, oldNode, newOldRegion, fChanges, fStart, fLengthToReplace);
} else {
replaceRegions(oldNode, holdOldRegions, newNode, holdNewRegions);
// -- need to update any down stream regions, within this
// 'oldNode'
// don't need with the way replaceRegions is implemented.
// It handles.
// if(holdNewRegions.size() > 0)
// updateDownStreamRegions(oldNode, (ITextRegion)
// holdNewRegions.lastElement());
result = new RegionsReplacedEvent(fStructuredDocument, fRequester, oldNode, holdOldRegions, holdNewRegions, fChanges, fStart, fLengthToReplace);
}
}
}
return result;
}
Aggregations