Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Query/Views/CombinedFilterView.cs @ 5604

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

Worked on OKB (#1174)

File size: 5.3 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;
23using System.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27
28namespace HeuristicLab.Clients.OKB {
29  [View("CombinedFilter View")]
30  [Content(typeof(CombinedFilter), true)]
31  public partial class CombinedFilterView : AsynchronousContentView {
32    public new CombinedFilter Content {
33      get { return (CombinedFilter)base.Content; }
34      set { base.Content = value; }
35    }
36
37    public CombinedFilterView() {
38      InitializeComponent();
39    }
40
41    protected override void OnInitialized(System.EventArgs e) {
42      base.OnInitialized(e);
43      IEnumerable<Filter> availableFilters = OKBClient.Instance.GetFilters();
44      filtersComboBox.DataSource = availableFilters;
45      filtersComboBox.DisplayMember = "Label";
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      RemoveFilters();
51      label.Text = string.Empty;
52      if (Content != null) {
53        string nl = Environment.NewLine;
54        label.Text = Content.Operation == BooleanOperation.And ? "A" + nl + "N" + nl + "D" : "O" + nl + "R";
55
56        foreach (Filter filter in Content.Filters)
57          AddFilter(filter);
58      }
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      addButton.Enabled = Content != null && !ReadOnly && filtersComboBox.SelectedIndex != -1;
64      filtersComboBox.Enabled = Content != null && !ReadOnly && filtersComboBox.Items.Count > 0;
65      tableLayoutPanel.Enabled = Content != null && !ReadOnly;
66    }
67
68    private void AddFilter(Filter filter) {
69      Content.Filters.Add(filter);
70      int rowIndex = Content.Filters.Count;
71
72      tableLayoutPanel.SuspendLayout();
73      tableLayoutPanel.RowCount++;
74      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
75
76      Button removeButton = new Button();
77      removeButton.Size = new System.Drawing.Size(24, 24);
78      removeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
79      removeButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Remove;
80      removeButton.Tag = filter;
81      removeButton.Click += new System.EventHandler(removeButton_Click);
82      tableLayoutPanel.Controls.Add(removeButton, 0, rowIndex);
83
84      ContentView filterView = (ContentView)MainFormManager.CreateDefaultView(filter.GetType());
85      filterView.Content = filter;
86      filterView.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
87      tableLayoutPanel.Controls.Add(filterView, 1, rowIndex);
88
89      tableLayoutPanel.ResumeLayout();
90    }
91    private void RemoveFilter(Filter filter) {
92      int rowIndex = Content.Filters.IndexOf(filter) + 1;
93      Content.Filters.Remove(filter);
94
95      tableLayoutPanel.SuspendLayout();
96      Control removeButton = tableLayoutPanel.GetControlFromPosition(0, rowIndex);
97      Control filterView = tableLayoutPanel.GetControlFromPosition(1, rowIndex);
98      tableLayoutPanel.Controls.Remove(removeButton);
99      removeButton.Dispose();
100      tableLayoutPanel.Controls.Remove(filterView);
101      filterView.Dispose();
102
103      for (int i = rowIndex + 1; i < tableLayoutPanel.RowCount; i++) {
104        tableLayoutPanel.SetRow(tableLayoutPanel.GetControlFromPosition(0, i), i - 1);
105        tableLayoutPanel.SetRow(tableLayoutPanel.GetControlFromPosition(1, i), i - 1);
106      }
107
108      tableLayoutPanel.RowCount--;
109      tableLayoutPanel.RowStyles.RemoveAt(rowIndex);
110      tableLayoutPanel.ResumeLayout();
111    }
112    private void RemoveFilters() {
113      tableLayoutPanel.SuspendLayout();
114      for (int i = tableLayoutPanel.RowCount - 1; i > 0; i--) {
115        Control removeButton = tableLayoutPanel.GetControlFromPosition(0, i);
116        Control filterView = tableLayoutPanel.GetControlFromPosition(1, i);
117        tableLayoutPanel.Controls.Remove(removeButton);
118        removeButton.Dispose();
119        tableLayoutPanel.Controls.Remove(filterView);
120        filterView.Dispose();
121        tableLayoutPanel.RowCount--;
122        tableLayoutPanel.RowStyles.RemoveAt(i);
123      }
124      tableLayoutPanel.ResumeLayout();
125    }
126
127    private void addButton_Click(object sender, System.EventArgs e) {
128      Filter filter = filtersComboBox.SelectedItem as Filter;
129      if (filter != null) AddFilter(filter.Clone());
130    }
131    private void removeButton_Click(object sender, System.EventArgs e) {
132      Filter filter = ((Button)sender).Tag as Filter;
133      if (filter != null) RemoveFilter(filter);
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.