#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; using System.IO; namespace HeuristicLab.Clients.OKB { [View("Algorithm View")] [Content(typeof(Algorithm), true)] public partial class AlgorithmView : NamedOKBItemView { public new Algorithm Content { get { return (Algorithm)base.Content; } set { base.Content = value; } } public AlgorithmView() { InitializeComponent(); } protected override void OnInitialized(System.EventArgs e) { base.OnInitialized(e); platformComboBox.DataSource = Administrator.Instance.Platforms.ToList(); algorithmClassComboBox.DataSource = Administrator.Instance.AlgorithmClasses.ToList(); dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.ToList(); } protected override void DeregisterContentEvents() { // ... base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); // ... } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { platformComboBox.SelectedIndex = -1; algorithmClassComboBox.SelectedIndex = -1; } else { platformComboBox.SelectedItem = Administrator.Instance.Platforms.FirstOrDefault(p => p.Id == Content.PlatformId); algorithmClassComboBox.SelectedItem = Administrator.Instance.AlgorithmClasses.FirstOrDefault(a => a.Id == Content.AlgorithmClassId); } usersListBox.DataSource = null; fileTextBox.Text = ""; dataTypeComboBox.SelectedIndex = -1; } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); platformComboBox.Enabled = Content != null; algorithmClassComboBox.Enabled = Content != null; refreshUsersButton.Enabled = Content != null; storeUsersButton.Enabled = usersListBox.DataSource != null; usersListBox.Enabled = usersListBox.DataSource != null; storeDataButton.Enabled = !string.IsNullOrEmpty(fileTextBox.Text) && dataTypeComboBox.SelectedIndex != -1; } protected override void OnContentPropertyChanged(string propertyName) { switch (propertyName) { case "PlatformId": platformComboBox.SelectedItem = Administrator.Instance.Platforms.FirstOrDefault(p => p.Id == Content.PlatformId); break; case "AlgorithmClassId": algorithmClassComboBox.SelectedItem = Administrator.Instance.AlgorithmClasses.FirstOrDefault(a => a.Id == Content.AlgorithmClassId); break; } } private void platformComboBox_SelectedValueChanged(object sender, System.EventArgs e) { if (Content != null) { Platform selected = platformComboBox.SelectedItem as Platform; if (selected != null) Content.PlatformId = selected.Id; } } private void algorithmClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) { if (Content != null) { AlgorithmClass selected = algorithmClassComboBox.SelectedItem as AlgorithmClass; if (selected != null) Content.AlgorithmClassId = selected.Id; } } private void refreshUsersButton_Click(object sender, System.EventArgs e) { Guid[] ids = Administrator.Instance.GetAlgorithmUsers(Content.Id); if (ids != null) { List users = Administrator.Instance.Users.ToList(); usersListBox.DataSource = users; usersListBox.DisplayMember = "Name"; usersListBox.SelectedItems.Clear(); foreach (Guid id in ids) usersListBox.SelectedItems.Add(users.First(u => u.Id == id)); usersListBox.Enabled = true; storeUsersButton.Enabled = false; } } private void storeUsersButton_Click(object sender, System.EventArgs e) { if (Administrator.Instance.StoreAlgorithmUsers(Content.Id, usersListBox.SelectedItems.Cast().Select(u => u.Id).ToArray())) storeUsersButton.Enabled = false; } private void usersListBox_SelectedIndexChanged(object sender, EventArgs e) { storeUsersButton.Enabled = true; } private void storeDataButton_Click(object sender, EventArgs e) { AlgorithmData data = new AlgorithmData(); data.AlgorithmId = Content.Id; data.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id; using (FileStream stream = new FileStream(fileTextBox.Text, FileMode.Open, FileAccess.Read)) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Close(); data.Data = bytes; } if (Administrator.Instance.StoreAlgorithmData(data)) storeDataButton.Enabled = false; } private void openFileButton_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog(this) == DialogResult.OK) { fileTextBox.Text = openFileDialog.FileName; storeDataButton.Enabled = true; } } private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { storeDataButton.Enabled = !string.IsNullOrEmpty(fileTextBox.Text); } } }