Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2547


Ignore:
Timestamp:
12/08/09 02:35:40 (15 years ago)
Author:
swagner
Message:

Continued work on Optimizer (#770)

Location:
trunk/sources
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/IItemView.cs

    r2546 r2547  
    4040    /// </summary>
    4141    event EventHandler ItemChanged;
     42
     43    event EventHandler Closed;
    4244  }
    4345}
  • trunk/sources/HeuristicLab.Core.Views/3.3/ItemViewBase.cs

    r2546 r2547  
    5656      }
    5757    }
    58     private string myCaption;
    59     /// <summary>
    60     /// Gets or sets the caption of the current instance.
    61     /// </summary>
    62     /// <remarks>Call <see cref="OnCaptionChanged"/> in the setter if a new item is set.</remarks>
    63     public string Caption {
    64       get { return myCaption; }
    65       set {
    66         if (value != myCaption) {
    67           myCaption = value;
    68           OnCaptionChanged();
    69         }
    70       }
    71     }
    7258
    7359    /// <summary>
     
    9783    public override void Refresh() {
    9884      if (InvokeRequired) {
    99         Invoke(new MethodInvoker(Refresh));
     85        Invoke(new Action(Refresh));
    10086      } else {
    10187        UpdateControls();
     
    11096        Caption = "View";
    11197      else
    112         Caption = "View (" + Item.GetType().Name + ")";
     98        Caption = Item.Name;
    11399     
    114100    }
     
    125111        ItemChanged(this, new EventArgs());
    126112    }
    127     /// <summary>
    128     /// Occurs when the current caption was changed.
    129     /// </summary>
    130     public event EventHandler CaptionChanged;
    131     /// <summary>
    132     /// Fires a new <c>CaptionChanged</c> event.
    133     /// </summary>
    134     protected virtual void OnCaptionChanged() {
    135       if (CaptionChanged != null)
    136         CaptionChanged(this, new EventArgs());
     113
     114    public event EventHandler Closed;
     115    public override void OnClosed(object sender, EventArgs e) {
     116      base.OnClosed(sender, e);
     117      if (Closed != null)
     118        Closed(this, e);
    137119    }
    138120
  • trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs

    r2546 r2547  
    33using System.Linq;
    44using System.Text;
     5using System.Threading;
     6using System.IO;
    57using System.Windows.Forms;
    68using HeuristicLab.MainForm;
     
    1214  internal static class FileManager {
    1315
     16    #region Private Class FileInfo
    1417    private class FileInfo {
    1518      public string Filename { get; set; }
     
    2730      }
    2831    }
    29 
    30     private static Dictionary<IItem, FileInfo> files = new Dictionary<IItem, FileInfo>();
    31     private static NewItemDialog newItemDialog = null;
    32     private static OpenFileDialog openFileDialog = null;
    33     private static SaveFileDialog saveFileDialog = null;
     32    #endregion
     33
     34    private static Dictionary<IItemView, FileInfo> files;
     35    private static NewItemDialog newItemDialog;
     36    private static OpenFileDialog openFileDialog;
     37    private static SaveFileDialog saveFileDialog;
     38    private static int waitingCursors;
     39    private static int newDocumentsCounter;
     40
     41    static FileManager() {
     42      files = new Dictionary<IItemView, FileInfo>();
     43      newItemDialog = null;
     44      openFileDialog = null;
     45      saveFileDialog = null;
     46      waitingCursors = 0;
     47      newDocumentsCounter = 1;
     48    }
    3449
    3550    public static void New() {
    3651      if (newItemDialog == null) newItemDialog = new NewItemDialog();
    3752      if (newItemDialog.ShowDialog() == DialogResult.OK) {
    38         MainFormManager.MainForm.ShowView(MainFormManager.CreateDefaultView(newItemDialog.Item));
    39         files.Add(newItemDialog.Item, new FileInfo());
     53        IItemView view = MainFormManager.CreateDefaultView(newItemDialog.Item) as IItemView;
     54        if (view != null) {
     55          view.Closed += new EventHandler(ViewClosed);
     56          view.Caption = "Item" + newDocumentsCounter.ToString() + ".hl";
     57          newDocumentsCounter++;
     58          MainFormManager.MainForm.ShowView(view);
     59        }
    4060      }
    4161    }
     
    5272
    5373      if (openFileDialog.ShowDialog() == DialogResult.OK) {
    54         foreach (string file in openFileDialog.FileNames) {
    55           IItem item = (IItem)XmlParser.Deserialize(file);
    56           MainFormManager.MainForm.ShowView(MainFormManager.CreateDefaultView(item));
    57           files.Add(item, new FileInfo(file));
    58         }
     74        foreach (string filename in openFileDialog.FileNames)
     75          LoadItemAsync(filename);
    5976      }
    6077    }
     
    6380      IItemView activeView = MainFormManager.MainForm.ActiveView as IItemView;
    6481      if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Item.GetType()))) {
    65         Save(activeView.Item);
    66       }
    67     }
    68     private static void Save(IItem item) {
    69       if (files[item].Filename != string.Empty) {
    70         if (files[item].Compressed)
    71           XmlGenerator.Serialize(item, saveFileDialog.FileName, 9);
     82        Save(activeView);
     83      }
     84    }
     85    private static void Save(IItemView view) {
     86      if (!files.ContainsKey(view)) {
     87        SaveAs(view);
     88      } else {
     89        if (files[view].Compressed)
     90          SaveItemAsync(view, files[view].Filename, 9);
    7291        else
    73           XmlGenerator.Serialize(item, saveFileDialog.FileName, 0);
    74       } else {
    75         SaveAs(item);
     92          SaveItemAsync(view, files[view].Filename, 0);
    7693      }
    7794    }
     
    8097      IItemView activeView = MainFormManager.MainForm.ActiveView as IItemView;
    8198      if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Item.GetType()))) {
    82         SaveAs(activeView.Item);
    83       }
    84     }
    85     public static void SaveAs(IItem item) {
     99        SaveAs(activeView);
     100      }
     101    }
     102    public static void SaveAs(IItemView view) {
    86103      if (saveFileDialog == null) {
    87104        saveFileDialog = new SaveFileDialog();
     
    92109      }
    93110
    94       saveFileDialog.FileName = files[item].Filename;
    95       if (saveFileDialog.FileName == string.Empty) saveFileDialog.FileName = "Item";
    96       if (! files[item].Compressed)
     111      if (!files.ContainsKey(view)) {
     112        files.Add(view, new FileInfo());
     113        saveFileDialog.FileName = view.Caption;
     114      } else {
     115        saveFileDialog.FileName = files[view].Filename;
     116      }
     117      if (! files[view].Compressed)
    97118        saveFileDialog.FilterIndex = 1;
    98119      else
     
    101122      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    102123        if (saveFileDialog.FilterIndex == 1) {
    103           XmlGenerator.Serialize(item, saveFileDialog.FileName, 0);
     124          SaveItemAsync(view, saveFileDialog.FileName, 0);
    104125        } else {
    105           XmlGenerator.Serialize(item, saveFileDialog.FileName, 9);
     126          SaveItemAsync(view, saveFileDialog.FileName, 9);
    106127        }
    107         files[item].Filename = saveFileDialog.FileName;
    108         files[item].Compressed = saveFileDialog.FilterIndex != 1;
    109128      }
    110129    }
     
    117136
    118137      foreach (IItemView view in views) {
    119         Save(view.Item);
    120       }
    121     }
     138        Save(view);
     139      }
     140    }
     141
     142    private static void ViewClosed(object sender, EventArgs e) {
     143      IItemView view = (IItemView)sender;
     144      view.Closed -= new EventHandler(ViewClosed);
     145      files.Remove(view);
     146    }
     147
     148    #region Asynchronous Save/Load Operations
     149    private static void Invoke(Action a) {
     150      Form form = MainFormManager.MainForm as Form;
     151      if (form.InvokeRequired)
     152        form.Invoke(a);
     153      else
     154        a.Invoke();
     155    }
     156
     157    private static void SaveItemAsync(IItemView view, string filename, int compression) {
     158      ThreadPool.QueueUserWorkItem(
     159        new WaitCallback(
     160          delegate(object arg) {
     161            try {
     162              SetWaitingCursor();
     163              XmlGenerator.Serialize(view.Item, filename, compression);
     164              Invoke(delegate() {
     165                view.Caption = Path.GetFileName(filename);
     166                files[view].Filename = filename;
     167                files[view].Compressed = compression > 0;
     168              });
     169            }
     170            catch (Exception ex) {
     171              Auxiliary.ShowErrorMessageBox(ex);
     172            } finally {
     173              ResetWaitingCursor();
     174            }
     175          }
     176        )
     177      );
     178    }
     179    private static void LoadItemAsync(string filename) {
     180      ThreadPool.QueueUserWorkItem(
     181        new WaitCallback(
     182          delegate(object arg) {
     183            try {
     184              SetWaitingCursor();
     185              IItem item = (IItem)XmlParser.Deserialize(filename);
     186              Invoke(delegate() {
     187                IItemView view = MainFormManager.CreateDefaultView(item) as IItemView;
     188                if (view != null) {
     189                  view.Closed += new EventHandler(ViewClosed);
     190                  view.Caption = Path.GetFileName(filename);
     191                  files.Add(view, new FileInfo(filename));
     192                  MainFormManager.MainForm.ShowView(view);
     193                }
     194              });
     195            } catch (Exception ex) {
     196              Auxiliary.ShowErrorMessageBox(ex);
     197            } finally {
     198              ResetWaitingCursor();
     199            }
     200          }
     201        )
     202      );
     203    }
     204
     205    private static void SetWaitingCursor() {
     206      Invoke(delegate() {
     207        waitingCursors++;
     208        ((Form)MainFormManager.MainForm).Cursor = Cursors.AppStarting;
     209      });
     210    }
     211    private static void ResetWaitingCursor() {
     212      Invoke(delegate() {
     213        waitingCursors--;
     214        if (waitingCursors == 0) ((Form)MainFormManager.MainForm).Cursor = Cursors.Default;
     215      });
     216    }
     217    #endregion
    122218  }
    123219}
  • trunk/sources/HeuristicLab.Optimizer/3.3/HeuristicLab.Optimizer-3.3.csproj

    r2546 r2547  
    7070      <DependentUpon>NewItemDialog.cs</DependentUpon>
    7171    </Compile>
     72    <Compile Include="OptimizerMainForm.cs">
     73      <SubType>Form</SubType>
     74    </Compile>
     75    <Compile Include="OptimizerMainForm.Designer.cs">
     76      <DependentUpon>OptimizerMainForm.cs</DependentUpon>
     77    </Compile>
    7278    <Compile Include="Properties\AssemblyInfo.cs" />
    7379    <Compile Include="Properties\Resources.Designer.cs">
     
    119125      <DependentUpon>NewItemDialog.cs</DependentUpon>
    120126    </EmbeddedResource>
     127    <EmbeddedResource Include="OptimizerMainForm.resx">
     128      <DependentUpon>OptimizerMainForm.cs</DependentUpon>
     129    </EmbeddedResource>
    121130    <EmbeddedResource Include="Properties\Resources.resx">
    122131      <Generator>ResXFileCodeGenerator</Generator>
  • trunk/sources/HeuristicLab.Optimizer/3.3/HeuristicLabOptimizerApplication.cs

    r2546 r2547  
    3333  internal class HeuristicLabOptimizerApplication : ApplicationBase {
    3434    public override void Run() {
    35       DockingMainForm mainForm = new DockingMainForm(typeof(IOptimizerUserInterfaceItemProvider));
    36       mainForm.Title = "HeuristicLab Optimizer " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
    37       mainForm.Icon = Resources.HeuristicLabIcon;
    38       mainForm.Width = 800;
    39       mainForm.Height = 600;
     35      OptimizerMainForm mainForm = new OptimizerMainForm(typeof(IOptimizerUserInterfaceItemProvider));
    4036      Application.Run(mainForm);
    4137    }
Note: See TracChangeset for help on using the changeset viewer.