KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > log > DetachedTreeLoggerWindow


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util.log;
17
18 import com.google.gwt.dev.shell.LowLevel;
19
20 import org.eclipse.swt.layout.FillLayout;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23
24 /**
25  * Useful for debugging, this class manages to standalone window
26  * and provides access to a logger you can use to write to it.
27  *
28  */

29 public class DetachedTreeLoggerWindow implements Runnable JavaDoc {
30   private static DetachedTreeLoggerWindow singleton;
31
32   /**
33    * Provides a reference to a singleton <code>DetachedTreeLoggerWindow</code>.
34    *
35    * @param caption the text to appear in the windows title bar.
36    * @param width the widget of the window
37    * @param height the height of the window
38    * @param autoScroll whether or not the window should autoscroll as output is
39    * produced
40    * @return a proxy object providing limited control of the window.
41    */

42   public static synchronized DetachedTreeLoggerWindow getInstance(
43       final String JavaDoc caption, final int width, final int height,
44       final boolean autoScroll) {
45     if (singleton == null) {
46       singleton = new DetachedTreeLoggerWindow(caption, width, height,
47           autoScroll);
48     }
49     return singleton;
50   }
51
52   private final Shell shell;
53   private final AbstractTreeLogger logger;
54   private boolean isRunning = false;
55
56   private DetachedTreeLoggerWindow(final String JavaDoc caption, final int width,
57       final int height, final boolean autoScroll) {
58
59     shell = new Shell(Display.getCurrent());
60     shell.setText(caption);
61     FillLayout fillLayout = new FillLayout();
62     fillLayout.marginWidth = 0;
63     fillLayout.marginHeight = 0;
64     shell.setLayout(fillLayout);
65
66     final TreeLoggerWidget treeLoggerWidget = new TreeLoggerWidget(shell);
67     treeLoggerWidget.setAutoScroll(autoScroll);
68     logger = treeLoggerWidget.getLogger();
69
70     shell.setImage(LowLevel.loadImage("gwt.ico"));
71     shell.setSize(width, height);
72     shell.open();
73   }
74
75   public AbstractTreeLogger getLogger() {
76     return logger;
77   }
78
79   public synchronized boolean isRunning() {
80     return isRunning;
81   }
82    
83   public void run() {
84     if (!maybeStart()) {
85       throw new IllegalStateException JavaDoc(
86           "DetachedTreeLogger window is already running.");
87     }
88
89     final Display display = shell.getDisplay();
90     while (!shell.isDisposed()) {
91       if (!display.readAndDispatch()) {
92         display.sleep();
93       }
94     }
95   }
96
97   private synchronized boolean maybeStart() {
98     if (isRunning) {
99       return false;
100     }
101     isRunning = true;
102     return true;
103   }
104 }
105
Popular Tags