src/Tools/jEdit/src/symbols_dockable.scala
author wenzelm
Fri, 08 Jan 2016 18:18:40 +0100
changeset 62104 fb73c0d7bb37
parent 62024 e3e22a5e85f2
child 63870 6db1aac936db
permissions -rw-r--r--
clarified symbol insertion, depending on buffer encoding;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/symbols_dockable.scala
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     2
    Author:     Fabian Immler
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     3
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     4
Dockable window for Symbol Palette.
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     5
*/
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     6
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     7
package isabelle.jedit
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     8
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     9
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    10
import isabelle._
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    11
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    12
import scala.swing.event.{SelectionChanged, ValueChanged}
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    13
import scala.swing.{Action, Button, TabbedPane, TextField, BorderPanel, Label, ScrollPane}
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    14
50299
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    15
import org.gjt.sp.jedit.View
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    16
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    17
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    18
class Symbols_Dockable(view: View, position: String) extends Dockable(view, position)
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    19
{
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    20
  private class Symbol_Component(val symbol: String, is_control: Boolean) extends Button
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    21
  {
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53786
diff changeset
    22
    private val font_size = Font_Info.main_size(PIDE.options.real("jedit_font_scale")).round
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    23
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    24
    font =
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    25
      Symbol.fonts.get(symbol) match {
57908
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57612
diff changeset
    26
        case None => GUI.font(size = font_size)
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57612
diff changeset
    27
        case Some(font_family) => GUI.font(family = font_family, size = font_size)
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    28
      }
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    29
    action =
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    30
      Action(Symbol.decode(symbol)) {
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    31
        val text_area = view.getTextArea
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    32
        if (is_control && HTML.control.isDefinedAt(symbol))
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    33
          Token_Markup.edit_control_style(text_area, symbol)
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    34
        else
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62024
diff changeset
    35
          text_area.setSelectedText(Isabelle_Encoding.maybe_decode(text_area.getBuffer, symbol))
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    36
        text_area.requestFocus
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    37
      }
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    38
    tooltip =
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    39
      GUI.tooltip_lines(
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    40
        cat_lines(symbol :: Symbol.abbrevs.get_list(symbol).map(a => "abbrev: " + a)))
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    41
  }
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    42
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    43
  private class Reset_Component extends Button
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    44
  {
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    45
    action = Action("Reset") {
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    46
      val text_area = view.getTextArea
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    47
      Token_Markup.edit_control_style(text_area, "")
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    48
      text_area.requestFocus
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    49
    }
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    50
    tooltip = "Reset control symbols within text"
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    51
  }
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    52
56756
wenzelm
parents: 56755
diff changeset
    53
  private val group_tabs: TabbedPane = new TabbedPane {
50192
22d0f64362a5 tuned -- Symbol.groups already sorted;
wenzelm
parents: 50191
diff changeset
    54
    pages ++=
56753
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    55
      Symbol.groups.map({ case (group, symbols) =>
50151
5f5e74365f14 capitalize lowercase groups;
immler
parents: 50146
diff changeset
    56
        new TabbedPane.Page(group,
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    57
          new ScrollPane(new Wrap_Panel {
62024
e3e22a5e85f2 clarified meaning of \<^bold> action, depending on group;
wenzelm
parents: 59391
diff changeset
    58
            val control = group == "control"
e3e22a5e85f2 clarified meaning of \<^bold> action, depending on group;
wenzelm
parents: 59391
diff changeset
    59
            contents ++= symbols.map(new Symbol_Component(_, control))
e3e22a5e85f2 clarified meaning of \<^bold> action, depending on group;
wenzelm
parents: 59391
diff changeset
    60
            if (control) contents += new Reset_Component
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    61
          }), null)
56753
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    62
      })
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    63
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    64
    val search_field = new TextField(10)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    65
    val search_page =
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    66
      new TabbedPane.Page("search", new BorderPanel {
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    67
        val results_panel = new Wrap_Panel
59391
wenzelm
parents: 59063
diff changeset
    68
        layout(search_field) = BorderPanel.Position.North
wenzelm
parents: 59063
diff changeset
    69
        layout(new ScrollPane(results_panel)) = BorderPanel.Position.Center
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    70
  
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    71
        val search_space =
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    72
          (for (sym <- Symbol.names.keysIterator) yield (sym, Word.lowercase(sym))).toList
56769
wenzelm
parents: 56756
diff changeset
    73
        val search_delay =
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56769
diff changeset
    74
          GUI_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
56756
wenzelm
parents: 56755
diff changeset
    75
            val search_words = Word.explode(Word.lowercase(search_field.text))
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    76
            val search_limit = PIDE.options.int("jedit_symbols_search_limit") max 0
56756
wenzelm
parents: 56755
diff changeset
    77
            val results =
wenzelm
parents: 56755
diff changeset
    78
              for ((sym, s) <- search_space; if search_words.forall(s.contains(_))) yield sym
wenzelm
parents: 56755
diff changeset
    79
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    80
            results_panel.contents.clear
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    81
            for (sym <- results.take(search_limit))
62024
e3e22a5e85f2 clarified meaning of \<^bold> action, depending on group;
wenzelm
parents: 59391
diff changeset
    82
              results_panel.contents += new Symbol_Component(sym, false)
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    83
            if (results.length > search_limit)
56756
wenzelm
parents: 56755
diff changeset
    84
              results_panel.contents +=
wenzelm
parents: 56755
diff changeset
    85
                new Label("...") { tooltip = "(" + (results.length - search_limit) + " more)" }
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    86
            revalidate
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    87
            repaint
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    88
          }
56769
wenzelm
parents: 56756
diff changeset
    89
          search_field.reactions += { case ValueChanged(_) => search_delay.invoke() }
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    90
      }, "Search Symbols")
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    91
    pages += search_page
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    92
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    93
    listenTo(selection)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    94
    reactions += {
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    95
      case SelectionChanged(_) if selection.page == search_page => search_field.requestFocus
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    96
    }
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    97
56756
wenzelm
parents: 56755
diff changeset
    98
    for (page <- pages)
wenzelm
parents: 56755
diff changeset
    99
      page.title = Word.implode(Word.explode('_', page.title).map(Word.perhaps_capitalize(_)))
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
   100
  }
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
   101
  set_content(group_tabs)
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
   102
}