void terminate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ContextMenu  cm  = (ContextMenu)ContextMenu.ItemsControlFromItemContainer((MenuItem)e.OriginalSource);
                CEc2Instance ins = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;

                MessageBoxResult result = MessageBox.Show(
                    "You are about to terminate the selected instance. Are you sure you want to continue?",
                    "Terminate Instance",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Warning,
                    MessageBoxResult.No);
                if (result == MessageBoxResult.No)
                {
                    return;
                }

                if (!string.IsNullOrEmpty(ins.instanceId))
                {
                    CEc2Service serv = new CEc2Service();
                    serv.terminate(ins.instanceId);
                }
                else
                {
                    MessageBox.Show("No instance ID available");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void launch()
        {
            try
            {
                CEc2Instance inst = new CEc2Instance();
                inst.imageId = _amiId;
                if (string.IsNullOrEmpty(_selectedKeyPair) == false)
                {
                    inst.keyPairName = _selectedKeyPair;
                }
                if (string.IsNullOrEmpty(_selectedSecurityGroups) == false)
                {
                    inst.securityGroups = _selectedSecurityGroups;
                }

                inst.launch();
                _launchSucceed = true;
            }
            catch (ThreadAbortException)
            {
                _launchSucceed = false;
                //don't do anything
            }
            catch (Exception ex)
            {
                _launchSucceed = false;
                MessageBox.Show(ex.Message);
            }

            Dispatcher.Invoke(new StopProgressbarCallback(disableProgressBar));
            _launchThread = null;
        }
        private void remoteConnect_Click(object sender, RoutedEventArgs e)
        {
            string       argument = null;
            InstanceList inslist  = this.clientR.Children[0] as InstanceList;

            if (inslist != null)
            {
                if (inslist.instancesLV.SelectedItem != null)
                {
                    CEc2Instance inst = inslist.instancesLV.SelectedItem as CEc2Instance;
                    if (inst != null)
                    {
                        if (string.IsNullOrEmpty(inst.publicDns) == false)
                        {
                            argument = "/v:" + inst.publicDns;
                        }
                    }
                }
            }

            System.Diagnostics.ProcessStartInfo procStartInfo;
            if (string.IsNullOrEmpty(argument) == true)
            {
                procStartInfo = new System.Diagnostics.ProcessStartInfo("mstsc.exe ");
            }
            else
            {
                procStartInfo = new System.Diagnostics.ProcessStartInfo("mstsc.exe ", argument);
            }

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = procStartInfo;
            process.Start();
        }
        private void contextMenu_Opened(object sender, RoutedEventArgs e)
        {
            try
            {
                ContextMenu  cm   = (ContextMenu)e.OriginalSource;
                CEc2Instance inst = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;
                if (string.Compare(inst.status, "running") != 0)
                {
                    for (int i = 0; i < cm.Items.Count; ++i)
                    {
                        MenuItem item = cm.Items[i] as MenuItem;
                        if (item != null)
                        {
                            item.IsEnabled = false;
                        }
                    }
                    return;
                }

                //remote connect is not available for non windows system
                if (string.Compare(inst.platform, "windows", true) == 0)
                {
                    for (int i = 0; i < cm.Items.Count; ++i)
                    {
                        MenuItem item = cm.Items[i] as MenuItem;
                        if (item != null)
                        {
                            if (string.Compare(item.Header.ToString(), "Remote Connect") == 0)
                            {
                                item.IsEnabled = false;
                                break;
                            }
                        }
                    }
                }

                //deployment is not available for any other image id
                if (string.Compare(inst.imageId, CEc2Instance.deployableAmiImageId, true) != 0)
                {
                    for (int i = 0; i < cm.Items.Count; ++i)
                    {
                        MenuItem item = cm.Items[i] as MenuItem;
                        if (item != null)
                        {
                            if (string.Compare(item.Header.ToString(), "Deploy") == 0)
                            {
                                item.IsEnabled = false;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public FetchPassword(CEc2Instance ins)
        {
            this.InitializeComponent();
            StatusBk.Text = ConstantString.ContactAmazon;

            oThread = new Thread(getPassword);
            oThread.Start(ins);

            okButton.IsEnabled = false;
            enableProgressBar();
        }
        void deploy_Click(object sender, RoutedEventArgs e)
        {
            ContextMenu  cm  = (ContextMenu)ContextMenu.ItemsControlFromItemContainer((MenuItem)e.OriginalSource);
            CEc2Instance ins = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;

            if (ins != null)
            {
                AppDeployment pw = new AppDeployment(_dashboard);
                pw.instance = ins;
                pw.Show();
            }
        }
        void remoteConnect_Click(object sender, RoutedEventArgs e)
        {
            ContextMenu  cm   = (ContextMenu)ContextMenu.ItemsControlFromItemContainer((MenuItem)e.OriginalSource);
            CEc2Instance inst = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;

            System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("mstsc.exe ", "/v:" + inst.publicDns);

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = procStartInfo;
            process.Start();
        }
        //private void instancesLV_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    foreach (Border b in lstBorders)
        //    {
        //        b.Width = instancesLV.ActualWidth - 15;
        //    }
        //}

        //private void border_Loaded(object sender, RoutedEventArgs e)
        //{
        //    Border border = (Border)sender;
        //    border.Width = instancesLV.ActualWidth - 15;
        //    lstBorders.Add(border);
        //}

        private void instancesLV_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //enable/disable deploy menu based on the image id
            CEc2Instance inst = instancesLV.SelectedItem as CEc2Instance;

            if (inst != null)
            {
                if (string.Compare(inst.imageId, CEc2Instance.deployableAmiImageId, true) == 0)
                {
                    if (string.Compare(inst.status, "running") == 0)
                    {
                        _dashboard.DeployMenu.IsEnabled = true;
                        return;
                    }
                }
                _dashboard.DeployMenu.IsEnabled = false;
            }
        }
 void reboot_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         ContextMenu  cm  = (ContextMenu)ContextMenu.ItemsControlFromItemContainer((MenuItem)e.OriginalSource);
         CEc2Instance ins = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;
         if (ins != null)
         {
             ins.reboot();
             removeInstance(ins.instanceId);
         }
         else
         {
             MessageBox.Show("No instance available");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
        void password_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ContextMenu  cm  = (ContextMenu)ContextMenu.ItemsControlFromItemContainer((MenuItem)e.OriginalSource);
                CEc2Instance ins = (CEc2Instance)((FrameworkElement)(((Panel)(cm.PlacementTarget)).Children[0])).DataContext;
                if (ins != null)
                {
                    //check key file
                    string keyFile = CAwsConfig.Instance.getKeyFilePath(ins.keyPairName);
                    if (string.IsNullOrEmpty(keyFile) == true ||
                        File.Exists(keyFile) == false)
                    {
                        KeyFileInputDlg kfInput = new KeyFileInputDlg(ins.keyPairName);
                        kfInput.ShowDialog();
                        keyFile = CAwsConfig.Instance.getKeyFilePath(ins.keyPairName);
                        if (string.IsNullOrEmpty(keyFile) == true ||
                            File.Exists(keyFile) == false)
                        {
                            MessageBox.Show("Cannot find the key file.", "Get Password", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }

                    FetchPassword pw = new FetchPassword(ins);
                    pw.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Cannot fetch the password for this instance.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void instanceMonitor()
        {
            try
            {
                do
                {
                    if (instances == null)
                    {
                        return;
                    }

                    //every half minutes
                    for (int index = 0; index < instances.Count; ++index)
                    {
                        CEc2Instance item = instances[index];
                        if (item != null)
                        {
                            if (item.updateWebStatus() == true)
                            {
                                instances.RemoveAt(index);
                                instances.Insert(index, item);
                            }
                        }
                    }
                    Thread.Sleep(30 * 1000);
                } while (true);
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show("Monitor thread caught exception: " + ex.Message);
            }
            _monitor = null;
        }
 private void deploy_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         InstanceList inslist = this.clientR.Children[0] as InstanceList;
         if (inslist != null)
         {
             if (inslist.instancesLV.SelectedItem != null)
             {
                 CEc2Instance inst = inslist.instancesLV.SelectedItem as CEc2Instance;
                 if (inst != null)
                 {
                     AppDeployment pw = new AppDeployment(this);
                     pw.instance = inst;
                     pw.Show();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }