Search in sources :

Example 1 with StableElement

use of com.intellij.util.xml.StableElement in project intellij-community by JetBrains.

the class StableInvocationHandler method invoke.

@Override
public final Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
    if (StableElement.class.equals(method.getDeclaringClass())) {
        try {
            return method.invoke(this, args);
        } catch (InvocationTargetException e) {
            throw e.getCause();
        }
    }
    if (AdvancedProxy.FINALIZE_METHOD.equals(method))
        return null;
    if (isNotValid(myCachedValue)) {
        if (myCachedValue != null) {
            myOldValue = myCachedValue;
        }
        myCachedValue = myProvider.create();
        if (isNotValid(myCachedValue)) {
            if (AdvancedProxy.EQUALS_METHOD.equals(method)) {
                final Object arg = args[0];
                if (!(arg instanceof StableElement))
                    return false;
                final StableInvocationHandler handler = DomManagerImpl.getStableInvocationHandler(arg);
                if (handler == null || handler.getWrappedElement() != null)
                    return false;
                return Comparing.equal(myOldValue, handler.myOldValue);
            }
            if (myOldValue != null && Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(myOldValue, args);
            }
            if ("isValid".equals(method.getName())) {
                return Boolean.FALSE;
            }
            throw new AssertionError("Calling methods on invalid value");
        }
    }
    if (AdvancedProxy.EQUALS_METHOD.equals(method)) {
        final Object arg = args[0];
        if (arg instanceof StableElement) {
            return myCachedValue.equals(((StableElement) arg).getWrappedElement());
        }
        return myCachedValue.equals(arg);
    }
    if (AdvancedProxy.HASHCODE_METHOD.equals(method)) {
        return myCachedValue.hashCode();
    }
    try {
        return method.invoke(myCachedValue, args);
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}
Also used : StableElement(com.intellij.util.xml.StableElement) MergedObject(com.intellij.util.xml.MergedObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with StableElement

use of com.intellij.util.xml.StableElement in project intellij-community by JetBrains.

the class MockDomElementsEditor method initFileEditor.

protected final DomFileEditor initFileEditor(final Project project, final VirtualFile virtualFile, final String name, final Factory<? extends BasicDomElementComponent> component) {
    myFileEditor = new DomFileEditor<BasicDomElementComponent>(project, virtualFile, name, component) {

        @Override
        public JComponent getPreferredFocusedComponent() {
            return null;
        }

        @Override
        @NotNull
        protected JComponent createCustomComponent() {
            final JComponent customComponent = super.createCustomComponent();
            myContents = getDomComponent();
            return customComponent;
        }

        @Override
        public void reset() {
            for (final Map.Entry<EditedElementDescription<? extends DomElement>, DomElement> entry : myDomElements.entrySet()) {
                final DomElement newValue = entry.getKey().find();
                final DomElement oldValue = entry.getValue();
                if (newValue != null && !newValue.equals(oldValue) || newValue == null && !oldValue.getManager().isMockElement(oldValue)) {
                    ((StableElement) oldValue).revalidate();
                }
            }
            super.reset();
        }

        @Override
        public void commit() {
            super.commit();
            final List<EditedElementDescription> descriptions = new ArrayList<>();
            final Set<PsiFile> changedFiles = new HashSet<>();
            for (final Map.Entry<EditedElementDescription<? extends DomElement>, DomElement> entry : myDomElements.entrySet()) {
                final EditedElementDescription description = entry.getKey();
                final DomElement editedElement = entry.getValue();
                if (description.find() == null && editedElement.getXmlTag() != null) {
                    descriptions.add(description);
                    final XmlFile xmlFile = description.getEditedFile();
                    if (xmlFile != null) {
                        changedFiles.add(xmlFile);
                    }
                }
            }
            new WriteCommandAction(project, PsiUtilCore.toPsiFileArray(changedFiles)) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    for (EditedElementDescription description : descriptions) {
                        final DomElement editedElement = myDomElements.get(description);
                        DomElement element = description.addElement();
                        element.copyFrom(editedElement);
                        description.initialize(element);
                        removeWatchedElement(editedElement);
                        ((StableElement) editedElement).invalidate();
                    }
                }
            }.execute();
        }
    };
    final DomManager domManager = DomManager.getDomManager(project);
    for (final DomElement element : myDomElements.values()) {
        if (domManager.isMockElement(element)) {
            myFileEditor.addWatchedElement(element);
        }
    }
    return myFileEditor;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) StableElement(com.intellij.util.xml.StableElement) DomElement(com.intellij.util.xml.DomElement)

Aggregations

StableElement (com.intellij.util.xml.StableElement)2 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 XmlFile (com.intellij.psi.xml.XmlFile)1 DomElement (com.intellij.util.xml.DomElement)1 DomManager (com.intellij.util.xml.DomManager)1 MergedObject (com.intellij.util.xml.MergedObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NotNull (org.jetbrains.annotations.NotNull)1