Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3361 was 3361, checked in by mkommend, 14 years ago

added operator graph tab page to EngineAlgorithmView (ticket #973)

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