src/Tools/jEdit/src/hyperlink.scala
author wenzelm
Fri, 24 Aug 2012 16:45:55 +0200
changeset 48921 5d8d409b897e
child 48923 a2df77fcf1eb
permissions -rw-r--r--
support for direct hyperlinks, without the Hyperlinks plugin;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48921
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/hyperlink.scala
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Munich
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     4
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     5
Hyperlinks within jEdit buffers for PIDE proof documents.
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     6
*/
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     7
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
     9
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    10
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    11
import isabelle._
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    12
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    13
import java.io.{File => JFile}
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    14
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    15
import org.gjt.sp.jedit.{View, jEdit}
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    16
import org.gjt.sp.jedit.textarea.JEditTextArea
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    17
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    18
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    19
object Hyperlink
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    20
{
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    21
  def apply(file: JFile, line: Int, column: Int): Hyperlink =
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    22
    File_Link(file, line, column)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    23
}
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    24
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    25
abstract class Hyperlink
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    26
{
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    27
  def follow(view: View): Unit
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    28
}
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    29
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    30
private case class File_Link(file: JFile, line: Int, column: Int) extends Hyperlink
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    31
{
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    32
  override def follow(view: View)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    33
  {
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    34
    Swing_Thread.require()
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    35
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    36
    val full_name = file.getCanonicalPath
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    37
    val base_name = file.getName
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    38
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    39
    Isabelle.jedit_buffer(full_name) match {
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    40
      case Some(buffer) =>
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    41
        view.setBuffer(buffer)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    42
        val text_area = view.getTextArea
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    43
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    44
        try {
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    45
          val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    46
          text_area.moveCaretPosition(line_start)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    47
          if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    48
        }
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    49
        catch {
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    50
          case _: ArrayIndexOutOfBoundsException =>
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    51
          case _: IllegalArgumentException =>
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    52
        }
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    53
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    54
      case None =>
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    55
        val args =
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    56
          if (line <= 0) Array(base_name)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    57
          else if (column <= 0) Array(base_name, "+line:" + line.toInt)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    58
          else Array(base_name, "+line:" + line.toInt + "," + column.toInt)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    59
        jEdit.openFiles(view, file.getParent, args)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    60
    }
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    61
  }
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    62
}
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents:
diff changeset
    63