src/Tools/jEdit/src/timing_dockable.scala
author wenzelm
Tue Mar 26 11:26:13 2013 +0100 (2013-03-26)
changeset 51533 3f6280aedbcc
child 51534 123bd97fcea1
permissions -rw-r--r--
dockable window for timing information;
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@51533
    12
import scala.actors.Actor._
wenzelm@51533
    13
import scala.swing.{FlowPanel, Label, ListView, Alignment, ScrollPane, Component, TextField}
wenzelm@51533
    14
import scala.swing.event.{EditDone, MouseClicked}
wenzelm@51533
    15
wenzelm@51533
    16
import java.lang.System
wenzelm@51533
    17
import java.awt.{BorderLayout, Graphics2D, Insets}
wenzelm@51533
    18
import javax.swing.{JList, BorderFactory}
wenzelm@51533
    19
import javax.swing.border.{BevelBorder, SoftBevelBorder}
wenzelm@51533
    20
wenzelm@51533
    21
import org.gjt.sp.jedit.{View, jEdit}
wenzelm@51533
    22
wenzelm@51533
    23
wenzelm@51533
    24
class Timing_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@51533
    25
{
wenzelm@51533
    26
  /* entry */
wenzelm@51533
    27
wenzelm@51533
    28
  private object Entry
wenzelm@51533
    29
  {
wenzelm@51533
    30
    object Ordering extends scala.math.Ordering[Entry]
wenzelm@51533
    31
    {
wenzelm@51533
    32
      def compare(entry1: Entry, entry2: Entry): Int =
wenzelm@51533
    33
        entry2.timing compare entry1.timing
wenzelm@51533
    34
    }
wenzelm@51533
    35
wenzelm@51533
    36
    object Renderer_Component extends Label
wenzelm@51533
    37
    {
wenzelm@51533
    38
      opaque = false
wenzelm@51533
    39
      xAlignment = Alignment.Leading
wenzelm@51533
    40
      border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@51533
    41
    }
wenzelm@51533
    42
wenzelm@51533
    43
    class Renderer extends ListView.Renderer[Entry]
wenzelm@51533
    44
    {
wenzelm@51533
    45
      def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@51533
    46
        entry: Entry, index: Int): Component =
wenzelm@51533
    47
      {
wenzelm@51533
    48
        val component = Renderer_Component
wenzelm@51533
    49
        component.text = Time.print_seconds(entry.timing) + "s " + entry.command.name
wenzelm@51533
    50
        component
wenzelm@51533
    51
      }
wenzelm@51533
    52
    }
wenzelm@51533
    53
  }
wenzelm@51533
    54
wenzelm@51533
    55
  private case class Entry(command: Command, timing: Double)
wenzelm@51533
    56
  {
wenzelm@51533
    57
    def follow(snapshot: Document.Snapshot)
wenzelm@51533
    58
    {
wenzelm@51533
    59
      val node = snapshot.version.nodes(command.node_name)
wenzelm@51533
    60
      if (node.commands.contains(command)) {
wenzelm@51533
    61
        val sources = node.commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@51533
    62
        val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@51533
    63
        Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@51533
    64
      }
wenzelm@51533
    65
    }
wenzelm@51533
    66
  }
wenzelm@51533
    67
wenzelm@51533
    68
wenzelm@51533
    69
  /* timing view */
wenzelm@51533
    70
wenzelm@51533
    71
  private val timing_view = new ListView(Nil: List[Entry]) {
wenzelm@51533
    72
    listenTo(mouse.clicks)
wenzelm@51533
    73
    reactions += {
wenzelm@51533
    74
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@51533
    75
        val index = peer.locationToIndex(point)
wenzelm@51533
    76
        if (index >= 0) listData(index).follow(PIDE.session.snapshot())
wenzelm@51533
    77
    }
wenzelm@51533
    78
  }
wenzelm@51533
    79
  timing_view.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@51533
    80
  timing_view.peer.setVisibleRowCount(0)
wenzelm@51533
    81
  timing_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@51533
    82
  timing_view.renderer = new Entry.Renderer
wenzelm@51533
    83
wenzelm@51533
    84
  set_content(new ScrollPane(timing_view))
wenzelm@51533
    85
wenzelm@51533
    86
wenzelm@51533
    87
  /* timing threshold */
wenzelm@51533
    88
wenzelm@51533
    89
  private var timing_threshold = PIDE.options.real("jedit_timing_threshold")
wenzelm@51533
    90
wenzelm@51533
    91
  private val threshold_label = new Label("Timing threshold: ")
wenzelm@51533
    92
