src/Tools/jEdit/src/jedit_options.scala
author wenzelm
Thu, 21 Jan 2016 20:50:34 +0100
changeset 62227 6eeaaefcea56
parent 61742 fd3b214b0979
child 65236 4fa82bbb394e
permissions -rw-r--r--
clarified errors: more explicit treatment of uninitialized state;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_options.scala
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     3
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     4
Options for Isabelle/jEdit.
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     5
*/
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     6
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     8
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     9
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    10
import isabelle._
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    11
61607
a99125aa964f prefer static Font -- evade spontaneous change of TextField.font seen with Metal L&F in Plugin Options / Isabelle / General / Apply;
wenzelm
parents: 60844
diff changeset
    12
import java.awt.{Font, Color}
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    13
import javax.swing.{InputVerifier, JComponent, UIManager}
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    14
import javax.swing.text.JTextComponent
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    15
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    16
import scala.swing.{Component, CheckBox, TextArea}
60843
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    17
import scala.swing.event.ButtonClicked
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    18
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    19
import org.gjt.sp.jedit.gui.ColorWellButton
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    20
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    21
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    22
trait Option_Component extends Component
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    23
{
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    24
  val title: String
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    25
  def load(): Unit
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    26
  def save(): Unit
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    27
}
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    28
52065
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51616
diff changeset
    29
object JEdit_Options
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51616
diff changeset
    30
{
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51616
diff changeset
    31
  val RENDERING_SECTION = "Rendering of Document Content"
60843
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    32
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    33
  class Check_Box(name: String, label: String, description: String) extends CheckBox(label)
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    34
  {
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    35
    tooltip = description
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    36
    reactions += { case ButtonClicked(_) => update(selected) }
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    37
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    38
    def stored: Boolean = PIDE.options.bool(name)
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    39
    def update(b: Boolean): Unit =
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    40
      GUI_Thread.require {
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    41
        if (selected != b) selected = b
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    42
        if (stored != b) {
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    43
          PIDE.options.bool(name) = b
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    44
          PIDE.session.update_options(PIDE.options.value)
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    45
        }
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    46
      }
60844
f7f2bc0e4293 proper initialization;
wenzelm
parents: 60843
diff changeset
    47
    def load() { selected = stored }
f7f2bc0e4293 proper initialization;
wenzelm
parents: 60843
diff changeset
    48
    load()
60843
9980f3bcdea2 tuned signature;
wenzelm
parents: 57612
diff changeset
    49
  }
52065
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51616
diff changeset
    50
}
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51616
diff changeset
    51
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    52
class JEdit_Options extends Options_Variable
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    53
{
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49317
diff changeset
    54
  def color_value(s: String): Color = Color_Value(string(s))
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49317
diff changeset
    55
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    56
  def make_color_component(opt: Options.Opt): Option_Component =
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    57
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56752
diff changeset
    58
    GUI_Thread.require {}
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    59
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    60
    val opt_name = opt.name
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    61
    val opt_title = opt.title("jedit")
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    62
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    63
    val button = new ColorWellButton(Color_Value(opt.value))
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    64
    val component = new Component with Option_Component {
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    65
      override lazy val peer = button
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    66
      name = opt_name
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    67
      val title = opt_title
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    68
      def load = button.setSelectedColor(Color_Value(string(opt_name)))
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    69
      def save = string(opt_name) = Color_Value.print(button.getSelectedColor)
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    70
    }
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56611
diff changeset
    71
    component.tooltip = GUI.tooltip_lines(opt.print_default)
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    72
    component
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    73
  }
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    74
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    75
  def make_component(opt: Options.Opt): Option_Component =
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    76
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56752
diff changeset
    77
    GUI_Thread.require {}
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    78
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    79
    val opt_name = opt.name
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    80
    val opt_title = opt.title("jedit")
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    81
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    82
    val component =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    83
      if (opt.typ == Options.Bool)
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    84
        new CheckBox with Option_Component {
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    85
          name = opt_name
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    86
          val title = opt_title
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    87
          def load = selected = bool(opt_name)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    88
          def save = bool(opt_name) = selected
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    89
        }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    90
      else {
61742
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61607
diff changeset
    91
        val default_font = GUI.copy_font(UIManager.getFont("TextField.font"))
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    92
        val text_area =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    93
          new TextArea with Option_Component {
61742
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61607
diff changeset
    94
            if (default_font != null) font = default_font
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    95
            name = opt_name
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    96
            val title = opt_title
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    97
            def load = text = value.check_name(opt_name).value
49317
5eff42e69edb tuned error;
wenzelm
parents: 49296
diff changeset
    98
            def save =
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61742
diff changeset
    99
              try { store(value + (opt_name, text)) }
49317
5eff42e69edb tuned error;
wenzelm
parents: 49296
diff changeset
   100
              catch {
5eff42e69edb tuned error;
wenzelm
parents: 49296
diff changeset
   101
                case ERROR(msg) =>
51616
949e2cf02a3d tuned signature -- concentrate GUI tools;
wenzelm
parents: 49701
diff changeset
   102
                  GUI.error_dialog(this.peer, "Failed to update options",
949e2cf02a3d tuned signature -- concentrate GUI tools;
wenzelm
parents: 49701
diff changeset
   103
                    GUI.scrollable_text(msg))
49317
5eff42e69edb tuned error;
wenzelm
parents: 49296
diff changeset
   104
              }
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   105
          }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   106
        text_area.peer.setInputVerifier(new InputVerifier {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   107
          def verify(jcomponent: JComponent): Boolean =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   108
            jcomponent match {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   109
              case text: JTextComponent =>
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   110
                try { value + (opt_name, text.getText); true }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   111
                catch { case ERROR(_) => false }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   112
              case _ => true
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   113
            }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   114
          })
56752
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56662
diff changeset
   115
        GUI.plain_focus_traversal(text_area.peer)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   116
        text_area
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   117
      }
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   118
    component.load()
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56611
diff changeset
   119
    component.tooltip = GUI.tooltip_lines(opt.print_default)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   120
    component
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   121
  }
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   122
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   123
  def make_components(predefined: List[Option_Component], filter: String => Boolean)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   124
    : List[(String, List[Option_Component])] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   125
  {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   126
    def mk_component(opt: Options.Opt): List[Option_Component] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   127
      predefined.find(opt.name == _.name) match {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   128
        case Some(c) => List(c)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   129
        case None => if (filter(opt.name)) List(make_component(opt)) else Nil
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   130
      }
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   131
    value.sections.sortBy(_._1).map(
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   132
        { case (a, opts) => (a, opts.sortBy(_.title("jedit")).flatMap(mk_component _)) })
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   133
      .filterNot(_._2.isEmpty)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   134
  }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
   135
}
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
   136