/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.platform.templates;

import com.intellij.ide.util.projectWizard.ProjectTemplateFileProcessor;
import com.intellij.openapi.components.PathMacroManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.impl.ComponentManagerImpl;
import com.intellij.openapi.components.impl.stores.StoreUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dmitry Avdeev
 *         Date: 2/15/13
 */
public class SystemFileProcessor extends ProjectTemplateFileProcessor {

  private static final String[] COMPONENT_NAMES = new String[] {
    FileEditorManagerImpl.FILE_EDITOR_MANAGER,
    "org.jetbrains.idea.maven.project.MavenWorkspaceSettingsComponent"
  };

  @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<Object>();
      for (String componentName : COMPONENT_NAMES) {
        Object component = project.getComponent(componentName);
        if (component == null) {
          try {
            component = ServiceManager.getService(project, Class.forName(componentName));
          }
          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);
          UIUtil.invokeAndWaitIfNeeded(new Runnable() {
            @Override
            public void run() {
              if (component instanceof JDOMExternalizable) {
                try {
                  ((JDOMExternalizable)component).writeExternal(element);
                }
                catch (WriteExternalException ignore) {
                  LOG.error(ignore);
                }
              }
              else {
                Object state = ((PersistentStateComponent)component).getState();
                Element element1 = XmlSerializer.serialize(state);
                element.addContent(element1.cloneContent());
                element.setAttribute("name", StoreUtil.getStateSpec((PersistentStateComponent)component).name());
              }
            }
          });
        }
        PathMacroManager.getInstance(project).collapsePaths(root);
        return JDOMUtil.writeElement(root);
      }
    }
    return null;
  }

  private static final Logger LOG = Logger.getInstance(SystemFileProcessor.class);
}

@@ -1100,21 +1100,16 @@
 eEditor.
-impl.
 FileEdit
@@ -1113,28 +1113,24 @@
 ditorManager
-Impl
 ;%0Aimport com
@@ -1864,32 +1864,24 @@
 ager
-Impl.FILE_EDITOR_MANAGER
+.class.getName()
 ,%0A  
@@ -2461,32 +2461,184 @@
           try %7B%0A
+            Class%3C?%3E aClass = Class.forName(componentName);%0A            component = project.getComponent(aClass);%0A            if(component == null) %7B%0A  
             comp
@@ -2684,38 +2684,30 @@
 ct, 
+a
 Class
-.forName(componentName));
+);%0A            %7D
 %0A   
@@ -3619,16 +3619,67 @@
     else
+ if (component instanceof PersistentStateComponent)
  %7B%0A     
@@ -3774,26 +3774,149 @@
     
-Element element1 =
+if(state == null)%7B%0A                  return;%0A                %7D%0A                Element element1 = state instanceof Element ? (Element)state :
  Xml

Fix saving of FileEditorManager state