src/Tools/jEdit/src/timing_dockable.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 56715 52125652e82a
child 59319 677615cba30d
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
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@51533
    68
      def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@51533
    69
        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@51534
    89
    def print: String = Time.print_seconds(timing) + "s theory " + quote(name.theory)
wenzelm@55877
    90
    def follow(snapshot: Document.Snapshot) { PIDE.editor.goto_file(view, name.node) }
wenzelm@51534
    91
  }
wenzelm@51534
    92
wenzelm@51534
    93
  private case class Command_Entry(command: Command, timing: Double) extends Entry
wenzelm@51534
    94
  {
wenzelm@51534
    95
    def print: String = "  " + Time.print_seconds(timing) + "s command " + quote(command.name)
wenzelm@51533
    96
    def follow(snapshot: Document.Snapshot)
wenzelm@52980
    97
    { PIDE.editor.hyperlink_command(snapshot, command).foreach(_.follow(view)) }
wenzelm@51533
    98
  }
wenzelm@51533
    99
wenzelm@51533
   100
wenzelm@51533
   101
  /* timing view */
wenzelm@51533
   102
wenzelm@51533
   103
  private val timing_view = new ListView(Nil: List[Entry]) {
wenzelm@51533
   104
    listenTo(mouse.clicks)
wenzelm@51533
   105
    reactions += {
wenzelm@51533
   106
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@51533
   107
        val index = peer.locationToIndex(point)
wenzelm@51533
   108
        if (index >= 0) listData(index).follow(PIDE.session.snapshot())
wenzelm@51533
   109
    }
wenzelm@51533
   110
  }
wenzelm@51533
   111
  timing_view.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@51533
   112
  timing_view.peer.setVisibleRowCount(0)
wenzelm@51533
   113
  timing_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@51533
   114
  timing_view.renderer = new Entry.Renderer
wenzelm@51533
   115
wenzelm@51533
   116
  set_content(new ScrollPane(timing_view))
wenzelm@51533
   117
wenzelm@51533
   118
wenzelm@51533
   119
  /* timing threshold */
wenzelm@51533
   120
wenzelm@51533
   121
  private var timing_threshold = PIDE.options.real("jedit_timing_threshold")
wenzelm@51533
   122
wenzelm@51549
   123
  private val threshold_tooltip = "Threshold for timing display (seconds)"
wenzelm@51549
   124
wenzelm@51549
   125
  private val threshold_label = new Label("Threshold: ") {
wenzelm@51549
   126
    tooltip = threshold_tooltip
wenzelm@51549
   127
  }
wenzelm@51533
   128
wenzelm@51533
   129
  private val threshold_value = new TextField(Time.print_seconds(timing_threshold)) {
wenzelm@51533
   130
    reactions += {
wenzelm@51538
   131
      case _: ValueChanged =>
wenzelm@51533
   132
        text match {
wenzelm@51533
   133
          case Properties.Value.Double(x) if x >= 0.0 => timing_threshold = x
wenzelm@51533
   134
          case _ =>
wenzelm@51533
   135
        }
wenzelm@51533
   136
        handle_update()
wenzelm@51533
   137
    }
wenzelm@51549
   138
    tooltip = threshold_tooltip
wenzelm@51538
   139
    verifier = ((s: String) =>
wenzelm@51538
   140
      s match { case Properties.Value.Double(x) => x >= 0.0 case _ => false })
wenzelm@51533
   141
  }
wenzelm@51533
   142
wenzelm@53711
   143
  private val controls =
wenzelm@53711
   144
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(threshold_label, threshold_value)
wenzelm@51533
   145
  add(controls.peer, BorderLayout.NORTH)
wenzelm@51533
   146
wenzelm@51533
   147
wenzelm@57612
   148
  /* component state -- owned by GUI thread */
wenzelm@51533
   149
wenzelm@51533
   150
  private var nodes_timing = Map.empty[Document.Node.Name, Protocol.Node_Timing]
