/**
 * Copyright 2014 Microsoft Open Technologies Inc.
 *
 * 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.microsoftopentechnologies.intellij.serviceexplorer.azure;

import com.intellij.openapi.project.Project;
import com.microsoftopentechnologies.intellij.forms.ManageSubscriptionForm;
import com.microsoftopentechnologies.intellij.helpers.UIHelper;
import com.microsoftopentechnologies.intellij.serviceexplorer.Node;
import com.microsoftopentechnologies.intellij.serviceexplorer.NodeActionEvent;
import com.microsoftopentechnologies.intellij.serviceexplorer.NodeActionListener;
import com.microsoftopentechnologies.intellij.serviceexplorer.azure.mobileservice.MobileServiceModule;
import com.microsoftopentechnologies.intellij.serviceexplorer.azure.vm.VMServiceModule;

import java.util.Map;

public class AzureServiceModule extends Node {
    private static final String AZURE_SERVICE_MODULE_ID = AzureServiceModule.class.getName();
    private static final String ICON_PATH = "azure.png";
    private static final String BASE_MODULE_NAME = "Azure";

    private Project project;
    private MobileServiceModule mobileServiceModule = new MobileServiceModule(this);
    private VMServiceModule vmServiceModule = new VMServiceModule(this);

    public AzureServiceModule(Project project) {
        this(null, ICON_PATH, null);
        this.project = project;
    }

    public AzureServiceModule(Node parent, String iconPath, Object data) {
        super(AZURE_SERVICE_MODULE_ID, BASE_MODULE_NAME, parent, iconPath, true);
    }

    @Override
    protected Map<String, Class<? extends NodeActionListener>> initActions() {
        // register the "manage subscriptions" action
        addAction("Manage Subscriptions", new ManageSubscriptionsAction());
        return null;
    }

    public class ManageSubscriptionsAction extends NodeActionListener {
        @Override
        public void actionPerformed(NodeActionEvent e) {
            ManageSubscriptionForm form = new ManageSubscriptionForm(getProject());
            UIHelper.packAndCenterJDialog(form);
            form.setVisible(true);
            load();
        }
    }

    @Override
    protected void refreshItems() {
        // add the mobile service module; we check if the node has
        // already been added first because this method can be called
        // multiple times when the user clicks the "Refresh" context
        // menu item
        if(!isDirectChild(mobileServiceModule)) {
            addChildNode(mobileServiceModule);
        }
        mobileServiceModule.load();

        // add the VM service module
        if(!isDirectChild(vmServiceModule)) {
            addChildNode(vmServiceModule);
        }
        vmServiceModule.load();
    }

    @Override
    public Project getProject() {
        return project;
    }
}

@@ -2930,16 +2930,17 @@
 nu item%0A
+%0A
         
@@ -2933,32 +2933,84 @@
 item%0A%0A        if
+(!mobileServiceModule.isLoading()) %7B%0A            if 
 (!isDirectChild(
@@ -3037,32 +3037,36 @@
 ) %7B%0A            
+    
 addChildNode(mob
@@ -3084,34 +3084,42 @@
 odule);%0A        
+   
+ 
 %7D%0A
+    
         mobileSe
@@ -3138,25 +3138,24 @@
 load();%0A
-%0A
         
 // add t
@@ -3150,45 +3150,64 @@
     
-// add the VM s
+%7D%0A%0A        if(!vmS
 ervice
- m
+M
 odule
-%0A
+.isLoading()) %7B%0A  
         
 if(!
@@ -3202,18 +3202,21 @@
         
+  
 if
+ 
 (!isDire
@@ -3247,32 +3247,36 @@
 ) %7B%0A            
+    
 addChildNode(vmS
@@ -3290,34 +3290,42 @@
 odule);%0A        
+ 
-%7D%0A
+   %7D%0A    
         vmServic
@@ -3336,24 +3336,34 @@
 ule.load();%0A
+        %7D%0A
     %7D%0A%0A    @

Fix issue #163