KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > AboutSystemDialog


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.ui.internal.dialogs;
12
13 import java.io.File JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.resource.JFaceResources;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.dnd.Clipboard;
35 import org.eclipse.swt.dnd.TextTransfer;
36 import org.eclipse.swt.dnd.Transfer;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.about.ISystemSummarySection;
47 import org.eclipse.ui.internal.IWorkbenchConstants;
48 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
49 import org.eclipse.ui.internal.WorkbenchMessages;
50 import org.eclipse.ui.internal.WorkbenchPlugin;
51 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
52
53 import com.ibm.icu.text.Collator;
54 import com.ibm.icu.text.DateFormat;
55
56 /**
57  * Displays system information about the eclipse application. The content of
58  * what is displayed is selectable through the
59  * <code>org.eclipse.ui.systemSummaryExtensions</code> extension point.
60  */

61 public final class AboutSystemDialog extends ProductInfoDialog {
62
63     private Text text;
64     
65     private final static int BROWSE_ERROR_LOG_BUTTON = IDialogConstants.CLIENT_ID;
66
67     private final static int COPY_TO_CLIPBOARD_BUTTON = IDialogConstants.CLIENT_ID + 1;
68     
69     private final static String JavaDoc ERROR_LOG_COPY_FILENAME = "log"; //$NON-NLS-1$
70

71     /**
72      * Create an instance of this dialog.
73      *
74      * @param parentShell the parent shell
75      */

76     public AboutSystemDialog(Shell parentShell) {
77         super(parentShell);
78         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
79     }
80
81     /* (non-Javadoc)
82      * Method declared on Window.
83      */

84     protected void configureShell(Shell newShell) {
85         super.configureShell(newShell);
86         newShell.setText(WorkbenchMessages.SystemSummary_title);
87         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
88                 IWorkbenchHelpContextIds.SYSTEM_SUMMARY_DIALOG);
89     }
90
91     /* (non-Javadoc)
92      * Method declared on Dialog.
93      */

94     protected void createButtonsForButtonBar(Composite parent) {
95         parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
96
97         Button button = createButton(parent, BROWSE_ERROR_LOG_BUTTON, WorkbenchMessages.AboutSystemDialog_browseErrorLogName, false);
98         String JavaDoc filename = Platform.getLogFileLocation().toOSString();
99         button.setEnabled(new File JavaDoc(filename).exists());
100
101         createButton(parent, COPY_TO_CLIPBOARD_BUTTON, WorkbenchMessages.AboutSystemDialog_copyToClipboardName, false);
102
103         new Label(parent, SWT.NONE).setLayoutData(new GridData(
104                 GridData.FILL_HORIZONTAL));
105         GridLayout layout = (GridLayout) parent.getLayout();
106         layout.numColumns++;
107         layout.makeColumnsEqualWidth = false;
108
109         createButton(parent, IDialogConstants.CLOSE_ID,
110                 IDialogConstants.CLOSE_LABEL, true);
111     }
112
113     /* (non-Javadoc)
114      * Method declared on Dialog.
115      */

116     protected Control createDialogArea(Composite parent) {
117         Composite outer = (Composite) super.createDialogArea(parent);
118
119         text = new Text(outer, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY
120                 | SWT.V_SCROLL | SWT.NO_FOCUS | SWT.H_SCROLL);
121         text.setBackground(parent.getDisplay().getSystemColor(
122                 SWT.COLOR_LIST_BACKGROUND));
123         GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
124                 | GridData.VERTICAL_ALIGN_FILL);
125         gridData.grabExcessVerticalSpace = true;
126         gridData.grabExcessHorizontalSpace = true;
127         gridData.heightHint = convertVerticalDLUsToPixels(300);
128         gridData.widthHint = convertHorizontalDLUsToPixels(400);
129         text.setLayoutData(gridData);
130         text.setText(getSystemSummary());
131         text.setFont(JFaceResources.getTextFont());
132         return outer;
133     }
134
135     private String JavaDoc getSystemSummary() {
136         StringWriter JavaDoc out = new StringWriter JavaDoc();
137         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(out);
138         writer.println(NLS.bind(WorkbenchMessages.SystemSummary_timeStamp,
139                 DateFormat
140                         .getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
141                         .format(new Date JavaDoc())));
142
143         appendExtensions(writer);
144         writer.close();
145         return out.toString();
146     }
147
148     /*
149      * Appends the contents of all extentions to the configurationLogSections
150      * extension point.
151      */

