1 11 12 package org.eclipse.ui.internal; 13 14 import java.io.OutputStream ; 15 import java.io.PrintStream ; 16 import java.util.LinkedList ; 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 class StartupProgressMonitor implements IProgressMonitor { 24 25 private class AsynchronousPrinter extends Thread { 30 private PrintStream printStream; 31 32 private LinkedList tasks = new LinkedList (); 37 38 private AsynchronousPrinter(OutputStream stream) { 39 this.printStream = new PrintStream (stream, false); 40 setName("Startup Progress Printer"); } 42 43 public void run() { 44 while (true) { 45 Object task; 46 synchronized (tasks) { 47 while (tasks.isEmpty()) { 48 try { 49 tasks.wait(); 50 } catch (InterruptedException e) { 51 return; 52 } 53 } 54 task = tasks.removeFirst(); 55 } 56 if (task == null) { 57 printStream.close(); 58 return; 59 } else if ("".equals(task)) { printStream.flush(); 61 } else { 62 printStream.print(task.toString() + "\n"); } 64 } 65 } 66 67 private void addTask(Object o) { 68 synchronized (tasks) { 69 tasks.addLast(o); 70 tasks.notifyAll(); 71 } 72 } 73 74 void println(String string) { 75 addTask(string); 76 } 77 78 void flush() { 79 addTask(""); } 81 82 void close() { 83 addTask(null); 84 } 85 } 86 87 private static boolean progressMonitorReturned = false; 88 89 96 static IProgressMonitor getInstance() { 97 if (!progressMonitorReturned) { 98 OutputStream 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 os) { 117 printer = new AsynchronousPrinter(os); 118 } 119 120 private void reportWork(int value) { 121 if (lastReportedWork != value) { 122 printer.println("value=" + value); printer.flush(); 124 lastReportedWork = value; 125 } 126 } 127 128 public void beginTask(String name, int total) { 129 this.totalWork = total; 130 printer.start(); 131 printInitializationData(); 132 } 133 134 private void printInitializationData() { 135 String progressRect = null; 136 String messageRect = null; 137 String 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"; } 150 if (messageRect == null) { 151 messageRect = "10,35,300,15"; } 153 int foregroundColorInteger; 154 try { 155 foregroundColorInteger = Integer.parseInt(foregroundColor, 16); 156 } catch (Exception ex) { 157 foregroundColorInteger = 13817855; } 159 printer.println("foreground=" + foregroundColorInteger); printer.println("messageRect=" + messageRect); printer.println("progressRect=" + progressRect); printer.println("maximum=" + totalWork); 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 } 194 195 public void setTaskName(String name) { 196 } 198 199 public void subTask(String name) { 200 printer.println("message=" + name.replace('\n', ' ')); printer.flush(); 202 } 203 204 public void worked(int work) { 205 internalWorked(work); 206 } 207 } 208 | Popular Tags |