changeset 1875:31da4bd4e6b8 default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 05 Apr 2025 09:58:40 -0600
parents 54e65b36947b
children
files src/luan/modules/swing/TextAreaLineNumbersLuan.java src/luan/modules/swing/TextAreaLuan.java
diffstat 2 files changed, 56 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/swing/TextAreaLineNumbersLuan.java	Sat Apr 05 08:20:43 2025 -0600
+++ b/src/luan/modules/swing/TextAreaLineNumbersLuan.java	Sat Apr 05 09:58:40 2025 -0600
@@ -1,9 +1,14 @@
 package luan.modules.swing;
 
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeEvent;
 import java.awt.Font;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.Color;
+import java.awt.event.ComponentListener;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
 import javax.swing.JPanel;
 import javax.swing.BoxLayout;
 import javax.swing.JLabel;
@@ -35,6 +40,8 @@
 		fixWidth();
 		fixHeights();
 		textArea.getDocument().addDocumentListener(new WeakDocumentListener(this));
+		textArea.addPropertyChangeListener("lineWrap",lineWrapListener);
+		textArea.addComponentListener(resizeListener);
 	}
 
 	private JLabel newJLabel(int i) {
@@ -66,10 +73,10 @@
 		}
 	}
 
-	private void doFixHeights() {
-		Component[] a = getComponents();
-		for( int i=0; i<a.length; i++ ) {
-			Component label = a[i];
+	private void doFixHeights(int start,int end) {
+		boolean changed = false;
+		for( int i=start; i<=end; i++ ) {
+			Component label = getComponent(i);
 			Dimension size = label.getPreferredSize();
 			int height;
 			try {
@@ -79,23 +86,26 @@
 			}
 			//logger.info("height "+i+" "+height);
 			if( height != size.height ) {
+				changed = true;
 				Dimension newSize = new Dimension( size.width, height );
 				label.setPreferredSize(newSize);
 				label.setMaximumSize(newSize);
 			}
 		}
-		revalidate();
-		//repaint();
+		if( changed )
+			revalidate();
 	}
 
-	private final Runnable fixHeightsRunnable = new Runnable() {
-		public void run() {
-			doFixHeights();
-		}
-	};
+	private void fixHeights(final int start,final int end) {
+		SwingUtilities.invokeLater(new Runnable() {
+			public void run() {
+				doFixHeights(start,end);
+			}
+		});
+	}
 
 	private void fixHeights() {
-		SwingUtilities.invokeLater(fixHeightsRunnable);
+		fixHeights(0,lines-1);
 	}
 
 	@Override public void setForeground(Color fg) {
@@ -105,16 +115,30 @@
 		}
 	}
 
-	@Override public void changedUpdate(DocumentEvent e) {
+	@Override public void changedUpdate(DocumentEvent event) {
 		//logger.info(e.getType().toString());
 		int n = textArea.getLineCount();
-		if( lines < n ) {
+		if( lines == n ) {
+			if( textArea.getLineWrap() ) {
+				int offset = event.getOffset();
+				int start, end;
+				try {
+					start = textArea.getLineOfOffset(offset);
+					end = textArea.getLineOfOffset( offset + event.getLength() - 1 );
+				} catch(BadLocationException e) {
+					throw new RuntimeException(e);
+				}
+				fixHeights(start,end);
+			}
+		} else if( lines < n ) {
 			for( int i=lines+1; i<=n; i++ ) {
 				JLabel label = newJLabel(i);
 				add(label);
 			}
 			lines = n;
-		} else if( lines > n ) {
+			if( textArea.getLineWrap() )
+				fixHeights();
+		} else {  // lines > n
 			for( int i=n; i<lines; i++ ) {
 				//logger.info("getComponentCount "+getComponentCount());
 				//logger.info("remove "+n);
@@ -122,10 +146,10 @@
 				//logger.info("getComponentCount "+getComponentCount());
 			}
 			lines = n;
-			//revalidate();
+			if( textArea.getLineWrap() )
+				fixHeights();
 			repaint();
 		}
-		fixHeights();
 	}
 
 	@Override public void removeUpdate(DocumentEvent e) {
@@ -136,4 +160,16 @@
 		changedUpdate(e);
 	}
 
+	private final PropertyChangeListener lineWrapListener = new PropertyChangeListener() {
+		@Override public void propertyChange(PropertyChangeEvent evt) {
+			fixHeights();
+		}
+	};
+
+	private final ComponentListener resizeListener = new ComponentAdapter() {
+		@Override public void componentResized(ComponentEvent e) {
+			if( textArea.getLineWrap() )
+				fixHeights();
+		}
+	};
 }
--- a/src/luan/modules/swing/TextAreaLuan.java	Sat Apr 05 08:20:43 2025 -0600
+++ b/src/luan/modules/swing/TextAreaLuan.java	Sat Apr 05 09:58:40 2025 -0600
@@ -11,9 +11,9 @@
 	private static final Logger logger = LoggerFactory.getLogger(TextAreaLuan.class);
 
 	public int getLineHeight(int line) throws BadLocationException {
-		Rectangle r1 = modelToView(getLineStartOffset(line));
-//		if( r1 == null )  return 0;  // hack
-		int startOffset = r1.y;
+		if( !getLineWrap() )
+			return getRowHeight();
+		int startOffset = modelToView(getLineStartOffset(line)).y;
 		int endOffset = modelToView(getLineEndOffset(line)).y;
 		int height = endOffset - startOffset;
 		if( height == 0 )