001: package net.sourceforge.tracelog.listeners;
002:
003: import net.sourceforge.tracelog.config.LogFile;
004: import net.sourceforge.tracelog.ui.ShellMainLogGroupTabFolder;
005: import net.sourceforge.tracelog.ui.ShellOptionLogFile;
006: import net.sourceforge.tracelog.ui.UIUtil;
007: import net.sourceforge.tracelog.utils.Util;
008:
009: import org.apache.commons.lang.StringUtils;
010: import org.apache.log4j.Logger;
011: import org.eclipse.swt.custom.StyleRange;
012: import org.eclipse.swt.custom.StyledText;
013: import org.eclipse.swt.graphics.Color;
014:
015: public class LogViewerHandler {
016: private static Logger log = Logger
017: .getLogger(LogViewerHandler.class);
018:
019: private Color backgroundColor;
020: private Color foregroundColor;
021: private String logName;
022: private StyledText logST;
023: private StyledText mainLogST;
024:
025: public LogViewerHandler(LogFile logFile, StyledText mainLogST,
026: StyledText logST) {
027: this .mainLogST = mainLogST;
028: this .logST = logST;
029: this .foregroundColor = UIUtil.getColor(logFile
030: .getForegroundColor());
031: this .backgroundColor = UIUtil.getColor(logFile
032: .getBackgroundColor());
033: this .logName = "["
034: + StringUtils.rightPad(logFile.getLogName(),
035: ShellOptionLogFile.MAX_LOG_NAME_LENGTH) + "] ";
036: }
037:
038: /**
039: * Displays the message in the specific and main log viewers. The message in
040: * the main log viewer will have the colors applied to it. The logs will be
041: * purged once it reaches the threshold. The viewers will automatically
042: * scroll to the bottom when new messages are added.
043: *
044: * @param msg
045: * Message to be added to the log viewer.
046: */
047: public void write(final String msg) {
048: UIUtil.getDisplay().asyncExec(new Runnable() {
049: public void run() {
050: if (logST.isDisposed() || mainLogST.isDisposed()) {
051: return;
052: }
053:
054: int lineCount = mainLogST.getLineCount() - 1;
055: String lineNum = "["
056: + StringUtils.leftPad(
057: String.valueOf(lineCount), 7, '0')
058: + "] ";
059: String line = lineNum + logName + msg + Util.LINE_BREAK;
060:
061: logST.append(line);
062: mainLogST.append(line);
063:
064: // highlights the foreground color of the line
065: StyleRange logStyleRange = new StyleRange();
066: logStyleRange.start = mainLogST.getCharCount()
067: - line.length();
068: logStyleRange.length = line.length();
069: logStyleRange.foreground = foregroundColor;
070: mainLogST.setStyleRange(logStyleRange);
071:
072: // instead of using styleRange to highlight the background, it
073: // is better to use styledText.setLineBackground() because this
074: // highlights the whole row instead of just up to the end of the
075: // character of a line.
076: try {
077: mainLogST.setLineBackground(lineCount++, 1,
078: backgroundColor);
079: }
080: // should not even happen
081: catch (Exception e) {
082: log
083: .error("Cannot highlight main log background color at line "
084: + (lineCount - 1) + "." + e);
085: e.printStackTrace();
086: // mainLogST.setLineBackground(0, 1, backgroundColor);
087: }
088:
089: checkLogSize(logST);
090: checkLogSize(mainLogST);
091:
092: autoScroll(logST);
093: autoScroll(mainLogST);
094: }
095: });
096: }
097:
098: /**
099: * Scrolls the log viewer down automatically.
100: *
101: * @param styledText
102: * StyledText of a particular log viewer.
103: */
104: private void autoScroll(StyledText styledText) {
105: Object key = styledText
106: .getData(ShellMainLogGroupTabFolder.LOG_VIEWER_DATA_KEY_SCROLL_LOCK);
107:
108: // if scroll lock key exists, then get it, else don't lock the scroll
109: boolean toScrollLock = (key != null) ? (Boolean) key : false;
110:
111: if (!toScrollLock) {
112: styledText.setHorizontalPixel(0);
113: styledText.setTopPixel(styledText.getLineHeight()
114: * styledText.getLineCount());
115: }
116: }
117:
118: /**
119: * Invokes log size handler to see whether to purge the log or not.
120: *
121: * @param styledText
122: * StyledText of a particular log viewer.
123: */
124: private void checkLogSize(StyledText styledText) {
125: ((LogSizeHandler) styledText.getData()).run();
126: }
127:
128: }
|