Search in sources :

Example 56 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONModelNotifierImpl method notifyDeferred.

/**
 */
private void notifyDeferred() {
    if (this.fEvents == null)
        return;
    if (this.flushing)
        return;
    // force notification
    this.flushing = true;
    int count = this.fEvents.size();
    if (!doingNewModel && fOptimizeDeferred) {
        Map notifyEvents = new HashMap();
        for (int i = 0; i < count; i++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
            if (event == null)
                // error
                continue;
            event.index = i;
            if (event.type == INodeNotifier.REMOVE) {
                addToMap(event.oldValue, event, notifyEvents);
            }
            if (event.type == INodeNotifier.ADD) {
                addToMap(event.newValue, event, notifyEvents);
            }
        }
        Iterator it = notifyEvents.values().iterator();
        while (it.hasNext()) {
            NotifyEvent[] es = (NotifyEvent[]) it.next();
            for (int i = 0; i < es.length - 1; i++) {
                NotifyEvent event = es[i];
                if (es[i].discarded)
                    continue;
                NotifyEvent next = es[i + 1];
                if (es[i].type == INodeNotifier.ADD && next.type == INodeNotifier.REMOVE) {
                    // Added then removed later, discard both
                    event.discarded = true;
                    next.discarded = true;
                    if (Debug.debugNotifyDeferred) {
                        event.reason = event.reason + ADDED_THEN_REMOVED + "(see " + next.index + // $NON-NLS-1$ //$NON-NLS-2$
                        ")";
                        next.reason = next.reason + ADDED_THEN_REMOVED + "(see " + event.index + // $NON-NLS-1$ //$NON-NLS-2$
                        ")";
                    }
                }
            }
        }
        for (int i = 0; i < count; i++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
            if (event == null)
                // error
                continue;
            if (event.discarded)
                continue;
            if (event.notifier != null && fOptimizeDeferredAccordingToParentAdded) {
                if (event.type == INodeNotifier.ADD) {
                    NotifyEvent[] es = (NotifyEvent[]) notifyEvents.get(event.notifier);
                    if (es != null)
                        for (int p = 0; p < es.length && es[p].index < event.index; p++) {
                            NotifyEvent prev = es[p];
                            if (prev.type == INodeNotifier.REMOVE && prev.oldValue == event.notifier) {
                                // parent is reparented, do not discard
                                if (Debug.debugNotifyDeferred) {
                                    event.reason = event.reason + PARENT_IS_REPARENTED + "(see " + prev.index + // $NON-NLS-1$ //$NON-NLS-2$
                                    ")";
                                }
                                break;
                            } else if (prev.type == INodeNotifier.ADD && prev.newValue == event.notifier) {
                                // parent has been added, discard this
                                event.discarded = true;
                                if (Debug.debugNotifyDeferred) {
                                    event.reason = event.reason + PARENT_IS_ADDED + "(see " + prev.index + // $NON-NLS-1$ //$NON-NLS-2$
                                    ")";
                                }
                                break;
                            }
                        }
                }
            }
            if (event.discarded)
                continue;
            if (event.notifier != null && fOptimizeDeferredAccordingToParentRemoved) {
                if (event.type == INodeNotifier.REMOVE) {
                    NotifyEvent[] es = (NotifyEvent[]) notifyEvents.get(event.notifier);
                    if (es != null)
                        for (int n = 0; n < es.length; n++) {
                            NotifyEvent next = es[n];
                            if (next.index > event.index && next.type == INodeNotifier.REMOVE) {
                                if (next.oldValue == event.notifier) {
                                    // parent will be removed, discard this
                                    event.discarded = true;
                                    if (Debug.debugNotifyDeferred) {
                                        event.reason = event.reason + PARENT_IS_REMOVED_TOO + "(see " + next.index + // $NON-NLS-1$ //$NON-NLS-2$
                                        ")";
                                    }
                                    break;
                                }
                            }
                        }
                }
            }
            if (event.discarded)
                continue;
        }
    }
    for (int i = 0; i < count; i++) {
        NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
        if (event == null)
            // error
            continue;
        if (event.discarded)
            continue;
        notify(event.notifier, event.type, event.changedFeature, event.oldValue, event.newValue, event.pos);
    }
    if (Debug.debugNotifyDeferred) {
        for (int l = 0; l < count; l++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(l);
            Object o = null;
            String t = null;
            if (event.type == INodeNotifier.ADD) {
                o = event.newValue;
                // $NON-NLS-1$
                t = " + ";
            } else if (event.type == INodeNotifier.REMOVE) {
                o = event.oldValue;
                // $NON-NLS-1$
                t = " - ";
            }
            if (o instanceof IJSONObject) {
                String p = ((IJSONNode) event.notifier).getNodeName();
                String c = ((IJSONNode) o).getNodeName();
                // $NON-NLS-1$ //$NON-NLS-2$
                String d = (event.discarded ? "! " : "  ");
                System.out.println(d + p + t + c);
            }
        }
    }
    this.flushing = false;
    this.fEvents = null;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) Map(java.util.Map) HashMap(java.util.HashMap) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1