1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* *************************************************************************
IT Mill Toolkit
Development of Browser User Interfaces Made Easy
Copyright (C) 2000-2006 IT Mill Ltd
*************************************************************************
This product is distributed under commercial license that can be found
from the product package on license/license.txt. Use of this product might
require purchasing a commercial license from IT Mill Ltd. For guidelines
on usage, see license/licensing-guidelines.html
*************************************************************************
For more information, contact:
IT Mill Ltd phone: +358 2 4802 7180
Ruukinkatu 2-4 fax: +358 2 4802 7181
20540, Turku email: info@itmill.com
Finland company www: www.itmill.com
Primary source for information and releases: www.itmill.com
********************************************************************** */
package com.itmill.toolkit.terminal.web;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import javax.servlet.http.HttpSession;
import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.util.BeanItem;
import com.itmill.toolkit.data.util.MethodProperty;
import com.itmill.toolkit.terminal.FileResource;
import com.itmill.toolkit.ui.*;
/**
* This class provides a debugging window where one may view the UIDL of
* the current window, or in a tabset the UIDL of an active frameset.
*
* It is primarily inteded for creating and debugging themes.
*
* @author IT Mill Ltd.
* @version @VERSION@
* @since 3.0
*/
public class DebugWindow extends Window {
protected static String WINDOW_NAME = "debug";
private Application debuggedApplication;
private HashMap rawUIDL = new HashMap();
private ApplicationServlet servlet;
private HttpSession session;
private TabSheet tabs = new TabSheet();
private Select themeSelector;
private Label applicationInfo = new Label("", Label.CONTENT_XHTML);
/**Create new debug window for an application.
* @param debuggedApplication Application to be debugged.
* @param session Session to be debugged.
* @param servlet Servlet to be debugged.
*/
protected DebugWindow(
Application debuggedApplication,
HttpSession session,
ApplicationServlet servlet) {
super("Debug window");
setName(WINDOW_NAME);
setServlet(servlet);
setSession(session);
setBorder(Window.BORDER_NONE);
// Create control buttons
OrderedLayout controls =
new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL);
controls.addComponent(
new Button("Restart Application", this, "restartApplication"));
controls.addComponent(
new Button("Clear Session", this, "clearSession"));
Collection themes = servlet.getThemeSource().getThemes();
Collection names = new LinkedList();
for (Iterator i = themes.iterator(); i.hasNext();) {
names.add(((Theme) i.next()).getName());
}
// Create theme selector
themeSelector = new Select("Application Theme", names);
themeSelector.setWriteThrough(false);
// Terminal type editor
Label terminal =
new Label("<h2>Terminal Information</h2> ", Label.CONTENT_XHTML);
Form browser = new Form();
browser.setItemDataSource(
new BeanItem(WebBrowserProbe.getTerminalType(session)));
browser.removeItemProperty("class");
browser.replaceWithSelect(
"javaScriptVersion",
WebBrowser.JAVASCRIPT_VERSIONS,
WebBrowser.JAVASCRIPT_VERSIONS);
browser.replaceWithSelect(
"markupVersion",
WebBrowser.MARKUP_VERSIONS,
WebBrowser.MARKUP_VERSIONS);
browser.setWriteThrough(false);
Button setbrowser =
new Button("Set terminal information", browser, "commit");
setbrowser.dependsOn(browser);
// Arrange the UI in tabsheet
TabSheet infoTabs = new TabSheet();
addComponent(infoTabs);
OrderedLayout appInfo = new OrderedLayout();
infoTabs.addTab(appInfo, "Application",null);
appInfo.addComponent(applicationInfo);
appInfo.addComponent(controls);
appInfo.addComponent(themeSelector);
appInfo.addComponent(new Button("Change theme", this, "commitTheme"));
OrderedLayout winInfo = new OrderedLayout();
infoTabs.addTab(winInfo, "Windows",null);
winInfo.addComponent(tabs);
winInfo.addComponent(new Button("Save UIDL", this, "saveUIDL"));
OrderedLayout termInfo = new OrderedLayout();
infoTabs.addTab(termInfo, "Terminal",null);
termInfo.addComponent(terminal);
termInfo.addComponent(browser);
termInfo.addComponent(setbrowser);
// Set the debugged application
setDebuggedApplication(debuggedApplication);
}
protected Select createSelect(
String caption,
Object[] keys,
String[] names) {
Select s = new Select(caption);
s.addContainerProperty("name", String.class, "");
for (int i = 0; i < keys.length; i++) {
s.addItem(keys[i]).getItemProperty("name").setValue(names[i]);
}
s.setItemCaptionPropertyId("name");
return s;
}
public void saveUIDL() {
synchronized (rawUIDL) {
String currentUIDL = (String) rawUIDL.get(tabs.getSelectedTab());
if (currentUIDL == null)
return;
DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
File file =
new File(
"/uidl-debug"
+ df.format(new Date(System.currentTimeMillis()))
+ ".xml");
try {
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file)));
out.write(currentUIDL);
out.close();
//Open the UIDL also
open(new FileResource(file, this.getApplication()));
Log.info("UIDL written to file " + file);
} catch (FileNotFoundException e) {
Log.info("Failed to write debug to " + file + ": " + e);
} catch (IOException e) {
Log.info("Failed to write debug to " + file + ": " + e);
}
}
}
public void commitTheme() {
themeSelector.commit();
}
public void clearSession() {
session.invalidate();
}
public void restartApplication() {
if (debuggedApplication != null)
debuggedApplication.close();
}
protected void setWindowUIDL(Window window, String uidl) {
String caption = "UIDL:" + window.getName();
synchronized (tabs) {
for (Iterator i = tabs.getComponentIterator(); i.hasNext();) {
Component c = (Component) i.next();
if (tabs.getTabCaption(c).equals(caption)) {
((Label) c).setValue(getHTMLFormattedUIDL(caption, uidl));
((Label) c).setContentMode(Label.CONTENT_XHTML);
rawUIDL.put(c, uidl);
caption = null;
}
}
// Add new tab
if (caption != null) {
Label l = new Label(getHTMLFormattedUIDL(caption, uidl));
l.setContentMode(Label.CONTENT_XHTML);
rawUIDL.put(l, uidl);
tabs.addTab(l, caption, null);
}
}
}
protected String getHTMLFormattedUIDL(String caption, String uidl) {
StringBuffer sb = new StringBuffer();
// Print formatted UIDL with errors embedded
//Perl5Util util = new Perl5Util();
int row = 0;
int prev = 0;
int index = 0;
boolean lastLineWasEmpty = false;
sb.append(
"<TABLE WIDTH=\"100%\" STYLE=\"border-left: 1px solid black; "
+ "border-right: 1px solid black; border-bottom: "
+ "1px solid black; border-top: 1px solid black\""
+ " cellpadding=\"0\" cellspacing=\"0\" BORDER=\"0\">");
if (caption != null)
sb.append(
"<TR><TH BGCOLOR=\"#ddddff\" COLSPAN=\"2\">"
+ "<FONT SIZE=\"+2\">"
+ caption
+ "</FONT></TH></TR>\n");
boolean unfinished = true;
while (unfinished) {
row++;
// Get individual line
index = uidl.indexOf('\n', prev);
String line;
if (index < 0) {
unfinished = false;
line = uidl.substring(prev);
} else {
line = uidl.substring(prev, index);
prev = index + 1;
}
// Escape the XML
line = WebPaintTarget.escapeXML(line);
// Code beautification : Comment lines
line =
replaceAll(
line,
"<!--",
"<SPAN STYLE = \"color: #00dd00\"><!--");
line = replaceAll(line, "-->", "--></SPAN>");
while (line.length() > 0 && line.charAt(0) == ' ') {
line = line.substring(1);
}
boolean isEmpty = (line.length() == 0 || line.equals("\r"));
line = " " + line;
if (!(isEmpty && lastLineWasEmpty))
sb.append(
"<TR"
+ ((row % 10) > 4 ? " BGCOLOR=\"#eeeeff\"" : "")
+ ">"
+ "<TD VALIGN=\"top\" ALIGN=\"rigth\" STYLE=\"border-right: 1px solid gray\"> "
+ String.valueOf(row)
+ " </TD><TD>"
+ line
+ "</TD></TR>\n");
lastLineWasEmpty = isEmpty;
}
sb.append("</TABLE>\n");
return sb.toString();
}
/**
* Replaces the characters in a substring of this <code>String</code>
* with characters in the specified <code>String</code>. The substring
* begins at the specified <code>start</code> and extends to the character
* at index <code>end - 1</code> or to the end of the
* <code>String</code> if no such character exists. First the
* characters in the substring are removed and then the specified
* <code>String</code> is inserted at <code>start</code>. (The
* <code>StringBuffer</code> will be lengthened to accommodate the
* specified String if necessary.)
* <p>
* NOTE: This operation is slow.
* </p>
*
* @param start The beginning index, inclusive.
* @param end The ending index, exclusive.
* @param str String that will replace previous contents.
* @return This string buffer.
* @exception StringIndexOutOfBoundsException if <code>start</code>
* is negative, greater than <code>length()</code>, or
* greater than <code>end</code>.
*/
protected static String replace(
String text,
int start,
int end,
String str) {
return new StringBuffer(text).replace(start, end, str).toString();
}
protected static String replaceAll(
String text,
String oldStr,
String newStr) {
StringBuffer sb = new StringBuffer(text);
int newStrLen = newStr.length();
int oldStrLen = oldStr.length();
if (oldStrLen <= 0)
return text;
int i = 0;
while (i <= sb.length() - oldStrLen) {
if (sb.substring(i, i + oldStrLen).equals(oldStr)) {
sb.replace(i, i + oldStrLen, newStr);
i += newStrLen;
} else {
i++;
}
}
return sb.toString();
}
/**
* Sets the application.
* @param application The application to set
*/
protected void setDebuggedApplication(Application application) {
this.debuggedApplication = application;
if (application != null) {
applicationInfo.setValue(
"<h2>Application Class</h2> "
+ application.getClass().getName());
themeSelector.setPropertyDataSource(
new MethodProperty(application, "theme"));
}
}
/**
* Returns the servlet.
* @return WebAdapterServlet
*/
protected ApplicationServlet getServlet() {
return servlet;
}
/**
* Returns the session.
* @return HttpSession
*/
protected HttpSession getSession() {
return session;
}
/**
* Sets the servlet.
* @param servlet The servlet to set
*/
protected void setServlet(ApplicationServlet servlet) {
this.servlet = servlet;
}
/**
* Sets the session.
* @param session The session to set
*/
protected void setSession(HttpSession session) {
this.session = session;
}
}
|