src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Wed Jul 31 21:49:29 2013 +0200 (2013-07-31)
changeset 52817 408fb2e563df
parent 52816 c608e0ade554
child 52819 7ce3ebc268a1
permissions -rw-r--r--
added home-made tooltips;
tuned signature;
wenzelm@50299
     1
/*  Title:      Tools/jEdit/src/theories_dockable.scala
wenzelm@39515
     2
    Author:     Makarius
wenzelm@39515
     3
wenzelm@50299
     4
Dockable window for theories managed by prover.
wenzelm@39515
     5
*/
wenzelm@39515
     6
wenzelm@39515
     7
package isabelle.jedit
wenzelm@39515
     8
wenzelm@39515
     9
wenzelm@39515
    10
import isabelle._
wenzelm@39515
    11
wenzelm@39515
    12
import scala.actors.Actor._
wenzelm@52807
    13
import scala.swing.{FlowPanel, Button, TextArea, Label, ListView, Alignment,
wenzelm@52815
    14
  ScrollPane, Component, CheckBox, BorderPanel}
wenzelm@52817
    15
import scala.swing.event.{ButtonClicked, MouseClicked, MouseMoved}
wenzelm@39593
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@52816
    18
import java.awt.{BorderLayout, Graphics2D, Insets, Point, Dimension}
wenzelm@47588
    19
import javax.swing.{JList, BorderFactory}
wenzelm@52802
    20
import javax.swing.border.{BevelBorder, SoftBevelBorder}
wenzelm@39515
    21
wenzelm@44865
    22
import org.gjt.sp.jedit.{View, jEdit}
wenzelm@39515
    23
wenzelm@39515
    24
wenzelm@50299
    25
