src/Tools/jEdit/src/monitor_dockable.scala
author wenzelm
Mon, 06 Apr 2020 12:53:45 +0200
changeset 71704 b9a5eb0f3b43
parent 71652 721f143a679b
child 72135 f67e83608745
permissions -rw-r--r--
clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/monitor_dockable.scala
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     3
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
     4
Monitor for ML statistics.
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     5
*/
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     6
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     8
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     9
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    10
import isabelle._
66591
6efa351190d0 more robust: provide docking framework via base plugin;
wenzelm
parents: 66206
diff changeset
    11
import isabelle.jedit_base.Dockable
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    12
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    13
import java.awt.BorderLayout
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    14
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    15
import scala.collection.immutable.Queue
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    16
import scala.swing.{TextArea, TextField, ScrollPane, Component, ComboBox, Button}
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    17
import scala.swing.event.{SelectionChanged, ButtonClicked, ValueChanged}
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    18
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    19
import org.jfree.chart.ChartPanel
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    20
import org.jfree.data.xy.XYSeriesCollection
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    21
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    22
import org.gjt.sp.jedit.View
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    23
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    24
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    25
class Monitor_Dockable(view: View, position: String) extends Dockable(view, position)
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    26
{
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    27
  /* chart data -- owned by GUI thread */
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    28
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    29
  private var statistics = Queue.empty[Properties.T]
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    30
  private var statistics_length = 0
56770
e160ae47db94 mane delayed events outside of Swing thread -- triggers no longer require Swing_Thread.later;
wenzelm
parents: 56715
diff changeset
    31
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    32
  private def add_statistics(stats: Properties.T)
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    33
  {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    34
    statistics = statistics.enqueue(stats)
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    35
    statistics_length += 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    36
    limit_data.text match {
63805
c272680df665 clarified modules;
wenzelm
parents: 61751
diff changeset
    37
      case Value.Int(limit) =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    38
        while (statistics_length > limit) {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    39
          statistics = statistics.dequeue._2
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    40
          statistics_length -= 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    41
        }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    42
      case _ =>
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    43
    }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    44
  }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    45
  private def clear_statistics()
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    46
  {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    47
    statistics = Queue.empty
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    48
    statistics_length = 0
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    49
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    50
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    51
  private var data_name = ML_Statistics.all_fields(0)._1
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    52
  private val chart = ML_Statistics.empty.chart(null, Nil)
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    53
  private val data = chart.getXYPlot.getDataset.asInstanceOf[XYSeriesCollection]
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    54
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    55
  private def update_chart: Unit =
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    56
    ML_Statistics.all_fields.find(_._1 == data_name) match {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    57
      case None =>
65851
c103358a5559 tuned signature;
wenzelm
parents: 65053
diff changeset
    58
      case Some((_, fields)) => ML_Statistics(statistics.toList).update_data(data, fields)
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    59
    }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    60
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    61
  private val input_delay =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 71652
diff changeset
    62
    Delay.first(PIDE.options.seconds("editor_input_delay"), gui = true) { update_chart }
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    63
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    64
  private val update_delay =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 71652
diff changeset
    65
    Delay.first(PIDE.options.seconds("editor_chart_delay"), gui = true) { update_chart }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    66
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    67
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    68
  /* controls */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    69
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
    70
  private val ml_stats = new Isabelle.ML_Stats
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
    71
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    72
  private val select_data = new ComboBox[String](ML_Statistics.all_fields.map(_._1))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    73
  {
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    74
    tooltip = "Select visualized data collection"
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    75
    listenTo(selection)
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    76
    reactions += {
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    77
      case SelectionChanged(_) =>
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    78
        data_name = selection.item
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    79
        update_chart
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    80
    }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    81
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    82
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    83
  private val reset_data = new Button("Reset") {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    84
    tooltip = "Reset accumulated data"
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    85
    reactions += {
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    86
      case ButtonClicked(_) => clear_statistics()
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    87
        update_chart
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    88
    }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    89
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    90
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    91
  private val limit_data = new TextField("200", 5) {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    92
    tooltip = "Limit for accumulated data"
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    93
    verifier = (s: String) =>
63805
c272680df665 clarified modules;
wenzelm
parents: 61751
diff changeset
    94
      s match { case Value.Int(x) => x > 0 case _ => false }
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    95
    reactions += { case ValueChanged(_) => input_delay.invoke() }
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    96
  }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    97
66206
2d2082db735a clarified defaults;
wenzelm
parents: 66205
diff changeset
    98
  private val controls = Wrap_Panel(List(ml_stats, select_data, reset_data, limit_data))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    99
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   100
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   101
  /* layout */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   102
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
   103
  set_content(new ChartPanel(chart))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   104
  add(controls.peer, BorderLayout.NORTH)
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   105
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   106
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   107
  /* main */
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   108
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   109
  private val main =
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   110
    Session.Consumer[Any](getClass.getName) {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   111
      case stats: Session.Runtime_Statistics =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
   112
        add_statistics(stats.props)
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
   113
        update_delay.invoke()
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   114
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   115
      case _: Session.Global_Options =>
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   116
        GUI_Thread.later { ml_stats.load() }
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   117
    }
53177
dcac8d837b9c more uniform treatment of Swing_Thread context switch: prefer asynchronous Swing_Thread.later from actor;
wenzelm
parents: 50982
diff changeset
   118
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   119
  override def init()
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   120
  {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   121
    PIDE.session.runtime_statistics += main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   122
    PIDE.session.global_options += main
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   123
  }
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   124
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   125
  override def exit()
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   126
  {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   127
    PIDE.session.runtime_statistics -= main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   128
    PIDE.session.global_options -= main
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   129
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   130
}