KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > views > InstallationHistoryAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.update.internal.ui.views;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.BufferedWriter JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
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 Shell shell;
41
private BufferedReader JavaDoc buffRead;
42     private PrintWriter JavaDoc htmlLog;
43     private File JavaDoc tempFile;
44     private String JavaDoc rowType;
45     private IPath path;
46     private static final String JavaDoc lightBlue = "#EEEEFF"; //$NON-NLS-1$
47
private static final String JavaDoc white = "#FFFFFF"; //$NON-NLS-1$
48
private static final String JavaDoc darkBlue = "#99AADD"; //$NON-NLS-1$
49
// private static final String CONFIGURATION = "CONFIGURATION"; //$NON-NLS-1$
50
private static final String JavaDoc ACTIVITY = "ACTIVITY"; //$NON-NLS-1$
51

52     public InstallationHistoryAction(Shell shell, String JavaDoc text, ImageDescriptor desc) {
53         super(text, desc);
54         //this.shell = shell;
55
String JavaDoc location =
56             ConfiguratorUtils
57                 .getCurrentPlatformConfiguration()
58                 .getConfigurationLocation()
59                 .getFile();
60         path = new Path(location).removeLastSegments(1).append("install.log"); //$NON-NLS-1$
61
rowType = "light-row"; //$NON-NLS-1$
62
}
63
64     public void run() {
65         try {
66             openLog();
67             parseLog();
68             UpdateUI.showURL("file:" + getTempFile().getPath().toString()); //$NON-NLS-1$
69
} catch (CoreException e) {
70             UpdateUI.logException(e);
71         } finally {
72             closeLog();
73         }
74     }
75
76     private void openLog() throws CoreException {
77         try {
78             // throws FileNotFoundException, IOException
79
InputStream JavaDoc is = new FileInputStream JavaDoc(path.toOSString());
80             // throws UnsupportedEncodingException
81
InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(is,"UTF-8"); //$NON-NLS-1$
82
buffRead = new BufferedReader JavaDoc(isr);
83             htmlLog = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(getTempFile()), "UTF-8"))); //$NON-NLS-1$
84
} catch (Exception JavaDoc e) {
85             throwCoreException(e);
86         }
87     }
88
89     private File JavaDoc getTempFile() throws CoreException {
90         if (tempFile == null) {
91             try {
92                 tempFile = File.createTempFile("install-log", ".html"); //$NON-NLS-1$ //$NON-NLS-2$
93
tempFile.deleteOnExit();
94             } catch (IOException JavaDoc e) {
95                 throwCoreException(e);
96             }
97         }
98         return tempFile;
99     }
100
101     private void parseLog() throws CoreException {
102         // !CONFIGURATION <configuration-date>
103
// !ACTIVITY <date> <target> <action> <status>
104

105         try {
106             String JavaDoc type, date, status, target, action;
107             StringTokenizer JavaDoc htmlCode;
108
109             htmlLog.println("<html>"); //$NON-NLS-1$
110
htmlLog.println("<head>"); //$NON-NLS-1$
111
htmlLog.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); //$NON-NLS-1$
112
htmlLog.println("<title>Install-Log</title>"); //$NON-NLS-1$
113
addCSS();
114             htmlLog.println("</head>"); //$NON-NLS-1$
115
htmlLog.println("<body>"); //$NON-NLS-1$
116
String JavaDoc title = UpdateUIMessages.InstallationHistoryAction_title;
117             String JavaDoc desc = UpdateUIMessages.InstallationHistoryAction_desc;
118             htmlLog.println("<h1 class=title>"+title+"</h1>"); //$NON-NLS-1$ //$NON-NLS-2$
119
htmlLog.println("<p class=bodyText>"+desc+"</p>"); //$NON-NLS-1$ //$NON-NLS-2$
120

121             htmlLog.println("<center>"); //$NON-NLS-1$
122

123             htmlLog.println("<table width =100% border=0 cellspacing=1 cellpadding=2>"); //$NON-NLS-1$
124

125             while (buffRead.ready()) {
126
127                 htmlCode = new StringTokenizer JavaDoc(buffRead.readLine());
128                 while (!(htmlCode.hasMoreElements())) {
129                     if (!buffRead.ready())
130                         return;
131                     htmlCode = new StringTokenizer JavaDoc(buffRead.readLine());
132                 }
133
134                 type = htmlCode.nextToken();
135                 type = type.substring(type.indexOf("!") + 1, type.length()); //$NON-NLS-1$
136

137                 if (type.equals(ACTIVITY)) {
138                     target = ""; //$NON-NLS-1$
139
Date JavaDoc d = new Date JavaDoc(new Long JavaDoc(htmlCode.nextToken()).longValue());
140                     DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
141                     date = df.format(d);
142                     // ignore string date
143
htmlCode.nextToken("."); //$NON-NLS-1$
144
htmlCode.nextToken(" "); //$NON-NLS-1$
145
while (htmlCode.countTokens() > 2)
146                         target = target + " " + htmlCode.nextToken(); //$NON-NLS-1$
147

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>"); //$NON-NLS-1$
157
htmlLog.println();
158                     Date JavaDoc d = new Date JavaDoc(new Long JavaDoc(htmlCode.nextToken()).longValue());
159                     DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
160                     date = df.format(d);
161 // date = ""; //$NON-NLS-1$
162
// while (htmlCode.countTokens() > 0)
163
// date = date + " " + htmlCode.nextToken(); //$NON-NLS-1$
164
addConfigurationHeader(date);
165                     addActivityHeader();
166                 }
167
168             }
169             htmlLog.println("</table>"); //$NON-NLS-1$
170
htmlLog.println("</body>"); //$NON-NLS-1$
171
htmlLog.println("</html>"); //$NON-NLS-1$
172
htmlLog.flush();
173
174         } catch (Exception JavaDoc e) {
175             throwCoreException(e);
176         }
177
178     }
179
180     private void addActivity(
181         String JavaDoc type,
182         String JavaDoc date,
183         String JavaDoc status,
184         String JavaDoc target) {
185         htmlLog.print("<tr class=" + rowType + ">"); //$NON-NLS-1$ //$NON-NLS-2$
186
htmlLog.print("<td class=log-text width=30%>"); //$NON-NLS-1$
187
htmlLog.print(date);
188         htmlLog.println("</td>"); //$NON-NLS-1$
189
htmlLog.print("<td class=log-text width=40%>"); //$NON-NLS-1$
190
htmlLog.print(target);
191         htmlLog.println("</td>"); //$NON-NLS-1$
192
htmlLog.print("<td class=log-text width=20%>"); //$NON-NLS-1$
193
htmlLog.print(type);
194         htmlLog.println("</td>"); //$NON-NLS-1$
195
htmlLog.print("<td class=log-text width=10%>"); //$NON-NLS-1$
196
htmlLog.print(status);
197         htmlLog.println("</td></tr>"); //$NON-NLS-1$
198
toggleRowColor();
199     }
200
201     private void addCSS() {
202         htmlLog.println("<STYLE type=\"text/css\">"); //$NON-NLS-1$
203
htmlLog.println("H1.title { font-family: sans-serif; color: #99AACC }"); //$NON-NLS-1$
204
htmlLog.println("P.bodyText { font-family: sans-serif; font-size: 9pt; }"); //$NON-NLS-1$
205
htmlLog.println(
206             "TD.log-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white}"); //$NON-NLS-1$
207
htmlLog.println(
208             "TD.log-text { font-family: sans-serif; font-style: normal; font-weight: lighter; font-size: 8pt; color:black}"); //$NON-NLS-1$
209
htmlLog.println(
210         // "TD.config-log-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white; ;text-align: right; border-top:10px solid white}");
211
"TD.config-log-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white; border-top:10px solid white}"); //$NON-NLS-1$
212
htmlLog.println("TR.light-row {background:" + white + "}"); //$NON-NLS-1$ //$NON-NLS-2$
213
htmlLog.println("TR.dark-row {background:" + lightBlue + "}"); //$NON-NLS-1$ //$NON-NLS-2$
214
htmlLog.println("TR.header {background:" + darkBlue + "}"); //$NON-NLS-1$ //$NON-NLS-2$
215
htmlLog.println("</STYLE>"); //$NON-NLS-1$
216
}
217
218     private void addActivityHeader() {
219         htmlLog.print("<tr class=header>"); //$NON-NLS-1$
220
htmlLog.print("<td class=log-header>"); //$NON-NLS-1$
221
htmlLog.print(UpdateUIMessages.InstallationHistoryAction_dateTime);
222         htmlLog.print("</td>"); //$NON-NLS-1$
223
htmlLog.print("<td class=log-header>"); //$NON-NLS-1$
224
htmlLog.print(UpdateUIMessages.InstallationHistoryAction_target);
225         htmlLog.print("</td>"); //$NON-NLS-1$
226
htmlLog.print("<td class=log-header>"); //$NON-NLS-1$
227
htmlLog.print(UpdateUIMessages.InstallationHistoryAction_action);
228         htmlLog.print("</td>"); //$NON-NLS-1$
229
htmlLog.print("<td class=log-header>"); //$NON-NLS-1$
230
htmlLog.print(UpdateUIMessages.InstallationHistoryAction_status);
231         htmlLog.println("</td></tr>"); //$NON-NLS-1$
232
}
233
234     private void addConfigurationHeader(String JavaDoc date) {
235         if (date == null)
236             return;
237
238         htmlLog.print("<tr class=header>"); //$NON-NLS-1$
239
htmlLog.print("<td class=config-log-header colspan=4>"); //$NON-NLS-1$
240
htmlLog.print(date);
241         htmlLog.println("</td></tr>"); //$NON-NLS-1$
242
}
243
244     private void toggleRowColor() {
245         if (rowType.equals("light-row")) //$NON-NLS-1$
246
rowType = "dark-row"; //$NON-NLS-1$
247
else
248             rowType = "light-row"; //$NON-NLS-1$
249
}
250
251     private void throwCoreException(Throwable JavaDoc 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 JavaDoc e) {
268         } finally {
269             buffRead = null;
270             htmlLog = null;
271         }
272     }
273
274 }
275
Popular Tags