src/Tools/jEdit/src/monitor_dockable.scala
author wenzelm
Sun, 26 Feb 2017 22:13:07 +0100
changeset 65053 460f0fd2f77a
parent 63805 c272680df665
child 65851 c103358a5559
permissions -rw-r--r--
clarified defaults;
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._
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    11
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    12
import java.awt.BorderLayout
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    13
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    14
import scala.collection.immutable.Queue
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    15
import scala.swing.{TextArea, TextField, ScrollPane, Component, ComboBox, Button}
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    16
import scala.swing.event.{SelectionChanged, ButtonClicked, ValueChanged}
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    17
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    18
import org.jfree.chart.ChartPanel
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    19
import org.jfree.data.xy.XYSeriesCollection
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    20
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    21
import org.gjt.sp.jedit.View
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    22
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
class Monitor_Dockable(view: View, position: String) extends Dockable(view, position)
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    25
{
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    26
  /* chart data -- owned by GUI thread */
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    27
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    28
  private var statistics = Queue.empty[Properties.T]
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    29
  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
    30
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    31
  private def add_statistics(stats: Properties.T)
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    32
  {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    33
    statistics = statistics.enqueue(stats)
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    34
    statistics_length += 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    35
    limit_data.text match {
63805
c272680df665 clarified modules;
wenzelm
parents: 61751
diff changeset
    36
      case Value.Int(limit) =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    37
        while (statistics_length > limit) {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    38
          statistics = statistics.dequeue._2
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    39
          statistics_length -= 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    40
        }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    41
      case _ =>
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    42
    }
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
  private def clear_statistics()
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    45
  {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    46
    statistics = Queue.empty
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    47
    statistics_length = 0
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    48
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    49
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    50
  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
    51
  private val chart = ML_Statistics.empty.chart(null, Nil)
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    52
  private val data = chart.getXYPlot.getDataset.asInstanceOf[XYSeriesCollection]
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    53
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    54
  private def update_chart: Unit =
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    55
    ML_Statistics.all_fields.find(_._1 == data_name) match {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    56
      case None =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    57
      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
    58
    }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    59
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    60
  private val input_delay =
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    61
    GUI_Thread.delay_first(PIDE.options.seconds("editor_input_delay")) { update_chart }
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    62
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    63
  private val update_delay =
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    64
    GUI_Thread.delay_first(PIDE.options.seconds("editor_chart_delay")) { update_chart }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    65
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    66
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    67
  /* controls */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    68
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
    69
  private val ml_stats = new Isabelle.ML_Stats
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
    70
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    71
  private val select_data = new ComboBox[String](ML_Statistics.all_fields.map(_._1))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    72
  {
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    73
    tooltip = "Select visualized data collection"
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    74
    listenTo(selection)
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    75
    reactions += {
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    76
      case SelectionChanged(_) =>
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    77
        data_name = selection.item
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    78
        update_chart
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    79
    }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    80
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    81
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    82
  private val reset_data = new Button("Reset") {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    83
    tooltip = "Reset accumulated data"
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    84
    reactions += {
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    85
      case ButtonClicked(_) => clear_statistics()
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    86
        update_chart
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    87
    }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    88
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    89
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    90
  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
    91
    tooltip = "Limit for accumulated data"
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    92
    verifier = (s: String) =>
63805
c272680df665 clarified modules;
wenzelm
parents: 61751
diff changeset
    93
      s match { case Value.Int(x) => x > 0 case _ => false }
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    94
    reactions += { case ValueChanged(_) => input_delay.invoke() }
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    95
  }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    96
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
    97
  private val controls =
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    98
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(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) {
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   111
      case stats: Session.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
  {
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   121
    PIDE.session.statistics += main
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
  {
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   127
    PIDE.session.statistics -= main
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
}