[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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;
|
---|
[8107] | 23 | using System.Collections.Generic;
|
---|
[6976] | 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[8107] | 26 | using System.Windows.Forms;
|
---|
| 27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
[6976] | 28 | using HeuristicLab.Core.Views;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
[7410] | 30 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
[6976] | 31 |
|
---|
| 32 | namespace HeuristicLab.Clients.Hive.Views {
|
---|
| 33 | [View("StateLogGanttChartList View")]
|
---|
| 34 | [Content(typeof(StateLogListList), true)]
|
---|
| 35 | public sealed partial class StateLogGanttChartListView : ItemView {
|
---|
[8107] | 36 | private IList<LegendItem> invisibleLegendItems;
|
---|
| 37 |
|
---|
[6976] | 38 | public new StateLogListList Content {
|
---|
| 39 | get { return (StateLogListList)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public StateLogGanttChartListView() {
|
---|
| 44 | InitializeComponent();
|
---|
[8107] | 45 | invisibleLegendItems = new List<LegendItem>();
|
---|
[8656] | 46 | ganttChart.chart.MouseMove += new System.Windows.Forms.MouseEventHandler(chart_MouseMove);
|
---|
| 47 | ganttChart.chart.MouseDown += new System.Windows.Forms.MouseEventHandler(chart_MouseDown);
|
---|
| 48 | ganttChart.chart.CustomizeLegend += new EventHandler<CustomizeLegendEventArgs>(chart_CustomizeLegend);
|
---|
[6976] | 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void DeregisterContentEvents() {
|
---|
| 52 | base.DeregisterContentEvents();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void RegisterContentEvents() {
|
---|
| 56 | base.RegisterContentEvents();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | protected override void OnContentChanged() {
|
---|
| 60 | base.OnContentChanged();
|
---|
[8656] | 61 |
|
---|
| 62 | ganttChart.Reset();
|
---|
| 63 | if (Content != null) {
|
---|
[6976] | 64 | SetupCategories(ganttChart);
|
---|
| 65 | if (Content.Count > 0) {
|
---|
| 66 | DateTime maxValue = Content.Max(x => x.Count > 0 ? x.Max(y => y.DateTime) : DateTime.MinValue);
|
---|
[7191] | 67 | DateTime minValue = Content.Min(x => x.Count > 0 ? x.Min(y => y.DateTime) : DateTime.MinValue);
|
---|
[6976] | 68 | DateTime upperLimit;
|
---|
| 69 | if (Content.All(x => x.Count > 0 ? (x.Last().State == TaskState.Finished || x.Last().State == TaskState.Failed || x.Last().State == TaskState.Aborted) : true)) {
|
---|
| 70 | upperLimit = DateTime.FromOADate(Math.Min(DateTime.Now.AddSeconds(10).ToOADate(), maxValue.AddSeconds(10).ToOADate()));
|
---|
| 71 | } else {
|
---|
| 72 | upperLimit = DateTime.Now;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[7191] | 75 | if ((upperLimit - minValue) > TimeSpan.FromDays(1)) {
|
---|
| 76 | this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
|
---|
| 77 | } else {
|
---|
| 78 | this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[7728] | 81 | for (int i = Content.Count - 1; i >= 0; i--) {
|
---|
[6976] | 82 | for (int j = 0; j < Content[i].Count - 1; j++) {
|
---|
[8107] | 83 | if (Content[i][j].State != TaskState.Offline && invisibleLegendItems.All(x => x.Name != Content[i][j].State.ToString()))
|
---|
[6976] | 84 | AddData(ganttChart, i.ToString(), Content[i][j], Content[i][j + 1], upperLimit);
|
---|
| 85 | }
|
---|
[8107] | 86 | if (Content[i].Count > 0 && invisibleLegendItems.All(x => x.Name != Content[i][Content[i].Count - 1].State.ToString())) {
|
---|
[6976] | 87 | AddData(ganttChart, i.ToString(), Content[i][Content[i].Count - 1], null, upperLimit);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public static void SetupCategories(GanttChart ganttChart) {
|
---|
| 95 | ganttChart.AddCategory(TaskState.Offline.ToString(), Color.Gainsboro);
|
---|
| 96 | ganttChart.AddCategory(TaskState.Waiting.ToString(), Color.NavajoWhite);
|
---|
| 97 | ganttChart.AddCategory(TaskState.Paused.ToString(), Color.PaleVioletRed);
|
---|
| 98 | ganttChart.AddCategory(TaskState.Transferring.ToString(), Color.CornflowerBlue);
|
---|
| 99 | ganttChart.AddCategory(TaskState.Calculating.ToString(), Color.DarkGreen);
|
---|
| 100 | ganttChart.AddCategory(TaskState.Finished.ToString(), Color.White);
|
---|
| 101 | ganttChart.AddCategory(TaskState.Aborted.ToString(), Color.Orange);
|
---|
| 102 | ganttChart.AddCategory(TaskState.Failed.ToString(), Color.Red);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public static void AddData(GanttChart ganttChart, string name, StateLog from, StateLog to, DateTime upperLimit) {
|
---|
| 106 | DateTime until = to != null ? to.DateTime : upperLimit;
|
---|
| 107 | TimeSpan duration = until - from.DateTime;
|
---|
[7728] | 108 | string tooltip = string.Format("Task: {0} " + Environment.NewLine + "Task Id: {1}" + Environment.NewLine + "State: {2} " + Environment.NewLine + "Duration: {3} " + Environment.NewLine + "{4} - {5}" + Environment.NewLine, from.TaskName, from.TaskId, from.State, duration, from.DateTime, until);
|
---|
| 109 |
|
---|
| 110 | if (to != null && to.SlaveId != null)
|
---|
| 111 | tooltip += "Slave: " + to.SlaveId;
|
---|
| 112 |
|
---|
[6976] | 113 | if (!string.IsNullOrEmpty(from.Exception))
|
---|
| 114 | tooltip += Environment.NewLine + from.Exception;
|
---|
| 115 | ganttChart.AddData(name, from.State.ToString(), from.DateTime, until, tooltip, false);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | protected override void SetEnabledStateOfControls() {
|
---|
| 119 | base.SetEnabledStateOfControls();
|
---|
| 120 | }
|
---|
[8107] | 121 |
|
---|
| 122 | #region Events
|
---|
| 123 | void chart_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
| 124 | HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
|
---|
| 125 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 126 | Cursor = Cursors.Hand;
|
---|
| 127 | else
|
---|
| 128 | Cursor = Cursors.Default;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | private void chart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
| 132 | HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
|
---|
| 133 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 134 | ToggleLegendItemVisibility(result.Object as LegendItem);
|
---|
| 135 | ganttChart.Reset();
|
---|
| 136 | OnContentChanged();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 140 | foreach (var item in e.LegendItems)
|
---|
| 141 | foreach (var cell in item.Cells)
|
---|
| 142 | cell.ForeColor = invisibleLegendItems.Any(x => x.Name == item.Name) ? Color.Gray : Color.Black;
|
---|
| 143 | }
|
---|
| 144 | #endregion
|
---|
| 145 |
|
---|
| 146 | #region Helpers
|
---|
| 147 | private void ToggleLegendItemVisibility(LegendItem legendItem) {
|
---|
| 148 | var item = invisibleLegendItems.FirstOrDefault(x => x.Name == legendItem.Name);
|
---|
| 149 | if (item != null) invisibleLegendItems.Remove(item);
|
---|
| 150 | else invisibleLegendItems.Add(legendItem);
|
---|
| 151 | }
|
---|
| 152 | #endregion
|
---|
[6976] | 153 | }
|
---|
| 154 | }
|
---|