[4481] | 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 |
|
---|
| 22 | using System;
|
---|
[4558] | 23 | using System.Collections.Generic;
|
---|
[4492] | 24 | using System.ComponentModel;
|
---|
[4481] | 25 | using System.IO;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core.Views;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
| 31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 34 | using HeuristicLab.PluginInfrastructure;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Clients.OKB {
|
---|
| 37 | [View("AlgorithmData View")]
|
---|
| 38 | [Content(typeof(AlgorithmData), true)]
|
---|
| 39 | public partial class AlgorithmDataView : AsynchronousContentView {
|
---|
[4558] | 40 | private List<DataType> dataTypeComboBoxValues;
|
---|
| 41 |
|
---|
[4492] | 42 | private long algorithmId;
|
---|
| 43 | public long AlgorithmId {
|
---|
| 44 | get { return algorithmId; }
|
---|
| 45 | set {
|
---|
| 46 | algorithmId = value;
|
---|
| 47 | if (Content != null) {
|
---|
| 48 | Content.AlgorithmId = value;
|
---|
| 49 | SetEnabledStateOfControls();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[4481] | 53 |
|
---|
| 54 | public new AlgorithmData Content {
|
---|
| 55 | get { return (AlgorithmData)base.Content; }
|
---|
| 56 | set { base.Content = value; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public AlgorithmDataView() {
|
---|
| 60 | InitializeComponent();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | protected override void OnInitialized(System.EventArgs e) {
|
---|
| 64 | base.OnInitialized(e);
|
---|
[4558] | 65 | dataTypeComboBoxValues = OKBClient.Instance.DataTypes.ToList();
|
---|
| 66 | dataTypeComboBox.DataSource = dataTypeComboBoxValues;
|
---|
[4481] | 67 | dataTypeComboBox.SelectedIndex = -1;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[4492] | 70 | protected override void DeregisterContentEvents() {
|
---|
| 71 | Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
| 72 | base.DeregisterContentEvents();
|
---|
| 73 | }
|
---|
| 74 | protected override void RegisterContentEvents() {
|
---|
| 75 | base.RegisterContentEvents();
|
---|
| 76 | Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[4481] | 79 | protected override void OnContentChanged() {
|
---|
| 80 | base.OnContentChanged();
|
---|
| 81 | if (Content == null) {
|
---|
| 82 | dataTypeComboBox.SelectedIndex = -1;
|
---|
| 83 | viewHost.Content = null;
|
---|
| 84 | noViewAvailableLabel.Visible = false;
|
---|
| 85 | } else {
|
---|
[4558] | 86 | dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
|
---|
[4481] | 87 |
|
---|
| 88 | using (MemoryStream stream = new MemoryStream(Content.Data)) {
|
---|
| 89 | try {
|
---|
| 90 | viewHost.Content = XmlParser.Deserialize<IContent>(stream);
|
---|
| 91 | noViewAvailableLabel.Visible = false;
|
---|
| 92 | }
|
---|
| 93 | catch (Exception) {
|
---|
| 94 | viewHost.Content = null;
|
---|
| 95 | noViewAvailableLabel.Visible = true;
|
---|
| 96 | }
|
---|
| 97 | stream.Close();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | fileTextBox.Text = "-";
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected override void SetEnabledStateOfControls() {
|
---|
| 104 | base.SetEnabledStateOfControls();
|
---|
[4492] | 105 | newDataButton.Enabled = !ReadOnly;
|
---|
| 106 | openFileButton.Enabled = !ReadOnly;
|
---|
| 107 | saveFileButton.Enabled = (Content != null) && (((Content.Data != null) && (Content.Data.Length != 0)) || (viewHost.Content != null));
|
---|
| 108 | storeDataButton.Enabled = saveFileButton.Enabled && (Content.AlgorithmId != 0) && !ReadOnly;
|
---|
| 109 | dataTypeComboBox.Enabled = Content != null && viewHost.Content == null && !ReadOnly;
|
---|
[4481] | 110 | fileTextBox.Enabled = Content != null;
|
---|
| 111 | groupBox.Enabled = Content != null;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[4492] | 114 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 115 | switch (e.PropertyName) {
|
---|
| 116 | case "DataTypeId":
|
---|
[4558] | 117 | dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
|
---|
[4492] | 118 | break;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[4481] | 122 | private void refreshDataButton_Click(object sender, EventArgs e) {
|
---|
[4492] | 123 | BeginAsyncCall();
|
---|
| 124 | var call = new Action(delegate() {
|
---|
[4549] | 125 | AlgorithmData data = OKBClient.Instance.GetAlgorithmData(AlgorithmId);
|
---|
[4492] | 126 | Content = data;
|
---|
| 127 | });
|
---|
| 128 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 129 | call.EndInvoke(result);
|
---|
| 130 | EndAsyncCall();
|
---|
| 131 | }, null);
|
---|
[4481] | 132 | }
|
---|
| 133 | private void storeDataButton_Click(object sender, EventArgs e) {
|
---|
| 134 | if (viewHost.Content != null) {
|
---|
| 135 | try {
|
---|
| 136 | using (MemoryStream stream = new MemoryStream()) {
|
---|
| 137 | IAlgorithm algorithm = viewHost.Content as IAlgorithm;
|
---|
| 138 | algorithm.Prepare(true);
|
---|
| 139 | XmlGenerator.Serialize(algorithm, stream);
|
---|
| 140 | stream.Close();
|
---|
| 141 | Content.Data = stream.ToArray();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | catch (Exception ex) {
|
---|
| 145 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[4549] | 148 | OKBClient.Instance.UpdateAlgorithmData(Content);
|
---|
[4481] | 149 | }
|
---|
| 150 | private void newDataButton_Click(object sender, EventArgs e) {
|
---|
| 151 | using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
|
---|
| 152 | dialog.Caption = "Select Algorithm";
|
---|
| 153 | dialog.TypeSelector.Caption = "Available Algorithms";
|
---|
| 154 | dialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
|
---|
| 155 | if (dialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 156 | try {
|
---|
[4492] | 157 | if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
|
---|
[4481] | 158 | viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
[4587] | 159 | DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
|
---|
| 160 | dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
|
---|
| 161 | dataTypeComboBox.DataSource = dataTypeComboBoxValues;
|
---|
[4481] | 162 | dataTypeComboBox.SelectedItem = dataType;
|
---|
| 163 | fileTextBox.Text = "-";
|
---|
| 164 | noViewAvailableLabel.Visible = false;
|
---|
[4492] | 165 | SetEnabledStateOfControls();
|
---|
[4481] | 166 | }
|
---|
| 167 | catch (Exception ex) {
|
---|
| 168 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | private void openFileButton_Click(object sender, EventArgs e) {
|
---|
| 174 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 175 | try {
|
---|
[4492] | 176 | if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
|
---|
[4481] | 177 | IAlgorithm algorithm = null;
|
---|
| 178 | try {
|
---|
| 179 | algorithm = XmlParser.Deserialize<IAlgorithm>(openFileDialog.FileName);
|
---|
| 180 | }
|
---|
| 181 | catch (Exception) { }
|
---|
| 182 |
|
---|
| 183 | if (algorithm != null) {
|
---|
| 184 | viewHost.Content = algorithm;
|
---|
| 185 | noViewAvailableLabel.Visible = false;
|
---|
[4587] | 186 | DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
|
---|
| 187 | dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
|
---|
| 188 | dataTypeComboBox.DataSource = dataTypeComboBoxValues;
|
---|
[4481] | 189 | dataTypeComboBox.SelectedItem = dataType;
|
---|
| 190 | } else {
|
---|
| 191 | viewHost.Content = null;
|
---|
| 192 | noViewAvailableLabel.Visible = true;
|
---|
| 193 | using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
|
---|
| 194 | byte[] bytes = new byte[stream.Length];
|
---|
| 195 | stream.Read(bytes, 0, bytes.Length);
|
---|
| 196 | stream.Close();
|
---|
| 197 | Content.Data = bytes;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | fileTextBox.Text = openFileDialog.FileName;
|
---|
[4492] | 201 | SetEnabledStateOfControls();
|
---|
[4481] | 202 | }
|
---|
| 203 | catch (Exception ex) {
|
---|
| 204 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | private void saveFileButton_Click(object sender, EventArgs e) {
|
---|
| 209 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 210 | try {
|
---|
| 211 | if (viewHost.Content != null) {
|
---|
| 212 | XmlGenerator.Serialize(viewHost.Content, saveFileDialog.FileName);
|
---|
| 213 | } else {
|
---|
| 214 | using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
|
---|
| 215 | stream.Write(Content.Data, 0, Content.Data.Length);
|
---|
| 216 | stream.Close();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | fileTextBox.Text = saveFileDialog.FileName;
|
---|
| 220 | }
|
---|
| 221 | catch (Exception ex) {
|
---|
| 222 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 227 | if (Content != null) Content.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
|
---|
| 228 | }
|
---|
[4492] | 229 |
|
---|
| 230 | private void BeginAsyncCall() {
|
---|
| 231 | if (InvokeRequired)
|
---|
| 232 | Invoke(new Action(BeginAsyncCall));
|
---|
| 233 | else {
|
---|
| 234 | Cursor = Cursors.AppStarting;
|
---|
| 235 | Enabled = false;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | private void EndAsyncCall() {
|
---|
| 239 | if (InvokeRequired)
|
---|
| 240 | Invoke(new Action(EndAsyncCall));
|
---|
| 241 | else {
|
---|
| 242 | Cursor = Cursors.Default;
|
---|
| 243 | Enabled = true;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[4481] | 246 | }
|
---|
| 247 | }
|
---|