src/Tools/jEdit/src/status_widget.scala
author wenzelm
Fri, 15 Nov 2024 13:31:36 +0100
changeset 81448 9b2e13b3ee43
parent 81443 7f3416f35b5d
permissions -rw-r--r--
more rebust mechanics of refresh (see also 82110cbcf9a1 and 2d9b6e32632d): painter.getFontRenderContext might be in undefined state (notably on macOS due to display scaling);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72248
71378e7d148e clarified modules;
wenzelm
parents: 72144
diff changeset
     1
/*  Title:      Tools/jEdit/src/status_widget.scala
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     3
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     4
ML status bar: heap and garbage collection.
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     5
*/
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     6
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     8
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
     9
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    10
import isabelle._
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    11
import java.awt.{Color, Dimension, Graphics, Graphics2D, Insets, RenderingHints}
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    12
import java.awt.font.FontRenderContext
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    13
import java.awt.event.{ActionEvent, ActionListener, MouseAdapter, MouseEvent}
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    14
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    15
import javax.swing.{JComponent, JLabel, Timer}
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    16
import org.gjt.sp.jedit.{View, jEdit}
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    17
import org.gjt.sp.jedit.gui.statusbar.{StatusWidgetFactory, Widget}
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    18
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    20
object Status_Widget {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    21
  /** generic GUI **/
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    22
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    23
  private val template = "ABC: 99999/99999MB"
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    24
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    25
  abstract class GUI(view: View) extends JComponent {
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    26
    /* init */
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    27
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    28
    setFont(new JLabel().getFont)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    29
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    30
    private val font_render_context = new FontRenderContext(null, true, false)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    31
    private val line_metrics = getFont.getLineMetrics(template, font_render_context)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    32
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    33
    {
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    34
      val bounds = getFont.getStringBounds(template, font_render_context)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    35
      val dim = new Dimension(bounds.getWidth.toInt, bounds.getHeight.toInt)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    36
      setPreferredSize(dim)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    37
      setMaximumSize(dim)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    38
    }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    39
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    40
    setForeground(jEdit.getColorProperty("view.status.foreground"))
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    41
    setBackground(jEdit.getColorProperty("view.status.background"))
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    42
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    43
    def progress_foreground: Color = jEdit.getColorProperty("view.status.memory.foreground")
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    44
    def progress_background: Color = jEdit.getColorProperty("view.status.memory.background")
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    45
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    46
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    47
    /* paint */
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    48
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    49
    def get_status: (String, Double)
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    50
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    51
    override def paintComponent(gfx: Graphics): Unit = {
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    52
      super.paintComponent(gfx)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    53
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    54
      val insets = new Insets(0, 0, 0, 0)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    55
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    56
      val width = getWidth - insets.left - insets.right
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    57
      val height = getHeight - insets.top - insets.bottom - 1
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    58
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    59
      val (text, fraction) = get_status
72142
wenzelm
parents: 72141
diff changeset
    60
      val width2 = (width * fraction).toInt
72140
ae6544cf1c8c show GC progress as "ML cleanup";
wenzelm
parents: 72139
diff changeset
    61
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    62
      val text_bounds = gfx.getFont.getStringBounds(text, font_render_context)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    63
      val text_x = insets.left + ((width - text_bounds.getWidth).toInt / 2)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    64
      val text_y = (insets.top + line_metrics.getAscent).toInt
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    65
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    66
      gfx.asInstanceOf[Graphics2D].
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    67
        setRenderingHint(
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    68
          RenderingHints.KEY_TEXT_ANTIALIASING,
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    69
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    70
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    71
      gfx.setColor(progress_background)
72142
wenzelm
parents: 72141
diff changeset
    72
      gfx.fillRect(insets.left, insets.top, width2, height)
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    73
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    74
      gfx.setColor(progress_foreground)
72142
wenzelm
parents: 72141
diff changeset
    75
      gfx.setClip(insets.left, insets.top, width2, height)
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    76
      gfx.drawString(text, text_x, text_y)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    77
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    78
      gfx.setColor(getForeground)
72142
wenzelm
parents: 72141
diff changeset
    79
      gfx.setClip(insets.left + width2, insets.top, getWidth - insets.left - width2, height)
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    80
      gfx.drawString(text, text_x, text_y)
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    81
    }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    82
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
    83
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    84
    /* mouse listener */
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    85
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    86
    def action: String
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    87
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    88
    addMouseListener(new MouseAdapter {
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    89
      override def mouseClicked(evt: MouseEvent): Unit = {
81443
7f3416f35b5d more careful isConsumed() / consume() for key and mouse events;
wenzelm
parents: 77708
diff changeset
    90
        if (!evt.isConsumed() && evt.getClickCount == 2) {
7f3416f35b5d more careful isConsumed() / consume() for key and mouse events;
wenzelm
parents: 77708
diff changeset
    91
          evt.consume()
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    92
          view.getInputHandler.invokeAction(action)
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    93
        }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    94
      }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    95
    })
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    96
  }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    97
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    98
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
    99
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   100
  /** Java **/
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   101
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   102
  class Java_GUI(view: View) extends GUI(view) {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   103
    /* component state -- owned by GUI thread */
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   104
72250
13976f92a2d0 clarified modules;
wenzelm
parents: 72249
diff changeset
   105
    private var status = Java_Statistics.memory_status()
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   106
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   107
    def get_status: (String, Double) = {
77708
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 75393
diff changeset
   108
      val text = "JVM: " + status.heap_used.MiB.round + "/" + status.heap_size.MiB.round + "MiB"
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 75393
diff changeset
   109
      (text, status.heap_used_fraction)
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   110
    }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   111
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   112
    private def update_status(new_status: Java_Statistics.Memory_Status): Unit = {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   113
      if (status != new_status) {
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   114
        status = new_status
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   115
        repaint()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   116
      }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   117
    }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   118
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   119
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   120
    /* timer */
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   121
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   122
    private val timer =
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   123
      new Timer(500, new ActionListener {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72976
diff changeset
   124
        override def actionPerformed(e: ActionEvent): Unit =
72250
13976f92a2d0 clarified modules;
wenzelm
parents: 72249
diff changeset
   125
          update_status(Java_Statistics.memory_status())
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   126
      })
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   127
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   128
    override def addNotify(): Unit = {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   129
      super.addNotify()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   130
      timer.start()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   131
    }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   132
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   133
    override def removeNotify(): Unit = {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   134
      super.removeNotify()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   135
      timer.stop()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   136
    }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   137
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   138
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   139
    /* action */
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   140
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   141
    setToolTipText("Java heap memory (double-click for monitor application)")
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   142
72976
51442c6dc296 more robust Java monitor: avoid odd warning about insecure connection;
wenzelm
parents: 72250
diff changeset
   143
    override def action: String = "isabelle.java-monitor"
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   144
  }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   145
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   146
  class Java_Factory extends StatusWidgetFactory {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   147
    override def getWidget(view: View): Widget =
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   148
      new Widget { override def getComponent: JComponent = new Java_GUI(view) }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   149
  }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   150
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   151
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   152
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   153
  /** ML **/
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   154
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   155
  class ML_GUI(view: View) extends GUI(view) {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   156
    /* component state -- owned by GUI thread */
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   157
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   158
    private var status = ML_Statistics.memory_status(Nil)
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   159
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   160
    def get_status: (String, Double) =
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   161
      status.gc_progress match {
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   162
        case Some(p) => ("ML cleanup", 1.0 - p)
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   163
        case None =>
77708
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 75393
diff changeset
   164
          val text = "ML: " + status.heap_used.MiB.round + "/" + status.heap_size.MiB.round + "MiB"
f137bf5d3d94 clarified signature: more explicit types;
wenzelm
parents: 75393
diff changeset
   165
          (text, status.heap_used_fraction)
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   166
      }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   167
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   168
    private def update_status(new_status: ML_Statistics.Memory_Status): Unit = {
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   169
      if (status != new_status) {
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   170
        status = new_status
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   171
        repaint()
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   172
      }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   173
    }
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   174
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   175
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   176
    /* main */
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   177
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   178
    private val main =
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   179
      Session.Consumer[Session.Runtime_Statistics](getClass.getName) {
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   180
        case stats =>
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   181
          val status = ML_Statistics.memory_status(stats.props)
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   182
          GUI_Thread.later { update_status(status) }
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   183
      }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   184
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   185
    override def addNotify(): Unit = {
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   186
      super.addNotify()
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   187
      PIDE.session.runtime_statistics += main
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   188
    }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   189
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   190
    override def removeNotify(): Unit = {
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   191
      super.removeNotify()
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   192
      PIDE.session.runtime_statistics -= main
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   193
    }
72141
262d3c11a24d clarified GUI;
wenzelm
parents: 72140
diff changeset
   194
262d3c11a24d clarified GUI;
wenzelm
parents: 72140
diff changeset
   195
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   196
    /* action */
72141
262d3c11a24d clarified GUI;
wenzelm
parents: 72140
diff changeset
   197
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   198
    setToolTipText("ML heap memory (double-click for monitor panel)")
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   199
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   200
    override def action: String = "isabelle-monitor-float"
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   201
  }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   202
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   203
  class ML_Factory extends StatusWidgetFactory {
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   204
    override def getWidget(view: View): Widget =
72249
4bf8a8a2d2ad more uniform JVM vs. ML status widget;
wenzelm
parents: 72248
diff changeset
   205
      new Widget { override def getComponent: JComponent = new ML_GUI(view) }
72139
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   206
  }
f5c085dfa02f ML status widget similar to org.gjt.sp.jedit.gui.statusbar.MemoryStatusWidgetFactory;
wenzelm
parents:
diff changeset
   207
}