class Theories_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@39515
    26
{
wenzelm@48021
    27
  /* status */
wenzelm@39591
    28
wenzelm@48021
    29
  private val status = new ListView(Nil: List[Document.Node.Name]) {
wenzelm@44991
    30
    listenTo(mouse.clicks)
wenzelm@52817
    31
    listenTo(mouse.moves)
wenzelm@44991
    32
    reactions += {
wenzelm@52816
    33
      case MouseClicked(_, point, _, clicks, _) =>
wenzelm@44991
    34
        val index = peer.locationToIndex(point)
wenzelm@52817
    35
        if (index >= 0) {
wenzelm@52817
    36
          if (in_checkbox(peer.indexToLocation(index), point)) {
wenzelm@52817
    37
            if (clicks == 1) {
wenzelm@52817
    38
              for {
wenzelm@52817
    39
                buffer <- JEdit_Lib.jedit_buffer(listData(index).node)
wenzelm@52817
    40
                model <- PIDE.document_model(buffer)
wenzelm@52817
    41
              } model.node_required = !model.node_required
wenzelm@52817
    42
            }
wenzelm@52816
    43
          }
wenzelm@52817
    44
          else if (clicks == 2) Hyperlink(listData(index).node).follow(view)
wenzelm@52816
    45
        }
wenzelm@52817
    46
      case MouseMoved(_, point, _) =>
wenzelm@52817
    47
        val index = peer.locationToIndex(point)
wenzelm@52817
    48
        if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
wenzelm@52817
    49
          tooltip = "Mark as required for continuous checking"
wenzelm@52817
    50
        else
wenzelm@52817
    51
          tooltip = null
wenzelm@44991
    52
    }
wenzelm@44991
    53
  }
wenzelm@50250
    54
  status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
wenzelm@50250
    55
  status.peer.setVisibleRowCount(0)
wenzelm@44609
    56
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    57
wenzelm@49038
    58
  set_content(new ScrollPane(status))
wenzelm@39515
    59
wenzelm@39515
    60
wenzelm@39593
    61
  /* controls */
wenzelm@39593
    62
wenzelm@50299
    63
  def phase_text(phase: Session.Phase): String =
wenzelm@50299
    64
    "Prover: " + Library.lowercase(phase.toString)
wenzelm@50299
    65
wenzelm@50299
    66
  private val session_phase = new Label(phase_text(PIDE.session.phase))
wenzelm@39697
    67
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@50299
    68
  session_phase.tooltip = "Status of prover session"
wenzelm@39635
    69
wenzelm@48018
    70
  private def handle_phase(phase: Session.Phase)
wenzelm@48018
    71
  {
wenzelm@50299
    72
    Swing_Thread.later { session_phase.text = " " + phase_text(phase) + " " }
wenzelm@48018
    73
  }
wenzelm@48018
    74
wenzelm@52807
    75
  private val continuous_checking = new CheckBox("Continuous checking") {
wenzelm@52807
    76
    tooltip = "Continuous checking of proof document (visible and required parts)"
wenzelm@52815
    77
    reactions += { case ButtonClicked(_) => Isabelle.continuous_checking = selected }
wenzelm@52815
    78
    def load() { selected = Isabelle.continuous_checking }
wenzelm@52763
    79
    load()
wenzelm@44776
    80
  }
wenzelm@44776
    81
wenzelm@49246
    82
  private val logic = Isabelle_Logic.logic_selector(true)
wenzelm@39702
    83
wenzelm@44775
    84
  private val controls =
wenzelm@52807
    85
    new FlowPanel(FlowPanel.Alignment.Right)(continuous_checking, session_phase, logic)
wenzelm@39593
    86
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    87
wenzelm@39593
    88
wenzelm@44609
    89
  /* component state -- owned by Swing thread */
wenzelm@44609
    90
wenzelm@45709
    91
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@52815
    92
  private var nodes_required: Set[Document.Node.Name] = Set.empty
wenzelm@44866
    93
wenzelm@52815
    94
  private def update_nodes_required()
wenzelm@52815
    95
  {
wenzelm@52815
    96
    nodes_required = Set.empty
wenzelm@52815
    97
    for {
wenzelm@52815
    98
      buffer <- JEdit_Lib.jedit_buffers
wenzelm@52815
    99
      model <- PIDE.document_model(buffer)
wenzelm@52815
   100
      if model.node_required
wenzelm@52815
   101
    } nodes_required += model.name
wenzelm@52815
   102
  }
wenzelm@52815
   103
wenzelm@52817
   104
  private def in_checkbox(loc0: Point, p: Point): Boolean =
wenzelm@52817
   105
    Node_Renderer_Component != null &&
wenzelm@52817
   106
      (Node_Renderer_Component.checkbox_geometry match {
wenzelm@52817
   107
        case Some((loc, size)) =>
wenzelm@52817
   108
          loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
wenzelm@52817
   109
          loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
wenzelm@52817
   110
        case None => false
wenzelm@52817
   111
      })
wenzelm@52817
   112
wenzelm@52815
   113
  private object Node_Renderer_Component extends BorderPanel
wenzelm@44866
   114
  {
wenzelm@44990
   115
    opaque = false
wenzelm@44989
   116
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   117
wenzelm@44957
   118
    var node_name = Document.Node.Name.empty
wenzelm@52816
   119
wenzelm@52816
   120
    var checkbox_geometry: Option[(Point, Dimension)] = None
wenzelm@52816
   121
    val checkbox = new CheckBox {
wenzelm@52816
   122
      override def paintComponent(gfx: Graphics2D)
wenzelm@52816
   123
      {
wenzelm@52816
   124
        super.paintComponent(gfx)
wenzelm@52816
   125
        if (location != null && size != null)
wenzelm@52816
   126
          checkbox_geometry = Some((location, size))
wenzelm@52816
   127
      }
wenzelm@52816
   128
    }
wenzelm@52815
   129
wenzelm@52815
   130
    val label = new Label {
wenzelm@52815
   131
      opaque = false
wenzelm@52815
   132
      xAlignment = Alignment.Leading
wenzelm@52809
   133
wenzelm@52815
   134
      override def paintComponent(gfx: Graphics2D)
wenzelm@52815
   135
      {
wenzelm@52815
   136
        val size = peer.getSize()
wenzelm@52815
   137
        val insets = border.getBorderInsets(peer)
wenzelm@52815
   138
        val w = size.width - insets.left - insets.right
wenzelm@52815
   139
        val h = size.height - insets.top - insets.bottom
wenzelm@50900
   140
wenzelm@52815
   141
        nodes_status.get(node_name) match {
wenzelm@52815
   142
          case Some(st) if st.total > 0 =>
wenzelm@52815
   143
            val colors = List(
wenzelm@52815
   144
              (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@52815
   145
              (st.running, PIDE.options.color_value("running_color")),
wenzelm@52815
   146
              (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@52815
   147
              (st.failed, PIDE.options.color_value("error_color")))
wenzelm@52815
   148
wenzelm@52815
   149
            var end = size.width - insets.right
wenzelm@52815
   150
            for { (n, color) <- colors }
wenzelm@52815
   151
            {
wenzelm@52815
   152
              gfx.setColor(color)
wenzelm@52815
   153
              val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
wenzelm@52815
   154
              gfx.fillRect(end - v, insets.top, v, h)
wenzelm@52815
   155
              end = end - v - 1
wenzelm@52815
   156
            }
wenzelm@52815
   157
          case _ =>
wenzelm@52815
   158
            gfx.setColor(PIDE.options.color_value("unprocessed1_color"))
wenzelm@52815
   159
            gfx.fillRect(insets.left, insets.top, w, h)
wenzelm@52815
   160
        }
wenzelm@52815
   161
        super.paintComponent(gfx)
wenzelm@44866
   162
      }
wenzelm@44866
   163
    }
wenzelm@52815
   164
wenzelm@52816
   165
    layout(checkbox) = BorderPanel.Position.West
wenzelm@52815
   166
    layout(label) = BorderPanel.Position.Center
wenzelm@44866
   167
  }
wenzelm@44866
   168
wenzelm@44990
   169
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   170
  {
wenzelm@44990
   171
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   172
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   173
    {
wenzelm@44990
   174
      val component = Node_Renderer_Component
wenzelm@44957
   175
      component.node_name = name
wenzelm@52816
   176
      component.checkbox.selected = nodes_required.contains(name)
wenzelm@52815
   177
      component.label.text = name.theory
wenzelm@44990
   178
      component
wenzelm@44957
   179
    }
wenzelm@44957
   180
  }
wenzelm@44957
   181
  status.renderer = new Node_Renderer
wenzelm@44609
   182
wenzelm@45011
   183
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   184
  {
wenzelm@44609
   185
    Swing_Thread.now {
wenzelm@50205
   186
      val snapshot = PIDE.session.snapshot()
wenzelm@44613
   187
wenzelm@46723
   188
      val iterator =
wenzelm@46723
   189
        restriction match {
wenzelm@46723
   190
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@46723
   191
          case None => snapshot.version.nodes.entries
wenzelm@46723
   192
        }
wenzelm@46681
   193
      val nodes_status1 =
wenzelm@46723
   194
        (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@50205
   195
            if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@46739
   196
            else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   197
wenzelm@44672
   198
      if (nodes_status != nodes_status1) {
wenzelm@44672
   199
        nodes_status = nodes_status1
wenzelm@44866
   200
        status.listData =
wenzelm@46723
   201
          snapshot.version.nodes.topological_order.filter(
wenzelm@44960
   202
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   203
      }
wenzelm@44609
   204
    }
wenzelm@44609
   205
  }
wenzelm@44609
   206
wenzelm@44609
   207
wenzelm@39515
   208
  /* main actor */
wenzelm@39515
   209
wenzelm@39515
   210
  private val main_actor = actor {
wenzelm@39515
   211
    loop {
wenzelm@39515
   212
      react {
wenzelm@48018
   213
        case phase: Session.Phase => handle_phase(phase)
wenzelm@39635
   214
wenzelm@52759
   215
        case _: Session.Global_Options =>
wenzelm@52759
   216
          Swing_Thread.later {
wenzelm@52807
   217
            continuous_checking.load()
wenzelm@52759
   218
            logic.load ()
wenzelm@52815
   219
            update_nodes_required()
wenzelm@52815
   220
            status.repaint()
wenzelm@52759
   221
          }
wenzelm@49247
   222
wenzelm@45011
   223
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   224
wenzelm@50299
   225
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   226
      }
wenzelm@39515
   227
    }
wenzelm@39515
   228
  }
wenzelm@39515
   229
wenzelm@48018
   230
  override def init()
wenzelm@48018
   231
  {
wenzelm@50205
   232
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   233
    PIDE.session.global_options += main_actor
wenzelm@50205
   234
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   235
  }
wenzelm@39635
   236
wenzelm@48018
   237
  override def exit()
wenzelm@48018
   238
  {
wenzelm@50205
   239
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   240
    PIDE.session.global_options -= main_actor
wenzelm@50205
   241
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   242
  }
wenzelm@39515
   243
}