1 11 12 package org.eclipse.ui.console; 13 14 import java.util.HashMap ; 15 16 import org.eclipse.core.runtime.jobs.ISchedulingRule; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.jface.resource.JFaceResources; 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.BadPositionCategoryException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IRegion; 23 import org.eclipse.jface.text.Position; 24 import org.eclipse.jface.text.Region; 25 import org.eclipse.swt.graphics.Color; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.ui.internal.console.ConsoleDocument; 28 import org.eclipse.ui.internal.console.ConsoleHyperlinkPosition; 29 import org.eclipse.ui.internal.console.ConsolePatternMatcher; 30 import org.eclipse.ui.part.IPageBookViewPage; 31 32 45 public abstract class TextConsole extends AbstractConsole { 46 47 51 private int fConsoleWidth; 52 55 private int fTabWidth; 56 59 private Font fFont; 60 61 64 private Color fBackground; 65 66 69 private ConsolePatternMatcher fPatternMatcher; 70 71 74 private ConsoleDocument fDocument; 75 76 79 private boolean fPartitionerFinished = false; 80 81 85 private boolean fMatcherFinished = false; 86 87 90 private boolean fCompleteFired = false; 91 92 93 96 private HashMap fAttributes = new HashMap (); 97 98 private IConsoleManager fConsoleManager = ConsolePlugin.getDefault().getConsoleManager(); 99 100 101 104 protected void dispose() { 105 super.dispose(); 106 fFont = null; 107 synchronized(fAttributes) { 108 fAttributes.clear(); 109 } 110 } 111 120 public TextConsole(String name, String consoleType, ImageDescriptor imageDescriptor, boolean autoLifecycle) { 121 super(name, consoleType, imageDescriptor, autoLifecycle); 122 fDocument = new ConsoleDocument(); 123 fDocument.addPositionCategory(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY); 124 fPatternMatcher = new ConsolePatternMatcher(this); 125 fDocument.addDocumentListener(fPatternMatcher); 126 fTabWidth = IConsoleConstants.DEFAULT_TAB_SIZE; 127 } 128 129 132 public IPageBookViewPage createPage(IConsoleView view) { 133 return new TextConsolePage(this, view); 134 } 135 136 147 public IDocument getDocument() { 148 return fDocument; 149 } 150 151 157 public int getConsoleWidth() { 158 return fConsoleWidth; 159 } 160 161 168 public void setConsoleWidth(int width) { 169 if (fConsoleWidth != width) { 170 int old = fConsoleWidth; 171 fConsoleWidth = width; 172 173 firePropertyChange(this, IConsoleConstants.P_CONSOLE_WIDTH, new Integer (old), new Integer (fConsoleWidth)); 174 } 175 } 176 177 182 public void setTabWidth(final int newTabWidth) { 183 if (fTabWidth != newTabWidth) { 184 final int oldTabWidth = fTabWidth; 185 fTabWidth = newTabWidth; 186 ConsolePlugin.getStandardDisplay().asyncExec(new Runnable () { 187 public void run() { 188 firePropertyChange(TextConsole.this, IConsoleConstants.P_TAB_SIZE, new Integer (oldTabWidth), new Integer (fTabWidth)); 189 } 190 }); 191 } 192 } 193 194 199 public int getTabWidth() { 200 return fTabWidth; 201 } 202 203 208 public Font getFont() { 209 if (fFont == null) { 210 fFont = getDefaultFont(); 211 } 212 return fFont; 213 } 214 215 220 private Font getDefaultFont() { 221 return JFaceResources.getFont(JFaceResources.TEXT_FONT); 222 } 223 224 230 public void setFont(Font newFont) { 231 getFont(); 233 if (newFont == null) { 235 newFont = getDefaultFont(); 236 } 237 if (!fFont.equals(newFont)) { 239 Font old = fFont; 240 fFont = newFont; 241 firePropertyChange(this, IConsoleConstants.P_FONT, old, fFont); 242 } 243 } 244 245 253 public void setBackgrond(Color background) { 254 setBackground(background); 255 } 256 257 264 public void setBackground(Color background) { 265 if (fBackground == null) { 266 if (background == null) { 267 return; 268 } 269 } else if (fBackground.equals(background)){ 270 return; 271 } 272 Color old = fBackground; 273 fBackground = background; 274 firePropertyChange(this, IConsoleConstants.P_BACKGROUND_COLOR, old, fBackground); 275 } 276 277 284 public Color getBackground() { 285 return fBackground; 286 } 287 288 297 public void clearConsole() { 298 IDocument document = getDocument(); 299 if (document != null) { 300 document.set(""); } 302 } 303 304 308 protected abstract IConsoleDocumentPartitioner getPartitioner(); 309 310 315 public IHyperlink[] getHyperlinks() { 316 try { 317 Position[] positions = getDocument().getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY); 318 IHyperlink[] hyperlinks = new IHyperlink[positions.length]; 319 for (int i = 0; i < positions.length; i++) { 320 ConsoleHyperlinkPosition position = (ConsoleHyperlinkPosition) positions[i]; 321 hyperlinks[i] = position.getHyperLink(); 322 } 323 return hyperlinks; 324 } catch (BadPositionCategoryException e) { 325 return new IHyperlink[0]; 326 } 327 } 328 329 335 public IHyperlink getHyperlink(int offset) { 336 try { 337 IDocument document = getDocument(); 338 if (document != null) { 339 Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY); 340 Position position = findPosition(offset, positions); 341 if (position instanceof ConsoleHyperlinkPosition) { 342 return ((ConsoleHyperlinkPosition) position).getHyperLink(); 343 } 344 } 345 } catch (BadPositionCategoryException e) { 346 } 347 return null; 348 } 349 350 356 private Position findPosition(int offset, Position[] positions) { 357 358 if (positions.length == 0) 359 return null; 360 361 int left= 0; 362 int right= positions.length -1; 363 int mid= 0; 364 Position position= null; 365 366 while (left < right) { 367 368 mid= (left + right) / 2; 369 370 position= positions[mid]; 371 if (offset < position.getOffset()) { 372 if (left == mid) 373 right= left; 374 else 375 right= mid -1; 376 } else if (offset > (position.getOffset() + position.getLength() - 1)) { 377 if (right == mid) 378 left= right; 379 else 380 left= mid +1; 381 } else { 382 left= right= mid; 383 } 384 } 385 386 position= positions[left]; 387 if (offset >= position.getOffset() && (offset < (position.getOffset() + position.getLength()))) { 388 return position; 389 } 390 return null; 391 } 392 393 400 public void addPatternMatchListener(IPatternMatchListener listener) { 401 fPatternMatcher.addPatternMatchListener(listener); 402 } 403 404 411 public void removePatternMatchListener(IPatternMatchListener listener) { 412 fPatternMatcher.removePatternMatchListener(listener); 413 } 414 415 416 420 private class MatcherSchedulingRule implements ISchedulingRule { 421 public boolean contains(ISchedulingRule rule) { 422 return rule == this; 423 } 424 425 public boolean isConflicting(ISchedulingRule rule) { 426 if (contains(rule)) { 427 return true; 428 } 429 if (rule != this && rule instanceof MatcherSchedulingRule) { 430 return (((MatcherSchedulingRule)rule).getConsole() == TextConsole.this); 431 } 432 return false; 433 } 434 435 public TextConsole getConsole() { 436 return TextConsole.this; 437 } 438 } 439 440 453 public ISchedulingRule getSchedulingRule() { 454 return new MatcherSchedulingRule(); 455 } 456 457 461 public void partitionerFinished() { 462 fPatternMatcher.forceFinalMatching(); 463 fPartitionerFinished = true; 464 checkFinished(); 465 } 466 467 473 public void matcherFinished() { 474 fMatcherFinished = true; 475 fDocument.removeDocumentListener(fPatternMatcher); 476 checkFinished(); 477 } 478 479 482 private synchronized void checkFinished() { 483 if (!fCompleteFired && fPartitionerFinished && fMatcherFinished ) { 484 fCompleteFired = true; 485 firePropertyChange(this, IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE, null, null); 486 } 487 } 488 489 497 public void addHyperlink(IHyperlink hyperlink, int offset, int length) throws BadLocationException { 498 IDocument document = getDocument(); 499 ConsoleHyperlinkPosition hyperlinkPosition = new ConsoleHyperlinkPosition(hyperlink, offset, length); 500 try { 501 document.addPosition(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY, hyperlinkPosition); 502 fConsoleManager.refresh(this); 503 } catch (BadPositionCategoryException e) { 504 ConsolePlugin.log(e); 505 } 506 } 507 508 514 public IRegion getRegion(IHyperlink link) { 515 try { 516 IDocument doc = getDocument(); 517 if (doc != null) { 518 Position[] positions = doc.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY); 519 for (int i = 0; i < positions.length; i++) { 520 ConsoleHyperlinkPosition position = (ConsoleHyperlinkPosition)positions[i]; 521 if (position.getHyperLink().equals(link)) { 522 return new Region(position.getOffset(), position.getLength()); 523 } 524 } 525 } 526 } catch (BadPositionCategoryException e) { 527 } 528 return null; 529 } 530 531 537 public Object getAttribute(String key) { 538 synchronized (fAttributes) { 539 return fAttributes.get(key); 540 } 541 } 542 543 549 public void setAttribute(String key, Object value) { 550 synchronized(fAttributes) { 551 fAttributes.put(key, value); 552 } 553 } 554 } 555 | Popular Tags |