wenzelm@51534
   151
wenzelm@51534
   152
  private def make_entries(): List[Entry] =
wenzelm@51534
   153
  {
wenzelm@57612
   154
    GUI_Thread.require {}
wenzelm@51534
   155
wenzelm@51534
   156
    val name =
wenzelm@51534
   157
      Document_View(view.getTextArea) match {
wenzelm@51534
   158
        case None => Document.Node.Name.empty
wenzelm@52973
   159
        case Some(doc_view) => doc_view.model.node_name
wenzelm@51534
   160
      }
wenzelm@52888
   161
    val timing = nodes_timing.getOrElse(name, Protocol.empty_node_timing)
wenzelm@51534
   162
wenzelm@51534
   163
    val theories =
wenzelm@51536
   164
      (for ((node_name, node_timing) <- nodes_timing.toList if !node_timing.commands.isEmpty)
wenzelm@51536
   165
        yield Theory_Entry(node_name, node_timing.total, false)).sorted(Entry.Ordering)
wenzelm@51534
   166
    val commands =
wenzelm@51534
   167
      (for ((command, command_timing) <- timing.commands.toList)
wenzelm@51534
   168
        yield Command_Entry(command, command_timing)).sorted(Entry.Ordering)
wenzelm@51534
   169
wenzelm@51536
   170
    theories.flatMap(entry =>
wenzelm@51536
   171
      if (entry.name == name) entry.copy(current = true) :: commands
wenzelm@51536
   172
      else List(entry))
wenzelm@51534
   173
  }
wenzelm@51533
   174
wenzelm@51533
   175
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@51533
   176
  {
wenzelm@57612
   177
    GUI_Thread.require {}
wenzelm@53177
   178
wenzelm@53177
   179
    val snapshot = PIDE.session.snapshot()
wenzelm@51533
   180
wenzelm@53177
   181
    val iterator =
wenzelm@53177
   182
      restriction match {
wenzelm@53177
   183
        case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@56372
   184
        case None => snapshot.version.nodes.iterator
wenzelm@53177
   185
      }
wenzelm@53177
   186
    val nodes_timing1 =
wenzelm@53177
   187
      (nodes_timing /: iterator)({ case (timing1, (name, node)) =>
wenzelm@56208
   188
          if (PIDE.resources.loaded_theories(name.theory)) timing1
wenzelm@53177
   189
          else {
wenzelm@53177
   190
            val node_timing =
wenzelm@53177
   191
              Protocol.node_timing(snapshot.state, snapshot.version, node, timing_threshold)
wenzelm@53177
   192
            timing1 + (name -> node_timing)
wenzelm@53177
   193
          }
wenzelm@53177
   194
      })
wenzelm@53177
   195
    nodes_timing = nodes_timing1
wenzelm@51533
   196
wenzelm@53177
   197
    val entries = make_entries()
wenzelm@53177
   198
    if (timing_view.listData.toList != entries) timing_view.listData = entries
wenzelm@51533
   199
  }
wenzelm@51533
   200
wenzelm@51533
   201
wenzelm@56715
   202
  /* main */
wenzelm@51533
   203
wenzelm@56715
   204
  private val main =
wenzelm@56715
   205
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
wenzelm@56715
   206
      case changed =>
wenzelm@57612
   207
        GUI_Thread.later { handle_update(Some(changed.nodes)) }
wenzelm@51533
   208
    }
wenzelm@51533
   209
wenzelm@51533
   210
  override def init()
wenzelm@51533
   211
  {
wenzelm@56715
   212
    PIDE.session.commands_changed += main
wenzelm@51533
   213
    handle_update()
wenzelm@51533
   214
  }
wenzelm@51533
   215
wenzelm@51533
   216
  override def exit()
wenzelm@51533
   217
  {
wenzelm@56715
   218
    PIDE.session.commands_changed -= main
wenzelm@51533
   219
  }
wenzelm@51533
   220
}