Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Views/CombinedFilterView.cs @ 6387

Last change on this file since 6387 was 5289, checked in by swagner, 13 years ago

Renamed VS2008ImageLibrary to VSImageLibrary (#1174)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Windows.Forms;
24using HeuristicLab.MainForm;
25using HeuristicLab.MainForm.WindowsForms;
26
27namespace HeuristicLab.Clients.OKB {
28  [View("CombinedFilter View")]
29  [Content(typeof(CombinedFilter), true)]
30  public partial class CombinedFilterView : AsynchronousContentView {
31    public new CombinedFilter Content {
32      get { return (CombinedFilter)base.Content; }
33      set { base.Content = value; }
34    }
35
36    public CombinedFilterView() {
37      InitializeComponent();
38    }
39
40    protected override void OnInitialized(System.EventArgs e) {
41      base.OnInitialized(e);
42      IEnumerable<Filter> availableFilters = OKBClient.Instance.GetFilters();
43      filtersComboBox.DataSource = availableFilters;
44      filtersComboBox.DisplayMember = "Label";
45    }
46
47    protected override void OnContentChanged() {
48      base.OnContentChanged();
49      RemoveFilters();
50      if (Content != null) {
51        foreach (Filter filter in Content.Filters)
52          AddFilter(filter);
53      }
54    }
55
56    protected override void SetEnabledStateOfControls() {
57      base.SetEnabledStateOfControls();
58      addButton.Enabled = Content != null && !ReadOnly && filtersComboBox.SelectedIndex != -1;
59      filtersComboBox.Enabled = Content != null && !ReadOnly && filtersComboBox.Items.Count > 0;
60      tableLayoutPanel.Enabled = Content != null && !ReadOnly;
61    }
62
63    private void AddFilter(Filter filter) {
64      Content.Filters.Add(filter);
65      int rowIndex = Content.Filters.Count;
66
67      tableLayoutPanel.SuspendLayout();
68      tableLayoutPanel.RowCount++;
69      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
70
71      Button removeButton = new Button();
72      removeButton.Size = new System.Drawing.Size(24, 24);
73      removeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
74      removeButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Remove;
75      removeButton.Tag = filter;
76      removeButton.Click += new System.EventHandler(removeButton_Click);
77      tableLayoutPanel.Controls.Add(removeButton, 0, rowIndex);
78
79      ContentView filterView = (ContentView)MainFormManager.CreateDefaultView(filter.GetType());
80      filterView.Content = filter;
81      filterView.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
82      tableLayoutPanel.Controls.Add(filterView, 1, rowIndex);
83
84      tableLayoutPanel.ResumeLayout();
85    }
86    private void RemoveFilter(Filter filter) {
87      int rowIndex = Content.Filters.IndexOf(filter) + 1;
88      Content.Filters.Remove(filter);
89
90      tableLayoutPanel.SuspendLayout();
91      Control removeButton = tableLayoutPanel.GetControlFromPosition(0, rowIndex);
92      Control filterView = tableLayoutPanel.GetControlFromPosition(1, rowIndex);
93      tableLayoutPanel.Controls.Remove(removeButton);
94      removeButton.Dispose();
95      tableLayoutPanel.Controls.Remove(filterView);
96      filterView.Dispose();
97
98      for (int i = rowIndex + 1; i < tableLayoutPanel.RowCount; i++) {
99        tableLayoutPanel.SetRow(tableLayoutPanel.GetControlFromPosition(0, i), i - 1);
100        tableLayoutPanel.SetRow(tableLayoutPanel.GetControlFromPosition(1, i), i - 1);
101      }
102
103      tableLayoutPanel.RowCount--;
104      tableLayoutPanel.RowStyles.RemoveAt(rowIndex);
105      tableLayoutPanel.ResumeLayout();
106    }
107    private void RemoveFilters() {
108      tableLayoutPanel.SuspendLayout();
109      for (int i = tableLayoutPanel.RowCount - 1; i > 0; i--) {
110        Control removeButton = tableLayoutPanel.GetControlFromPosition(0, i);
111        Control filterView = tableLayoutPanel.GetControlFromPosition(1, i);
112        tableLayoutPanel.Controls.Remove(removeButton);
113        removeButton.Dispose();
114        tableLayoutPanel.Controls.Remove(filterView);
115        filterView.Dispose();
116        tableLayoutPanel.RowCount--;
117        tableLayoutPanel.RowStyles.RemoveAt(i);
118      }
119      tableLayoutPanel.ResumeLayout();
120    }
121
122    private void addButton_Click(object sender, System.EventArgs e) {
123      Filter filter = filtersComboBox.SelectedItem as Filter;
124      if (filter != null) AddFilter(filter.Clone());
125    }
126    private void removeButton_Click(object sender, System.EventArgs e) {
127      Filter filter = ((Button)sender).Tag as Filter;
128      if (filter != null) RemoveFilter(filter);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.