KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > listeners > LogViewerHandler


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 JavaDoc NO_DATA = "STOP";
18     private static int lineCount = 0;
19     private Color backgroundColor;
20     private Display display;
21     private Color foregroundColor;
22     private String JavaDoc 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     /**
39      * Displays the message in the specific and main log viewers. The message in
40      * the main log viewer will have the colors applied to it. The logs will be
41      * purged once it reaches the threshold. The viewers will automatically
42      * scroll to the bottom when new messages are added.
43      *
44      * @param msg
45      * Message to be added to the log viewer.
46      */

47     public void write(final String JavaDoc msg) {
48         display.asyncExec(new Runnable JavaDoc() {
49             public void run() {
50                 // writes to log viewers
51
String JavaDoc line = logName + msg + "\n";
52                 logST.append(line);
53                 mainLogST.append(line);
54
55                 // highlights the foreground color of the line
56
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                 // instead of using styleRange to highlight the background, it
63
// is better to use styledText.setLineBackground() because this
64
// highlights the whole row instead of just up to the end of the
65
// character of a line.
66
try {
67                     mainLogST.setLineBackground(lineCount++, 1, backgroundColor);
68                 }
69                 // this exception occurs when user clears the log
70
catch (Exception JavaDoc e) {
71                     mainLogST.setLineBackground(0, 1, backgroundColor);
72                 }
73
74                 updateLog(logST);
75                 updateLog(mainLogST);
76
77                 // recalculates the line count again in case if the log is
78
// purged
79
lineCount = mainLogST.getLineCount() - 1;
80
81                 autoScroll(logST);
82                 autoScroll(mainLogST);
83
84                 mainLogST.notifyListeners(SWT.Modify, noDataEvent);
85             }
86         });
87     }
88
89     /**
90      * Scrolls the log viewer down automatically.
91      *
92      * @param styledText
93      * StyledText of a particular log viewer.
94      */

95     private void autoScroll(StyledText styledText) {
96         Object JavaDoc key = styledText.getData(ShellMainLogViewer.LOG_VIEWER_DATA_KEY_SCROLL_LOCK);
97
98         // if scroll lock key exists, then get it, else don't lock the scroll
99
boolean toScrollLock = (key != null) ? (Boolean JavaDoc) key : false;
100
101         if (!toScrollLock) {
102             styledText.setHorizontalPixel(0);
103             styledText.setTopPixel(styledText.getLineHeight() * styledText.getLineCount());
104         }
105     }
106
107     /**
108      * Invokes log size handler to see whether to purge the log or not.
109      *
110      * @param styledText
111      * StyledText of a particular log viewer.
112      */

113     private void updateLog(StyledText styledText) {
114         ((LogSizeHandler) styledText.getData()).run();
115     }
116
117 }
118
Popular Tags