[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Data;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.Text;
|
---|
[2591] | 28 | using System.Linq;
|
---|
[2] | 29 | using System.Threading;
|
---|
| 30 | using System.Windows.Forms;
|
---|
| 31 | using WeifenLuo.WinFormsUI.Docking;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 | using HeuristicLab.Core;
|
---|
[659] | 34 | using System.IO;
|
---|
[2] | 35 |
|
---|
| 36 | namespace HeuristicLab.AdvancedOptimizationFrontend {
|
---|
[1185] | 37 | /// <summary>
|
---|
| 38 | /// The main form of the application.
|
---|
| 39 | /// </summary>
|
---|
[2] | 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 |
|
---|
[1185] | 59 | /// <summary>
|
---|
| 60 | /// Initializes a new instance of <see cref="MainForm"/>.
|
---|
| 61 | /// </summary>
|
---|
[2] | 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
|
---|
[2591] | 72 | var creatables = from x in ApplicationManager.Manager.GetTypes(typeof(IEditable))
|
---|
| 73 | orderby x.Name
|
---|
| 74 | select x;
|
---|
[2] | 75 | foreach (Type type in creatables) {
|
---|
| 76 | if (!type.IsAbstract) {
|
---|
| 77 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
| 78 | item.Tag = type;
|
---|
| 79 | item.Text = "&" + type.Name + "...";
|
---|
| 80 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
| 81 | newToolStripMenuItem.DropDownItems.Add(item);
|
---|
| 82 |
|
---|
| 83 | item = new ToolStripMenuItem();
|
---|
| 84 | item.Tag = type;
|
---|
| 85 | item.Text = "&" + type.Name + "...";
|
---|
| 86 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
| 87 | newToolStripDropDownButton.DropDownItems.Add(item);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | #region IControlManager Members
|
---|
[1185] | 93 | /// <summary>
|
---|
| 94 | /// Displays the given <paramref name="control"/>.
|
---|
| 95 | /// </summary>
|
---|
| 96 | /// <exception cref="InvalidOperationException">Thrown when the given <paramref name="control"/>
|
---|
| 97 | /// is neither a view nor an editor.</exception>
|
---|
| 98 | /// <param name="control">The control to display.</param>
|
---|
[2591] | 99 | public void ShowControl(object control) {
|
---|
| 100 | if (InvokeRequired) Invoke((Action<object>)ShowControl, control);
|
---|
[2105] | 101 | else {
|
---|
| 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.");
|
---|
[2] | 109 |
|
---|
[2105] | 110 | content.TabText = content.Text;
|
---|
| 111 | content.Show(dockPanel);
|
---|
| 112 | }
|
---|
[2] | 113 | }
|
---|
| 114 | #endregion
|
---|
| 115 |
|
---|
| 116 | private void EnableDisableItems() {
|
---|
| 117 | closeToolStripMenuItem.Enabled = false;
|
---|
| 118 | closeAllToolStripMenuItem.Enabled = false;
|
---|
| 119 | saveToolStripMenuItem.Enabled = false;
|
---|
| 120 | saveToolStripButton.Enabled = false;
|
---|
| 121 | saveAsToolStripMenuItem.Enabled = false;
|
---|
| 122 | saveAllToolStripMenuItem.Enabled = false;
|
---|
| 123 | saveAllToolStripButton.Enabled = false;
|
---|
| 124 |
|
---|
| 125 | if (ActiveMdiChild != null) {
|
---|
| 126 | closeToolStripMenuItem.Enabled = true;
|
---|
| 127 | closeAllToolStripMenuItem.Enabled = true;
|
---|
| 128 | saveAllToolStripMenuItem.Enabled = true;
|
---|
| 129 | saveAllToolStripButton.Enabled = true;
|
---|
| 130 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
[2591] | 131 | if (form != null) {
|
---|
[2] | 132 | if (((Control)form.Editor).Enabled) {
|
---|
| 133 | saveToolStripMenuItem.Enabled = true;
|
---|
| 134 | saveToolStripButton.Enabled = true;
|
---|
| 135 | saveAsToolStripMenuItem.Enabled = true;
|
---|
| 136 | } else {
|
---|
| 137 | closeToolStripMenuItem.Enabled = false;
|
---|
| 138 | closeAllToolStripMenuItem.Enabled = false;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | #region Open and Save Methods
|
---|
| 145 | private void Open() {
|
---|
| 146 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 147 | lock (locker) runningTasks++;
|
---|
| 148 | Cursor = Cursors.AppStarting;
|
---|
| 149 | Task task = new Task(openFileDialog.FileName, null, null);
|
---|
| 150 | ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | private void AsynchronousLoad(object state) {
|
---|
| 154 | Task task = (Task)state;
|
---|
[659] | 155 | try {
|
---|
| 156 | task.storable = PersistenceManager.Load(task.filename);
|
---|
[2591] | 157 | }
|
---|
| 158 | catch (FileNotFoundException fileNotFoundEx) {
|
---|
[676] | 159 | 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] | 160 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
[2591] | 161 | }
|
---|
| 162 | catch (TypeLoadException typeLoadEx) {
|
---|
| 163 | 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.",
|
---|
[676] | 164 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
[659] | 165 | }
|
---|
[2591] | 166 | finally {
|
---|
| 167 | LoadFinished(task);
|
---|
| 168 | }
|
---|
[2] | 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;
|
---|
[2143] | 185 | editor.SaveFinished += new EventHandler(SaveFinished);
|
---|
[2591] | 186 | ControlManager.Manager.ShowControl(editor);
|
---|
[2] | 187 | }
|
---|
| 188 | lock (locker) {
|
---|
| 189 | runningTasks--;
|
---|
| 190 | if (runningTasks == 0)
|
---|
| 191 | Cursor = Cursors.Default;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | private void Save(EditorForm form) {
|
---|
| 196 | if (form.Editor.Filename == null)
|
---|
| 197 | SaveAs(form);
|
---|
| 198 | else {
|
---|
| 199 | lock (locker) runningTasks++;
|
---|
| 200 | Cursor = Cursors.AppStarting;
|
---|
| 201 | EnableDisableItems();
|
---|
[2143] | 202 | form.Editor.Save();
|
---|
[2] | 203 | }
|
---|
| 204 | }
|
---|
| 205 | private void SaveAs(EditorForm form) {
|
---|
| 206 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 207 | form.Editor.Filename = saveFileDialog.FileName;
|
---|
| 208 | Save(form);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
[2143] | 211 | private void SaveFinished(object sender, EventArgs e) {
|
---|
[2] | 212 | if (InvokeRequired)
|
---|
[2143] | 213 | Invoke(new EventHandler(SaveFinished), sender, e);
|
---|
[2] | 214 | else {
|
---|
| 215 | EnableDisableItems();
|
---|
| 216 | lock (locker) {
|
---|
| 217 | runningTasks--;
|
---|
| 218 | if (runningTasks == 0)
|
---|
| 219 | Cursor = Cursors.Default;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | #endregion
|
---|
| 224 |
|
---|
| 225 | private void MainForm_MdiChildActivate(object sender, EventArgs e) {
|
---|
| 226 | EnableDisableItems();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | #region Menu Events
|
---|
| 230 | private void newToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 231 | ToolStripItem item = (ToolStripItem)sender;
|
---|
| 232 | Type type = (Type)item.Tag;
|
---|
| 233 | IEditable editable = (IEditable)Activator.CreateInstance(type);
|
---|
| 234 | if (editable == null) {
|
---|
| 235 | MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 236 | } else {
|
---|
| 237 | IEditor editor = editable.CreateEditor();
|
---|
| 238 | if (editor == null) {
|
---|
| 239 | MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 240 | } else {
|
---|
[2143] | 241 | editor.SaveFinished += new EventHandler(SaveFinished);
|
---|
[2591] | 242 | ControlManager.Manager.ShowControl(editor);
|
---|
[2] | 243 | EnableDisableItems();
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | private void openToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 248 | Open();
|
---|
| 249 | }
|
---|
| 250 | private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 251 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 252 | Save(form);
|
---|
| 253 | }
|
---|
| 254 | private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 255 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 256 | SaveAs(form);
|
---|
| 257 | }
|
---|
| 258 | private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 259 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
| 260 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
| 261 | if (((Control)form.Editor).Enabled) Save(form);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 265 | ActiveMdiChild.Close();
|
---|
| 266 | EnableDisableItems();
|
---|
| 267 | }
|
---|
| 268 | private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 269 | while (MdiChildren.Length > 0)
|
---|
| 270 | MdiChildren[0].Close();
|
---|
| 271 | EnableDisableItems();
|
---|
| 272 | }
|
---|
| 273 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 274 | Application.Exit();
|
---|
| 275 | }
|
---|
| 276 | private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 277 | AvailableOperatorsForm form = new AvailableOperatorsForm();
|
---|
| 278 | form.Show(dockPanel);
|
---|
| 279 | }
|
---|
| 280 | private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 281 | GC.Collect();
|
---|
| 282 | }
|
---|
| 283 | private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 284 | AboutDialog dialog = new AboutDialog();
|
---|
| 285 | dialog.ShowDialog(this);
|
---|
| 286 | dialog.Dispose();
|
---|
| 287 | }
|
---|
| 288 | #endregion
|
---|
| 289 |
|
---|
| 290 | #region ToolStrip Events
|
---|
| 291 | private void openToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 292 | Open();
|
---|
| 293 | }
|
---|
| 294 | private void saveToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 295 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 296 | Save(form);
|
---|
| 297 | }
|
---|
| 298 | private void saveAllToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 299 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
| 300 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
[2591] | 301 | if (form != null && ((Control)form.Editor).Enabled) Save(form);
|
---|
[2] | 302 | }
|
---|
| 303 | }
|
---|
| 304 | #endregion
|
---|
| 305 | }
|
---|
| 306 | }
|
---|