src/Tools/jEdit/src/sledgehammer_dockable.scala
author wenzelm
Fri Apr 25 12:51:08 2014 +0200 (2014-04-25)
changeset 56715 52125652e82a
parent 56662 f373fb77e0a4
child 56918 a442dc6d244d
permissions -rw-r--r--
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
eliminated old actors;
     1 /*  Title:      Tools/jEdit/src/sledgehammer_dockable.scala
     2     Author:     Makarius
     3 
     4 Dockable window for Sledgehammer.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import scala.swing.{Button, Component, Label, TextField, CheckBox}
    13 import scala.swing.event.ButtonClicked
    14 
    15 import java.awt.BorderLayout
    16 import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent}
    17 
    18 import org.gjt.sp.jedit.View
    19 import org.gjt.sp.jedit.gui.HistoryTextField
    20 
    21 
    22 class Sledgehammer_Dockable(view: View, position: String) extends Dockable(view, position)
    23 {
    24   val pretty_text_area = new Pretty_Text_Area(view)
    25   set_content(pretty_text_area)
    26 
    27 
    28   /* query operation */
    29 
    30   private val process_indicator = new Process_Indicator
    31 
    32   private def consume_status(status: Query_Operation.Status.Value)
    33   {
    34     status match {
    35       case Query_Operation.Status.WAITING =>
    36         process_indicator.update("Waiting for evaluation of context ...", 5)
    37       case Query_Operation.Status.RUNNING =>
    38         process_indicator.update("Sledgehammering ...", 15)
    39       case Query_Operation.Status.FINISHED =>
    40         process_indicator.update(null, 0)
    41     }
    42   }
    43 
    44   private val sledgehammer =
    45     new Query_Operation(PIDE.editor, view, "sledgehammer", consume_status _,
    46       (snapshot, results, body) =>
    47         pretty_text_area.update(snapshot, results, Pretty.separate(body)))
    48 
    49 
    50   /* resize */
    51 
    52   private var zoom_factor = 100
    53 
    54   private def handle_resize()
    55   {
    56     Swing_Thread.require {}
    57 
    58     pretty_text_area.resize(
    59       Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom_factor / 100))
    60   }
    61 
    62   private val delay_resize =
    63     Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
    64 
    65   addComponentListener(new ComponentAdapter {
    66     override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
    67   })
    68 
    69 
    70   /* controls */
    71 
    72   private def clicked {
    73     PIDE.options.string("sledgehammer_provers") = provers.getText
    74     sledgehammer.apply_query(List(provers.getText, isar_proofs.selected.toString))
    75   }
    76 
    77   private val provers_label = new Label("Provers:") {
    78     tooltip =
    79       GUI.tooltip_lines(
    80         "Automatic provers as space-separated list, e.g.\ne spass remote_vampire")
    81   }
    82 
    83   private val provers = new HistoryTextField("isabelle-sledgehammer-provers") {
    84     override def processKeyEvent(evt: KeyEvent)
    85     {
    86       if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) clicked
    87       super.processKeyEvent(evt)
    88     }
    89     setToolTipText(provers_label.tooltip)
    90     setColumns(30)
    91   }
    92 
    93   private def update_provers()
    94   {
    95     val new_provers = PIDE.options.string("sledgehammer_provers")
    96     if (new_provers != provers.getText) {
    97       provers.setText(new_provers)
    98       if (provers.getCaret != null)
    99         provers.getCaret.setDot(0)
   100     }
   101   }
   102 
   103   private val isar_proofs = new CheckBox("Isar proofs") {
   104     tooltip = "Specify whether Isar proofs should be output in addition to metis line"
   105     selected = false
   106   }
   107 
   108   private val apply_query = new Button("Apply") {
   109     tooltip = "Search for first-order proof using automatic theorem provers"
   110     reactions += { case ButtonClicked(_) => clicked }
   111   }
   112 
   113   private val cancel_query = new Button("Cancel") {
   114     tooltip = "Interrupt unfinished sledgehammering"
   115     reactions += { case ButtonClicked(_) => sledgehammer.cancel_query() }
   116   }
   117 
   118   private val locate_query = new Button("Locate") {
   119     tooltip = "Locate context of current query within source text"
   120     reactions += { case ButtonClicked(_) => sledgehammer.locate_query() }
   121   }
   122 
   123   private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
   124     tooltip = "Zoom factor for output font size"
   125   }
   126 
   127   private val controls =
   128     new Wrap_Panel(Wrap_Panel.Alignment.Right)(
   129       provers_label, Component.wrap(provers), isar_proofs,
   130       process_indicator.component, apply_query, cancel_query, locate_query, zoom)
   131   add(controls.peer, BorderLayout.NORTH)
   132 
   133   override def focusOnDefaultComponent { provers.requestFocus }
   134 
   135 
   136   /* main */
   137 
   138   private val main =
   139     Session.Consumer[Session.Global_Options](getClass.getName) {
   140       case _: Session.Global_Options => Swing_Thread.later { update_provers(); handle_resize() }
   141     }
   142 
   143   override def init()
   144   {
   145     PIDE.session.global_options += main
   146     update_provers()
   147     handle_resize()
   148     sledgehammer.activate()
   149   }
   150 
   151   override def exit()
   152   {
   153     sledgehammer.deactivate()
   154     PIDE.session.global_options -= main
   155     delay_resize.revoke()
   156   }
   157 }