src/Tools/jEdit/src/jedit/StateViewDockable.scala
author wenzelm
Sun Oct 19 16:51:55 2008 +0200 (2008-10-19)
changeset 34318 c13e168a8ae6
child 34353 aa0d2f0bde83
permissions -rw-r--r--
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm@34318
     1
package isabelle.jedit
wenzelm@34318
     2
wenzelm@34318
     3
import java.io.{ ByteArrayInputStream, FileInputStream, InputStream }
wenzelm@34318
     4
wenzelm@34318
     5
import java.awt.GridLayout
wenzelm@34318
     6
import javax.swing.{ JPanel, JTextArea, JScrollPane }
wenzelm@34318
     7
wenzelm@34318
     8
import isabelle.IsabelleSystem.getenv
wenzelm@34318
     9
wenzelm@34318
    10
import org.xml.sax.InputSource;
wenzelm@34318
    11
wenzelm@34318
    12
import org.w3c.dom.Document
wenzelm@34318
    13
wenzelm@34318
    14
import org.xhtmlrenderer.simple.XHTMLPanel
wenzelm@34318
    15
import org.xhtmlrenderer.context.AWTFontResolver
wenzelm@34318
    16
import org.xhtmlrenderer.swing.NaiveUserAgent
wenzelm@34318
    17
import org.xhtmlrenderer.resource.CSSResource
wenzelm@34318
    18
wenzelm@34318
    19
import org.gjt.sp.jedit.View
wenzelm@34318
    20
wenzelm@34318
    21
object StateViewDockable {
wenzelm@34318
    22
  val baseURL = "file://localhost" + getenv("ISABELLE_HOME") + "/lib/html/"
wenzelm@34318
    23
  val userStylesheet = 
wenzelm@34318
    24
    "file://localhost" + getenv("ISABELLE_HOME_USER") + "/etc/user.css";
wenzelm@34318
    25
  val stylesheet = """
wenzelm@34318
    26
wenzelm@34318
    27
@import "isabelle.css";
wenzelm@34318
    28
wenzelm@34318
    29
@import '""" + userStylesheet + """';
wenzelm@34318
    30
wenzelm@34318
    31
messages, message {
wenzelm@34318
    32
  display: block;
wenzelm@34318
    33
  white-space: pre-wrap;
wenzelm@34318
    34
  font-family: Isabelle;
wenzelm@34318
    35
}
wenzelm@34318
    36
"""
wenzelm@34318
    37
}
wenzelm@34318
    38
wenzelm@34318
    39
class UserAgent extends NaiveUserAgent {
wenzelm@34318
    40
  override def getCSSResource(uri : String) : CSSResource = {
wenzelm@34318
    41
    if (uri == StateViewDockable.baseURL + "style")
wenzelm@34318
    42
      new CSSResource(
wenzelm@34318
    43
        new ByteArrayInputStream(StateViewDockable.stylesheet.getBytes()))
wenzelm@34318
    44
    else {
wenzelm@34318
    45
      val stream = resolveAndOpenStream(uri)
wenzelm@34318
    46
      val resource = new CSSResource(stream)
wenzelm@34318
    47
      if (stream == null)
wenzelm@34318
    48
        resource.getResourceInputSource().setByteStream(
wenzelm@34318
    49
          new ByteArrayInputStream(new Array[Byte](0)))
wenzelm@34318
    50
      resource
wenzelm@34318
    51
    }
wenzelm@34318
    52
  }
wenzelm@34318
    53
}
wenzelm@34318
    54
wenzelm@34318
    55
class StateViewDockable(view : View, position : String) extends JPanel {
wenzelm@34318
    56
  {
wenzelm@34318
    57
    val panel = new XHTMLPanel(new UserAgent())
wenzelm@34318
    58
    setLayout(new GridLayout(1, 1))
wenzelm@34318
    59
    add(new JScrollPane(panel))
wenzelm@34318
    60
    
wenzelm@34318
    61
    val fontResolver =
wenzelm@34318
    62
      panel.getSharedContext.getFontResolver.asInstanceOf[AWTFontResolver]
wenzelm@34318
    63
    if (Plugin.plugin.viewFont != null)
wenzelm@34318
    64
      fontResolver.setFontMapping("Isabelle", Plugin.plugin.viewFont)
wenzelm@34318
    65
wenzelm@34318
    66
    Plugin.plugin.viewFontChanged.add(font => {
wenzelm@34318
    67
      if (Plugin.plugin.viewFont != null)
wenzelm@34318
    68
        fontResolver.setFontMapping("Isabelle", Plugin.plugin.viewFont)
wenzelm@34318
    69
      
wenzelm@34318
    70
      panel.relayout()
wenzelm@34318
    71
    })
wenzelm@34318
    72
    
wenzelm@34318
    73
    Plugin.plugin.stateUpdate.add(state => {
wenzelm@34318
    74
      if (state == null)
wenzelm@34318
    75
        panel.setDocument(null : Document)
wenzelm@34318
    76
      else
wenzelm@34318
    77
        panel.setDocument(state.document, StateViewDockable.baseURL)
wenzelm@34318
    78
    })
wenzelm@34318
    79
  }
wenzelm@34318
    80
}