src/Tools/jEdit/src/jedit/TheoryView.scala
changeset 34406 f81cd75ae331
parent 34405 a67a4eaebcff
child 34407 aad6834ba380
equal deleted inserted replaced
34405:a67a4eaebcff 34406:f81cd75ae331
    16 import org.gjt.sp.jedit.buffer.{ BufferListener, JEditBuffer }
    16 import org.gjt.sp.jedit.buffer.{ BufferListener, JEditBuffer }
    17 import org.gjt.sp.jedit.textarea.{ JEditTextArea, TextAreaExtension, TextAreaPainter }
    17 import org.gjt.sp.jedit.textarea.{ JEditTextArea, TextAreaExtension, TextAreaPainter }
    18 import org.gjt.sp.jedit.syntax.SyntaxStyle
    18 import org.gjt.sp.jedit.syntax.SyntaxStyle
    19 
    19 
    20 object TheoryView {
    20 object TheoryView {
    21   val ISABELLE_THEORY_PROPERTY = "de.tum.in.isabelle.jedit.Theory";
       
    22 
    21 
    23   def chooseColor(state : Command) : Color = {
    22   def chooseColor(state : Command) : Color = {
    24     if (state == null)
    23     if (state == null)
    25       Color.red
    24       Color.red
    26     else
    25     else
    56       case "doc_source"  | "ML_source" | "ML_antic" | "doc_antiq" | "antiq"
    55       case "doc_source"  | "ML_source" | "ML_antic" | "doc_antiq" | "antiq"
    57         => new Color(0, 192, 0)
    56         => new Color(0, 192, 0)
    58       case _ => Color.white
    57       case _ => Color.white
    59     }
    58     }
    60   }
    59   }
    61   
       
    62   def withView(view : JEditTextArea, f : (TheoryView) => Unit) {
       
    63     if (view != null && view.getBuffer() != null)
       
    64       view.getBuffer().getProperty(ISABELLE_THEORY_PROPERTY) match {
       
    65         case null => null
       
    66         case view: TheoryView => f(view)
       
    67         case _ => null
       
    68       }
       
    69   }
       
    70 	
       
    71   def activateTextArea(textArea : JEditTextArea) {
       
    72     withView(textArea, _.activate(textArea))
       
    73   }	
       
    74 	
       
    75   def deactivateTextArea(textArea : JEditTextArea) {
       
    76     withView(textArea, _.deactivate(textArea))
       
    77   }
       
    78 }
    60 }
    79 
    61 
    80 class TheoryView(prover : Prover, val buffer : JEditBuffer)
    62 class TheoryView (text_area : JEditTextArea)
    81     extends TextAreaExtension with Text with BufferListener {
    63     extends TextAreaExtension with Text with BufferListener {
    82   import TheoryView._
    64   import TheoryView._
    83   import Text.Changed
    65   import Text.Changed
    84   
    66 
    85   var textArea : JEditTextArea = null;
    67   var col : Changed = null
    86   var col : Changed = null;
    68 
       
    69   val buffer = text_area.getBuffer
       
    70   buffer.addBufferListener(this)
    87   
    71   
    88   val colTimer = new Timer(300, new ActionListener() {
    72   val colTimer = new Timer(300, new ActionListener() {
    89     override def actionPerformed(e : ActionEvent) { commit() }
    73     override def actionPerformed(e : ActionEvent) { commit() }
    90   })
    74   })
    91   
    75   
    92   val changesSource = new EventSource[Changed]
    76   val changesSource = new EventSource[Changed]
    93 
    77   val phase_overview = new PhaseOverviewPanel(Plugin.prover(buffer))
    94   {
       
    95     buffer.addBufferListener(this)
       
    96     buffer.setProperty(ISABELLE_THEORY_PROPERTY, this)
       
    97 
    78 
    98     val repaint_delay = new isabelle.utils.Delay(100, () => repaintAll())
    79     val repaint_delay = new isabelle.utils.Delay(100, () => repaintAll())
    99     prover.commandInfo.add(_ => repaint_delay.delay_or_ignore())
    80     Plugin.prover(buffer).commandInfo.add(_ => repaint_delay.delay_or_ignore())
   100     // could also use this:
       
   101     // prover.commandInfo.add(c => repaint(c.command))
       
   102     Plugin.plugin.viewFontChanged.add(font => updateFont())
    81     Plugin.plugin.viewFontChanged.add(font => updateFont())
   103     
    82 
   104     colTimer.stop
    83     colTimer.stop
   105     colTimer.setRepeats(true)
    84     colTimer.setRepeats(true)
   106   }
    85 
   107 
    86   def activate() {
   108   def activate(area : JEditTextArea) {
    87     text_area.addCaretListener(selectedStateController)
   109     textArea = area
    88     phase_overview.textarea = text_area
   110     textArea.addCaretListener(selectedStateController)
    89     text_area.addLeftOfScrollBar(phase_overview)
   111     textArea.addLeftOfScrollBar(new PhaseOverviewPanel(textArea))
    90     val painter = text_area.getPainter()
   112     val painter = textArea.getPainter()
       
   113     painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, this)
    91     painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, this)
   114     updateFont()
    92     updateFont()
   115   }
    93   }
   116   
    94   
   117   private def updateFont() {
    95   private def updateFont() {
   118     if (textArea != null) {
    96     if (text_area != null) {
   119       val painter = textArea.getPainter()
    97       val painter = text_area.getPainter()
   120       if (Plugin.plugin.viewFont != null) {
    98       if (Plugin.plugin.viewFont != null) {
   121         painter.setStyles(painter.getStyles().map(style =>
    99         painter.setStyles(painter.getStyles().map(style =>
   122           new SyntaxStyle(style.getForegroundColor, 
   100           new SyntaxStyle(style.getForegroundColor, 
   123                           style.getBackgroundColor, 
   101                           style.getBackgroundColor, 
   124                           Plugin.plugin.viewFont)
   102                           Plugin.plugin.viewFont)
   127         repaintAll()
   105         repaintAll()
   128       }
   106       }
   129     }
   107     }
   130   }
   108   }
   131   
   109   
   132   def deactivate(area : JEditTextArea) {
   110   def deactivate() {
   133     textArea.getPainter().removeExtension(this)
   111     text_area.getPainter().removeExtension(this)
   134     textArea.removeCaretListener(selectedStateController)
   112     text_area.removeCaretListener(selectedStateController)
   135     textArea = null
   113     text_area.removeLeftOfScrollBar(phase_overview)
   136   }
   114   }
   137   
   115   
   138   val selectedStateController = new CaretListener() {
   116   val selectedStateController = new CaretListener() {
   139     override def caretUpdate(e : CaretEvent) {
   117     override def caretUpdate(e : CaretEvent) {
   140       val cmd = prover.document.getNextCommandContaining(e.getDot())
   118       val cmd = Plugin.prover(buffer).document.getNextCommandContaining(e.getDot())
   141       if (cmd != null && cmd.start <= e.getDot() && 
   119       if (cmd != null && cmd.start <= e.getDot() && 
   142             Plugin.plugin.selectedState != cmd)
   120             Plugin.prover_setup(buffer).selectedState != cmd)
   143         Plugin.plugin.selectedState = cmd
   121         Plugin.prover_setup(buffer).selectedState = cmd
   144     }
   122     }
   145   }
   123   }
   146 
   124 
   147   private def fromCurrent(pos : Int) =
   125   private def fromCurrent(pos : Int) =
   148     if (col != null && col.start <= pos)
   126     if (col != null && col.start <= pos)
   157     else pos
   135     else pos
   158   
   136   
   159   def repaint(cmd : Command)
   137   def repaint(cmd : Command)
   160   {
   138   {
   161     val ph = cmd.phase
   139     val ph = cmd.phase
   162     if (textArea != null && ph != Phase.REMOVE && ph != Phase.REMOVED) {
   140     if (text_area != null && ph != Phase.REMOVE && ph != Phase.REMOVED) {
   163       var start = textArea.getLineOfOffset(toCurrent(cmd.start))
   141       var start = text_area.getLineOfOffset(toCurrent(cmd.start))
   164       var stop = textArea.getLineOfOffset(toCurrent(cmd.stop) - 1)
   142       var stop = text_area.getLineOfOffset(toCurrent(cmd.stop) - 1)
   165       textArea.invalidateLineRange(start, stop)
   143       text_area.invalidateLineRange(start, stop)
   166       
   144       
   167       if (Plugin.plugin.selectedState == cmd)
   145       if (Plugin.prover_setup(buffer).selectedState == cmd)
   168         Plugin.plugin.selectedState = cmd // update State view 
   146         Plugin.prover_setup(buffer).selectedState = cmd // update State view
   169     }
   147     }
   170   }
   148   }
   171   
   149   
   172   def repaintAll()
   150   def repaintAll()
   173   {
   151   {
   174     if (textArea != null)
   152     if (text_area != null)
   175       textArea.invalidateLineRange(textArea.getFirstPhysicalLine, 
   153       text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
   176                                    textArea.getLastPhysicalLine)
   154                                    text_area.getLastPhysicalLine)
   177   }
   155   }
   178 
   156 
   179   def encolor(gfx : Graphics2D, y : Int, height : Int, begin : Int, finish : Int, color : Color, fill : Boolean){
   157   def encolor(gfx : Graphics2D, y : Int, height : Int, begin : Int, finish : Int, color : Color, fill : Boolean){
   180       val fm = textArea.getPainter.getFontMetrics
   158       val fm = text_area.getPainter.getFontMetrics
   181       val startP = textArea.offsetToXY(begin)
   159       val startP = text_area.offsetToXY(begin)
   182       val stopP = if (finish < buffer.getLength()) textArea.offsetToXY(finish)
   160       val stopP = if (finish < buffer.getLength()) text_area.offsetToXY(finish)
   183                   else { var p = textArea.offsetToXY(finish - 1)
   161                   else { var p = text_area.offsetToXY(finish - 1)
   184                          p.x = p.x + fm.charWidth(' ')
   162                          p.x = p.x + fm.charWidth(' ')
   185                          p }
   163                          p }
   186 			
   164 			
   187       if (startP != null && stopP != null) {
   165       if (startP != null && stopP != null) {
   188         gfx.setColor(color)
   166         gfx.setColor(color)
   192   }
   170   }
   193 
   171 
   194   override def paintValidLine(gfx : Graphics2D, screenLine : Int,
   172   override def paintValidLine(gfx : Graphics2D, screenLine : Int,
   195                               pl : Int, start : Int, end : Int, y : Int)
   173                               pl : Int, start : Int, end : Int, y : Int)
   196   {	
   174   {	
   197     val fm = textArea.getPainter.getFontMetrics
   175     val fm = text_area.getPainter.getFontMetrics
   198     var savedColor = gfx.getColor
   176     var savedColor = gfx.getColor
   199     var e = prover.document.getNextCommandContaining(fromCurrent(start))
   177     var e = Plugin.prover(buffer).document.getNextCommandContaining(fromCurrent(start))
   200 
   178 
   201     //Encolor Phase
   179     //Encolor Phase
   202     while (e != null && toCurrent(e.start) < end) {
   180     while (e != null && toCurrent(e.start) < end) {
   203       val begin =  start max toCurrent(e.start)
   181       val begin =  start max toCurrent(e.start)
   204       val finish = end - 1 min  toCurrent(e.stop)
   182       val finish = end - 1 min  toCurrent(e.stop)
   215 
   193 
   216     gfx.setColor(savedColor)
   194     gfx.setColor(savedColor)
   217   }
   195   }
   218 	
   196 	
   219   def content(start : Int, stop : Int) = buffer.getText(start, stop - start)
   197   def content(start : Int, stop : Int) = buffer.getText(start, stop - start)
   220   def length = buffer.getLength()
   198   override def length = buffer.getLength
   221 
   199 
   222   def changes = changesSource
   200   def changes = changesSource
   223 
   201 
   224   private def commit() {
   202   private def commit() {
   225     if (col != null)
   203     if (col != null)