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