1 11 package org.eclipse.update.internal.ui.views; 12 13 import java.io.BufferedReader ; 14 import java.io.BufferedWriter ; 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.InputStreamReader ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PrintWriter ; 23 import java.text.DateFormat ; 24 import java.util.Date ; 25 import java.util.StringTokenizer ; 26 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.core.runtime.Status; 32 import org.eclipse.jface.action.Action; 33 import org.eclipse.jface.resource.ImageDescriptor; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.update.configurator.ConfiguratorUtils; 36 import org.eclipse.update.internal.ui.UpdateUI; 37 import org.eclipse.update.internal.ui.UpdateUIMessages; 38 39 public class InstallationHistoryAction extends Action { 40 private BufferedReader buffRead; 42 private PrintWriter htmlLog; 43 private File tempFile; 44 private String rowType; 45 private IPath path; 46 private static final String lightBlue = "#EEEEFF"; private static final String white = "#FFFFFF"; private static final String darkBlue = "#99AADD"; private static final String ACTIVITY = "ACTIVITY"; 52 public InstallationHistoryAction(Shell shell, String text, ImageDescriptor desc) { 53 super(text, desc); 54 String location = 56 ConfiguratorUtils 57 .getCurrentPlatformConfiguration() 58 .getConfigurationLocation() 59 .getFile(); 60 path = new Path(location).removeLastSegments(1).append("install.log"); rowType = "light-row"; } 63 64 public void run() { 65 try { 66 openLog(); 67 parseLog(); 68 UpdateUI.showURL("file:" + getTempFile().getPath().toString()); } catch (CoreException e) { 70 UpdateUI.logException(e); 71 } finally { 72 closeLog(); 73 } 74 } 75 76 private void openLog() throws CoreException { 77 try { 78 InputStream is = new FileInputStream (path.toOSString()); 80 InputStreamReader isr = new InputStreamReader (is,"UTF-8"); buffRead = new BufferedReader (isr); 83 htmlLog = new PrintWriter (new BufferedWriter (new OutputStreamWriter (new FileOutputStream (getTempFile()), "UTF-8"))); } catch (Exception e) { 85 throwCoreException(e); 86 } 87 } 88 89 private File getTempFile() throws CoreException { 90 if (tempFile == null) { 91 try { 92 tempFile = File.createTempFile("install-log", ".html"); tempFile.deleteOnExit(); 94 } catch (IOException e) { 95 throwCoreException(e); 96 } 97 } 98 return tempFile; 99 } 100 101 private void parseLog() throws CoreException { 102 105 try { 106 String type, date, status, target, action; 107 StringTokenizer htmlCode; 108 109 htmlLog.println("<html>"); htmlLog.println("<head>"); htmlLog.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); htmlLog.println("<title>Install-Log</title>"); addCSS(); 114 htmlLog.println("</head>"); htmlLog.println("<body>"); String title = UpdateUIMessages.InstallationHistoryAction_title; 117 String desc = UpdateUIMessages.InstallationHistoryAction_desc; 118 htmlLog.println("<h1 class=title>"+title+"</h1>"); htmlLog.println("<p class=bodyText>"+desc+"</p>"); 121 htmlLog.println("<center>"); 123 htmlLog.println("<table width =100% border=0 cellspacing=1 cellpadding=2>"); 125 while (buffRead.ready()) { 126 127 htmlCode = new StringTokenizer (buffRead.readLine()); 128 while (!(htmlCode.hasMoreElements())) { 129 if (!buffRead.ready()) 130 return; 131 htmlCode = new StringTokenizer (buffRead.readLine()); 132 } 133 134 type = htmlCode.nextToken(); 135 type = type.substring(type.indexOf("!") + 1, type.length()); 137 if (type.equals(ACTIVITY)) { 138 target = ""; Date d = new Date (new Long (htmlCode.nextToken()).longValue()); 140 DateFormat df = DateFormat.getDateTimeInstance(); 141 date = df.format(d); 142 htmlCode.nextToken("."); htmlCode.nextToken(" "); while (htmlCode.countTokens() > 2) 146 target = target + " " + htmlCode.nextToken(); 148 action = htmlCode.nextToken(); 149 status = htmlCode.nextToken(); 150 151 addActivity(action, date, status, target); 152 153 } else { 154 htmlLog.println(); 155 htmlLog.println( 156 "<tr id=separator><td colspan=4></td></tr>"); htmlLog.println(); 158 Date d = new Date (new Long (htmlCode.nextToken()).longValue()); 159 DateFormat df = DateFormat.getDateTimeInstance(); 160 date = df.format(d); 161 addConfigurationHeader(date); 165 addActivityHeader(); 166 } 167 168 } 169 htmlLog.println("</table>"); htmlLog.println("</body>"); htmlLog.println("</html>"); htmlLog.flush(); 173 174 } catch (Exception e) { 175 throwCoreException(e); 176 } 177 178 } 179 180 private void addActivity( 181 String type, 182 String date, 183 String status, 184 String target) { 185 htmlLog.print("<tr class=" + rowType + ">"); htmlLog.print("<td class=log-text width=30%>"); htmlLog.print(date); 188 htmlLog.println("</td>"); htmlLog.print("<td class=log-text width=40%>"); htmlLog.print(target); 191 htmlLog.println("</td>"); htmlLog.print("<td class=log-text width=20%>"); htmlLog.print(type); 194 htmlLog.println("</td>"); htmlLog.print("<td class=log-text width=10%>"); htmlLog.print(status); 197 htmlLog.println("</td></tr>"); toggleRowColor(); 199 } 200 201 private void addCSS() { 202 htmlLog.println("<STYLE type=\"text/css\">"); htmlLog.println("H1.title { font-family: sans-serif; color: #99AACC }"); htmlLog.println("P.bodyText { font-family: sans-serif; font-size: 9pt; }"); htmlLog.println( 206 "TD.log-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white}"); htmlLog.println( 208 "TD.log-text { font-family: sans-serif; font-style: normal; font-weight: lighter; font-size: 8pt; color:black}"); htmlLog.println( 210 "TD.config-log-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white; border-top:10px solid white}"); htmlLog.println("TR.light-row {background:" + white + "}"); htmlLog.println("TR.dark-row {background:" + lightBlue + "}"); htmlLog.println("TR.header {background:" + darkBlue + "}"); htmlLog.println("</STYLE>"); } 217 218 private void addActivityHeader() { 219 htmlLog.print("<tr class=header>"); htmlLog.print("<td class=log-header>"); htmlLog.print(UpdateUIMessages.InstallationHistoryAction_dateTime); 222 htmlLog.print("</td>"); htmlLog.print("<td class=log-header>"); htmlLog.print(UpdateUIMessages.InstallationHistoryAction_target); 225 htmlLog.print("</td>"); htmlLog.print("<td class=log-header>"); htmlLog.print(UpdateUIMessages.InstallationHistoryAction_action); 228 htmlLog.print("</td>"); htmlLog.print("<td class=log-header>"); htmlLog.print(UpdateUIMessages.InstallationHistoryAction_status); 231 htmlLog.println("</td></tr>"); } 233 234 private void addConfigurationHeader(String date) { 235 if (date == null) 236 return; 237 238 htmlLog.print("<tr class=header>"); htmlLog.print("<td class=config-log-header colspan=4>"); htmlLog.print(date); 241 htmlLog.println("</td></tr>"); } 243 244 private void toggleRowColor() { 245 if (rowType.equals("light-row")) rowType = "dark-row"; else 248 rowType = "light-row"; } 250 251 private void throwCoreException(Throwable e) throws CoreException { 252 throw new CoreException( 253 new Status( 254 IStatus.ERROR, 255 UpdateUI.getPluginId(), 256 IStatus.ERROR, 257 UpdateUIMessages.InstallationHistoryAction_errors, 258 e)); 259 } 260 261 private void closeLog() { 262 try { 263 if (buffRead != null) 264 buffRead.close(); 265 if (htmlLog != null) 266 htmlLog.close(); 267 } catch (IOException e) { 268 } finally { 269 buffRead = null; 270 htmlLog = null; 271 } 272 } 273 274 } 275 | Popular Tags |