ICE 3.4.2
[php5-ice-freebsdport.git] / vsaddin / src / IncludePathView.cs
bloba754e64b9aa0c1a0c908546974e13b6ef3795ca6
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 using System;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Drawing;
14 using System.Data;
16 using System.Text;
17 using System.Windows.Forms;
19 using System.IO;
21 using EnvDTE;
23 namespace Ice.VisualStudio
25 public partial class IncludePathView : UserControl
27 public IncludePathView()
29 InitializeComponent();
32 public void load()
34 sliceIncludeList.Items.Clear();
35 IncludePathList list =
36 new IncludePathList(Util.getProjectProperty(_project, Util.PropertyIceIncludePath));
37 foreach(String s in list)
39 sliceIncludeList.Items.Add(s.Trim());
40 if(Path.IsPathRooted(s.Trim()))
42 sliceIncludeList.SetItemCheckState(sliceIncludeList.Items.Count - 1, CheckState.Checked);
47 public void setEnabled(bool enabled)
49 sliceIncludeList.Enabled = enabled;
50 btnAdd.Enabled = enabled;
51 btnEdit.Enabled = enabled;
52 btnRemove.Enabled = enabled;
53 btnUp.Enabled = enabled;
54 btnDown.Enabled = enabled;
57 public bool hasUnsavedChanges()
59 if(_editingIncludes &&
60 !_txtIncludeDir.Text.Trim().TrimEnd(Path.DirectorySeparatorChar).Equals(_editingIncludeDir))
62 return true;
65 if(!sliceIncludes().Equal(new IncludePathList(
66 Util.getProjectProperty(_project, Util.PropertyIceIncludePath))))
68 return true;
70 return false;
73 public bool apply()
75 bool changed = false;
76 IncludePathList includes = sliceIncludes();
77 if(!includes.Equal(new IncludePathList(
78 Util.getProjectProperty(_project, Util.PropertyIceIncludePath))))
80 String s = includes.ToString();
81 Util.setProjectProperty(_project, Util.PropertyIceIncludePath, s);
82 changed = true;
84 return changed;
87 public void setIceConfigurationDialog(IceConfigurationDialog dialog)
89 _dialog = dialog;
92 public bool editingIncludeDir()
94 return _editingIncludes;
97 private void sliceIncludeList_SelectedIndexChanged(object sender, EventArgs e)
99 if(_editingIncludes)
101 endEditIncludeDir(true);
105 private void beginEditIncludeDir()
107 if(_editingIncludes)
109 endEditIncludeDir(true);
111 _editingIncludes = true;
112 _dialog.unsetCancelButton();
113 if(_editingIndex != -1)
115 _txtIncludeDir = new TextBox();
116 _txtIncludeDir.Text = sliceIncludeList.Items[sliceIncludeList.SelectedIndex].ToString();
117 _editingIncludeDir = _txtIncludeDir.Text;
118 sliceIncludeList.SelectionMode = SelectionMode.One;
120 Rectangle rect = sliceIncludeList.GetItemRectangle(sliceIncludeList.SelectedIndex);
121 _txtIncludeDir.Location = new Point(sliceIncludeList.Location.X + 2,
122 sliceIncludeList.Location.Y + rect.Y);
123 _txtIncludeDir.Width = sliceIncludeList.Width - 50;
124 _txtIncludeDir.Parent = sliceIncludeList;
125 _txtIncludeDir.KeyDown += new KeyEventHandler(includeDirKeyDown);
126 _txtIncludeDir.KeyUp += new KeyEventHandler(includeDirKeyUp);
127 groupBox1.Controls.Add(_txtIncludeDir);
129 _btnSelectInclude = new Button();
130 _btnSelectInclude.Text = "...";
131 _btnSelectInclude.Location = new Point(sliceIncludeList.Location.X + _txtIncludeDir.Width,
132 sliceIncludeList.Location.Y + rect.Y);
133 _btnSelectInclude.Width = 49;
134 _btnSelectInclude.Height = _txtIncludeDir.Height;
135 _btnSelectInclude.Click += new EventHandler(selectIncludeClicked);
136 groupBox1.Controls.Add(_btnSelectInclude);
139 _txtIncludeDir.Show();
140 _txtIncludeDir.BringToFront();
141 _txtIncludeDir.Focus();
143 _btnSelectInclude.Show();
144 _btnSelectInclude.BringToFront();
148 public void endEditIncludeDir(bool saveChanges)
150 if(!_editingIncludes)
152 _dialog.needSave();
153 return;
155 _editingIncludes = false;
156 String path = null;
157 if(_editingIndex > -1 && _editingIndex < sliceIncludeList.Items.Count)
159 path = sliceIncludeList.Items[_editingIndex].ToString();
162 lock(this)
164 _dialog.setCancelButton();
165 if(_txtIncludeDir == null || _btnSelectInclude == null)
167 _dialog.needSave();
168 return;
170 if(saveChanges)
172 path = _txtIncludeDir.Text;
173 if(path != null)
175 path = path.Trim();
179 this.groupBox1.Controls.Remove(_txtIncludeDir);
180 _txtIncludeDir = null;
182 this.groupBox1.Controls.Remove(_btnSelectInclude);
183 _btnSelectInclude = null;
186 if(String.IsNullOrEmpty(path))
188 if(_editingIndex != -1)
190 sliceIncludeList.Items.RemoveAt(_editingIndex);
191 sliceIncludeList.SelectedIndex = sliceIncludeList.Items.Count - 1;
192 _editingIndex = -1;
195 else if(_editingIndex != -1 && saveChanges)
197 if(!path.Equals(sliceIncludeList.Items[_editingIndex].ToString(),
198 StringComparison.CurrentCultureIgnoreCase))
200 IncludePathList includes = sliceIncludes();
201 if(includes.Count > _editingIndex)
204 // We don't want an item to be considered a duplicate of itself.
206 includes.RemoveAt(_editingIndex);
208 if(includes.Contains(_project, path))
210 MessageBox.Show(this, "The Slice Include Path doesn't allow duplicates.\n" +
211 "Value: `" + path + "' will be removed.\n",
212 "Ice Visual Studio Add-in", MessageBoxButtons.OK,
213 MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
214 (MessageBoxOptions)0);
215 sliceIncludeList.Items.RemoveAt(_editingIndex);
216 sliceIncludeList.SelectedIndex = sliceIncludeList.Items.Count - 1;
217 _editingIndex = -1;
219 else
221 sliceIncludeList.Items[_editingIndex] = path;
222 if(Path.IsPathRooted(path))
224 sliceIncludeList.SetItemCheckState(_editingIndex, CheckState.Checked);
226 else
228 sliceIncludeList.SetItemCheckState(_editingIndex, CheckState.Unchecked);
233 resetIncludeDirChecks();
234 _dialog.needSave();
237 private void includeDirKeyDown(object sender, KeyEventArgs e)
239 if(e.KeyCode.Equals(Keys.Escape))
241 endEditIncludeDir(false);
243 else if(e.KeyCode.Equals(Keys.Enter))
245 endEditIncludeDir(true);
249 private void includeDirKeyUp(object sender, KeyEventArgs e)
251 if(!e.KeyCode.Equals(Keys.Enter) && !e.KeyCode.Equals(Keys.Escape) &&
252 _editingIncludes)
254 _dialog.needSave();
258 private void selectIncludeClicked(object sender, EventArgs e)
260 FolderBrowserDialog dialog = new FolderBrowserDialog();
261 string projectDir = Path.GetFullPath(Path.GetDirectoryName(_project.FileName));
262 dialog.SelectedPath = projectDir;
263 dialog.Description = "Slice Include Directory";
264 DialogResult result = dialog.ShowDialog();
265 if(result == DialogResult.OK)
267 string path = dialog.SelectedPath;
268 if(!Util.containsEnvironmentVars(path))
270 path = Util.relativePath(_project, Path.GetFullPath(path));
272 _txtIncludeDir.Text = path;
274 endEditIncludeDir(true);
277 private void btnAdd_Click(object sender, EventArgs e)
279 if(_editingIncludes)
281 endEditIncludeDir(true);
283 sliceIncludeList.Items.Add("");
284 sliceIncludeList.SelectedIndex = sliceIncludeList.Items.Count - 1;
285 _editingIndex = sliceIncludeList.SelectedIndex;
286 beginEditIncludeDir();
289 private void btnEdit_Click(object sender, EventArgs e)
291 if(sliceIncludeList.SelectedIndex != -1)
293 _editingIndex = sliceIncludeList.SelectedIndex;
294 beginEditIncludeDir();
298 private void btnRemove_Click(object sender, EventArgs e)
300 Cursor = Cursors.WaitCursor;
301 int index = sliceIncludeList.SelectedIndex;
302 if(_editingIncludes)
304 index = _editingIndex;
305 endEditIncludeDir(true);
307 if(index > -1 && index < sliceIncludeList.Items.Count)
309 int selected = index;
310 sliceIncludeList.Items.RemoveAt(selected);
311 if(sliceIncludeList.Items.Count > 0)
313 if(selected > 0)
315 selected -= 1;
317 sliceIncludeList.SelectedIndex = selected;
320 _dialog.needSave();
321 Cursor = Cursors.Default;
324 private IncludePathList sliceIncludes()
326 IncludePathList paths = new IncludePathList();
327 foreach(String s in sliceIncludeList.Items)
329 paths.Add(s.Trim());
331 return paths;
334 private void btnUp_Click(object sender, EventArgs e)
336 Cursor = Cursors.WaitCursor;
337 if(_editingIncludes)
339 endEditIncludeDir(true);
341 int index = sliceIncludeList.SelectedIndex;
342 if(index > 0)
344 string current = sliceIncludeList.SelectedItem.ToString();
345 sliceIncludeList.Items.RemoveAt(index);
346 sliceIncludeList.Items.Insert(index - 1, current);
347 sliceIncludeList.SelectedIndex = index - 1;
348 resetIncludeDirChecks();
349 _dialog.needSave();
351 Cursor = Cursors.Default;
354 private void btnDown_Click(object sender, EventArgs e)
356 Cursor = Cursors.WaitCursor;
357 if(_editingIncludes)
359 endEditIncludeDir(true);
361 int index = sliceIncludeList.SelectedIndex;
362 if(index < sliceIncludeList.Items.Count - 1 && index > -1)
364 string current = sliceIncludeList.SelectedItem.ToString();
365 sliceIncludeList.Items.RemoveAt(index);
366 sliceIncludeList.Items.Insert(index + 1, current);
367 sliceIncludeList.SelectedIndex = index + 1;
368 resetIncludeDirChecks();
369 _dialog.needSave();
371 Cursor = Cursors.Default;
375 // Reset the include dir checkboxes that indicate if the path
376 // is absolute or relative. This should be called after the list
377 // is populated or the includes list order is modified.
379 private void resetIncludeDirChecks()
381 for(int i = 0; i < sliceIncludeList.Items.Count; ++i)
383 String path = sliceIncludeList.Items[i].ToString();
384 if(String.IsNullOrEmpty(path))
386 continue;
389 if(Path.IsPathRooted(path))
391 sliceIncludeList.SetItemCheckState(i, CheckState.Checked);
393 else
395 sliceIncludeList.SetItemCheckState(i, CheckState.Unchecked);
400 private void sliceIncludeList_ItemCheck(object sender, ItemCheckEventArgs e)
402 if(_editingIncludes)
404 return;
406 string path = sliceIncludeList.Items[e.Index].ToString();
407 if(Util.containsEnvironmentVars(path))
409 return;
412 if(e.NewValue == CheckState.Unchecked)
414 path = Util.relativePath(_project, path);
416 else if(e.NewValue == CheckState.Checked)
418 if(!Path.IsPathRooted(path))
420 path = Util.absolutePath(_project, path);
424 sliceIncludeList.Items[e.Index] = path;
426 _dialog.needSave();
429 public void init(IceConfigurationDialog dialog, Project project)
431 _dialog = dialog;
432 _project = project;
435 private Project _project;
436 private IceConfigurationDialog _dialog;
437 private int _editingIndex = -1;
438 private bool _editingIncludes;
439 private string _editingIncludeDir = null;
440 private TextBox _txtIncludeDir;
441 private Button _btnSelectInclude;