1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.io.File ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileReader ; 16 import java.io.FileWriter ; 17 import java.io.IOException ; 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 import java.util.Arrays ; 21 import java.util.Comparator ; 22 import java.util.Date ; 23 import java.util.Locale ; 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 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 ERROR_LOG_COPY_FILENAME = "log"; 71 76 public AboutSystemDialog(Shell parentShell) { 77 super(parentShell); 78 setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX); 79 } 80 81 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 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 filename = Platform.getLogFileLocation().toOSString(); 99 button.setEnabled(new File (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 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 getSystemSummary() { 136 StringWriter out = new StringWriter (); 137 PrintWriter writer = new PrintWriter (out); 138 writer.println(NLS.bind(WorkbenchMessages.SystemSummary_timeStamp, 139 DateFormat 140 .getDateTimeInstance(DateFormat.FULL, DateFormat.FULL) 141 .format(new Date ()))); 142 143 appendExtensions(writer); 144 writer.close(); 145 return out.toString(); 146 } 147 148 152 private void appendExtensions(PrintWriter writer) { 153 IConfigurationElement[] configElements = getSortedExtensions(); 154 for (int i = 0; i < configElements.length; ++i) { 155 IConfigurationElement element = configElements[i]; 156 157 Object 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", e.getStatus()); 165 } 166 167 writer.println(); 168 writer.println(NLS.bind(WorkbenchMessages.SystemSummary_sectionTitle, element.getAttribute("sectionTitle") )); 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 () { 186 Collator collator = Collator.getInstance(Locale.getDefault()); 187 188 public int compare(Object a, Object b) { 189 IConfigurationElement element1 = (IConfigurationElement) a; 190 IConfigurationElement element2 = (IConfigurationElement) b; 191 192 String id1 = element1.getAttribute("id"); String id2 = element2.getAttribute("id"); 195 if (id1 != null && id2 != null && !id1.equals(id2)) { 196 return collator.compare(id1, id2); 197 } 198 199 String title1 = element1.getAttribute("sectionTitle"); String title2 = element2.getAttribute("sectionTitle"); 202 if (title1 == null) { 203 title1 = ""; } 205 if (title2 == null) { 206 title2 = ""; } 208 209 return collator.compare(title1, title2); 210 } 211 }); 212 213 return configElements; 214 } 215 216 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 filename = Platform.getLogFileLocation().toOSString(); 236 237 File log = new File (filename); 238 if (log.exists()) { 239 File logCopy = makeDisplayCopy(log); 244 if (logCopy != null) { 245 openLink("file:///" + logCopy.getAbsolutePath()); return; 247 } 248 openLink("file:///" + filename); return; 258 } 259 MessageDialog.openInformation(getShell(), WorkbenchMessages.AboutSystemDialog_noLogTitle, 260 NLS.bind(WorkbenchMessages.AboutSystemDialog_noLogMessage, filename )); 261 } 262 263 269 private File makeDisplayCopy(File 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 copy = path.toFile(); 276 FileReader in = null; 277 FileWriter out = null; 278 try { 279 in = new FileReader (file); 280 out = new FileWriter (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 e) { 288 return null; 289 } catch (IOException 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 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 [] { text.getText() }, 316 new Transfer[] { TextTransfer.getInstance() }); 317 } finally { 318 if (clipboard != null) { 319 clipboard.dispose(); 320 } 321 } 322 } 323 } 324 | Popular Tags |