Mercurial Hosting > luan
view src/luan/modules/swing/TextFieldLuan.java @ 1994:035996323891 default tip
paste fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Jul 2025 22:23:58 -0600 |
parents | 593ebfad2ef4 |
children |
line wrap: on
line source
package luan.modules.swing; import java.awt.Rectangle; import java.awt.Graphics; import java.awt.Color; import javax.swing.JTextField; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultEditorKit; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; public class TextFieldLuan extends JTextField { private static final Logger logger = LoggerFactory.getLogger(TextFieldLuan.class); private boolean showWhitespace = false; public TextFieldLuan() { super(); getActionMap().put( DefaultEditorKit.selectWordAction, SwingLuan.selectWordAction ); getActionMap().put( DefaultEditorKit.selectLineAction, SwingLuan.selectWordAndDotsAction ); } public boolean isWhitespaceVisible() { return showWhitespace; } public void setWhitespaceVisible(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 ); } } private UndoManagerLuan getUndoManagerLuan() { return (UndoManagerLuan)getDocument().getProperty("undo"); } @Override public void replaceSelection(String content) { UndoManagerLuan undo = getUndoManagerLuan(); undo.beginTransaction(); try { super.replaceSelection(content); } finally { undo.endTransaction(); } } }