OSDN Git Service

Merge branch 'master' of ttp@git.sourceforge.jp:/gitroot/applistation/AppliStation
[applistation/AppliStation.git] / AppliStation / UserPrefForm.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Drawing;\r
4 using System.Windows.Forms;\r
5 \r
6 using NaGet.Packages;\r
7 \r
8 namespace AppliStation\r
9 {\r
10         /// <summary>\r
11         /// ユーザ設定用フォーム\r
12         /// </summary>\r
13         public partial class UserPrefForm : Form\r
14         {\r
15                 private List<RepositoryInfo> repos;\r
16                 \r
17                 private bool isRepositoriesChanged;\r
18                 \r
19                 public UserPrefForm()\r
20                 {\r
21                         repos = new List<RepositoryInfo>();\r
22                         \r
23                         InitializeComponent();\r
24                         \r
25                         loadCurrentPref();\r
26                 }\r
27                 \r
28                 /// <summary>\r
29                 /// 現在の設定を読み込む\r
30                 /// </summary>\r
31                 public void loadCurrentPref()\r
32                 {\r
33                         this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
34                 }\r
35                 \r
36                 public RepositoriesList RepositoriesListSetting {\r
37                         get {\r
38                                 RepositoriesList repoList = new RepositoriesList();\r
39                                 repoList.Repositories = repos.ToArray();\r
40                                 return repoList;\r
41                         }\r
42                         set {\r
43                                 repos.Clear();\r
44                                 repos.AddRange(value.Repositories);\r
45                                 isRepositoriesChanged = false;\r
46                                 \r
47                                 updateRepos();\r
48                         }\r
49                 }\r
50                 \r
51                 private void updateRepos()\r
52                 {\r
53                         repoListCheckedListBox.Items.Clear();\r
54                         \r
55                         foreach (RepositoryInfo repo in repos) {\r
56                                 string label = repoListCheckedListBoxRenderer(repo);\r
57                                 repoListCheckedListBox.Items.Add(label, repo.Enabled);\r
58                         }\r
59                 }\r
60                 \r
61                 public string repoListCheckedListBoxRenderer(RepositoryInfo repo)\r
62                 {\r
63                         return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
64                 }\r
65                 \r
66                 \r
67                 void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
68                 {\r
69                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
70                         bool selected = (selectedIndex >= 0);\r
71                         \r
72                         removeRepoButton.Enabled        = selected;\r
73                         upRepoButton.Enabled            = selected && ((1 <= selectedIndex) && (selectedIndex < repos.Count));\r
74                         downRepoButton.Enabled          = selected && ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1)));\r
75                         repoUrlLabel.Enabled            = selected;\r
76                         repoUrlTextBox.Enabled          = selected;\r
77                         if (selected) {\r
78                                 repoUrlTextBox.Text = repos[repoListCheckedListBox.SelectedIndex].Url.Href;\r
79                                 repoUrlTextBox.SelectAll();\r
80                         } else {\r
81                                 repoUrlTextBox.Clear();\r
82                         }\r
83                 }\r
84                 \r
85                 void AddRepoButtonClick(object sender, EventArgs e)\r
86                 {\r
87                         RepositoryInfo repo = new RepositoryInfo();\r
88                         repo.Name = string.Format("新しいレポジトリ");\r
89                         repo.Url = new LocationEntry();\r
90                         repo.Enabled = true;\r
91                         \r
92                         repos.Add(repo);\r
93                         \r
94                         updateRepos();\r
95                         repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
96                         \r
97                         isRepositoriesChanged = true;\r
98                 }\r
99                 \r
100                 void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
101                 {\r
102                         repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
103                         \r
104                         isRepositoriesChanged = true;\r
105                 }\r
106                 \r
107                 void RemoveRepoButtonClick(object sender, EventArgs e)\r
108                 {\r
109                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
110                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
111                                 string text = string.Format("本当にレポジトリ「{0}」を消去しますか?", repos[selectedIndex].Name);\r
112                                 DialogResult result = MessageBox.Show(text, "レポジトリの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
113                                 if (result == DialogResult.OK) {\r
114                                         repos.RemoveAt(selectedIndex);\r
115                                         repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
116                                         \r
117                                         isRepositoriesChanged = true;\r
118                                 }\r
119                         }\r
120                 }\r
121                 \r
122                 void UpRepoButtonClick(object sender, EventArgs e)\r
123                 {\r
124                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
125                         if ((1 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
126                                 NaGet.Utils.ListSwap(repos, selectedIndex-1, selectedIndex);\r
127                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
128                                 repoListCheckedListBox.SelectedIndex --;\r
129                                 \r
130                                 isRepositoriesChanged = true;\r
131                         }\r
132                 }\r
133                 \r
134                 void DownRepoButtonClick(object sender, EventArgs e)\r
135                 {\r
136                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
137                         if ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1))) {\r
138                                 NaGet.Utils.ListSwap(repos, selectedIndex, selectedIndex+1);\r
139                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
140                                 repoListCheckedListBox.SelectedIndex ++;\r
141                                 \r
142                                 isRepositoriesChanged = true;\r
143                         }\r
144                 }\r
145                 \r
146                 void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
147                 {\r
148                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
149                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
150                                 repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
151                                 repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
152                                 \r
153                                 isRepositoriesChanged = true;\r
154                         }\r
155                 }\r
156                 \r
157                 public bool IsRepositoryListSettingChanged {\r
158                         get { return isRepositoriesChanged; }\r
159                 }\r
160                 \r
161                 private void commitRepositorySetting()\r
162                 {\r
163                         if (isRepositoriesChanged) {\r
164                                 NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
165                         }\r
166                 }\r
167                 \r
168                 void OkButtonClick(object sender, EventArgs e)\r
169                 {\r
170                         commitRepositorySetting();\r
171                 }\r
172                 \r
173                 void CancelButtonClick(object sender, EventArgs e)\r
174                 {\r
175                 }\r
176         }\r
177 }\r