src/Tools/jEdit/src/timing_dockable.scala
author wenzelm
Thu Nov 03 17:51:10 2016 +0100 (2016-11-03)
changeset 64442 85adb337e32f
parent 63805 c272680df665
child 64443 857acb970dfa
permissions -rw-r--r--
updated to scala-2.12.0;
wenzelm@51533
     1
/*  Title:      Tools/jEdit/src/timing_dockable.scala
wenzelm@51533
     2
    Author:     Makarius
wenzelm@51533
     3
wenzelm@51533
     4
Dockable window for timing information.
wenzelm@51533
     5
*/
wenzelm@51533
     6
wenzelm@51533
     7
package isabelle.jedit
wenzelm@51533
     8
wenzelm@51533
     9
wenzelm@51533
    10
import isabelle._
wenzelm@51533
    11
wenzelm@53711
    12
import scala.swing.{Label, ListView, Alignment, ScrollPane, Component, TextField}
wenzelm@51538
    13
import scala.swing.event.{MouseClicked, ValueChanged}
wenzelm@51533
    14
wenzelm@51536
    15
import java.awt.{BorderLayout, Graphics2D, Insets, Color}
wenzelm@51533
    16
import javax.swing.{JList, BorderFactory}
wenzelm@51533
    17
import javax.swing.border.{BevelBorder, SoftBevelBorder}
wenzelm@51533
    18
wenzelm@51533
    19
import org.gjt.sp.jedit.{View, jEdit}
wenzelm@51533
    20
wenzelm@51533
    21
wenzelm@51533
    22
