Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Views/AlgorithmView.cs @ 4466

Last change on this file since 4466 was 4466, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 6.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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using System.IO;
29
30namespace HeuristicLab.Clients.OKB {
31  [View("Algorithm View")]
32  [Content(typeof(Algorithm), true)]
33  public partial class AlgorithmView : NamedOKBItemView {
34    public new Algorithm Content {
35      get { return (Algorithm)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public AlgorithmView() {
40      InitializeComponent();
41    }
42
43    protected override void OnInitialized(System.EventArgs e) {
44      base.OnInitialized(e);
45      platformComboBox.DataSource = Administrator.Instance.Platforms.ToList();
46      algorithmClassComboBox.DataSource = Administrator.Instance.AlgorithmClasses.ToList();
47      dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.ToList();
48    }
49
50    protected override void DeregisterContentEvents() {
51      // ...
52      base.DeregisterContentEvents();
53    }
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      // ...
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
62        platformComboBox.SelectedIndex = -1;
63        algorithmClassComboBox.SelectedIndex = -1;
64      } else {
65        platformComboBox.SelectedItem = Administrator.Instance.Platforms.FirstOrDefault(p => p.Id == Content.PlatformId);
66        algorithmClassComboBox.SelectedItem = Administrator.Instance.AlgorithmClasses.FirstOrDefault(a => a.Id == Content.AlgorithmClassId);
67      }
68      usersListBox.DataSource = null;
69      fileTextBox.Text = "";
70      dataTypeComboBox.SelectedIndex = -1;
71    }
72
73    protected override void SetEnabledStateOfControls() {
74      base.SetEnabledStateOfControls();
75      platformComboBox.Enabled = Content != null;
76      algorithmClassComboBox.Enabled = Content != null;
77      refreshUsersButton.Enabled = Content != null;
78      storeUsersButton.Enabled = usersListBox.DataSource != null;
79      usersListBox.Enabled = usersListBox.DataSource != null;
80      storeDataButton.Enabled = !string.IsNullOrEmpty(fileTextBox.Text) && dataTypeComboBox.SelectedIndex != -1;
81    }
82
83    protected override void OnContentPropertyChanged(string propertyName) {
84      switch (propertyName) {
85        case "PlatformId":
86          platformComboBox.SelectedItem = Administrator.Instance.Platforms.FirstOrDefault(p => p.Id == Content.PlatformId);
87          break;
88        case "AlgorithmClassId":
89          algorithmClassComboBox.SelectedItem = Administrator.Instance.AlgorithmClasses.FirstOrDefault(a => a.Id == Content.AlgorithmClassId);
90          break;
91      }
92    }
93
94    private void platformComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
95      if (Content != null) {
96        Platform selected = platformComboBox.SelectedItem as Platform;
97        if (selected != null) Content.PlatformId = selected.Id;
98      }
99    }
100    private void algorithmClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
101      if (Content != null) {
102        AlgorithmClass selected = algorithmClassComboBox.SelectedItem as AlgorithmClass;
103        if (selected != null) Content.AlgorithmClassId = selected.Id;
104      }
105    }
106
107    private void refreshUsersButton_Click(object sender, System.EventArgs e) {
108      Guid[] ids = Administrator.Instance.GetAlgorithmUsers(Content.Id);
109      if (ids != null) {
110        List<User> users = Administrator.Instance.Users.ToList();
111        usersListBox.DataSource = users;
112        usersListBox.DisplayMember = "Name";
113        usersListBox.SelectedItems.Clear();
114        foreach (Guid id in ids)
115          usersListBox.SelectedItems.Add(users.First(u => u.Id == id));
116        usersListBox.Enabled = true;
117        storeUsersButton.Enabled = false;
118      }
119    }
120    private void storeUsersButton_Click(object sender, System.EventArgs e) {
121      if (Administrator.Instance.StoreAlgorithmUsers(Content.Id, usersListBox.SelectedItems.Cast<User>().Select(u => u.Id).ToArray()))
122        storeUsersButton.Enabled = false;
123    }
124    private void usersListBox_SelectedIndexChanged(object sender, EventArgs e) {
125      storeUsersButton.Enabled = true;
126    }
127
128    private void storeDataButton_Click(object sender, EventArgs e) {
129      AlgorithmData data = new AlgorithmData();
130      data.AlgorithmId = Content.Id;
131      data.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
132
133      using (FileStream stream = new FileStream(fileTextBox.Text, FileMode.Open, FileAccess.Read)) {
134        byte[] bytes = new byte[stream.Length];
135        stream.Read(bytes, 0, bytes.Length);
136        stream.Close();
137        data.Data = bytes;
138      }
139
140      if (Administrator.Instance.StoreAlgorithmData(data))
141        storeDataButton.Enabled = false;
142    }
143    private void openFileButton_Click(object sender, EventArgs e) {
144      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
145        fileTextBox.Text = openFileDialog.FileName;
146        storeDataButton.Enabled = true;
147      }
148    }
149    private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
150      storeDataButton.Enabled = !string.IsNullOrEmpty(fileTextBox.Text);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.