/*
 * Copyright 2013-2016 consulo.io
 *
 * 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 consulo.google.appengine.python.module.extension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.annotation.Nonnull;
import javax.swing.*;

import org.jdom.Element;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModel;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Conditions;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ui.FormBuilder;
import com.jetbrains.python.sdk.PythonSdkType;
import consulo.bundle.SdkUtil;
import consulo.roots.ModuleRootLayer;
import consulo.roots.ui.configuration.SdkComboBox;
import consulo.util.pointers.NamedPointer;

/**
 * @author VISTALL
 * @since 28.09.13.
 */
public abstract class BasePyModuleExtension<T extends BasePyModuleExtension<T>> extends AbstractPyModuleExtension<T>
{
	private static final String RUNTIME_SDK_NAME = "runtime-sdk-name";
	protected NamedPointer<Sdk> myRuntimeSdkPointer;

	public BasePyModuleExtension(@Nonnull String id, @Nonnull ModuleRootLayer module)
	{
		super(id, module);
	}

	@Nonnull
	protected JComponent createRuntimeCheckBox()
	{
		SdkModel model = ProjectStructureConfigurable.getInstance(getModule().getProject()).getProjectSdksModel();

		final SdkComboBox box = new SdkComboBox(model, Conditions.<SdkTypeId>is(PythonSdkType.getInstance()), true);
		if(myRuntimeSdkPointer == null)
		{
			box.setSelectedNoneSdk();
		}
		else
		{
			box.setSelectedSdk(myRuntimeSdkPointer.getName());
		}

		box.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				SdkComboBox.SdkComboBoxItem selectedItem = box.getSelectedItem();
				String sdkName = selectedItem.getSdkName();
				myRuntimeSdkPointer = sdkName == null ? null : SdkUtil.createPointer(sdkName);
			}
		});

		return FormBuilder.createFormBuilder().addLabeledComponent("Runtime SDK: ", box).getPanel();
	}

	@Override
	public void commit(@Nonnull T mutableModuleExtension)
	{
		super.commit(mutableModuleExtension);

		myRuntimeSdkPointer = mutableModuleExtension.myRuntimeSdkPointer;
	}

	@SuppressWarnings("unused")
	public boolean isModifiedImpl(T t)
	{
		return super.isModifiedImpl(t) || !Comparing.equal(myRuntimeSdkPointer, t.myRuntimeSdkPointer);
	}

	@Override
	public Sdk getRuntimeSdk()
	{
		return myRuntimeSdkPointer == null ? null : myRuntimeSdkPointer.get();
	}

	@Override
	protected void getStateImpl(@Nonnull Element element)
	{
		super.getStateImpl(element);

		if(myRuntimeSdkPointer != null)
		{
			element.setAttribute(RUNTIME_SDK_NAME, myRuntimeSdkPointer.getName());
		}
	}

	@Override
	protected void loadStateImpl(@Nonnull Element element)
	{
		super.loadStateImpl(element);

		String runtimeSdkName = element.getAttributeValue(RUNTIME_SDK_NAME);
		myRuntimeSdkPointer = StringUtil.isEmpty(runtimeSdkName) ? null : SdkUtil.createPointer(runtimeSdkName);
	}
}

@@ -659,154 +659,53 @@
 ort 
-java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A%0Aimport javax.annotation.Nonnull;%0Aimport javax.swing.*;%0A%0Aimport org.jdom.Element
+com.intellij.openapi.options.ShowSettingsUtil
 ;%0Aim
@@ -855,89 +855,8 @@
 Id;%0A
-import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;%0A
 impo
@@ -1097,18 +1097,134 @@
 ulo.
-bundle.Sdk
+annotation.access.RequiredReadAction;%0Aimport consulo.bundle.SdkUtil;%0Aimport consulo.ide.settings.impl.ProjectStructureSettings
 Util
@@ -1356,16 +1356,97 @@
 Pointer;
+%0Aimport org.jdom.Element;%0A%0Aimport javax.annotation.Nonnull;%0Aimport javax.swing.*;
 %0A%0A/**%0A *
@@ -1900,25 +1900,45 @@
 %7B%0A%09%09
-SdkModel mode
+ProjectStructureSettingsUtil uti
 l = 
+(
 Proj
@@ -1953,68 +1953,80 @@
 ture
-Configurable.getInstance(getModule().getProject()).getProjec
+SettingsUtil) ShowSettingsUtil.getInstance();%0A%09%09SdkModel model = util.ge
 tSdk
@@ -2316,98 +2316,15 @@
 ner(
-new ActionListener()%0A%09%09%7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e)%0A%09%09%09
+e -%3E 
 %7B%0A
-%09
 %09%09%09S
@@ -2391,17 +2391,16 @@
 m();%0A%09%09%09
-%09
 String s
@@ -2435,17 +2435,16 @@
 Name();%0A
-%09
 %09%09%09myRun
@@ -2521,13 +2521,8 @@
 e);%0A
-%09%09%09%7D%0A
 %09%09%7D)
@@ -2615,32 +2615,53 @@
 getPanel();%0A%09%7D%0A%0A
+%09@RequiredReadAction%0A
 %09@Override%0A%09publ
@@ -3332,24 +3332,45 @@
 ));%0A%09%09%7D%0A%09%7D%0A%0A
+%09@RequiredReadAction%0A
 %09@Override%0A%09

fix compilation