/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.mustbe.consulo.dotnet.module.extension;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComponent;

import org.consulo.module.extension.MutableModuleInheritableNamedPointer;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
import com.intellij.openapi.roots.ui.configuration.SdkComboBox;
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.util.Condition;

/**
 * @author VISTALL
 * @since 02.09.14
 */
public class DotNetModuleExtensionWithSdkPanel
{
	public static JComponent create(@NotNull DotNetMutableModuleExtension<?> myExtensionWithSdk, @NotNull final Runnable updater)
	{
		final Class<? extends SdkType> sdkType = myExtensionWithSdk.getSdkTypeClass();
		final ProjectSdksModel projectSdksModel = ProjectStructureConfigurable.getInstance(myExtensionWithSdk.getModule().getProject())
				.getProjectSdksModel();
		final SdkComboBox mySdkComboBox = new SdkComboBox(projectSdksModel, new Condition<SdkTypeId>()
		{
			@Override
			public boolean value(SdkTypeId sdkTypeId)
			{
				return sdkType.isAssignableFrom(sdkTypeId.getClass());
			}
		}, true);
		mySdkComboBox.insertModuleItems(myExtensionWithSdk);

		final MutableModuleInheritableNamedPointer<Sdk> inheritableSdk = myExtensionWithSdk.getInheritableSdk();
		if(inheritableSdk.isNull())
		{
			mySdkComboBox.setSelectedNoneSdk();
		}
		else
		{
			final String sdkInheritModuleName = inheritableSdk.getModuleName();
			if(sdkInheritModuleName != null)
			{
				final Module sdkInheritModule = inheritableSdk.getModule();
				if(sdkInheritModule == null)
				{
					mySdkComboBox.addInvalidModuleItem(sdkInheritModuleName);
				}
				mySdkComboBox.setSelectedModule(sdkInheritModuleName);
			}
			else
			{
				mySdkComboBox.setSelectedSdk(inheritableSdk.getName());
			}
		}

		mySdkComboBox.addItemListener(new ItemListener()
		{
			@Override
			public void itemStateChanged(ItemEvent e)
			{
				inheritableSdk.set(mySdkComboBox.getSelectedModuleName(), mySdkComboBox.getSelectedSdkName());
				updater.run();
			}
		});

		return LabeledComponent.left(mySdkComboBox, "Sdk:");
	}
}

@@ -1467,16 +1467,26 @@
 Panel%0A%7B%0A
+%09@NotNull%0A
 %09public 
@@ -1525,16 +1525,22 @@
 l DotNet
+Simple
 MutableM
@@ -1558,17 +1558,21 @@
 ion%3C?%3E m
-y
+odule
 Extensio
@@ -1572,23 +1572,16 @@
 xtension
-WithSdk
 , @NotNu
@@ -1650,25 +1650,29 @@
  sdkType = m
-y
+odule
 ExtensionWit
@@ -1664,31 +1664,24 @@
 uleExtension
-WithSdk
 .getSdkTypeC
@@ -1774,17 +1774,21 @@
 stance(m
-y
+odule
 Extensio
@@ -1784,31 +1784,24 @@
 uleExtension
-WithSdk
 .getModule()
@@ -1863,19 +1863,17 @@
 omboBox 
-myS
+s
 dkComboB
@@ -2083,19 +2083,17 @@
 rue);%0A%09%09
-myS
+s
 dkComboB
@@ -2114,17 +2114,21 @@
 eItems(m
-y
+odule
 Extensio
@@ -2128,23 +2128,16 @@
 xtension
-WithSdk
 );%0A%0A%09%09fi
@@ -2200,17 +2200,21 @@
 eSdk = m
-y
+odule
 Extensio
@@ -2214,23 +2214,16 @@
 xtension
-WithSdk
 .getInhe
@@ -2273,19 +2273,17 @@
 %0A%09%09%7B%0A%09%09%09
-myS
+s
 dkComboB
@@ -2538,27 +2538,25 @@
 %0A%09%09%09%09%7B%0A%09%09%09%09%09
-myS
+s
 dkComboBox.a
@@ -2604,27 +2604,25 @@
 ;%0A%09%09%09%09%7D%0A%09%09%09%09
-myS
+s
 dkComboBox.s
@@ -2683,19 +2683,17 @@
 %09%09%7B%0A%09%09%09%09
-myS
+s
 dkComboB
@@ -2749,19 +2749,17 @@
 %0A%09%09%7D%0A%0A%09%09
-myS
+s
 dkComboB
@@ -2859,24 +2859,80 @@
 ent e)%0A%09%09%09%7B%0A
+%09%09%09%09if(e.getStateChange() == ItemEvent.SELECTED)%0A%09%09%09%09%7B%0A%09
 %09%09%09%09inherita
@@ -2942,19 +2942,17 @@
 Sdk.set(
-myS
+s
 dkComboB
@@ -2979,19 +2979,17 @@
 Name(), 
-myS
+s
 dkComboB
@@ -3018,16 +3018,17 @@
 ));%0A%09%09%09%09
+%09
 updater.
@@ -3034,16 +3034,22 @@
 .run();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3087,11 +3087,9 @@
 eft(
-myS
+s
 dkCo

allow simple extension