1 package org.antlr.works.utils; 2 3 import javax.swing.*; 4 import javax.swing.text.BadLocationException ; 5 import java.awt.*; 6 36 37 public class TextPane extends JTextPane { 38 39 protected TextPaneDelegate delegate = null; 40 protected boolean wrap = false; 41 42 public void setDelegate(TextPaneDelegate delegate) { 43 this.delegate = delegate; 44 } 45 46 public void setWordWrap(boolean flag) { 47 this.wrap = flag; 48 } 49 50 public boolean getWordWrap() { 51 return wrap; 52 } 53 54 public boolean getScrollableTracksViewportWidth() { 55 if(!wrap) 56 { 57 Component parent = getParent(); 58 return parent == null || getUI().getPreferredSize(this).width < parent.getSize().width; 59 } else 60 return super.getScrollableTracksViewportWidth(); 61 } 62 63 public void setBounds(int x, int y, int width, int height) { 64 if(!wrap) { 65 Dimension size = this.getPreferredSize(); 66 super.setBounds(x, y, 67 Math.max(size.width, width), Math.max(size.height, height)); 68 } else { 69 super.setBounds(x, y, width, height); 70 } 71 } 72 73 public void paintComponent(Graphics g) { 74 super.paintComponent(g); 75 if(delegate != null) 76 delegate.textPaneDidPaint(g); 77 } 78 79 public int getTextIndexAtLocation(Point point) { 80 int index = viewToModel(point); 81 Rectangle r; 82 try { 83 r = modelToView(index); 84 if(point.x <= r.x) { 85 return index-1; 86 } 87 } catch (BadLocationException e1) { 88 return -1; 89 } 90 return index; 91 } 92 } 93 | Popular Tags |