1 19 20 package org.netbeans.core.output2; 21 22 import java.awt.Component ; 23 import java.awt.Point ; 24 import java.awt.Toolkit ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.util.regex.Matcher ; 28 import javax.swing.Action ; 29 import javax.swing.JComponent ; 30 import javax.swing.SwingUtilities ; 31 import javax.swing.text.Document ; 32 import org.netbeans.core.output2.ui.AbstractOutputPane; 33 import org.netbeans.core.output2.ui.AbstractOutputTab; 34 35 38 final class OutputTab extends AbstractOutputTab { 39 private NbIO io; 40 41 OutputTab (NbIO io) { 42 this.io = io; 43 if (Controller.LOG) Controller.log ("Created an output component for " + io); 44 OutputDocument doc = new OutputDocument (((NbWriter) io.getOut()).out()); 45 setDocument (doc); 46 } 47 48 public void addNotify() { 49 super.addNotify(); 50 if (io != null) io.setClosed(false); 51 } 52 53 public void removeNotify() { 54 if (io != null) io.setClosed(true); 55 super.removeNotify(); 56 } 57 58 public void setDocument (Document doc) { 59 if (Controller.LOG) Controller.log ("Set document on " + this + " with " + io); 60 assert SwingUtilities.isEventDispatchThread(); 61 Document old = getDocument(); 62 hasOutputListeners = false; 63 firstNavigableListenerLine = -1; 64 super.setDocument(doc); 65 if (old != null && old instanceof OutputDocument) { 66 ((OutputDocument) old).dispose(); 67 } 68 } 69 70 public void setIO (NbIO io) { 71 if (Controller.LOG) Controller.log ("Replacing io on " + this + " with " + io + " out is " + (io != null ? io.getOut() : null)); 72 if (io != null) { 73 setDocument (new OutputDocument(((NbWriter) io.getOut()).out())); 74 io.setClosed(false); 75 } else { 76 if (this.io != null) this.io.setClosed(true); 77 this.io = null; 78 setDocument(null); 79 } 80 } 81 82 public OutputDocument getDocument() { 83 Document d = getOutputPane().getDocument(); 84 if (d instanceof OutputDocument) { 85 return (OutputDocument) d; 86 } 87 return null; 88 } 89 90 protected AbstractOutputPane createOutputPane() { 91 return new OutputPane(); 92 } 93 94 protected void inputSent(String txt) { 95 if (Controller.LOG) Controller.log("Input sent on OutputTab: " + txt); 96 getOutputPane().lockScroll(); 97 findOutputWindow().inputSent(this, txt); 98 } 99 100 protected void inputEof() { 101 if (Controller.LOG) Controller.log ("Input EOF on OutputTab: "); 102 findOutputWindow().inputEof(this); 103 } 104 105 public void hasSelectionChanged(boolean val) { 106 OutputWindow win = findOutputWindow(); 107 if (win != null) { 108 win.hasSelectionChanged(this, val); 109 } 110 } 111 112 public NbIO getIO() { 113 return io; 114 } 115 116 private long timestamp = 0; 117 void updateTimestamp() { 118 timestamp = System.currentTimeMillis(); 119 } 120 121 long getTimestamp() { 122 return timestamp; 123 } 124 125 private OutputWindow findOutputWindow() { 126 if (getParent() != null) { 127 return (OutputWindow) SwingUtilities.getAncestorOfClass(OutputWindow.class, this); 128 } else { 129 return (OutputWindow) getClientProperty ("outputWindow"); } 131 } 132 133 void requestActive() { 134 findOutputWindow().requestActive(); 135 } 136 137 public void lineClicked(int line) { 138 findOutputWindow().lineClicked (this, line); 139 } 140 141 boolean linePressed (int line, Point p) { 142 OutWriter out = getIO().out(); 143 if (out != null) { 144 return out.getLines().getListenerForLine(line) != null; 145 } else { 146 return false; 147 } 148 } 149 150 public void postPopupMenu(Point p, Component src) { 151 findOutputWindow().postPopupMenu(this, p, src); 152 } 153 154 public void caretEnteredLine(int line) { 155 findOutputWindow().caretEnteredLine(this, line); 156 } 157 158 private int firstNavigableListenerLine = -1; 159 163 public int getFirstNavigableListenerLine() { 164 if (firstNavigableListenerLine != -1) { 165 return firstNavigableListenerLine; 166 } 167 168 int result = -1; 169 OutWriter out = io.out(); 170 if (out != null) { 171 if (Controller.LOG) Controller.log ("Looking for first appropriate" + 172 " listener line to send the caret to"); 173 result = out.getLines().firstImportantListenerLine(); 174 } 175 return result; 176 } 177 178 public String toString() { 179 return "OutputTab@" + System.identityHashCode(this) + " for " + io; 180 } 181 182 private boolean hasOutputListeners = false; 183 public void documentChanged() { 184 OutputWindow win = findOutputWindow(); 185 if (win != null) { 186 boolean hadOutputListeners = hasOutputListeners; 187 if (getFirstNavigableListenerLine() == -1) { 188 return; 189 } 190 hasOutputListeners = getIO().out() != null && getIO().out().getLines().firstListenerLine() >= 0; 191 if (hasOutputListeners != hadOutputListeners) { 192 win.hasOutputListenersChanged(this, hasOutputListeners); 193 } 194 win.documentChanged(this); 195 } 196 } 197 198 205 public boolean shouldRelock(int dot) { 206 if (io != null) { 207 OutWriter w = io.out(); 208 if (w != null && !w.isClosed()) { 209 int dist = Math.abs(w.getLines().getCharCount() - dot); 210 return dist < 100; 211 } 212 } 213 return false; 214 } 215 216 ActionListener getFindActionListener(Action next, Action prev, Action copy) { 217 if (findActionListener == null) { 218 findActionListener = new FindActionListener(this, next, prev, copy); 219 } 220 return findActionListener; 221 } 222 223 private ActionListener findActionListener; 224 225 229 static class FindActionListener implements ActionListener { 230 OutputTab tab; 231 Action findNextAction; 232 Action findPreviousAction; 233 Action copyAction; 234 FindActionListener(OutputTab tab, Action findNextAction, Action findPreviousAction, Action copyAction) { 235 this.tab = tab; 236 this.findNextAction = findNextAction; 237 this.findPreviousAction = findPreviousAction; 238 this.copyAction = copyAction; 239 } 240 241 public void actionPerformed(ActionEvent e) { 242 FindDialogPanel panel = (FindDialogPanel) 243 SwingUtilities.getAncestorOfClass(FindDialogPanel.class, 244 (JComponent ) e.getSource()); 245 if (panel == null) { 246 panel = (FindDialogPanel) ((JComponent ) 248 e.getSource()).getClientProperty("panel"); } 250 251 String s = panel.getPattern(); 252 if (s == null || s.length() == 0) { 253 Toolkit.getDefaultToolkit().beep(); 254 255 return; 256 } 257 OutWriter out = tab.getIO().out(); 258 if (out != null && !out.isDisposed()) { 259 Matcher matcher = out.getLines().find(s); 260 if (matcher != null && matcher.find()) { 261 int start = matcher.start(); 262 int end = matcher.end(); 263 tab.getOutputPane().setSelection(start, end); 264 findNextAction.setEnabled(true); 265 findPreviousAction.setEnabled(true); 266 copyAction.setEnabled(true); 267 panel.getTopLevelAncestor().setVisible(false); 268 tab.requestFocus(); 269 } 270 } 271 } 272 } 273 } 274 | Popular Tags |