Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/11 04:28:19 (13 years ago)
Author:
swagner
Message:

Worked on OKB (#1174)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Administration/Views/ProblemView.cs

    r5550 r5587  
    2222using System;
    2323using System.Collections.Generic;
     24using System.IO;
    2425using System.Linq;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Clients.OKB.Authentication;
     28using HeuristicLab.Common;
     29using HeuristicLab.Core.Views;
    2730using HeuristicLab.MainForm;
    2831using HeuristicLab.MainForm.WindowsForms;
     32using HeuristicLab.Optimization;
     33using HeuristicLab.Persistence.Default.Xml;
     34using HeuristicLab.PluginInfrastructure;
    2935
    3036namespace HeuristicLab.Clients.OKB.Administration {
     
    3440    private List<Platform> platformComboBoxValues;
    3541    private List<ProblemClass> problemClassComboBoxValues;
     42    private TypeSelectorDialog typeSelectorDialog;
     43    private byte[] data;
    3644
    3745    public new Problem Content {
     
    4452    }
    4553
    46     protected override void OnInitialized(System.EventArgs e) {
    47       base.OnInitialized(e);
     54    protected override void Dispose(bool disposing) {
     55      if (disposing) {
     56        if (components != null) components.Dispose();
     57        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
     58      }
     59      base.Dispose(disposing);
     60    }
     61
     62    protected override void OnContentChanged() {
     63      base.OnContentChanged();
     64
     65      platformComboBox.SelectedValueChanged -= new EventHandler(platformComboBox_SelectedValueChanged);
    4866      platformComboBoxValues = AdministrationClient.Instance.Platforms.ToList();
    4967      platformComboBox.DataSource = platformComboBoxValues;
     68      platformComboBox.SelectedValueChanged += new EventHandler(platformComboBox_SelectedValueChanged);
     69
     70      problemClassComboBox.SelectedValueChanged -= new EventHandler(problemClassComboBox_SelectedValueChanged);
    5071      problemClassComboBoxValues = AdministrationClient.Instance.ProblemClasses.ToList();
    5172      problemClassComboBox.DataSource = problemClassComboBoxValues;
    52     }
    53 
    54     protected override void OnContentChanged() {
    55       base.OnContentChanged();
     73      problemClassComboBox.SelectedValueChanged += new EventHandler(problemClassComboBox_SelectedValueChanged);
     74
     75      usersListBox.DataSource = null;
     76      data = null;
     77      dataViewHost.Content = null;
    5678      if (Content == null) {
    5779        platformComboBox.SelectedIndex = -1;
    5880        problemClassComboBox.SelectedIndex = -1;
     81        dataTypeNameTextBox.Text = string.Empty;
     82        dataTypeTypeNameTextBox.Text = string.Empty;
    5983      } else {
    6084        platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
    6185        problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
    62       }
    63       usersListBox.DataSource = null;
     86        dataTypeNameTextBox.Text = Content.DataTypeName;
     87        dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
     88      }
    6489    }
    6590
     
    6893      platformComboBox.Enabled = (Content != null) && !ReadOnly;
    6994      problemClassComboBox.Enabled = (Content != null) && !ReadOnly;
    70       refreshUsersButton.Enabled = Content != null;
     95      dataTypeGroupBox.Enabled = (Content != null) && !ReadOnly;
     96      refreshUsersButton.Enabled = (Content != null) && (Content.Id != 0);
    7197      storeUsersButton.Enabled = (usersListBox.DataSource != null) && !ReadOnly;
    7298      usersListBox.Enabled = (usersListBox.DataSource != null) && !ReadOnly;
    73     }
    74 
     99      refreshDataButton.Enabled = (Content != null) && (Content.Id != 0);
     100      storeDataButton.Enabled = ((data != null) || (dataViewHost.Content != null)) && !ReadOnly;
     101      openFileButton.Enabled = (Content != null) && (Content.Id != 0);
     102      saveFileButton.Enabled = (data != null) || (dataViewHost.Content != null);
     103      noViewAvailableLabel.Visible = dataViewHost.Content == null;
     104
     105      bool isHL33Platform = platformComboBox.Text == "HeuristicLab 3.3";
     106      dataTypeNameTextBox.ReadOnly = isHL33Platform;
     107      dataTypeTypeNameTextBox.ReadOnly = isHL33Platform;
     108      newDataButton.Enabled = isHL33Platform && (Content.Id != 0) && !ReadOnly;
     109    }
     110
     111    #region Content Events
    75112    protected override void OnContentPropertyChanged(string propertyName) {
    76113      switch (propertyName) {
     114        case "Id":
     115          SetEnabledStateOfControls();
     116          break;
    77117        case "PlatformId":
    78118          platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
     119          SetEnabledStateOfControls();
    79120          break;
    80121        case "ProblemClassId":
    81122          problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
    82123          break;
    83       }
    84     }
    85 
     124        case "DataTypeName":
     125          dataTypeNameTextBox.Text = Content.DataTypeName;
     126          break;
     127        case "DataTypeTypeName":
     128          dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
     129          break;
     130      }
     131    }
     132    #endregion
     133
     134    #region Control Events
    86135    private void platformComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
    87136      if (Content != null) {
     
    97146    }
    98147
     148    private void dataTypeNameTextBox_TextChanged(object sender, EventArgs e) {
     149      if (dataTypeNameTextBox.Text != Content.DataTypeName)
     150        Content.DataTypeName = dataTypeNameTextBox.Text;
     151    }
     152    private void dataTypeTypeNameTextBox_TextChanged(object sender, EventArgs e) {
     153      if (dataTypeTypeNameTextBox.Text != Content.DataTypeTypeName)
     154        Content.DataTypeTypeName = dataTypeTypeNameTextBox.Text;
     155    }
     156
    99157    private void refreshUsersButton_Click(object sender, System.EventArgs e) {
    100       List<Guid> ids = AdministrationClient.GetProblemUsers(Content.Id);
    101       if (ids != null) {
    102         List<User> users = AuthenticationClient.Instance.Users.ToList();
    103         usersListBox.DataSource = users;
    104         usersListBox.DisplayMember = "Name";
    105         usersListBox.SelectedItems.Clear();
    106         foreach (Guid id in ids)
    107           usersListBox.SelectedItems.Add(users.First(u => u.Id == id));
    108         usersListBox.Enabled = !ReadOnly;
     158      try {
     159        List<Guid> ids = AdministrationClient.GetProblemUsers(Content.Id);
     160        if (ids != null) {
     161          if (AuthenticationClient.Instance.Users == null) AuthenticationClient.Instance.Refresh();
     162          List<User> users = AuthenticationClient.Instance.Users.ToList();
     163          usersListBox.DataSource = users;
     164          usersListBox.DisplayMember = "Name";
     165          for (int i = 0; i < users.Count; i++)
     166            usersListBox.SetItemChecked(i, ids.Contains(users[i].Id));
     167          usersListBox.Enabled = !ReadOnly;
     168          storeUsersButton.Enabled = false;
     169        }
     170      }
     171      catch (Exception ex) {
     172        ErrorHandling.ShowErrorDialog(this, "Refresh authorized users and groups failed.", ex);
     173      }
     174    }
     175    private void storeUsersButton_Click(object sender, System.EventArgs e) {
     176      try {
     177        AdministrationClient.UpdateProblemUsers(Content.Id, usersListBox.SelectedItems.Cast<User>().Select(u => u.Id).ToList());
    109178        storeUsersButton.Enabled = false;
    110179      }
    111     }
    112     private void storeUsersButton_Click(object sender, System.EventArgs e) {
    113       AdministrationClient.UpdateProblemUsers(Content.Id, usersListBox.SelectedItems.Cast<User>().Select(u => u.Id).ToList());
    114       storeUsersButton.Enabled = false;
    115     }
    116     private void usersListBox_SelectedIndexChanged(object sender, EventArgs e) {
     180      catch (Exception ex) {
     181        ErrorHandling.ShowErrorDialog(this, "Store authorized users and groups failed.", ex);
     182      }
     183    }
     184    private void usersListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
    117185      storeUsersButton.Enabled = !ReadOnly;
    118186    }
     187
     188    private void refreshDataButton_Click(object sender, EventArgs e) {
     189      CallAsync(
     190        () => {
     191          data = null;
     192          dataViewHost.Content = null;
     193          data = AdministrationClient.GetProblemData(Content.Id);
     194          if (data != null) {
     195            using (MemoryStream stream = new MemoryStream(data)) {
     196              try {
     197                dataViewHost.Content = XmlParser.Deserialize<IContent>(stream);
     198              }
     199              catch (Exception) { }
     200              stream.Close();
     201            }
     202          }
     203        },
     204        "Refresh problem data failed.",
     205        () => SetEnabledStateOfControls()
     206      );
     207    }
     208    private void storeDataButton_Click(object sender, EventArgs e) {
     209      CallAsync(
     210        () => {
     211          if (dataViewHost.Content != null) {
     212            using (MemoryStream stream = new MemoryStream()) {
     213              XmlGenerator.Serialize(dataViewHost.Content, stream);
     214              stream.Close();
     215              data = stream.ToArray();
     216            }
     217          }
     218          AdministrationClient.UpdateProblemData(Content.Id, data);
     219        },
     220        "Store problem data failed.",
     221        null
     222      );
     223    }
     224    private void newDataButton_Click(object sender, EventArgs e) {
     225      if (typeSelectorDialog == null) {
     226        typeSelectorDialog = new TypeSelectorDialog();
     227        typeSelectorDialog.Caption = "Select Problem";
     228        typeSelectorDialog.TypeSelector.Caption = "Available Problems";
     229        typeSelectorDialog.TypeSelector.Configure(typeof(IProblem), false, true);
     230      }
     231      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
     232        try {
     233          Content.DataTypeName = typeSelectorDialog.TypeSelector.SelectedType.Name;
     234          Content.DataTypeTypeName = typeSelectorDialog.TypeSelector.SelectedType.AssemblyQualifiedName;
     235          data = null;
     236          dataViewHost.Content = (IContent)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
     237        }
     238        catch (Exception ex) {
     239          ErrorHandling.ShowErrorDialog(this, "Create new problem data failed.", ex);
     240        }
     241        SetEnabledStateOfControls();
     242      }
     243    }
     244    private void openFileButton_Click(object sender, EventArgs e) {
     245      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
     246        CallAsync(
     247          () => {
     248            IContent problem = null;
     249            try {
     250              problem = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
     251            }
     252            catch (Exception) { }
     253
     254            if (problem != null) {
     255              Content.DataTypeName = problem.GetType().Name;
     256              Content.DataTypeTypeName = problem.GetType().AssemblyQualifiedName;
     257              data = null;
     258            } else {
     259              using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
     260                data = new byte[stream.Length];
     261                stream.Read(data, 0, data.Length);
     262                stream.Close();
     263              }
     264            }
     265            dataViewHost.Content = problem;
     266          },
     267          "Save problem data into file failed.",
     268          () => SetEnabledStateOfControls()
     269        );
     270      }
     271    }
     272    private void saveFileButton_Click(object sender, EventArgs e) {
     273      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
     274        CallAsync(
     275          () => {
     276            if (dataViewHost.Content != null) {
     277              XmlGenerator.Serialize(dataViewHost.Content, saveFileDialog.FileName);
     278            } else {
     279              using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
     280                stream.Write(data, 0, data.Length);
     281                stream.Close();
     282              }
     283            }
     284          },
     285          "Save problem data into file failed.",
     286          null
     287        );
     288      }
     289    }
     290    #endregion
     291
     292    #region Helpers
     293    private void CallAsync(Action call, string errorMessage, Action continueWith) {
     294      BeginAsyncCall();
     295      call.BeginInvoke(delegate(IAsyncResult result) {
     296        Exception exception = null;
     297        try {
     298          call.EndInvoke(result);
     299        }
     300        catch (Exception ex) {
     301          exception = ex;
     302        }
     303        EndAsyncCall(errorMessage, exception, continueWith);
     304      }, null);
     305    }
     306    private void BeginAsyncCall() {
     307      if (InvokeRequired)
     308        Invoke(new Action(BeginAsyncCall));
     309      else {
     310        Cursor = Cursors.AppStarting;
     311        Enabled = false;
     312      }
     313    }
     314    private void EndAsyncCall(string errorMessage, Exception exception, Action continueWith) {
     315      if (InvokeRequired)
     316        Invoke(new Action<string, Exception, Action>(EndAsyncCall), errorMessage, exception, continueWith);
     317      else {
     318        Cursor = Cursors.Default;
     319        Enabled = true;
     320        if (exception != null) ErrorHandling.ShowErrorDialog(this, errorMessage, exception);
     321        if (continueWith != null) continueWith();
     322      }
     323    }
     324    #endregion
    119325  }
    120326}
Note: See TracChangeset for help on using the changeset viewer.