/*
 * Copyright 2005 Sascha Weinreuter
 *
 * 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 org.intellij.lang.xpath.xslt.impl;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageFormatting;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.intellij.lang.xpath.xslt.XsltConfig;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

@State(name = "XSLT-Support.Configuration", storages = {@Storage(StoragePathMacros.NON_ROAMABLE_FILE)})
class XsltConfigImpl extends XsltConfig implements PersistentStateComponent<XsltConfigImpl> {
  public boolean SHOW_LINKED_FILES = true;

  @Nullable
  @Override
  public XsltConfigImpl getState() {
    return this;
  }

  @Override
  public void loadState(@NotNull XsltConfigImpl state) {
    XmlSerializerUtil.copyBean(state, this);
  }

  @Override
  public boolean isShowLinkedFiles() {
    return SHOW_LINKED_FILES;
  }

  public static class UIImpl extends JPanel implements SearchableConfigurable {
    private final JCheckBox myShowLinkedFiles;

    private final XsltConfigImpl myConfig;

    public UIImpl(XsltConfigImpl config) {
      myConfig = config;
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

      myShowLinkedFiles = new JCheckBox("Show Associated Files in Project View");
      myShowLinkedFiles.setMnemonic('A');
      myShowLinkedFiles.setSelected(myConfig.SHOW_LINKED_FILES);

      add(myShowLinkedFiles);

      final JPanel jPanel = new JPanel(new BorderLayout());
      jPanel.add(Box.createVerticalGlue(), BorderLayout.CENTER);

      final JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
      jPanel.add(panel, BorderLayout.SOUTH);
      jPanel.setAlignmentX(0);
      add(jPanel);
    }

    @Override
    @Nls
    public String getDisplayName() {
      return "XSLT";
    }

    @Override
    @NotNull
    @NonNls
    public String getHelpTopic() {
      return "settings.xslt";
    }

    @Override
    public JComponent createComponent() {
      return this;
    }

    @Override
    public boolean isModified() {
      return myConfig.SHOW_LINKED_FILES != myShowLinkedFiles.isSelected();
    }

    @Override
    public void apply() {
      boolean oldValue = myConfig.SHOW_LINKED_FILES;

      myConfig.SHOW_LINKED_FILES = myShowLinkedFiles.isSelected();

      // TODO: make this a ConfigListener
      if (oldValue != myConfig.SHOW_LINKED_FILES) {
        final Project[] projects = ProjectManager.getInstance().getOpenProjects();
        for (Project project : projects) {
          ProjectView.getInstance(project).refresh();
        }
      }
    }

    @Override
    public void reset() {
      myShowLinkedFiles.setSelected(myConfig.SHOW_LINKED_FILES);
    }

    @Override
    @NotNull
    public String getId() {
      return getHelpTopic();
    }
  }
}

@@ -2371,56 +2371,69 @@
 mpl(
-XsltConfigImpl config) %7B%0A      myConfig = config
+) %7B%0A      myConfig = (XsltConfigImpl)XsltConfig.getInstance()
 ;%0A  

Fix XSLT configurable initialization