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