clarified focus and key handling -- more like SideKick;
authorwenzelm
Tue, 12 Aug 2014 21:29:50 +0200
changeset 57920 c1953856cfca
parent 57919 a2a7c1de4752
child 57921 9225b2761126
child 57923 cdae2467311d
clarified focus and key handling -- more like SideKick; avoid resetting input map with its potentially confusion propagation of key events to unrelated components, e.g. main text area or tree scrollbar;
src/Tools/jEdit/src/documentation_dockable.scala
--- a/src/Tools/jEdit/src/documentation_dockable.scala	Tue Aug 12 20:42:39 2014 +0200
+++ b/src/Tools/jEdit/src/documentation_dockable.scala	Tue Aug 12 21:29:50 2014 +0200
@@ -10,7 +10,7 @@
 import isabelle._
 
 import java.awt.{Dimension, GridLayout}
-import java.awt.event.{MouseEvent, MouseAdapter}
+import java.awt.event.{KeyEvent, KeyAdapter, MouseEvent, MouseAdapter}
 import javax.swing.{JTree, JScrollPane, JComponent}
 import javax.swing.tree.{DefaultMutableTreeNode, TreeSelectionModel}
 import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener}
@@ -47,38 +47,57 @@
 
   private val tree = new JTree(root)
 
-  for (cond <-
-    List(JComponent.WHEN_FOCUSED,
-      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
-      JComponent.WHEN_IN_FOCUSED_WINDOW)) tree.setInputMap(cond, null)
+  override def focusOnDefaultComponent { tree.requestFocusInWindow }
 
   if (!OperatingSystem.isMacOSLF)
     tree.putClientProperty("JTree.lineStyle", "Angled")
   tree.getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION)
+
+  private def action(node: DefaultMutableTreeNode)
+  {
+    node.getUserObject match {
+      case Text_File(_, path) =>
+        PIDE.editor.goto_file(view, Isabelle_System.platform_path(path))
+      case Documentation(_, _, path) =>
+        if (path.is_file)
+          PIDE.editor.goto_file(view, Isabelle_System.platform_path(path))
+        else {
+          Future.fork {
+            try { Doc.view(path) }
+            catch {
+              case exn: Throwable =>
+                GUI.error_dialog(view,
+                  "Documentation error", GUI.scrollable_text(Exn.message(exn)))
+            }
+          }
+        }
+      case _ =>
+    }
+  }
+
+  tree.addKeyListener(new KeyAdapter {
+    override def keyPressed(e: KeyEvent)
+    {
+      if (e.getKeyCode == KeyEvent.VK_ENTER) {
+        e.consume
+        val path = tree.getSelectionPath
+        if (path != null) {
+          path.getLastPathComponent match {
+            case node: DefaultMutableTreeNode => action(node)
+            case _ =>
+          }
+        }
+      }
+    }
+  })
   tree.addMouseListener(new MouseAdapter {
-    override def mouseClicked(e: MouseEvent) {
+    override def mouseClicked(e: MouseEvent)
+    {
       val click = tree.getPathForLocation(e.getX, e.getY)
       if (click != null && e.getClickCount == 1) {
         (click.getLastPathComponent, tree.getLastSelectedPathComponent) match {
           case (node: DefaultMutableTreeNode, node1: DefaultMutableTreeNode) if node == node1 =>
-            node.getUserObject match {
-              case Text_File(_, path) =>
-                PIDE.editor.goto_file(view, Isabelle_System.platform_path(path))
-              case Documentation(_, _, path) =>
-                if (path.is_file)
-                  PIDE.editor.goto_file(view, Isabelle_System.platform_path(path))
-                else {
-                  Future.fork {
-                    try { Doc.view(path) }
-                    catch {
-                      case exn: Throwable =>
-                        GUI.error_dialog(view,
-                          "Documentation error", GUI.scrollable_text(Exn.message(exn)))
-                    }
-                  }
-                }
-              case _ =>
-            }
+            action(node)
           case _ =>
         }
       }