1 19 24 25 package org.netbeans.core.output2; 26 27 import java.awt.Rectangle ; 28 import javax.swing.JEditorPane ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.DefaultEditorKit ; 33 import javax.swing.text.Element ; 34 import javax.swing.text.JTextComponent ; 35 import javax.swing.text.Position ; 36 import org.openide.util.Exceptions; 37 38 43 final class OutputEditorKit extends DefaultEditorKit implements javax.swing.text.ViewFactory , ChangeListener { 44 private final boolean wrapped; 45 private final JTextComponent comp; 46 47 48 OutputEditorKit(boolean wrapped, JTextComponent comp) { 49 this.comp = comp; 50 this.wrapped = wrapped; 51 } 52 53 public WrappedTextView view() { 54 return lastWrappedView; 55 } 56 57 private WrappedTextView lastWrappedView = null; 58 public javax.swing.text.View create(Element element) { 59 javax.swing.text.View result = 60 wrapped ? (javax.swing.text.View ) new WrappedTextView(element, comp) : 61 (javax.swing.text.View ) new ExtPlainView (element); 62 lastWrappedView = wrapped ? (WrappedTextView) result : null; 63 if (wrapped) { 64 lastWrappedView.updateInfo(null); 65 } 66 return result; 67 } 68 69 public javax.swing.text.ViewFactory getViewFactory() { 70 return this; 71 } 72 73 public boolean isWrapped() { 74 return wrapped; 75 } 76 77 public void install(JEditorPane c) { 78 super.install(c); 79 if (wrapped) { 80 c.getCaret().addChangeListener(this); 81 } 82 } 83 84 public void deinstall(JEditorPane c) { 85 super.deinstall(c); 86 if (wrapped) { 87 c.getCaret().removeChangeListener(this); 88 } 89 } 90 91 private int lastMark = -1; 92 private int lastDot = -1; 93 private static final Rectangle scratch = new Rectangle (); 94 95 98 public void stateChanged (ChangeEvent ce) { 99 int mark = comp.getSelectionStart(); 100 int dot = comp.getSelectionEnd(); 101 boolean hasSelection = mark != dot; 102 boolean hadSelection = lastMark != lastDot; 103 104 106 if (lastMark != mark || lastDot != dot) { 107 int begin = Math.min (mark, dot); 108 int end = Math.max (mark, dot); 109 int oldBegin = Math.min (lastMark, lastDot); 110 int oldEnd = Math.max (lastMark, lastDot); 111 112 if (hadSelection && hasSelection) { 113 if (begin != oldBegin) { 114 int startChar = Math.min (begin, oldBegin); 115 int endChar = Math.max (begin, oldBegin); 116 repaintRange (startChar, endChar); 117 } else { 118 int startChar = Math.min (end, oldEnd); 119 int endChar = Math.max (end, oldEnd); 120 repaintRange (startChar, endChar); 121 } 122 } else if (hadSelection && !hasSelection) { 123 repaintRange (oldBegin, oldEnd); 124 } 125 126 } 127 lastMark = mark; 128 lastDot = dot; 129 } 130 131 private void repaintRange (int start, int end) { 132 try { 133 Rectangle r = (Rectangle ) view().modelToView(end, scratch, Position.Bias.Forward); 134 int y1 = r.y + r.height; 135 r = (Rectangle ) view().modelToView(start, scratch, Position.Bias.Forward); 136 r.x = 0; 137 r.width = comp.getWidth(); 138 r.height = y1 - r.y; 139 comp.repaint (r); 141 } catch (BadLocationException e) { 142 comp.repaint(); 143 Exceptions.printStackTrace(e); 144 } 145 } 146 } 147 | Popular Tags |