class Timing_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@51533
    23
{
wenzelm@51533
    24
  /* entry */
wenzelm@51533
    25
wenzelm@51533
    26
  private object Entry
wenzelm@51533
    27
  {
wenzelm@51533
    28
    object Ordering extends scala.math.Ordering[Entry]
wenzelm@51533
    29
    {
wenzelm@51533
    30
      def compare(entry1: Entry, entry2: Entry): Int =
wenzelm@51533
    31
        entry2.timing compare entry1.timing
wenzelm@51533
    32
    }
wenzelm@51533
    33
wenzelm@51533
    34
    object Renderer_Component extends Label
wenzelm@51533
    35
    {
wenzelm@51533
    36
      opaque = false
wenzelm@51533
    37
      xAlignment = Alignment.Leading
wenzelm@51533
    38
      border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@51536
    39
wenzelm@51536
    40
      var entry: Entry = null
wenzelm@51536
    41
      override def paintComponent(gfx: Graphics2D)
wenzelm@51536
    42
      {
wenzelm@51536
    43
        def paint_rectangle(color: Color)
wenzelm@51536
    44
        {
wenzelm@51536
    45
          val size = peer.getSize()
wenzelm@51536
    46
          val insets = border.getBorderInsets(peer)
wenzelm@51536
    47
          val x = insets.left
wenzelm@51536
    48
          val y = insets.top
wenzelm@51536
    49
          val w = size.width - x - insets.right
wenzelm@51536
    50
          val h = size.height - y - insets.bottom
wenzelm@51536
    51
          gfx.setColor(color)
wenzelm@51536
    52
          gfx.fillRect(x, y, w, h)
wenzelm@51536
    53
        }
wenzelm@51536
    54
wenzelm@51536
    55
        entry match {
wenzelm@51536
    56
          case theory_entry: Theory_Entry if theory_entry.current =>
wenzelm@51536
    57
            paint_rectangle(view.getTextArea.getPainter.getSelectionColor)
wenzelm@51536
    58
          case _: Command_Entry =>
wenzelm@51536
    59
            paint_rectangle(view.getTextArea.getPainter.getMultipleSelectionColor)
wenzelm@51536
    60
          case _ =>
wenzelm@51536
    61
        }
wenzelm@51536
    62
        super.paintComponent(gfx)
wenzelm@51536
    63
      }
wenzelm@51533
    64
    }
wenzelm@51533
    65
wenzelm@51533
    66
    class Renderer extends ListView.Renderer[Entry]
wenzelm@51533
    67
    {
wenzelm@64442
    68
      def componentFor(list: ListView[_ <: Timing_Dockable.this.Entry],
wenzelm@64442
    69
        isSelected: Boolean, focused: Boolean, entry: Entry, index: Int): Component =
wenzelm@51533
    70
      {
wenzelm@51533
    71
        val component = Renderer_Component
wenzelm@51536
    72
        component.entry = entry
wenzelm@51534
    73
        component.text = entry.print
wenzelm@51533
    74
        component
wenzelm@51533
    75
      }
wenzelm@51533
    76
    }
wenzelm@51533
    77
  }
wenzelm@51533
    78
wenzelm@51534
    79
  private abstract class Entry
wenzelm@51534
    80
  {
wenzelm@51534
    81
    def timing: Double
wenzelm@51534
    82
    def print: String
wenzelm@51534
    83
    def follow(snapshot: Document.Snapshot)
wenzelm@51534
    84
  }
wenzelm@51534
    85
wenzelm@51536
    86
  private case class Theory_Entry(name: Document.Node.Name, timing: Double, current: Boolean)
wenzelm@51536
    87
    extends Entry
wenzelm@51533
    88
  {
wenzelm@59735
    89
    def print: String =
wenzelm@59735
    90
      Time.print_seconds(timing) + "s theory " + quote(name.theory)
wenzelm@60893
    91
    def follow(snapshot: Document.Snapshot) { PIDE.editor.goto_file(true, view, name.node) }
wenzelm@51534
    92
  }
wenzelm@51534
    93
wenzelm@51534
    94
  private case class Command_Entry(command: Command, timing: Double) extends Entry
wenzelm@51534
    95
  {
wenzelm@59735
    96
    def print: String =
wenzelm@59735
    97
      "  " + Time.print_seconds(timing) + "s command " + quote(command.span.name)
wenzelm@51533
    98
    def follow(snapshot: Document.Snapshot)
wenzelm@60893
    99
    { PIDE.editor.hyperlink_command(true, snapshot, command).foreach(_.follow(view)) }
wenzelm@51533
   100
  }
wenzelm@51533
   101
wenzelm@51533
   102
wenzelm@51533
   103
  /* timing view */
wenzelm@51533
   104
wenzelm@51533
   105
  private val timing_view = new ListView(Nil: List[Entry]) {
wenzelm@51533
   106
    listenTo(mouse.clicks)
wenzelm@51533
   107
    reactions += {
wenzelm@51533
   108
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@51533
   109
        val index = peer.locationToIndex(point)
wenzelm@51533
   110
        if (index >= 0) listData(index).follow(PIDE.session.snapshot())
wenzelm@51533
   111
    }
wenzelm@51533
   112
  }
wenzelm@51533
   113
  timing_view.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@51533
   114
  timing_view.peer.setVisibleRowCount(0)
wenzelm@51533
   115
  timing_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@51533
   116
  timing_view.renderer = new Entry.Renderer
wenzelm@51533
   117
wenzelm@51533
   118
  set_content(new ScrollPane(timing_view))
wenzelm@51533
   119
wenzelm@51533
   120
wenzelm@51533
   121
  /* timing threshold */
wenzelm@51533
   122
wenzelm@51533
   123
  private var timing_threshold = PIDE.options.real("jedit_timing_threshold")
wenzelm@51533
   124
wenzelm@51549
   125
  private val threshold_tooltip = "Threshold for timing display (seconds)"
wenzelm@51549
   126
wenzelm@51549
   127
  private val threshold_label = new Label("Threshold: ") {
wenzelm@51549
   128
    tooltip = threshold_tooltip
wenzelm@51549
   129
  }
wenzelm@51533
   130
wenzelm@51533
   131
  private val threshold_value = new TextField(Time.print_seconds(timing_threshold)) {
wenzelm@51533
   132
    reactions += {
wenzelm@51538
   133
      case _: ValueChanged =>
wenzelm@51533
   134
        text match {
wenzelm@63805
   135
          case Value.Double(x) if x >= 0.0 => timing_threshold = x
wenzelm@51533
   136
          case _ =>
wenzelm@51533
   137
        }
wenzelm@51533
   138
        handle_update()
wenzelm@51533
   139
    }
wenzelm@51549
   140
    tooltip = threshold_tooltip
wenzelm@51538
   141
    verifier = ((s: String) =>
wenzelm@63805
   142
      s match { case Value.Double(x) => x >= 0.0 case _ => false })
wenzelm@51533
   143
  }
wenzelm@51533
   144
wenzelm@53711
   145
  private val controls =
wenzelm@53711
   146
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(threshold_label, threshold_value)
wenzelm@51533
   147
  add(controls.peer, BorderLayout.NORTH)
wenzelm@51533
   148
wenzelm@51533
   149
wenzelm@57612
   150
  /* component state -- owned by GUI thread */
wenzelm@51533
   151
wenzelm@51533
   152
  private var nodes_timing = Map.empty[Document.Node.Name, Protocol.Node_Timing]
wenzelm@51534
   153
wenzelm@51534
   154
  private def make_entries(): List[Entry] =
wenzelm@51534
   155
  {
wenzelm@57612
   156
    GUI_Thread.require {}
wenzelm@51534
   157
wenzelm@51534
   158
    val name =
wenzelm@51534
   159
      Document_View(view.getTextArea) match {
wenzelm@51534
   160
        case None => Document.Node.Name.empty
wenzelm@52973
   161
        case Some(doc_view) => doc_view.model.node_name
wenzelm@51534
   162
      }
wenzelm@52888
   163
    val timing = nodes_timing.getOrElse(name, Protocol.empty_node_timing)
wenzelm@51534
   164
wenzelm@51534
   165
    val theories =
wenzelm@59319
   166
      (for ((node_name, node_timing) <- nodes_timing.toList if node_timing.commands.nonEmpty)
wenzelm@51536
   167
        yield Theory_Entry(node_name, node_timing.total, false)).sorted(Entry.Ordering)
wenzelm@51534
   168
    val commands =
wenzelm@51534
   169
      (for ((command, command_timing) <- timing.commands.toList)
wenzelm@51534
   170
        yield Command_Entry(command, command_timing)).sorted(Entry.Ordering)
wenzelm@51534
   171
wenzelm@51536
   172
    theories.flatMap(entry =>
wenzelm@51536
   173
      if (entry.name == name) entry.copy(current = true) :: commands
wenzelm@51536
   174
      else List(entry))
wenzelm@51534
   175
  }
wenzelm@51533
   176
wenzelm@51533
   177
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@51533
   178
  {
wenzelm@57612
   179
    GUI_Thread.require {}
wenzelm@53177
   180
wenzelm@53177
   181
    val snapshot = PIDE.session.snapshot()
wenzelm@51533
   182
wenzelm@53177
   183
    val iterator =
wenzelm@53177
   184
      restriction match {
wenzelm@53177
   185
        case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@56372
   186
        case None => snapshot.version.nodes.iterator
wenzelm@53177
   187
      }
wenzelm@53177
   188
    val nodes_timing1 =
wenzelm@53177
   189
      (nodes_timing /: iterator)({ case (timing1, (name, node)) =>
wenzelm@56208
   190
          if (PIDE.resources.loaded_theories(name.theory)) timing1
wenzelm@53177
   191
          else {
wenzelm@53177
   192
            val node_timing =
wenzelm@53177
   193
              Protocol.node_timing(snapshot.state, snapshot.version, node, timing_threshold)
wenzelm@53177
   194
            timing1 + (name -> node_timing)
wenzelm@53177
   195
          }
wenzelm@53177
   196
      })
wenzelm@53177
   197
    nodes_timing = nodes_timing1
wenzelm@51533
   198
wenzelm@53177
   199
    val entries = make_entries()
wenzelm@53177
   200
    if (timing_view.listData.toList != entries) timing_view.listData = entries
wenzelm@51533
   201
  }
wenzelm@51533
   202
wenzelm@51533
   203
wenzelm@56715
   204
  /* main */
wenzelm@51533
   205
wenzelm@56715
   206
  private val main =
wenzelm@56715
   207
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
wenzelm@56715
   208
      case changed =>
wenzelm@57612
   209
        GUI_Thread.later { handle_update(Some(changed.nodes)) }
wenzelm@51533
   210
    }
wenzelm@51533
   211
wenzelm@51533
   212
  override def init()
wenzelm@51533
   213
  {
wenzelm@56715
   214
    PIDE.session.commands_changed += main
wenzelm@51533
   215
    handle_update()
wenzelm@51533
   216
  }
wenzelm@51533
   217
wenzelm@51533
   218
  override def exit()
wenzelm@51533
   219
  {
wenzelm@56715
   220
    PIDE.session.commands_changed -= main
wenzelm@51533
   221
  }
wenzelm@51533
   222
}