Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/3.2/MainForm.cs @ 2273

Last change on this file since 2273 was 2143, checked in by swagner, 15 years ago

Refactoring of the saving mechanism for editors (#685)

File size: 11.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Threading;
29using System.Windows.Forms;
30using WeifenLuo.WinFormsUI.Docking;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Core;
[659]33using System.IO;
[2]34
35namespace HeuristicLab.AdvancedOptimizationFrontend {
[1185]36  /// <summary>
37  /// The main form of the application.
38  /// </summary>
[2]39  public partial class MainForm : Form, IControlManager {
40    #region Inner Types
41    private class Task {
42      public string filename;
43      public IStorable storable;
44      public IEditor editor;
45
46      private Task() { }
47      public Task(string filename, IStorable storable, IEditor editor) {
48        this.filename = filename;
49        this.storable = storable;
50        this.editor = editor;
51      }
52    }
53    #endregion
54
55    private object locker;
56    private int runningTasks;
57
[1185]58    /// <summary>
59    /// Initializes a new instance of <see cref="MainForm"/>.
60    /// </summary>
[2]61    public MainForm() {
62      InitializeComponent();
63
64      locker = new object();
65      runningTasks = 0;
66
67      AvailableOperatorsForm form = new AvailableOperatorsForm();
68      form.Show(dockPanel);
69
70      DiscoveryService discoveryService = new DiscoveryService();
71
72      // discover creatable items
73      Type[] creatables = discoveryService.GetTypes(typeof(IEditable));
74      string[] names = new string[creatables.Length];
75      for (int i = 0; i < creatables.Length; i++)
76        names[i] = creatables[i].Name;
77      Array.Sort(names, creatables);
78      foreach (Type type in creatables) {
79        if (!type.IsAbstract) {
80          ToolStripMenuItem item = new ToolStripMenuItem();
81          item.Tag = type;
82          item.Text = "&" + type.Name + "...";
83          item.Click += new EventHandler(newToolStripMenuItem_Click);
84          newToolStripMenuItem.DropDownItems.Add(item);
85
86          item = new ToolStripMenuItem();
87          item.Tag = type;
88          item.Text = "&" + type.Name + "...";
89          item.Click += new EventHandler(newToolStripMenuItem_Click);
90          newToolStripDropDownButton.DropDownItems.Add(item);
91        }
92      }
93    }
94
95    #region IControlManager Members
[1185]96    /// <summary>
97    /// Displays the given <paramref name="control"/>.
98    /// </summary>
99    /// <exception cref="InvalidOperationException">Thrown when the given <paramref name="control"/>
100    /// is neither a view nor an editor.</exception>
101    /// <param name="control">The control to display.</param>
[2]102    public void ShowControl(IControl control) {
[2105]103      if (InvokeRequired) Invoke((Action<IControl>)ShowControl,control);
104      else {
105        DockContent content;
106        if (control is IEditor)
107          content = new EditorForm((IEditor)control);
108        else if (control is IView)
109          content = new ViewForm((IView)control);
110        else
111          throw new InvalidOperationException("Control is neither a view nor an editor.");
[2]112
[2105]113        content.TabText = content.Text;
114        content.Show(dockPanel);
115      }
[2]116    }
117    #endregion
118
119    private void EnableDisableItems() {
120      closeToolStripMenuItem.Enabled = false;
121      closeAllToolStripMenuItem.Enabled = false;
122      saveToolStripMenuItem.Enabled = false;
123      saveToolStripButton.Enabled = false;
124      saveAsToolStripMenuItem.Enabled = false;
125      saveAllToolStripMenuItem.Enabled = false;
126      saveAllToolStripButton.Enabled = false;
127
128      if (ActiveMdiChild != null) {
129        closeToolStripMenuItem.Enabled = true;
130        closeAllToolStripMenuItem.Enabled = true;
131        saveAllToolStripMenuItem.Enabled = true;
132        saveAllToolStripButton.Enabled = true;
133        EditorForm form = ActiveMdiChild as EditorForm;
134        if (form != null){
135          if (((Control)form.Editor).Enabled) {
136            saveToolStripMenuItem.Enabled = true;
137            saveToolStripButton.Enabled = true;
138            saveAsToolStripMenuItem.Enabled = true;
139          } else {
140            closeToolStripMenuItem.Enabled = false;
141            closeAllToolStripMenuItem.Enabled = false;
142          }
143        }
144      }
145    }
146
147    #region Open and Save Methods
148    private void Open() {
149      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
150        lock (locker) runningTasks++;
151        Cursor = Cursors.AppStarting;
152        Task task = new Task(openFileDialog.FileName, null, null);
153        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
154      }
155    }
156    private void AsynchronousLoad(object state) {
157      Task task = (Task)state;
[659]158      try {
159        task.storable = PersistenceManager.Load(task.filename);
[676]160      } catch(FileNotFoundException fileNotFoundEx) {
161        MessageBox.Show("Sorry couldn't open file \"" + task.filename + "\".\nThe file or plugin \"" + fileNotFoundEx.FileName + "\" is not available.\nPlease make sure you have all necessary plugins installed.",
[659]162          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
[676]163      } catch(TypeLoadException typeLoadEx) {
164        MessageBox.Show("Sorry couldn't open file \"" + task.filename + "\".\nThe type \"" + typeLoadEx.TypeName+ "\" is not available.\nPlease make sure that you have the correct version the plugin installed.",
165          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
[659]166      }
[2]167      LoadFinished(task);
168    }
169    private delegate void TaskFinishedDelegate(Task task);
170    private void LoadFinished(Task task) {
171      if (InvokeRequired)
172        Invoke(new TaskFinishedDelegate(LoadFinished), task);
173      else {
174        IEditor editor = null;
175        if (task.storable != null) {
176          IEditable editable = task.storable as IEditable;
177          if (editable != null)
178            editor = editable.CreateEditor();
179        }
180        if (editor == null)
181          MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
182        else {
183          editor.Filename = task.filename;
[2143]184          editor.SaveFinished += new EventHandler(SaveFinished);
[2]185          PluginManager.ControlManager.ShowControl(editor);
186        }
187        lock (locker) {
188          runningTasks--;
189          if (runningTasks == 0)
190            Cursor = Cursors.Default;
191        }
192      }
193    }
194    private void Save(EditorForm form) {
195      if (form.Editor.Filename == null)
196        SaveAs(form);
197      else {
198        lock (locker) runningTasks++;
199        Cursor = Cursors.AppStarting;
200        EnableDisableItems();
[2143]201        form.Editor.Save();
[2]202      }
203    }
204    private void SaveAs(EditorForm form) {
205      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
206        form.Editor.Filename = saveFileDialog.FileName;
207        Save(form);
208      }
209    }
[2143]210    private void SaveFinished(object sender, EventArgs e) {
[2]211      if (InvokeRequired)
[2143]212        Invoke(new EventHandler(SaveFinished), sender, e);
[2]213      else {
214        EnableDisableItems();
215        lock (locker) {
216          runningTasks--;
217          if (runningTasks == 0)
218            Cursor = Cursors.Default;
219        }
220      }
221    }
222    #endregion
223
224    private void MainForm_MdiChildActivate(object sender, EventArgs e) {
225      EnableDisableItems();
226    }
227
228    #region Menu Events
229    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
230      ToolStripItem item = (ToolStripItem)sender;
231      Type type = (Type)item.Tag;
232      IEditable editable = (IEditable)Activator.CreateInstance(type);
233      if (editable == null) {
234        MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
235      } else {
236        IEditor editor = editable.CreateEditor();
237        if (editor == null) {
238          MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
239        } else {
[2143]240          editor.SaveFinished += new EventHandler(SaveFinished);
[2]241          PluginManager.ControlManager.ShowControl(editor);
242          EnableDisableItems();
243        }
244      }
245    }
246    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
247      Open();
248    }
249    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
250      EditorForm form = ActiveMdiChild as EditorForm;
251      Save(form);
252    }
253    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
254      EditorForm form = ActiveMdiChild as EditorForm;
255      SaveAs(form);
256    }
257    private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
258      for (int i = 0; i < MdiChildren.Length; i++) {
259        EditorForm form = MdiChildren[i] as EditorForm;
260        if (((Control)form.Editor).Enabled) Save(form);
261      }
262    }
263    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
264      ActiveMdiChild.Close();
265      EnableDisableItems();
266    }
267    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
268      while (MdiChildren.Length > 0)
269        MdiChildren[0].Close();
270      EnableDisableItems();
271    }
272    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
273      Application.Exit();
274    }
275    private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
276      AvailableOperatorsForm form = new AvailableOperatorsForm();
277      form.Show(dockPanel);
278    }
279    private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
280      GC.Collect();
281    }
282    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
283      AboutDialog dialog = new AboutDialog();
284      dialog.ShowDialog(this);
285      dialog.Dispose();
286    }
287    #endregion
288
289    #region ToolStrip Events
290    private void openToolStripButton_Click(object sender, EventArgs e) {
291      Open();
292    }
293    private void saveToolStripButton_Click(object sender, EventArgs e) {
294      EditorForm form = ActiveMdiChild as EditorForm;
295      Save(form);
296    }
297    private void saveAllToolStripButton_Click(object sender, EventArgs e) {
298      for (int i = 0; i < MdiChildren.Length; i++) {
299        EditorForm form = MdiChildren[i] as EditorForm;
[639]300        if (form!=null && ((Control)form.Editor).Enabled) Save(form);
[2]301      }
302    }
303    #endregion
304  }
305}
Note: See TracBrowser for help on using the repository browser.