Search in sources :

Example 1 with PersistentStateComponent

use of com.intellij.openapi.components.PersistentStateComponent in project intellij-community by JetBrains.

the class FacetUtil method saveFacetConfiguration.

public static Element saveFacetConfiguration(final FacetConfiguration configuration) throws WriteExternalException {
    if (configuration instanceof PersistentStateComponent) {
        Object state = ((PersistentStateComponent) configuration).getState();
        if (state instanceof Element)
            return ((Element) state);
        return XmlSerializer.serialize(state, new SkipDefaultValuesSerializationFilters());
    } else {
        final Element config = new Element(JpsFacetSerializer.CONFIGURATION_TAG);
        configuration.writeExternal(config);
        return config;
    }
}
Also used : PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) SkipDefaultValuesSerializationFilters(com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters) Element(org.jdom.Element)

Example 2 with PersistentStateComponent

use of com.intellij.openapi.components.PersistentStateComponent in project intellij-community by JetBrains.

the class AbstractExternalProjectImportBuilder method executeAndRestoreDefaultProjectSettings.

@SuppressWarnings("unchecked")
private void executeAndRestoreDefaultProjectSettings(@NotNull Project project, @NotNull Runnable task) {
    AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(project, myExternalSystemId);
    Object systemStateToRestore = null;
    if (systemSettings instanceof PersistentStateComponent) {
        systemStateToRestore = ((PersistentStateComponent) systemSettings).getState();
    }
    systemSettings.copyFrom(myControl.getSystemSettings());
    Collection projectSettingsToRestore = systemSettings.getLinkedProjectsSettings();
    Set<ExternalProjectSettings> projects = ContainerUtilRt.newHashSet(systemSettings.getLinkedProjectsSettings());
    projects.add(getCurrentExternalProjectSettings());
    systemSettings.setLinkedProjectsSettings(projects);
    try {
        task.run();
    } finally {
        if (systemStateToRestore != null) {
            ((PersistentStateComponent) systemSettings).loadState(systemStateToRestore);
        } else {
            systemSettings.setLinkedProjectsSettings(projectSettingsToRestore);
        }
    }
}
Also used : AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) Collection(java.util.Collection) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)

Example 3 with PersistentStateComponent

use of com.intellij.openapi.components.PersistentStateComponent in project intellij-community by JetBrains.

the class SystemFileProcessor method encodeFileText.

@Nullable
@Override
protected String encodeFileText(String content, VirtualFile file, Project project) throws IOException {
    final String fileName = file.getName();
    if (file.getParent().getName().equals(Project.DIRECTORY_STORE_FOLDER) && fileName.equals("workspace.xml")) {
        List<Object> componentList = new ArrayList<>();
        for (String componentName : COMPONENT_NAMES) {
            Object component = project.getComponent(componentName);
            if (component == null) {
                try {
                    Class<?> aClass = Class.forName(componentName);
                    component = project.getComponent(aClass);
                    if (component == null) {
                        component = ServiceManager.getService(project, aClass);
                    }
                } catch (ClassNotFoundException ignore) {
                }
            }
            ContainerUtil.addIfNotNull(componentList, component);
        }
        if (!componentList.isEmpty()) {
            final Element root = new Element("project");
            for (final Object component : componentList) {
                final Element element = new Element("component");
                element.setAttribute("name", ComponentManagerImpl.getComponentName(component));
                root.addContent(element);
                ApplicationManager.getApplication().invokeAndWait(() -> {
                    if (component instanceof JDOMExternalizable) {
                        try {
                            ((JDOMExternalizable) component).writeExternal(element);
                        } catch (WriteExternalException ignore) {
                            LOG.error(ignore);
                        }
                    } else if (component instanceof PersistentStateComponent) {
                        Object state = WriteAction.compute(() -> ((PersistentStateComponent) component).getState());
                        if (state == null) {
                            return;
                        }
                        Element element1 = state instanceof Element ? (Element) state : XmlSerializer.serialize(state);
                        element.addContent(element1.cloneContent());
                        element.setAttribute("name", StoreUtil.getStateSpec((PersistentStateComponent) component).name());
                    }
                }, ModalityState.defaultModalityState());
            }
            PathMacroManager.getInstance(project).collapsePaths(root);
            return JDOMUtil.writeElement(root);
        }
    }
    return null;
}
Also used : PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) Element(org.jdom.Element) JDOMExternalizable(com.intellij.openapi.util.JDOMExternalizable) ArrayList(java.util.ArrayList) WriteExternalException(com.intellij.openapi.util.WriteExternalException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PersistentStateComponent

use of com.intellij.openapi.components.PersistentStateComponent in project intellij-community by JetBrains.

the class RootModelImpl method writeExternal.

public void writeExternal(@NotNull Element element) {
    for (ModuleExtension extension : myExtensions) {
        if (extension instanceof PersistentStateComponent) {
            //noinspection ConstantConditions
            XmlSerializer.serializeInto(((PersistentStateComponent) extension).getState(), element);
        } else {
            //noinspection deprecation
            extension.writeExternal(element);
        }
    }
    for (ContentEntry contentEntry : getContent()) {
        if (contentEntry instanceof ContentEntryImpl) {
            final Element subElement = new Element(ContentEntryImpl.ELEMENT_NAME);
            ((ContentEntryImpl) contentEntry).writeExternal(subElement);
            element.addContent(subElement);
        }
    }
    for (OrderEntry orderEntry : getOrderEntries()) {
        if (orderEntry instanceof WritableOrderEntry) {
            ((WritableOrderEntry) orderEntry).writeExternal(element);
        }
    }
}
Also used : PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) Element(org.jdom.Element)

Aggregations

PersistentStateComponent (com.intellij.openapi.components.PersistentStateComponent)4 Element (org.jdom.Element)3 AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)1 ExternalProjectSettings (com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)1 JDOMExternalizable (com.intellij.openapi.util.JDOMExternalizable)1 WriteExternalException (com.intellij.openapi.util.WriteExternalException)1 SkipDefaultValuesSerializationFilters (com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Nullable (org.jetbrains.annotations.Nullable)1