1 package net.sourceforge.tracelog.listeners; 2 3 import net.sourceforge.tracelog.config.LogFile; 4 import net.sourceforge.tracelog.ui.ShellMainLogViewer; 5 import net.sourceforge.tracelog.ui.ShellOptionLogConfig; 6 import net.sourceforge.tracelog.utils.Util; 7 8 import org.apache.commons.lang.StringUtils; 9 import org.eclipse.swt.SWT; 10 import org.eclipse.swt.custom.StyleRange; 11 import org.eclipse.swt.custom.StyledText; 12 import org.eclipse.swt.graphics.Color; 13 import org.eclipse.swt.widgets.Display; 14 import org.eclipse.swt.widgets.Event; 15 16 public class LogViewerHandler { 17 public static final String NO_DATA = "STOP"; 18 private static int lineCount = 0; 19 private Color backgroundColor; 20 private Display display; 21 private Color foregroundColor; 22 private String logName; 23 private StyledText logST; 24 private StyledText mainLogST; 25 private Event noDataEvent; 26 27 public LogViewerHandler(LogFile logFile, StyledText mainLogST, StyledText logST) { 28 this.mainLogST = mainLogST; 29 this.logST = logST; 30 this.display = Util.getDisplay(); 31 this.foregroundColor = display.getSystemColor(logFile.getForegroundColor()); 32 this.backgroundColor = display.getSystemColor(logFile.getBackgroundColor()); 33 this.logName = "[" + StringUtils.rightPad(logFile.getLogName(), ShellOptionLogConfig.MAX_LOG_NAME_LENGTH) + "] "; 34 this.noDataEvent = new Event(); 35 this.noDataEvent.data = NO_DATA; 36 } 37 38 47 public void write(final String msg) { 48 display.asyncExec(new Runnable () { 49 public void run() { 50 String line = logName + msg + "\n"; 52 logST.append(line); 53 mainLogST.append(line); 54 55 StyleRange logStyleRange = new StyleRange(); 57 logStyleRange.start = mainLogST.getCharCount() - line.length(); 58 logStyleRange.length = line.length(); 59 logStyleRange.foreground = foregroundColor; 60 mainLogST.setStyleRange(logStyleRange); 61 62 try { 67 mainLogST.setLineBackground(lineCount++, 1, backgroundColor); 68 } 69 catch (Exception e) { 71 mainLogST.setLineBackground(0, 1, backgroundColor); 72 } 73 74 updateLog(logST); 75 updateLog(mainLogST); 76 77 lineCount = mainLogST.getLineCount() - 1; 80 81 autoScroll(logST); 82 autoScroll(mainLogST); 83 84 mainLogST.notifyListeners(SWT.Modify, noDataEvent); 85 } 86 }); 87 } 88 89 95 private void autoScroll(StyledText styledText) { 96 Object key = styledText.getData(ShellMainLogViewer.LOG_VIEWER_DATA_KEY_SCROLL_LOCK); 97 98 boolean toScrollLock = (key != null) ? (Boolean ) key : false; 100 101 if (!toScrollLock) { 102 styledText.setHorizontalPixel(0); 103 styledText.setTopPixel(styledText.getLineHeight() * styledText.getLineCount()); 104 } 105 } 106 107 113 private void updateLog(StyledText styledText) { 114 ((LogSizeHandler) styledText.getData()).run(); 115 } 116 117 } 118 | Popular Tags |