changeset 1877:b30e4b1966a0 default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 05 Apr 2025 22:50:03 -0600
parents d74dca32fe7b
children
files src/luan/modules/swing/TextAreaLuan.java src/luan/modules/swing/Text_area.luan
diffstat 2 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/swing/TextAreaLuan.java	Sat Apr 05 19:22:51 2025 -0600
+++ b/src/luan/modules/swing/TextAreaLuan.java	Sat Apr 05 22:50:03 2025 -0600
@@ -1,6 +1,8 @@
 package luan.modules.swing;
 
 import java.awt.Rectangle;
+import java.awt.Graphics;
+import java.awt.Color;
 import javax.swing.JTextArea;
 import javax.swing.text.BadLocationException;
 import goodjava.logging.Logger;
@@ -10,6 +12,8 @@
 public class TextAreaLuan extends JTextArea {
 	private static final Logger logger = LoggerFactory.getLogger(TextAreaLuan.class);
 
+	private boolean showWhitespace = false;
+
 	public int getLineHeight(int line) throws BadLocationException {
 		if( !getLineWrap() )
 			return getRowHeight();
@@ -24,4 +28,37 @@
 	public int getLineRows(int line) throws BadLocationException {
 		return getLineHeight(line) / getRowHeight();
 	}
+
+	public void showWhitespace(boolean showWhitespace) {
+		this.showWhitespace = showWhitespace;
+		repaint();
+	}
+
+	@Override protected void paintComponent(Graphics g) {
+		super.paintComponent(g);
+		if( !showWhitespace )
+			return;
+		g.setColor(Color.LIGHT_GRAY);
+		String text = getText();
+		int ascent = g.getFontMetrics().getAscent();
+		Rectangle visible = getVisibleRect();
+		for( int pos=0; pos<text.length(); pos++ ) {
+			char ch = text.charAt(pos);
+			String symbol;
+			switch(ch) {
+				case ' ': symbol = "·"; break;
+				case '\t': symbol = "→"; break;
+				case '\n': symbol = "¶"; break;
+				default: continue;
+			}
+			Rectangle r;
+			try {
+				r = modelToView(pos);
+			} catch(BadLocationException e) {
+				throw new RuntimeException(e);
+			}
+			if( visible.contains(r)	)
+				g.drawString( symbol, r.x, r.y + ascent );
+		}
+	}
 }
--- a/src/luan/modules/swing/Text_area.luan	Sat Apr 05 19:22:51 2025 -0600
+++ b/src/luan/modules/swing/Text_area.luan	Sat Apr 05 22:50:03 2025 -0600
@@ -70,6 +70,7 @@
 	local text_area = { java = TextAreaLuan.new() }
 	new_text_component(text_area)
 	local jtext_area = text_area.java
+	text_area.show_whitespace = jtext_area.showWhitespace
 	set_metatable(text_area,mt)
 	return text_area
 end