Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs @ 2709

Last change on this file since 2709 was 2701, checked in by gkronber, 15 years ago

Repaired FunctionLibraryEditor. #748 (FunctionLibraryView is empty)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.GP.Interfaces;
28
29namespace HeuristicLab.GP {
30  public partial class FunctionLibraryEditor : EditorBase {
31    private ChooseItemDialog chooseFunctionDialog;
32    public FunctionLibrary FunctionLibrary {
33      get { return (FunctionLibrary)Item; }
34      set { base.Item = value; }
35    }
36
37    public FunctionLibraryEditor(FunctionLibrary library)
38      : base() {
39      InitializeComponent();
40      FunctionLibrary = library;
41    }
42
43    protected override void AddItemEvents() {
44      base.AddItemEvents();
45      base.Item.Changed += (sender, args) => UpdateControls();
46    }
47
48    protected override void UpdateControls() {
49      base.UpdateControls();
50      functionsListView.Clear();
51      functionsComboBox.Items.Clear();
52      foreach (IFunction fun in FunctionLibrary.Functions) {
53        functionsListView.Items.Add(CreateListViewItem(fun));
54        functionsComboBox.Items.Add(fun);
55        if (fun.Manipulator != null) {
56          mutationListView.Items.Add(CreateListViewItem(fun));
57        }
58        if (fun.Initializer != null) {
59          initListView.Items.Add(CreateListViewItem(fun));
60        }
61      }
62    }
63
64    private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
65      if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
66        IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
67        mutationVariableView.Enabled = true;
68        mutationVariableView.Variable = new Variable("Manipulator", manipulator);
69      } else {
70        mutationVariableView.Enabled = false;
71      }
72    }
73
74    private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
75      if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
76        IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
77        initVariableView.Enabled = true;
78        initVariableView.Variable = new Variable("Initializer", initializer);
79      } else {
80        initVariableView.Enabled = false;
81      }
82    }
83
84    private void addButton_Click(object sender, EventArgs e) {
85      if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
86      if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
87        FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
88      }
89    }
90
91    private void removeButton_Click(object sender, EventArgs e) {
92      // delete from the end of the list
93      List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList();
94      foreach (int selectedIndex in removeIndices) {
95        FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);       
96      }
97    }
98
99    private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
100      if (functionsListView.SelectedIndices.Count > 0) {
101        removeButton.Enabled = true;
102      } else {
103        removeButton.Enabled = false;
104      }
105    }
106
107    private ListViewItem CreateListViewItem(IFunction function) {
108      ListViewItem item = new ListViewItem();
109      item.Name = function.Name;
110      item.Text = function.Name;
111      item.Tag = function;
112      return item;
113    }
114
115    private void functionsListView_ItemDrag(object sender, ItemDragEventArgs e) {
116      ListViewItem item = (ListViewItem)e.Item;
117      IFunction fun = (IFunction)item.Tag;
118      DataObject data = new DataObject();
119      data.SetData("IFunction", fun);
120      data.SetData("DragSource", functionsListView);
121      DoDragDrop(data, DragDropEffects.Link);
122    }
123
124    private void functionsComboBox_SelectedIndexChanged(object sender, EventArgs e) {
125      if (functionsComboBox.SelectedItem != null) {
126        IFunction selectedFun = (IFunction)functionsComboBox.SelectedItem;
127        Control funView = (Control)selectedFun.CreateView();
128        funView.Dock = DockStyle.Fill;
129        functionDetailsPanel.Controls.Clear();
130        functionDetailsPanel.Controls.Add(funView);
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.