~charlie.poole/nunitv2/equality-handlers

« back to all changes in this revision

Viewing changes to src/ProjectEditor/editor/ConfigurationEditor/ConfigurationEditor.cs

  • Committer: Charlie Poole
  • Date: 2011-03-29 21:54:46 UTC
  • mfrom: (3257.4.15 standalone-editor)
  • Revision ID: charlie@nunit.org-20110329215446-pu1r6okg4www4zat
Remove integrated project editor and substitute new standalone editor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ****************************************************************
 
2
// Copyright 2011, Charlie Poole
 
3
// This is free software licensed under the NUnit license. You may
 
4
// obtain a copy of the license at http://nunit.org
 
5
// ****************************************************************
 
6
 
 
7
using System;
 
8
using System.Windows.Forms;
 
9
 
 
10
namespace NUnit.ProjectEditor
 
11
{
 
12
    public class ConfigurationEditor
 
13
    {
 
14
        #region Instance Variables
 
15
 
 
16
        private IProjectModel model;
 
17
        private IConfigurationEditorDialog view;
 
18
 
 
19
        #endregion
 
20
 
 
21
        #region Constructor
 
22
 
 
23
        public ConfigurationEditor(IProjectModel model, IConfigurationEditorDialog view)
 
24
        {
 
25
            this.model = model;
 
26
            this.view = view;
 
27
 
 
28
            UpdateConfigList();
 
29
 
 
30
            view.AddCommand.Execute += AddConfig;
 
31
            view.RemoveCommand.Execute += RemoveConfig;
 
32
            view.RenameCommand.Execute += RenameConfig;
 
33
            view.ActiveCommand.Execute += MakeActive;
 
34
 
 
35
            view.ConfigList.SelectionChanged += SelectedConfigChanged;
 
36
        }
 
37
 
 
38
        #endregion
 
39
 
 
40
        #region Command Event Handlers
 
41
 
 
42
        public void AddConfig()
 
43
        {
 
44
            IAddConfigurationDialog dlg = view.AddConfigurationDialog;
 
45
            new AddConfigurationPresenter(model, dlg);
 
46
 
 
47
            dlg.ShowDialog();
 
48
 
 
49
            UpdateConfigList();
 
50
        }
 
51
 
 
52
        public void RenameConfig()
 
53
        {
 
54
            string oldName = view.ConfigList.SelectedItem;
 
55
            if (oldName.EndsWith(" (active)"))
 
56
                oldName = oldName.Substring(0, oldName.Length - 9);
 
57
 
 
58
            IRenameConfigurationDialog dlg = view.RenameConfigurationDialog;
 
59
            new RenameConfigurationPresenter(model, dlg, oldName);
 
60
 
 
61
            dlg.ShowDialog();
 
62
 
 
63
            UpdateConfigList();
 
64
        }
 
65
 
 
66
        public void RemoveConfig()
 
67
        {
 
68
            model.RemoveConfigAt(view.ConfigList.SelectedIndex);
 
69
 
 
70
            UpdateConfigList();
 
71
        }
 
72
 
 
73
        public void MakeActive()
 
74
        {
 
75
            model.ActiveConfigName = view.ConfigList.SelectedItem;
 
76
 
 
77
            UpdateConfigList();
 
78
        }
 
79
 
 
80
        public void SelectedConfigChanged()
 
81
        {
 
82
            int index = view.ConfigList.SelectedIndex;
 
83
 
 
84
            view.AddCommand.Enabled = true;
 
85
            view.ActiveCommand.Enabled = index >= 0 && model.Configs[index].Name != model.ActiveConfigName;
 
86
            view.RenameCommand.Enabled = index >= 0;
 
87
            view.RemoveCommand.Enabled = index >= 0;
 
88
        }
 
89
 
 
90
        #endregion
 
91
 
 
92
        #region Helper Methods
 
93
 
 
94
        private void UpdateConfigList()
 
95
        {
 
96
            string selectedConfig = view.ConfigList.SelectedItem;
 
97
            if (selectedConfig != null && selectedConfig.EndsWith(" (active)"))
 
98
                selectedConfig = selectedConfig.Substring(0, selectedConfig.Length - 9);
 
99
            int selectedIndex = -1;
 
100
            int activeIndex = -1;
 
101
 
 
102
            int count = model.Configs.Count;
 
103
            string[] configList = new string[count];
 
104
 
 
105
            for (int index = 0; index < count; index++)
 
106
            {
 
107
                string config = model.Configs[index].Name;
 
108
 
 
109
                if (config == model.ActiveConfigName)
 
110
                    activeIndex = index;
 
111
                if (config == selectedConfig)
 
112
                    selectedIndex = index;
 
113
 
 
114
                configList[index] = config;
 
115
            }
 
116
 
 
117
            if (activeIndex >= 0)
 
118
                configList[activeIndex] += " (active)";
 
119
 
 
120
            view.ConfigList.SelectionList = configList;
 
121
 
 
122
            view.ConfigList.SelectedIndex = selectedIndex > 0
 
123
                ? selectedIndex
 
124
                : configList.Length > 0
 
125
                    ? 0 : -1;
 
126
 
 
127
            SelectedConfigChanged();
 
128
        }
 
129
 
 
130
        #endregion
 
131
    }
 
132
}