1 package org.catacomb.druid.gui.edit;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5
6 import org.catacomb.druid.swing.DBorderLayout;
7 import org.catacomb.druid.swing.DHTMLPane;
8 import org.catacomb.druid.swing.DScrollPane;
9 import org.catacomb.interlish.structure.Consumer;
10 import org.catacomb.interlish.structure.InfoLog;
11 import org.catacomb.interlish.structure.LogDisplay;
12 import org.catacomb.interlish.structure.LogNotificand;
13 import org.catacomb.interlish.util.JUtil;
14 import org.catacomb.report.E;
15 import org.catacomb.xdoc.XdocBase;
16
17
18
19 public class DruLogPanel extends DruGCPanel implements LogNotificand, Consumer, LogDisplay, Runnable {
20
21 static final long serialVersionUID = 1001;
22
23 DScrollPane dsp;
24 DHTMLPane htmlPane;
25
26
27 InfoLog infoLog;
28
29 String logHTMLTemplate;
30
31
32 Thread updateThread;
33 boolean pendingEntry = false;
34
35
36 public DruLogPanel(int h) {
37 super();
38
39 setBorderLayout(2, 2);
40 dsp = new DScrollPane();
41 dsp.setVerticalScrollBarAlways();
42
43 addDComponent(dsp, DBorderLayout.CENTER);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 htmlPane = new DHTMLPane();
61
62 htmlPane.setRelStyleSheet(new XdocBase(), "LogCSS.txt");
63
64
65 dsp.setViewportView(htmlPane);
66
67 setTooltipTarget(htmlPane);
68 }
69
70
71
72 public String getLogHTMLTemplate() {
73 if (logHTMLTemplate == null) {
74 logHTMLTemplate = JUtil.getRelativeResource(new XdocBase(), "LogHTMLTemplate.txt");
75 }
76 return logHTMLTemplate;
77 }
78
79
80
81 public void addInfoLog(InfoLog ilog) {
82 if (infoLog != null) {
83 E.warning("squashing existing log");
84 infoLog.removeLogNotificand(this);
85 }
86 infoLog = ilog;
87
88 infoLog.setLogNotificand(this);
89 }
90
91
92
93 public void setPreferredSize(int w, int h) {
94 dsp.setPreferredSize(new Dimension(w, h));
95 setPreferredSize(new Dimension(w, h));
96 }
97
98
99 public void setBg(Color c) {
100
101 dsp.setBg(c);
102 htmlPane.setBg(c);
103
104
105
106
107
108 super.setBg(c);
109 }
110
111
112 public void setNoHorizontalScroll() {
113 dsp.setHorizontalScrollbarNever();
114 }
115
116
117
118 public void clear() {
119 if (infoLog != null) {
120 infoLog.clear();
121 showHTML("");
122 }
123 }
124
125
126 public void save() {
127 E.missing();
128 }
129
130
131
132 public void itemLogged(InfoLog ilog) {
133 if (infoLog != ilog) {
134 E.error("log mixup?");
135 }
136 pendingEntry = true;
137
138 if (updateThread == null) {
139 updateThread = new Thread(this);
140 updateThread.setPriority(Thread.MIN_PRIORITY);
141 updateThread.start();
142 }
143 }
144
145
146 public void run() {
147 try {
148 while (true) {
149 Thread.sleep(2000);
150 if (pendingEntry) {
151 pendingEntry = false;
152 String sh = infoLog.getHTML();
153 showHTML(sh);
154 }
155 }
156 } catch (Exception ex) {
157 E.info("thread died?");
158 }
159 }
160
161
162
163
164
165
166 public void showHTML(String sin) {
167 String s = sin;
168 if (s == null) {
169 s = "";
170 }
171
172 String txt = getLogHTMLTemplate();
173 txt = txt.replaceAll("BODY", s);
174 htmlPane.showHTML(txt);
175 }
176
177
178
179 public void showText(String sin) {
180 String s = sin;
181 if (s == null) {
182 s = "";
183 }
184 s = s.replaceAll("-p-", "</p>\n<p>");
185 s = s.replaceAll("-br-", "<br>\n");
186
187 String sh = "<p>" + s + "</p>";
188
189 htmlPane.showHTML(sh);
190
191
192
193 }
194
195
196 }