152     private void appendExtensions(PrintWriter JavaDoc writer) {
153         IConfigurationElement[] configElements = getSortedExtensions();
154         for (int i = 0; i < configElements.length; ++i) {
155             IConfigurationElement element = configElements[i];
156
157             Object JavaDoc obj = null;
158             try {
159                 obj = WorkbenchPlugin.createExtension(element,
160                         IWorkbenchConstants.TAG_CLASS);
161             } catch (CoreException e) {
162                 WorkbenchPlugin.log(
163                         "could not create class attribute for extension", //$NON-NLS-1$
164
e.getStatus());
165             }
166
167             writer.println();
168             writer.println(NLS.bind(WorkbenchMessages.SystemSummary_sectionTitle, element.getAttribute("sectionTitle") )); //$NON-NLS-1$
169

170             if (obj instanceof ISystemSummarySection) {
171                 ISystemSummarySection logSection = (ISystemSummarySection) obj;
172                 logSection.write(writer);
173             } else {
174                 writer.println(WorkbenchMessages.SystemSummary_sectionError);
175             }
176         }
177     }
178
179     private IConfigurationElement[] getSortedExtensions() {
180         IConfigurationElement[] configElements = Platform
181                 .getExtensionRegistry().getConfigurationElementsFor(
182                         PlatformUI.PLUGIN_ID,
183                         IWorkbenchRegistryConstants.PL_SYSTEM_SUMMARY_SECTIONS);
184
185         Arrays.sort(configElements, new Comparator JavaDoc() {
186             Collator collator = Collator.getInstance(Locale.getDefault());
187
188             public int compare(Object JavaDoc a, Object JavaDoc b) {
189                 IConfigurationElement element1 = (IConfigurationElement) a;
190                 IConfigurationElement element2 = (IConfigurationElement) b;
191
192                 String JavaDoc id1 = element1.getAttribute("id"); //$NON-NLS-1$
193
String JavaDoc id2 = element2.getAttribute("id"); //$NON-NLS-1$
194

195                 if (id1 != null && id2 != null && !id1.equals(id2)) {
196                     return collator.compare(id1, id2);
197                 }
198
199                 String JavaDoc title1 = element1.getAttribute("sectionTitle"); //$NON-NLS-1$
200
String JavaDoc title2 = element2.getAttribute("sectionTitle"); //$NON-NLS-1$
201

202                 if (title1 == null) {
203                     title1 = ""; //$NON-NLS-1$
204
}
205                 if (title2 == null) {
206                     title2 = ""; //$NON-NLS-1$
207
}
208
209                 return collator.compare(title1, title2);
210             }
211         });
212
213         return configElements;
214     }
215
216     /* (non-Javadoc)
217      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
218      */

219     protected void buttonPressed(int buttonId) {
220         switch (buttonId) {
221         case IDialogConstants.CLOSE_ID:
222             close();
223             break;
224         case BROWSE_ERROR_LOG_BUTTON:
225             openErrorLogBrowser();
226             break;
227         case COPY_TO_CLIPBOARD_BUTTON:
228             runCopyToClipboard();
229             break;
230         }
231         super.buttonPressed(buttonId);
232     }
233
234     private void openErrorLogBrowser() {
235         String JavaDoc filename = Platform.getLogFileLocation().toOSString();
236
237         File JavaDoc log = new File JavaDoc(filename);
238         if (log.exists()) {
239             // Make a copy of the file with a temporary name.
240
// Working around an issue with windows file associations/browser malfunction
241
// whereby the browser doesn't open on ".log" and we aren't returned an error.
242
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97783
243
File JavaDoc logCopy = makeDisplayCopy(log);
244             if (logCopy != null) {
245                 openLink("file:///" + logCopy.getAbsolutePath()); //$NON-NLS-1$
246
return;
247             }
248             // Couldn't make copy, try to open the original log.
249
// We try the original in this case rather than putting up an error,
250
// because the copy could fail due to an I/O or out of space problem.
251
// In that case we may still be able to show the original log,
252
// depending on the platform. The risk is that users with configurations
253
// that have bug #97783 will still get nothing (vs. an error) but we'd rather
254
// try again than put up an error dialog on platforms where the ability to
255
// view the original log works just fine.
256
openLink("file:///" + filename); //$NON-NLS-1$
257
return;
258         }
259         MessageDialog.openInformation(getShell(), WorkbenchMessages.AboutSystemDialog_noLogTitle,
260                 NLS.bind(WorkbenchMessages.AboutSystemDialog_noLogMessage, filename ));
261     }
262     
263     /**
264      * Returns a copy of the given file to be used for display in
265      * a browser.
266      *
267      * @return the file, or <code>null</code>
268      */

269     private File JavaDoc makeDisplayCopy(File JavaDoc file) {
270         IPath path = WorkbenchPlugin.getDefault().getDataLocation();
271         if(path == null) {
272             return null;
273         }
274         path = path.append(ERROR_LOG_COPY_FILENAME);
275         File JavaDoc copy = path.toFile();
276         FileReader JavaDoc in = null;
277         FileWriter JavaDoc out = null;
278         try {
279             in = new FileReader JavaDoc(file);
280             // don't append data, overwrite what was there
281
out = new FileWriter JavaDoc(copy);
282             char buffer[] = new char[4096];
283             int count;
284             while ((count = in.read(buffer, 0, buffer.length)) > 0) {
285                 out.write(buffer, 0, count);
286             }
287         } catch (FileNotFoundException JavaDoc e) {
288             return null;
289         } catch (IOException JavaDoc e) {
290             return null;
291         } finally {
292             try {
293                 if (in != null) {
294                     in.close();
295                 }
296                 if (out != null) {
297                     out.close();
298                 }
299             } catch (IOException JavaDoc e) {
300                 return null;
301             }
302         }
303         return copy;
304
305     }
306
307     private void runCopyToClipboard() {
308         if (text == null) {
309             return;
310         }
311
312         Clipboard clipboard = null;
313         try {
314             clipboard = new Clipboard(getShell().getDisplay());
315             clipboard.setContents(new Object JavaDoc[] { text.getText() },
316                     new Transfer[] { TextTransfer.getInstance() });
317         } finally {
318             if (clipboard != null) {
319                 clipboard.dispose();
320             }
321         }
322     }
323 }
324
Popular Tags