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