wenzelm@51533
    93
  private val threshold_value = new TextField(Time.print_seconds(timing_threshold)) {
wenzelm@51533
    94
    reactions += {
wenzelm@51533
    95
      case EditDone(_) =>
wenzelm@51533
    96
        text match {
wenzelm@51533
    97
          case Properties.Value.Double(x) if x >= 0.0 => timing_threshold = x
wenzelm@51533
    98
          case _ =>
wenzelm@51533
    99
        }
wenzelm@51533
   100
        text = Time.print_seconds(timing_threshold)
wenzelm@51533
   101
        handle_update()
wenzelm@51533
   102
    }
wenzelm@51533
   103
    tooltip = "Threshold for timing display (seconds)"
wenzelm@51533
   104
  }
wenzelm@51533
   105
wenzelm@51533
   106
  private val controls = new FlowPanel(FlowPanel.Alignment.Right)(threshold_label, threshold_value)
wenzelm@51533
   107
  add(controls.peer, BorderLayout.NORTH)
wenzelm@51533
   108
wenzelm@51533
   109
wenzelm@51533
   110
  /* component state -- owned by Swing thread */
wenzelm@51533
   111
wenzelm@51533
   112
  private var nodes_timing = Map.empty[Document.Node.Name, Protocol.Node_Timing]
wenzelm@51533
   113
  private var current_name = Document.Node.Name.empty
wenzelm@51533
   114
  private var current_timing = Protocol.empty_node_timing
wenzelm@51533
   115
wenzelm@51533
   116
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@51533
   117
  {
wenzelm@51533
   118
    Swing_Thread.now {
wenzelm@51533
   119
      val snapshot = PIDE.session.snapshot()
wenzelm@51533
   120
wenzelm@51533
   121
      val iterator =
wenzelm@51533
   122
        restriction match {
wenzelm@51533
   123
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@51533
   124
          case None => snapshot.version.nodes.entries
wenzelm@51533
   125
        }
wenzelm@51533
   126
      val nodes_timing1 =
wenzelm@51533
   127
        (nodes_timing /: iterator)({ case (timing1, (name, node)) =>
wenzelm@51533
   128
            if (PIDE.thy_load.loaded_theories(name.theory)) timing1
wenzelm@51533
   129
            else {
wenzelm@51533
   130
              val node_timing =
wenzelm@51533
   131
                Protocol.node_timing(snapshot.state, snapshot.version, node, timing_threshold)
wenzelm@51533
   132
              timing1 + (name -> node_timing)
wenzelm@51533
   133
            }
wenzelm@51533
   134
        })
wenzelm@51533
   135
      nodes_timing = nodes_timing1
wenzelm@51533
   136
wenzelm@51533
   137
      Document_View(view.getTextArea) match {
wenzelm@51533
   138
        case Some(doc_view) =>
wenzelm@51533
   139
          val name = doc_view.model.name
wenzelm@51533
   140
          val timing = nodes_timing.get(name) getOrElse Protocol.empty_node_timing
wenzelm@51533
   141
          if (current_name != name || current_timing != timing) {
wenzelm@51533
   142
            current_name = name
wenzelm@51533
   143
            current_timing = timing
wenzelm@51533
   144
            timing_view.listData =
wenzelm@51533
   145
              timing.commands.toList.map(p => Entry(p._1, p._2)).sorted(Entry.Ordering)
wenzelm@51533
   146
          }
wenzelm@51533
   147
        case None =>
wenzelm@51533
   148
      }
wenzelm@51533
   149
    }
wenzelm@51533
   150
  }
wenzelm@51533
   151
wenzelm@51533
   152
wenzelm@51533
   153
  /* main actor */
wenzelm@51533
   154
wenzelm@51533
   155
  private val main_actor = actor {
wenzelm@51533
   156
    loop {
wenzelm@51533
   157
      react {
wenzelm@51533
   158
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@51533
   159
wenzelm@51533
   160
        case bad => System.err.println("Timing_Dockable: ignoring bad message " + bad)
wenzelm@51533
   161
      }
wenzelm@51533
   162
    }
wenzelm@51533
   163
  }
wenzelm@51533
   164
wenzelm@51533
   165
  override def init()
wenzelm@51533
   166
  {
wenzelm@51533
   167
    PIDE.session.commands_changed += main_actor
wenzelm@51533
   168
    handle_update()
wenzelm@51533
   169
  }
wenzelm@51533
   170
wenzelm@51533
   171
  override def exit()
wenzelm@51533
   172
  {
wenzelm@51533
   173
    PIDE.session.commands_changed -= main_actor
wenzelm@51533
   174
  }
wenzelm@51533
   175
}