001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import java.io.File;
013: import java.io.FileNotFoundException;
014: import java.io.FileReader;
015: import java.io.FileWriter;
016: import java.io.IOException;
017: import java.io.PrintWriter;
018: import java.io.StringWriter;
019: import java.util.Arrays;
020: import java.util.Comparator;
021: import java.util.Date;
022: import java.util.Locale;
023:
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IConfigurationElement;
026: import org.eclipse.core.runtime.IPath;
027: import org.eclipse.core.runtime.Platform;
028: import org.eclipse.jface.dialogs.IDialogConstants;
029: import org.eclipse.jface.dialogs.MessageDialog;
030: import org.eclipse.jface.resource.JFaceResources;
031: import org.eclipse.osgi.util.NLS;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.dnd.Clipboard;
034: import org.eclipse.swt.dnd.TextTransfer;
035: import org.eclipse.swt.dnd.Transfer;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.swt.widgets.Control;
041: import org.eclipse.swt.widgets.Label;
042: import org.eclipse.swt.widgets.Shell;
043: import org.eclipse.swt.widgets.Text;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.about.ISystemSummarySection;
046: import org.eclipse.ui.internal.IWorkbenchConstants;
047: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
048: import org.eclipse.ui.internal.WorkbenchMessages;
049: import org.eclipse.ui.internal.WorkbenchPlugin;
050: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
051:
052: import com.ibm.icu.text.Collator;
053: import com.ibm.icu.text.DateFormat;
054:
055: /**
056: * Displays system information about the eclipse application. The content of
057: * what is displayed is selectable through the
058: * <code>org.eclipse.ui.systemSummaryExtensions</code> extension point.
059: */
060: public final class AboutSystemDialog extends ProductInfoDialog {
061:
062: private Text text;
063:
064: private final static int BROWSE_ERROR_LOG_BUTTON = IDialogConstants.CLIENT_ID;
065:
066: private final static int COPY_TO_CLIPBOARD_BUTTON = IDialogConstants.CLIENT_ID + 1;
067:
068: private final static String ERROR_LOG_COPY_FILENAME = "log"; //$NON-NLS-1$
069:
070: /**
071: * Create an instance of this dialog.
072: *
073: * @param parentShell the parent shell
074: */
075: public AboutSystemDialog(Shell parentShell) {
076: super (parentShell);
077: }
078:
079: /* (non-Javadoc)
080: * Method declared on Window.
081: */
082: protected void configureShell(Shell newShell) {
083: super .configureShell(newShell);
084: newShell.setText(WorkbenchMessages.SystemSummary_title);
085: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
086: IWorkbenchHelpContextIds.SYSTEM_SUMMARY_DIALOG);
087: }
088:
089: /* (non-Javadoc)
090: * Method declared on Dialog.
091: */
092: protected void createButtonsForButtonBar(Composite parent) {
093: parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
094:
095: Button button = createButton(parent, BROWSE_ERROR_LOG_BUTTON,
096: WorkbenchMessages.AboutSystemDialog_browseErrorLogName,
097: false);
098: String filename = Platform.getLogFileLocation().toOSString();
099: button.setEnabled(new File(filename).exists());
100:
101: createButton(
102: parent,
103: COPY_TO_CLIPBOARD_BUTTON,
104: WorkbenchMessages.AboutSystemDialog_copyToClipboardName,
105: false);
106:
107: new Label(parent, SWT.NONE).setLayoutData(new GridData(
108: GridData.FILL_HORIZONTAL));
109: GridLayout layout = (GridLayout) parent.getLayout();
110: layout.numColumns++;
111: layout.makeColumnsEqualWidth = false;
112:
113: createButton(parent, IDialogConstants.CLOSE_ID,
114: IDialogConstants.CLOSE_LABEL, true);
115: }
116:
117: /* (non-Javadoc)
118: * Method declared on Dialog.
119: */
120: protected Control createDialogArea(Composite parent) {
121: Composite outer = (Composite) super .createDialogArea(parent);
122:
123: text = new Text(outer, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY
124: | SWT.V_SCROLL | SWT.NO_FOCUS | SWT.H_SCROLL);
125: text.setBackground(parent.getDisplay().getSystemColor(
126: SWT.COLOR_LIST_BACKGROUND));
127: GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
128: | GridData.VERTICAL_ALIGN_FILL);
129: gridData.grabExcessVerticalSpace = true;
130: gridData.grabExcessHorizontalSpace = true;
131: gridData.heightHint = convertVerticalDLUsToPixels(300);
132: gridData.widthHint = convertHorizontalDLUsToPixels(400);
133: text.setLayoutData(gridData);
134: text.setText(getSystemSummary());
135: text.setFont(JFaceResources.getTextFont());
136: return outer;
137: }
138:
139: private String getSystemSummary() {
140: StringWriter out = new StringWriter();
141: PrintWriter writer = new PrintWriter(out);
142: writer.println(NLS.bind(
143: WorkbenchMessages.SystemSummary_timeStamp, DateFormat
144: .getDateTimeInstance(DateFormat.FULL,
145: DateFormat.FULL).format(new Date())));
146:
147: appendExtensions(writer);
148: writer.close();
149: return out.toString();
150: }
151:
152: /*
153: * Appends the contents of all extentions to the configurationLogSections
154: * extension point.
155: */
156: private void appendExtensions(PrintWriter writer) {
157: IConfigurationElement[] configElements = getSortedExtensions();
158: for (int i = 0; i < configElements.length; ++i) {
159: IConfigurationElement element = configElements[i];
160:
161: Object obj = null;
162: try {
163: obj = WorkbenchPlugin.createExtension(element,
164: IWorkbenchConstants.TAG_CLASS);
165: } catch (CoreException e) {
166: WorkbenchPlugin
167: .log(
168: "could not create class attribute for extension", //$NON-NLS-1$
169: e.getStatus());
170: }
171:
172: writer.println();
173: writer.println(NLS.bind(
174: WorkbenchMessages.SystemSummary_sectionTitle,
175: element.getAttribute("sectionTitle"))); //$NON-NLS-1$
176:
177: if (obj instanceof ISystemSummarySection) {
178: ISystemSummarySection logSection = (ISystemSummarySection) obj;
179: logSection.write(writer);
180: } else {
181: writer
182: .println(WorkbenchMessages.SystemSummary_sectionError);
183: }
184: }
185: }
186:
187: private IConfigurationElement[] getSortedExtensions() {
188: IConfigurationElement[] configElements = Platform
189: .getExtensionRegistry()
190: .getConfigurationElementsFor(
191: PlatformUI.PLUGIN_ID,
192: IWorkbenchRegistryConstants.PL_SYSTEM_SUMMARY_SECTIONS);
193:
194: Arrays.sort(configElements, new Comparator() {
195: Collator collator = Collator.getInstance(Locale
196: .getDefault());
197:
198: public int compare(Object a, Object b) {
199: IConfigurationElement element1 = (IConfigurationElement) a;
200: IConfigurationElement element2 = (IConfigurationElement) b;
201:
202: String id1 = element1.getAttribute("id"); //$NON-NLS-1$
203: String id2 = element2.getAttribute("id"); //$NON-NLS-1$
204:
205: if (id1 != null && id2 != null && !id1.equals(id2)) {
206: return collator.compare(id1, id2);
207: }
208:
209: String title1 = element1.getAttribute("sectionTitle"); //$NON-NLS-1$
210: String title2 = element2.getAttribute("sectionTitle"); //$NON-NLS-1$
211:
212: if (title1 == null) {
213: title1 = ""; //$NON-NLS-1$
214: }
215: if (title2 == null) {
216: title2 = ""; //$NON-NLS-1$
217: }
218:
219: return collator.compare(title1, title2);
220: }
221: });
222:
223: return configElements;
224: }
225:
226: /* (non-Javadoc)
227: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
228: */
229: protected void buttonPressed(int buttonId) {
230: switch (buttonId) {
231: case IDialogConstants.CLOSE_ID:
232: close();
233: break;
234: case BROWSE_ERROR_LOG_BUTTON:
235: openErrorLogBrowser();
236: break;
237: case COPY_TO_CLIPBOARD_BUTTON:
238: runCopyToClipboard();
239: break;
240: }
241: super .buttonPressed(buttonId);
242: }
243:
244: private void openErrorLogBrowser() {
245: String filename = Platform.getLogFileLocation().toOSString();
246:
247: File log = new File(filename);
248: if (log.exists()) {
249: // Make a copy of the file with a temporary name.
250: // Working around an issue with windows file associations/browser malfunction
251: // whereby the browser doesn't open on ".log" and we aren't returned an error.
252: // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97783
253: File logCopy = makeDisplayCopy(log);
254: if (logCopy != null) {
255: openLink("file:///" + logCopy.getAbsolutePath()); //$NON-NLS-1$
256: return;
257: }
258: // Couldn't make copy, try to open the original log.
259: // We try the original in this case rather than putting up an error,
260: // because the copy could fail due to an I/O or out of space problem.
261: // In that case we may still be able to show the original log,
262: // depending on the platform. The risk is that users with configurations
263: // that have bug #97783 will still get nothing (vs. an error) but we'd rather
264: // try again than put up an error dialog on platforms where the ability to
265: // view the original log works just fine.
266: openLink("file:///" + filename); //$NON-NLS-1$
267: return;
268: }
269: MessageDialog
270: .openInformation(
271: getShell(),
272: WorkbenchMessages.AboutSystemDialog_noLogTitle,
273: NLS
274: .bind(
275: WorkbenchMessages.AboutSystemDialog_noLogMessage,
276: filename));
277: }
278:
279: /**
280: * Returns a copy of the given file to be used for display in
281: * a browser.
282: *
283: * @return the file, or <code>null</code>
284: */
285: private File makeDisplayCopy(File file) {
286: IPath path = WorkbenchPlugin.getDefault().getDataLocation();
287: if (path == null) {
288: return null;
289: }
290: path = path.append(ERROR_LOG_COPY_FILENAME);
291: File copy = path.toFile();
292: FileReader in = null;
293: FileWriter out = null;
294: try {
295: in = new FileReader(file);
296: // don't append data, overwrite what was there
297: out = new FileWriter(copy);
298: char buffer[] = new char[4096];
299: int count;
300: while ((count = in.read(buffer, 0, buffer.length)) > 0) {
301: out.write(buffer, 0, count);
302: }
303: } catch (FileNotFoundException e) {
304: return null;
305: } catch (IOException e) {
306: return null;
307: } finally {
308: try {
309: if (in != null) {
310: in.close();
311: }
312: if (out != null) {
313: out.close();
314: }
315: } catch (IOException e) {
316: return null;
317: }
318: }
319: return copy;
320:
321: }
322:
323: private void runCopyToClipboard() {
324: if (text == null) {
325: return;
326: }
327:
328: Clipboard clipboard = null;
329: try {
330: clipboard = new Clipboard(getShell().getDisplay());
331: clipboard.setContents(new Object[] { text.getText() },
332: new Transfer[] { TextTransfer.getInstance() });
333: } finally {
334: if (clipboard != null) {
335: clipboard.dispose();
336: }
337: }
338: }
339: }
|