src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Mon Aug 12 11:56:12 2013 +0200 (2013-08-12)
changeset 52973 d5f7fa1498b7
parent 52860 f155c38242c1
child 52980 28f59ca8ce78
permissions -rw-r--r--
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@52973
   101
    } nodes_required += model.node_name
wenzelm@52815
   102
  }
wenzelm@52860
   103
  update_nodes_required()
wenzelm@52815
   104
wenzelm@52817
   105
  private def in_checkbox(loc0: Point, p: Point): Boolean =
wenzelm@52817
   106
    Node_Renderer_Component != null &&
wenzelm@52817
   107
      (Node_Renderer_Component.checkbox_geometry match {
wenzelm@52817
   108
        case Some((loc, size)) =>
wenzelm@52817
   109
          loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
wenzelm@52817
   110
          loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
wenzelm@52817
   111
        case None => false
wenzelm@52817
   112
      })
wenzelm@52817
   113
wenzelm@52815
   114
  private object Node_Renderer_Component extends BorderPanel
wenzelm@44866
   115
  {
wenzelm@44990
   116
    opaque = false
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@52819
   122
      opaque = false
wenzelm@52816
   123
      override def paintComponent(gfx: Graphics2D)
wenzelm@52816
   124
      {
wenzelm@52816
   125
        super.paintComponent(gfx)
wenzelm@52816
   126
        if (location != null && size != null)
wenzelm@52816
   127
          checkbox_geometry = Some((location, size))
wenzelm@52816
   128
      }
wenzelm@52816
   129
    }
wenzelm@52815
   130
wenzelm@52815
   131
    val label = new Label {
wenzelm@52815
   132
      opaque = false
wenzelm@52815
   133
      xAlignment = Alignment.Leading
wenzelm@52809
   134
wenzelm@52815
   135
      override def paintComponent(gfx: Graphics2D)
wenzelm@52815
   136
      {
wenzelm@52819
   137
        border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@52815
   138
        val size = peer.getSize()
wenzelm@52815
   139
        val insets = border.getBorderInsets(peer)
wenzelm@52815
   140
        val w = size.width - insets.left - insets.right
wenzelm@52815
   141
        val h = size.height - insets.top - insets.bottom
wenzelm@50900
   142
wenzelm@52815
   143
        nodes_status.get(node_name) match {
wenzelm@52815
   144
          case Some(st) if st.total > 0 =>
wenzelm@52815
   145
            val colors = List(
wenzelm@52815
   146
              (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@52815
   147
              (st.running, PIDE.options.color_value("running_color")),
wenzelm@52815
   148
              (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@52815
   149
              (st.failed, PIDE.options.color_value("error_color")))
wenzelm@52815
   150
wenzelm@52815
   151
            var end = size.width - insets.right
wenzelm@52815
   152
            for { (n, color) <- colors }
wenzelm@52815
   153
            {
wenzelm@52815
   154
              gfx.setColor(color)
wenzelm@52815
   155
              val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
wenzelm@52815
   156
              gfx.fillRect(end - v, insets.top, v, h)
wenzelm@52815
   157
              end = end - v - 1
wenzelm@52815
   158
            }
wenzelm@52815
   159
          case _ =>
wenzelm@52815
   160
            gfx.setColor(PIDE.options.color_value("unprocessed1_color"))
wenzelm@52815
   161
            gfx.fillRect(insets.left, insets.top, w, h)
wenzelm@52815
   162
        }
wenzelm@52815
   163
        super.paintComponent(gfx)
wenzelm@44866
   164
      }
wenzelm@44866
   165
    }
wenzelm@52815
   166
wenzelm@52816
   167
    layout(checkbox) = BorderPanel.Position.West
wenzelm@52815
   168
    layout(label) = BorderPanel.Position.Center
wenzelm@44866
   169
  }
wenzelm@44866
   170
wenzelm@44990
   171
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   172
  {
wenzelm@44990
   173
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   174
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   175
    {
wenzelm@44990
   176
      val component = Node_Renderer_Component
wenzelm@44957
   177
      component.node_name = name
wenzelm@52816
   178
      component.checkbox.selected = nodes_required.contains(name)
wenzelm@52815
   179
      component.label.text = name.theory
wenzelm@44990
   180
      component
wenzelm@44957
   181
    }
wenzelm@44957
   182
  }
wenzelm@44957
   183
  status.renderer = new Node_Renderer
wenzelm@44609
   184
wenzelm@45011
   185
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   186
  {
wenzelm@44609
   187
    Swing_Thread.now {
wenzelm@50205
   188
      val snapshot = PIDE.session.snapshot()
wenzelm@44613
   189
wenzelm@46723
   190
      val iterator =
wenzelm@46723
   191
        restriction match {
wenzelm@46723
   192
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@46723
   193
          case None => snapshot.version.nodes.entries
wenzelm@46723
   194
        }
wenzelm@46681
   195
      val nodes_status1 =
wenzelm@46723
   196
        (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@50205
   197
            if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@46739
   198
            else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   199
wenzelm@44672
   200
      if (nodes_status != nodes_status1) {
wenzelm@44672
   201
        nodes_status = nodes_status1
wenzelm@44866
   202
        status.listData =
wenzelm@46723
   203
          snapshot.version.nodes.topological_order.filter(
wenzelm@44960
   204
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   205
      }
wenzelm@44609
   206
    }
wenzelm@44609
   207
  }
wenzelm@44609
   208
wenzelm@44609
   209
wenzelm@39515
   210
  /* main actor */
wenzelm@39515
   211
wenzelm@39515
   212
  private val main_actor = actor {
wenzelm@39515
   213
    loop {
wenzelm@39515
   214
      react {
wenzelm@48018
   215
        case phase: Session.Phase => handle_phase(phase)
wenzelm@39635
   216
wenzelm@52759
   217
        case _: Session.Global_Options =>
wenzelm@52759
   218
          Swing_Thread.later {
wenzelm@52807
   219
            continuous_checking.load()
wenzelm@52759
   220
            logic.load ()
wenzelm@52815
   221
            update_nodes_required()
wenzelm@52815
   222
            status.repaint()
wenzelm@52759
   223
          }
wenzelm@49247
   224
wenzelm@45011
   225
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   226
wenzelm@50299
   227
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   228
      }
wenzelm@39515
   229
    }
wenzelm@39515
   230
  }
wenzelm@39515
   231
wenzelm@48018
   232
  override def init()
wenzelm@48018
   233
  {
wenzelm@50205
   234
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   235
    PIDE.session.global_options += main_actor
wenzelm@50205
   236
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   237
  }
wenzelm@39635
   238
wenzelm@48018
   239
  override def exit()
wenzelm@48018
   240
  {
wenzelm@50205
   241
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   242
    PIDE.session.global_options -= main_actor
wenzelm@50205
   243
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   244
  }
wenzelm@39515
   245
}