Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.AdvancedOptimizationFrontend/3.3/MainForm.cs @ 3043

Last change on this file since 3043 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

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