src/Tools/jEdit/src/documentation_dockable.scala
author wenzelm
Fri, 01 Sep 2017 15:15:29 +0200
changeset 66591 6efa351190d0
parent 66082 2d12a730a380
child 71525 d7b0d078266d
permissions -rw-r--r--
more robust: provide docking framework via base plugin;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/documentation_dockable.scala
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     3
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     4
Dockable window for Isabelle documentation.
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     5
*/
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     6
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     8
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
     9
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    10
import isabelle._
66591
6efa351190d0 more robust: provide docking framework via base plugin;
wenzelm
parents: 66082
diff changeset
    11
import isabelle.jedit_base.Dockable
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    12
60847
wenzelm
parents: 60292
diff changeset
    13
import java.awt.Dimension
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    14
import java.awt.event.{KeyEvent, KeyAdapter, MouseEvent, MouseAdapter}
60847
wenzelm
parents: 60292
diff changeset
    15
import javax.swing.{JTree, JScrollPane}
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    16
import javax.swing.tree.{DefaultMutableTreeNode, TreeSelectionModel}
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    17
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    18
import org.gjt.sp.jedit.{View, OperatingSystem}
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    19
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    20
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    21
class Documentation_Dockable(view: View, position: String) extends Dockable(view, position)
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    22
{
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    23
  private val docs = Doc.contents()
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    24
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 55877
diff changeset
    25
  private case class Documentation(name: String, title: String, path: Path)
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    26
  {
57912
wenzelm
parents: 56729
diff changeset
    27
    override def toString: String =
62113
16de2a9b5b3d tuned -- according to ML version;
wenzelm
parents: 61660
diff changeset
    28
      "<html><b>" + HTML.output(name) + "</b>:  " + HTML.output(title) + "</html>"
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    29
  }
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    30
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    31
  private case class Text_File(name: String, path: Path)
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    32
  {
57912
wenzelm
parents: 56729
diff changeset
    33
    override def toString: String = name
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    34
  }
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    35
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    36
  private val root = new DefaultMutableTreeNode
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    37
  docs foreach {
56423
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    38
    case Doc.Section(text, _) =>
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    39
      root.add(new DefaultMutableTreeNode(text))
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 55877
diff changeset
    40
    case Doc.Doc(name, title, path) =>
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    41
      root.getLastChild.asInstanceOf[DefaultMutableTreeNode]
56422
7490555d7dff clarified Doc entry: more explicit path;
wenzelm
parents: 55877
diff changeset
    42
        .add(new DefaultMutableTreeNode(Documentation(name, title, path)))
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    43
    case Doc.Text_File(name: String, path: Path) =>
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    44
      root.getLastChild.asInstanceOf[DefaultMutableTreeNode]
52542
19d674acb764 more release notes according to availability in proper release vs. repository clone;
wenzelm
parents: 52541
diff changeset
    45
        .add(new DefaultMutableTreeNode(Text_File(name, path.expand)))
52541
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    46
  }
97c950217d7f quick access to release notes (imitating website/documentation.html);
wenzelm
parents: 52447
diff changeset
    47
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    48
  private val tree = new JTree(root)
60292
ba3c716144dd cell-specific row height based on its font, e.g. relevant for DPI scaling on Windows;
wenzelm
parents: 59390
diff changeset
    49
  tree.setRowHeight(0)
60847
wenzelm
parents: 60292
diff changeset
    50
  tree.getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION)
54687
795f8d3e06c9 no keyboard control -- avoid confusion about meaning of selection;
wenzelm
parents: 54686
diff changeset
    51
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    52
  override def focusOnDefaultComponent { tree.requestFocusInWindow }
54687
795f8d3e06c9 no keyboard control -- avoid confusion about meaning of selection;
wenzelm
parents: 54686
diff changeset
    53
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    54
  private def action(node: DefaultMutableTreeNode)
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    55
  {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    56
    node.getUserObject match {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    57
      case Text_File(_, path) =>
66082
2d12a730a380 clarified modules;
wenzelm
parents: 65246
diff changeset
    58
        PIDE.editor.goto_file(true, view, File.platform_path(path))
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    59
      case Documentation(_, _, path) =>
66082
2d12a730a380 clarified modules;
wenzelm
parents: 65246
diff changeset
    60
        PIDE.editor.goto_doc(view, path)
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    61
      case _ =>
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    62
    }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    63
  }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    64
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    65
  tree.addKeyListener(new KeyAdapter {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    66
    override def keyPressed(e: KeyEvent)
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    67
    {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    68
      if (e.getKeyCode == KeyEvent.VK_ENTER) {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    69
        e.consume
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    70
        val path = tree.getSelectionPath
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    71
        if (path != null) {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    72
          path.getLastPathComponent match {
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    73
            case node: DefaultMutableTreeNode => action(node)
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    74
            case _ =>
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    75
          }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    76
        }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    77
      }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    78
    }
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    79
  })
54686
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    80
  tree.addMouseListener(new MouseAdapter {
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    81
    override def mouseClicked(e: MouseEvent)
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    82
    {
54686
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    83
      val click = tree.getPathForLocation(e.getX, e.getY)
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    84
      if (click != null && e.getClickCount == 1) {
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    85
        (click.getLastPathComponent, tree.getLastSelectedPathComponent) match {
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    86
          case (node: DefaultMutableTreeNode, node1: DefaultMutableTreeNode) if node == node1 =>
57920
c1953856cfca clarified focus and key handling -- more like SideKick;
wenzelm
parents: 57912
diff changeset
    87
            action(node)
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    88
          case _ =>
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    89
        }
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
    90
      }
54686
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    91
    }
070d5e856798 directly react on click, assuming that document view operation is mostly idempotent;
wenzelm
parents: 53177
diff changeset
    92
  })
56423
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    93
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    94
  {
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    95
    var expand = true
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    96
    var visible = 0
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    97
    def make_visible(row: Int) { visible += 1; tree.expandRow(row) }
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    98
    for ((entry, row) <- docs.zipWithIndex) {
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
    99
      entry match {
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   100
        case Doc.Section(_, important) =>
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   101
          expand = important
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   102
          make_visible(row)
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   103
        case _ =>
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   104
          if (expand) make_visible(row)
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   105
      }
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   106
    }
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   107
    tree.setRootVisible(false)
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   108
    tree.setVisibleRowCount(visible)
c2f52824dbb2 explicit indication of important doc sections ("!"), which are expanded in the tree view;
wenzelm
parents: 56422
diff changeset
   109
  }
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
   110
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
   111
  private val tree_view = new JScrollPane(tree)
60849
wenzelm
parents: 60847
diff changeset
   112
  tree_view.setMinimumSize(new Dimension(200, 50))
52445
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
   113
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
   114
  set_content(tree_view)
18a720984855 dockable window for Isabelle documentation;
wenzelm
parents:
diff changeset
   115
}