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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventPreview;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ui.VLazyExecutor;
import com.vaadin.terminal.gwt.client.ui.VOverlay;
/**
* A helper console for client side development. The debug console can also be
* used to resolve layout issues, inspect the communication between browser and
* the server, start GWT dev mode and restart application.
*
* <p>
* This implementation is used vaadin is in debug mode (see manual) and
* developer appends "?debug" query parameter to url. Debug information can also
* be shown on browsers internal console only, by appending "?debug=quiet" query
* parameter.
* <p>
* This implementation can be overridden with GWT deferred binding.
*
*/
public class VDebugConsole extends VOverlay implements Console {
private static final String POS_COOKIE_NAME = "VDebugConsolePos";
Element caption = DOM.createDiv();
private Panel panel;
private Button clear = new Button("Clear console");
private Button restart = new Button("Restart app");
private Button forceLayout = new Button("Force layout");
private Button analyzeLayout = new Button("Analyze layouts");
private Button savePosition = new Button("Save pos");
private CheckBox hostedMode = new CheckBox("GWT dev mode ");
private CheckBox autoScroll = new CheckBox("Autoscroll ");
private HorizontalPanel actions;
private boolean collapsed = false;
private boolean resizing;
private int startX;
private int startY;
private int initialW;
private int initialH;
private boolean moving = false;
private int origTop;
private int origLeft;
private static final String help = "Drag=move, shift-drag=resize, doubleclick=min/max."
+ "Use debug=quiet to log only to browser console.";
public VDebugConsole() {
super(false, false);
}
private EventPreview dragpreview = new EventPreview() {
public boolean onEventPreview(Event event) {
onBrowserEvent(event);
return false;
}
};
private boolean quietMode;
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (DOM.eventGetType(event)) {
case Event.ONMOUSEDOWN:
if (DOM.eventGetShiftKey(event)) {
resizing = true;
DOM.setCapture(getElement());
startX = DOM.eventGetScreenX(event);
startY = DOM.eventGetScreenY(event);
initialW = VDebugConsole.this.getOffsetWidth();
initialH = VDebugConsole.this.getOffsetHeight();
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
DOM.addEventPreview(dragpreview);
} else if (DOM.eventGetTarget(event) == caption) {
moving = true;
startX = DOM.eventGetScreenX(event);
startY = DOM.eventGetScreenY(event);
origTop = getAbsoluteTop();
origLeft = getAbsoluteLeft();
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
DOM.addEventPreview(dragpreview);
}
break;
case Event.ONMOUSEMOVE:
if (resizing) {
int deltaX = startX - DOM.eventGetScreenX(event);
int detalY = startY - DOM.eventGetScreenY(event);
int w = initialW - deltaX;
if (w < 30) {
w = 30;
}
int h = initialH - detalY;
if (h < 40) {
h = 40;
}
VDebugConsole.this.setPixelSize(w, h);
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
} else if (moving) {
int deltaX = startX - DOM.eventGetScreenX(event);
int detalY = startY - DOM.eventGetScreenY(event);
int left = origLeft - deltaX;
if (left < 0) {
left = 0;
}
int top = origTop - detalY;
if (top < 0) {
top = 0;
}
VDebugConsole.this.setPopupPosition(left, top);
DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event);
}
break;
case Event.ONLOSECAPTURE:
case Event.ONMOUSEUP:
if (resizing) {
DOM.releaseCapture(getElement());
resizing = false;
} else if (moving) {
DOM.releaseCapture(getElement());
moving = false;
}
DOM.removeEventPreview(dragpreview);
break;
case Event.ONDBLCLICK:
if (DOM.eventGetTarget(event) == caption) {
if (collapsed) {
panel.setVisible(true);
setToDefaultSizeAndPos();
} else {
panel.setVisible(false);
setPixelSize(120, 20);
setPopupPosition(Window.getClientWidth() - 125,
Window.getClientHeight() - 25);
}
collapsed = !collapsed;
}
break;
default:
break;
}
}
private void setToDefaultSizeAndPos() {
String cookie = Cookies.getCookie(POS_COOKIE_NAME);
int width, height, top, left;
boolean autoScrollValue = false;
if (cookie != null) {
String[] split = cookie.split(",");
left = Integer.parseInt(split[0]);
top = Integer.parseInt(split[1]);
width = Integer.parseInt(split[2]);
height = Integer.parseInt(split[3]);
autoScrollValue = Boolean.valueOf(split[4]);
} else {
width = 400;
height = 150;
top = Window.getClientHeight() - 160;
left = Window.getClientWidth() - 410;
}
setPixelSize(width, height);
setPopupPosition(left, top);
autoScroll.setValue(autoScrollValue);
}
@Override
public void setPixelSize(int width, int height) {
if (height < 20) {
height = 20;
}
if (width < 2) {
width = 2;
}
panel.setHeight((height - 20) + "px");
panel.setWidth((width - 2) + "px");
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.gwt.client.Console#log(java.lang.String)
*/
public void log(String msg) {
if (msg == null) {
msg = "null";
}
// remoteLog(msg);
logToDebugWindow(msg, false);
GWT.log(msg);
consoleLog(msg);
}
private List<String> msgQueue = new LinkedList<String>();
private ScheduledCommand doSend = new ScheduledCommand() {
public void execute() {
if (!msgQueue.isEmpty()) {
RequestBuilder requestBuilder = new RequestBuilder(
RequestBuilder.POST, getRemoteLogUrl());
try {
String requestData = "";
for (String str : msgQueue) {
requestData += str;
requestData += "\n";
}
requestBuilder.sendRequest(requestData,
new RequestCallback() {
public void onResponseReceived(Request request,
Response response) {
// TODO Auto-generated method stub
}
public void onError(Request request,
Throwable exception) {
// TODO Auto-generated method stub
}
});
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msgQueue.clear();
}
}
};
private VLazyExecutor sendToRemoteLog = new VLazyExecutor(350, doSend);
protected String getRemoteLogUrl() {
return "http://sun-vehje.local:8080/remotelog/";
}
protected void remoteLog(String msg) {
msgQueue.add(msg);
sendToRemoteLog.trigger();
}
/**
* Logs the given message to the debug window.
*
* @param msg
* The message to log. Must not be null.
*/
private void logToDebugWindow(String msg, boolean error) {
Widget row;
if (error) {
row = createErrorHtml(msg);
} else {
row = new HTML(msg);
}
panel.add(row);
if (autoScroll.getValue()) {
row.getElement().scrollIntoView();
}
}
private HTML createErrorHtml(String msg) {
HTML html = new HTML(msg);
html.getElement().getStyle().setColor("#f00");
html.getElement().getStyle().setFontWeight(FontWeight.BOLD);
return html;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.gwt.client.Console#error(java.lang.String)
*/
public void error(String msg) {
if (msg == null) {
msg = "null";
}
logToDebugWindow(msg, true);
GWT.log(msg);
consoleErr(msg);
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.gwt.client.Console#printObject(java.lang.
* Object)
*/
public void printObject(Object msg) {
String str;
if (msg == null) {
str = "null";
} else {
str = msg.toString();
}
panel.add((new Label(str)));
consoleLog(str);
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.gwt.client.Console#dirUIDL(com.vaadin
* .terminal.gwt.client.UIDL)
*/
public void dirUIDL(UIDL u, ApplicationConfiguration conf) {
if (panel.isAttached()) {
panel.add(new VUIDLBrowser(u, conf));
}
consoleDir(u);
// consoleLog(u.getChildrenAsXML());
}
private static native void consoleDir(UIDL u)
/*-{
if($wnd.console && $wnd.console.log) {
if($wnd.console.dir) {
$wnd.console.dir(u);
} else {
$wnd.console.log(u);
}
}
}-*/;
private static native void consoleLog(String msg)
/*-{
if($wnd.console && $wnd.console.log) {
$wnd.console.log(msg);
}
}-*/;
private static native void consoleErr(String msg)
/*-{
if($wnd.console) {
if ($wnd.console.error)
$wnd.console.error(msg);
else if ($wnd.console.log)
$wnd.console.log(msg);
}
}-*/;
public void printLayoutProblems(ValueMap meta, ApplicationConnection ac,
Set<Paintable> zeroHeightComponents,
Set<Paintable> zeroWidthComponents) {
JsArray<ValueMap> valueMapArray = meta
.getJSValueMapArray("invalidLayouts");
int size = valueMapArray.length();
panel.add(new HTML("<div>************************</di>"
+ "<h4>Layouts analyzed on server, total top level problems: "
+ size + " </h4>"));
if (size > 0) {
Tree tree = new Tree();
// Position relative does not work here in IE7
DOM.setStyleAttribute(tree.getElement(), "position", "");
TreeItem root = new TreeItem("Root problems");
for (int i = 0; i < size; i++) {
printLayoutError(valueMapArray.get(i), root, ac);
}
panel.add(tree);
tree.addItem(root);
}
if (zeroHeightComponents.size() > 0 || zeroWidthComponents.size() > 0) {
panel.add(new HTML("<h4> Client side notifications</h4>"
+ " <em>The following relative sized components were "
+ "rendered to a zero size container on the client side."
+ " Note that these are not necessarily invalid "
+ "states, but reported here as they might be.</em>"));
if (zeroHeightComponents.size() > 0) {
panel.add(new HTML(
"<p><strong>Vertically zero size:</strong><p>"));
printClientSideDetectedIssues(zeroHeightComponents, ac);
}
if (zeroWidthComponents.size() > 0) {
panel.add(new HTML(
"<p><strong>Horizontally zero size:</strong><p>"));
printClientSideDetectedIssues(zeroWidthComponents, ac);
}
}
log("************************");
}
private void printClientSideDetectedIssues(
Set<Paintable> zeroHeightComponents, ApplicationConnection ac) {
for (final Paintable paintable : zeroHeightComponents) {
final Container layout = Util.getLayout((Widget) paintable);
VerticalPanel errorDetails = new VerticalPanel();
errorDetails.add(new Label("" + Util.getSimpleName(paintable)
+ " inside " + Util.getSimpleName(layout)));
final CheckBox emphasisInUi = new CheckBox(
"Emphasize components parent in UI (the actual component is not visible)");
emphasisInUi.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (paintable != null) {
Element element2 = ((Widget) layout).getElement();
Widget.setStyleName(element2, "invalidlayout",
emphasisInUi.getValue());
}
}
});
errorDetails.add(emphasisInUi);
panel.add(errorDetails);
}
}
private void printLayoutError(ValueMap valueMap, TreeItem parent,
final ApplicationConnection ac) {
final String pid = valueMap.getString("id");
final Paintable paintable = ac.getPaintable(pid);
TreeItem errorNode = new TreeItem();
VerticalPanel errorDetails = new VerticalPanel();
errorDetails.add(new Label(Util.getSimpleName(paintable) + " id: "
+ pid));
if (valueMap.containsKey("heightMsg")) {
errorDetails.add(new Label("Height problem: "
+ valueMap.getString("heightMsg")));
}
if (valueMap.containsKey("widthMsg")) {
errorDetails.add(new Label("Width problem: "
+ valueMap.getString("widthMsg")));
}
final CheckBox emphasisInUi = new CheckBox("Emphasize component in UI");
emphasisInUi.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (paintable != null) {
Element element2 = ((Widget) paintable).getElement();
Widget.setStyleName(element2, "invalidlayout",
emphasisInUi.getValue());
}
}
});
errorDetails.add(emphasisInUi);
errorNode.setWidget(errorDetails);
if (valueMap.containsKey("subErrors")) {
HTML l = new HTML(
"<em>Expand this node to show problems that may be dependent on this problem.</em>");
errorDetails.add(l);
JsArray<ValueMap> suberrors = valueMap
.getJSValueMapArray("subErrors");
for (int i = 0; i < suberrors.length(); i++) {
ValueMap value = suberrors.get(i);
printLayoutError(value, errorNode, ac);
}
}
parent.addItem(errorNode);
}
public void log(Throwable e) {
if (e instanceof UmbrellaException) {
UmbrellaException ue = (UmbrellaException) e;
for (Throwable t : ue.getCauses()) {
log(t);
}
return;
}
log(Util.getSimpleName(e) + ": " + e.getMessage());
GWT.log(e.getMessage(), e);
}
public void error(Throwable e) {
if (e instanceof UmbrellaException) {
UmbrellaException ue = (UmbrellaException) e;
for (Throwable t : ue.getCauses()) {
error(t);
}
return;
}
error(Util.getSimpleName(e) + ": " + e.getMessage());
GWT.log(e.getMessage(), e);
}
public void init() {
panel = new FlowPanel();
if (!quietMode) {
DOM.appendChild(getContainerElement(), caption);
setWidget(panel);
caption.setClassName("v-debug-console-caption");
setStyleName("v-debug-console");
DOM.setStyleAttribute(getElement(), "zIndex", 20000 + "");
DOM.setStyleAttribute(getElement(), "overflow", "hidden");
sinkEvents(Event.ONDBLCLICK);
sinkEvents(Event.MOUSEEVENTS);
panel.setStyleName("v-debug-console-content");
caption.setInnerHTML("Debug window");
caption.setTitle(help);
show();
setToDefaultSizeAndPos();
actions = new HorizontalPanel();
actions.add(clear);
actions.add(restart);
actions.add(forceLayout);
actions.add(analyzeLayout);
actions.add(savePosition);
savePosition
.setTitle("Saves the position and size of debug console to a cookie");
actions.add(autoScroll);
actions.add(hostedMode);
if (Location.getParameter("gwt.codesvr") != null) {
hostedMode.setValue(true);
}
hostedMode.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (hostedMode.getValue()) {
addHMParameter();
} else {
removeHMParameter();
}
}
private void addHMParameter() {
UrlBuilder createUrlBuilder = Location.createUrlBuilder();
createUrlBuilder.setParameter("gwt.codesvr",
"localhost:9997");
Location.assign(createUrlBuilder.buildString());
}
private void removeHMParameter() {
UrlBuilder createUrlBuilder = Location.createUrlBuilder();
createUrlBuilder.removeParameter("gwt.codesvr");
Location.assign(createUrlBuilder.buildString());
}
});
autoScroll
.setTitle("Automatically scroll so that new messages are visible");
panel.add(actions);
panel.add(new HTML("<i>" + help + "</i>"));
clear.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int width = panel.getOffsetWidth();
int height = panel.getOffsetHeight();
panel = new FlowPanel();
panel.setPixelSize(width, height);
panel.setStyleName("v-debug-console-content");
panel.add(actions);
setWidget(panel);
}
});
restart.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String queryString = Window.Location.getQueryString();
if (queryString != null
&& queryString.contains("restartApplications")) {
Window.Location.reload();
} else {
String url = Location.getHref();
String separator = "?";
if (url.contains("?")) {
separator = "&";
}
if (!url.contains("restartApplication")) {
url += separator;
url += "restartApplication";
}
if (!"".equals(Location.getHash())) {
String hash = Location.getHash();
url = url.replace(hash, "") + hash;
}
Window.Location.replace(url);
}
}
});
forceLayout.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// TODO for each client in appconf force layout
// VDebugConsole.this.client.forceLayout();
}
});
analyzeLayout.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
List<ApplicationConnection> runningApplications = ApplicationConfiguration
.getRunningApplications();
for (ApplicationConnection applicationConnection : runningApplications) {
applicationConnection.analyzeLayouts();
}
}
});
analyzeLayout
.setTitle("Analyzes currently rendered view and "
+ "reports possible common problems in usage of relative sizes."
+ "Will cause server visit/rendering of whole screen and loss of"
+ " all non committed variables form client side.");
savePosition.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String pos = getAbsoluteLeft() + "," + getAbsoluteTop()
+ "," + getOffsetWidth() + "," + getOffsetHeight()
+ "," + autoScroll.getValue();
Cookies.setCookie(POS_COOKIE_NAME, pos);
}
});
}
log("Starting Vaadin client side engine. Widgetset: "
+ GWT.getModuleName());
log("Widget set is built on version: "
+ ApplicationConfiguration.VERSION);
logToDebugWindow("<div class=\"v-theme-version v-theme-version-"
+ ApplicationConfiguration.VERSION.replaceAll("\\.", "_")
+ "\">Warning: widgetset version "
+ ApplicationConfiguration.VERSION
+ " does not seem to match theme version </div>", true);
}
public void setQuietMode(boolean quietDebugMode) {
quietMode = quietDebugMode;
}
}
|