Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/UserDefinedAlgorithmView.cs @ 3376

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 5.4 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("UserDefinedAlgorithm View")]
35  [Content(typeof(UserDefinedAlgorithm), true)]
36  public sealed partial class UserDefinedAlgorithmView : EngineAlgorithmView {
37    public new UserDefinedAlgorithm Content {
38      get { return (UserDefinedAlgorithm)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ItemBaseView"/>.
44    /// </summary>
45    public UserDefinedAlgorithmView() {
46      InitializeComponent();
47    }
48    /// <summary>
49    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
50    /// </summary>
51    /// <param name="item">The item that should be displayed.</param>
52    public UserDefinedAlgorithmView(UserDefinedAlgorithm content)
53      : this() {
54      Content = content;
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      if (Content == null)
60        globalScopeView.Content = null;
61      else
62        globalScopeView.Content = Content.GlobalScope;
63      SetEnableStateOfControls();
64    }
65
66    protected override void OnReadOnlyChanged() {
67      base.OnReadOnlyChanged();
68      SetEnableStateOfControls();
69    }
70    private void SetEnableStateOfControls() {
71      globalScopeView.ReadOnly = ReadOnly;
72      operatorGraphViewHost.ReadOnly = ReadOnly;
73      if (Content == null)
74        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
75      else
76        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = Content.ExecutionState != ExecutionState.Started;
77    }
78
79    private void newOperatorGraphButton_Click(object sender, EventArgs e) {
80      Content.OperatorGraph = new OperatorGraph();
81    }
82    private void openOperatorGraphButton_Click(object sender, EventArgs e) {
83      openFileDialog.Title = "Open Operator Graph";
84      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
85        this.Cursor = Cursors.AppStarting;
86        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
87        operatorGraphViewHost.Enabled = false;
88
89        var call = new Func<string, object>(XmlParser.Deserialize);
90        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
91          OperatorGraph operatorGraph = null;
92          try {
93            operatorGraph = call.EndInvoke(a) as OperatorGraph;
94          }
95          catch (Exception ex) {
96            Auxiliary.ShowErrorMessageBox(ex);
97          }
98          Invoke(new Action(delegate() {
99            if (operatorGraph == null)
100              MessageBox.Show(this, "The selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
101            else
102              Content.OperatorGraph = operatorGraph;
103            operatorGraphViewHost.Enabled = true;
104            newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
105            this.Cursor = Cursors.Default;
106          }));
107        }, null);
108      }
109    }
110    private void saveOperatorGraphButton_Click(object sender, EventArgs e) {
111      saveFileDialog.Title = "Save Operator Graph";
112      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
113        this.Cursor = Cursors.AppStarting;
114        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
115        operatorGraphViewHost.Enabled = false;
116
117        var call = new Action<OperatorGraph, string, int>(XmlGenerator.Serialize);
118        int compression = 9;
119        if (saveFileDialog.FilterIndex == 1) compression = 0;
120        call.BeginInvoke(Content.OperatorGraph, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
121          try {
122            call.EndInvoke(a);
123          }
124          catch (Exception ex) {
125            Auxiliary.ShowErrorMessageBox(ex);
126          }
127          Invoke(new Action(delegate() {
128            operatorGraphViewHost.Enabled = true;
129            newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
130            this.Cursor = Cursors.Default;
131          }));
132        }, null);
133      }
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.