1 19 package org.netbeans.core.output2; 20 21 import java.awt.Point ; 22 import java.awt.event.MouseEvent ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Segment ; 25 import org.netbeans.core.output2.ui.AbstractOutputPane; 26 27 import javax.swing.*; 28 import javax.swing.text.Document ; 29 import javax.swing.text.PlainDocument ; 30 import java.awt.*; 31 import java.awt.event.ComponentEvent ; 32 import java.awt.event.ComponentListener ; 33 import java.awt.event.KeyEvent ; 34 import org.openide.util.NbPreferences; 35 36 37 class OutputPane extends AbstractOutputPane implements ComponentListener { 38 protected void documentChanged() { 39 super.documentChanged(); 40 findOutputTab().documentChanged(); 41 } 42 43 protected void caretEnteredLine(int line) { 44 findOutputTab().caretEnteredLine(line); 45 } 46 47 protected void lineClicked(int line, Point p) { 48 if (!(getDocument() instanceof OutputDocument) || !inLeadingOrTrailingWhitespace(line, p)) { 49 findOutputTab().lineClicked(line); 50 } 51 } 52 53 private boolean linePressed (int line, Point p) { 54 boolean result; 55 if (!(getDocument() instanceof OutputDocument) || !inLeadingOrTrailingWhitespace(line, p)) { 56 result = findOutputTab().linePressed (line, p); 57 } else { 58 result = false; 59 } 60 return result; 61 } 62 63 protected void postPopupMenu(Point p, Component src) { 64 findOutputTab().postPopupMenu(p, src); 65 } 66 67 public void setMouseLine (int line, Point p) { 68 Document doc = getDocument(); 69 if (doc instanceof OutputDocument) { 70 boolean link = line != -1 && ((OutputDocument) doc).getLines().isHyperlink(line); 71 if (link && p != null) { 72 if (inLeadingOrTrailingWhitespace(line, p)) { 75 link = false; 76 line = -1; 77 } 78 } 79 textView.setCursor(link ? Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) : 80 Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 81 } 82 super.setMouseLine(line, p); 83 } 84 85 private boolean inLeadingOrTrailingWhitespace (int line, Point p) { 86 if (line == -1) { 87 return true; 88 } 89 assert getDocument() instanceof OutputDocument; 90 assert p != null; 91 OutputDocument doc = (OutputDocument) getDocument(); 92 int lineStart = doc.getLineStart(line); 93 int lineEnd = doc.getLineEnd(line); 94 95 try { 96 doc.getText (lineStart, lineEnd - lineStart, seg); 97 char curr = seg.first(); 98 while (Character.isWhitespace(curr) && curr != Segment.DONE) { 99 lineStart++; 100 curr = seg.next(); 101 } 102 curr = seg.last(); 103 while (Character.isWhitespace(curr) && curr != Segment.DONE) { 104 lineEnd--; 105 curr = seg.previous(); 106 } 107 if (lineEnd <= lineStart) { 108 line = -1; 109 } else { 110 Rectangle startRect = textView.modelToView(lineStart); 111 Rectangle endRect = textView.modelToView(lineEnd); 112 if (p.y >= startRect.y && p.y <= endRect.y && isWrapped()) { 113 endRect.x = 0; 114 endRect.width = getWidth(); 115 } 116 boolean cursorIsNotOverLeadingOrTrailingWhitespace = 117 p.x >= startRect.x && p.y >= startRect.y && 118 p.x <= endRect.x + endRect.width && 119 p.y <= endRect.y + endRect.height; 120 if (!cursorIsNotOverLeadingOrTrailingWhitespace) { 121 line = -1; 122 } 123 } 124 } catch (BadLocationException e) { 125 } 127 return line == -1; 128 } 129 130 private Segment seg = new Segment (); 131 132 137 public void mouseMoved (MouseEvent evt) { 138 Document doc = getDocument(); 139 if (doc instanceof OutputDocument) { 140 if (((OutputDocument) doc).getLines().hasHyperlinks()) { 141 super.mouseMoved(evt); 142 } 143 } 144 } 145 146 public void mousePressed(MouseEvent e) { 147 super.mousePressed(e); 148 if (e.getSource() == textView && SwingUtilities.isLeftMouseButton(e)) { 149 int pos = textView.viewToModel(e.getPoint()); 150 if (pos != -1) { 151 int line = textView.getDocument().getDefaultRootElement().getElementIndex(pos); 152 if (line >= 0) { 153 if (linePressed(line, e.getPoint())) { 154 e.consume(); 155 return; 156 } 157 } 158 } 159 } 160 findOutputTab().setToFocus((Component)e.getSource()); 162 findOutputTab().requestActive(); 163 } 164 165 private OutputTab findOutputTab() { 166 return (OutputTab) SwingUtilities.getAncestorOfClass (OutputTab.class, this); 167 } 168 169 protected void setDocument (Document doc) { 170 if (doc == null) { 171 Document d = getDocument(); 172 if (d != null) { 173 d.removeDocumentListener(this); 174 } 175 textView.setDocument (new PlainDocument ()); 176 return; 177 } 178 textView.setEditorKit (new OutputEditorKit(isWrapped(), textView)); 179 super.setDocument(doc); 180 updateKeyBindings(); 181 } 182 183 184 public void setWrapped (boolean val) { 185 if (val != isWrapped() || !(getEditorKit() instanceof OutputEditorKit)) { 186 NbPreferences.forModule(OutputPane.class).putBoolean("wrap", val); final int pos = textView.getCaret().getDot(); 188 Cursor cursor = textView.getCursor(); 189 try { 190 textView.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 191 setEditorKit (new OutputEditorKit(val, textView)); 192 } finally { 193 textView.setCursor (cursor); 194 } 195 if (val) { 196 getViewport().addChangeListener(this); 197 } else { 198 getViewport().removeChangeListener(this); 199 } 200 201 SwingUtilities.invokeLater (new Runnable () { 204 private boolean first = true; 205 public void run() { 206 if (first) { 207 first = false; 208 SwingUtilities.invokeLater(this); 209 return; 210 } 211 textView.getCaret().setDot(pos); 212 } 213 }); 214 if (getDocument() instanceof OutputDocument && ((OutputDocument) getDocument()).getLines().isGrowing()) { 215 lockScroll(); 216 } 217 if (!val) { 218 getHorizontalScrollBar().setValue(getHorizontalScrollBar().getModel().getMinimum()); 221 } 222 validate(); 223 } 224 } 225 226 public boolean isWrapped() { 227 if (getEditorKit() instanceof OutputEditorKit) { 228 return getEditorKit() instanceof OutputEditorKit 229 && ((OutputEditorKit) getEditorKit()).isWrapped(); 230 } else { 231 return NbPreferences.forModule(OutputPane.class).getBoolean("wrap", false); } 233 } 234 235 private static final boolean GTK = "GTK".equals(UIManager.getLookAndFeel().getID()); 236 protected JEditorPane createTextView() { 237 JEditorPane result = GTK ? new GEP() : new JEditorPane(); 238 result.addComponentListener(this); 239 240 result.setDisabledTextColor(result.getBackground()); 242 243 InputMap map = result.getInputMap(); 245 MyInputMap myMap = new MyInputMap(); 246 myMap.setParent(map); 247 result.setInputMap(result.WHEN_FOCUSED, myMap); 248 249 return result; 250 } 251 252 protected class MyInputMap extends InputMap { 254 255 public MyInputMap() { 256 super(); 257 } 258 259 public Object get(KeyStroke keyStroke) { 260 KeyStroke stroke = KeyStroke.getKeyStroke("control shift O"); 261 if (keyStroke.equals(stroke)) { 262 return null; 263 } 264 stroke = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, Event.CTRL_MASK); 265 if (keyStroke.equals(stroke)) { 266 return null; 267 } 268 Object retValue; 269 retValue = super.get(keyStroke); 270 return retValue; 271 } 272 273 } 274 275 276 private int prevW = -1; 277 public void componentResized(ComponentEvent e) { 278 int w = textView.getWidth(); 279 if (prevW != w) { 280 if (isWrapped()) { 281 WrappedTextView view = ((OutputEditorKit) getEditorKit()).view(); 282 if (view != null) { 283 view.setChanged(); 284 textView.repaint(); 285 } 286 } 287 } 288 prevW = w; 289 } 290 291 public void componentMoved(ComponentEvent e) { 292 } 294 295 public void componentShown(ComponentEvent e) { 296 } 298 299 public void componentHidden(ComponentEvent e) { 300 } 302 303 private static final class GEP extends JEditorPane { 304 public java.awt.Color getBackground() { 305 return UIManager.getColor("text"); 306 } 307 } 308 309 } 310 | Popular Tags |