/*
 * Copyright 2013-2015 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.unity3d.module;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.RequiredReadAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.roots.ModuleRootLayer;
import com.intellij.openapi.roots.ModuleRootLayerListener;
import com.intellij.openapi.roots.ModuleRootManager;
import lombok.val;

/**
 * @author VISTALL
 * @since 25.01.15
 */
public class UnitySyncModuleRootLayerListener extends ModuleRootLayerListener.Adapter
{
	@Override
	@RequiredReadAction
	public void currentLayerChanged(@NotNull Module module,
			@NotNull String oldName,
			@NotNull ModuleRootLayer moduleRootLayer,
			@NotNull String newName,
			@NotNull ModuleRootLayer moduleRootLayer2)
	{
		if(!module.getProject().isOpen())
		{
			return;
		}

		// if we dont have unity extension dont try sync switch
		if(moduleRootLayer.getExtension(Unity3dModuleExtension.class) == null)
		{
			return;
		}

		ModuleManager moduleManager = ModuleManager.getInstance(module.getProject());
		for(Module anotherModule : moduleManager.getModules())
		{
			if(anotherModule == module)
			{
				continue;
			}

			ModuleRootManager rootManager = ModuleRootManager.getInstance(anotherModule);
			val modifiableModel = rootManager.getModifiableModel();
			modifiableModel.setCurrentLayer(newName);

			ApplicationManager.getApplication().runWriteAction(new Runnable()
			{
				@Override
				public void run()
				{
					modifiableModel.commit();
				}
			});
		}
	}
}

@@ -867,32 +867,132 @@
 .ModuleManager;%0A
+import com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.roots.ModifiableRootModel;%0A
 import com.intel
@@ -1141,27 +1141,8 @@
 ger;
-%0Aimport lombok.val;
 %0A%0A/*
@@ -1518,12 +1518,26 @@
 %7B%0A%09%09
-if(!
+Project project = 
 modu
@@ -1551,16 +1551,31 @@
 roject()
+;%0A%09%09if(!project
 .isOpen(
@@ -1661,40 +1661,28 @@
 h%0A%09%09
-if(m
+M
 odule
-RootLayer.getExtension(
+ rootModule = 
 Unit
@@ -1703,15 +1703,52 @@
 sion
-.class)
+Util.getRootModule(project);%0A%09%09if(rootModule
  == 
@@ -1831,35 +1831,23 @@
 nstance(
-module.getP
+p
 roject
-()
 );%0A%09%09for
@@ -2046,10 +2046,32 @@
 %0A%09%09%09
-va
+final ModifiableRootMode
 l mo

correct change layer