KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > StartupProgressMonitor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import java.io.OutputStream JavaDoc;
15 import java.io.PrintStream JavaDoc;
16 import java.util.LinkedList JavaDoc;
17
18 import org.eclipse.core.runtime.IProduct;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.ui.branding.IProductConstants;
22
23 /* package */class StartupProgressMonitor implements IProgressMonitor {
24
25     // this class prints to the output stream and calls flush()
26
// on it in a separate thread so that the main thread will
27
// not block if the other end of the stream (the executable
28
// that displays the splash screen) misbehaves.
29
private class AsynchronousPrinter extends Thread JavaDoc {
30         private PrintStream JavaDoc printStream;
31
32         // this list holds strings to print. The empty string is
33
// added to the list if the output stream should be flushed.
34
// If the output stream should be closed, null is added
35
// to this list.
36
private LinkedList JavaDoc tasks = new LinkedList JavaDoc();
37
38         private AsynchronousPrinter(OutputStream JavaDoc stream) {
39             this.printStream = new PrintStream JavaDoc(stream, false);
40             setName("Startup Progress Printer"); //$NON-NLS-1$
41
}
42
43         public void run() {
44             while (true) {
45                 Object JavaDoc task;
46                 synchronized (tasks) {
47                     while (tasks.isEmpty()) {
48                         try {
49                             tasks.wait();
50                         } catch (InterruptedException JavaDoc e) {
51                             return;
52                         }
53                     }
54                     task = tasks.removeFirst();
55                 }
56                 if (task == null) {
57                     printStream.close();
58                     return;
59                 } else if ("".equals(task)) { //$NON-NLS-1$
60
printStream.flush();
61                 } else {
62                     printStream.print(task.toString() + "\n"); //$NON-NLS-1$
63
}
64             }
65         }
66
67         private void addTask(Object JavaDoc o) {
68             synchronized (tasks) {
69                 tasks.addLast(o);
70                 tasks.notifyAll();
71             }
72         }
73
74         void println(String JavaDoc string) {
75             addTask(string);
76         }
77
78         void flush() {
79             addTask(""); //$NON-NLS-1$
80
}
81
82         void close() {
83             addTask(null);
84         }
85     }
86
87     private static boolean progressMonitorReturned = false;
88
89     /**
90      * Returns a progress monitor to report startup progress, or
91      * <code>null</code> if progress cannot be reported. This method will
92      * return <code>null</code> if called more than once.
93      *
94      * @return a progress monitor, or null
95      */

96     /* package */static IProgressMonitor getInstance() {
97         if (!progressMonitorReturned) {
98             OutputStream JavaDoc outputStream = WorkbenchPlugin.getDefault()
99                     .getSplashStream();
100             if (outputStream != null) {
101                 progressMonitorReturned = true;
102                 return new StartupProgressMonitor(outputStream);
103             }
104         }
105         return null;
106     }
107
108     private double sumWorked = 0;
109
110     private int totalWork;
111
112     private int lastReportedWork = -1;
113
114     private AsynchronousPrinter printer;
115
116     private StartupProgressMonitor(OutputStream JavaDoc os) {
117         printer = new AsynchronousPrinter(os);
118     }
119
120     private void reportWork(int value) {
121         if (lastReportedWork != value) {
122             printer.println("value=" + value); //$NON-NLS-1$
123
printer.flush();
124             lastReportedWork = value;
125         }
126     }
127
128     public void beginTask(String JavaDoc name, int total) {
129         this.totalWork = total;
130         printer.start();
131         printInitializationData();
132     }
133
134     private void printInitializationData() {
135         String JavaDoc progressRect = null;
136         String JavaDoc messageRect = null;
137         String JavaDoc foregroundColor = null;
138         IProduct product = Platform.getProduct();
139         if (product != null) {
140             progressRect = product
141                     .getProperty(IProductConstants.STARTUP_PROGRESS_RECT);
142             messageRect = product
143                     .getProperty(IProductConstants.STARTUP_MESSAGE_RECT);
144             foregroundColor = product
145                     .getProperty(IProductConstants.STARTUP_FOREGROUND_COLOR);
146         }
147         if (progressRect == null) {
148             progressRect = "10,10,300,15"; //$NON-NLS-1$
149
}
150         if (messageRect == null) {
151             messageRect = "10,35,300,15"; //$NON-NLS-1$
152
}
153         int foregroundColorInteger;
154         try {
155             foregroundColorInteger = Integer.parseInt(foregroundColor, 16);
156         } catch (Exception JavaDoc ex) {
157             foregroundColorInteger = 13817855; // D2D7FF=white
158
}
159         printer.println("foreground=" + foregroundColorInteger); //$NON-NLS-1$
160
printer.println("messageRect=" + messageRect); //$NON-NLS-1$
161
printer.println("progressRect=" + progressRect); //$NON-NLS-1$
162
printer.println("maximum=" + totalWork); //$NON-NLS-1$
163
printer.flush();
164     }
165
166     public void done() {
167         if (lastReportedWork < totalWork) {
168             reportWork(totalWork);
169         }
170         printer.close();
171     }
172
173     public void internalWorked(double work) {
174         if (work == 0) {
175             return;
176         }
177         sumWorked += work;
178         if (sumWorked > totalWork) {
179             sumWorked = totalWork;
180         }
181         if (sumWorked < 0) {
182             sumWorked = 0;
183         }
184         reportWork((int) sumWorked);
185     }
186
187     public boolean isCanceled() {
188         return false;
189     }
190
191     public void setCanceled(boolean value) {
192         // cannot cancel
193
}
194
195     public void setTaskName(String JavaDoc name) {
196         // ignore, this does not change anything in the splash screen
197
}
198
199     public void subTask(String JavaDoc name) {
200         printer.println("message=" + name.replace('\n', ' ')); //$NON-NLS-1$
201
printer.flush();
202     }
203
204     public void worked(int work) {
205         internalWorked(work);
206     }
207 }
208
Popular Tags