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