1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.message.ui;
15 import java.lang.reflect.InvocationTargetException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.*;
25 * Displays the error log in non-Win32 platforms - see bug 55314.
27 public final class OpenLogDialog extends TrayDialog {
30 // location/size configuration
31 private IDialogSettings dialogSettings;
32 private Point dialogLocation;
33 private Point dialogSize;
34 private int DEFAULT_WIDTH = 750;
35 private int DEFAULT_HEIGHT = 800;
37 public OpenLogDialog(Shell parentShell, File logFile) {
39 this.logFile = logFile;
40 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN | SWT.MODELESS);
45 * (non-Javadoc) Method declared on Window.
47 protected void configureShell(Shell newShell) {
48 super.configureShell(newShell);
49 newShell.setText(Messages.OpenLogDialog_title);
54 * (non-Javadoc) Method declared on Dialog.
56 protected void createButtonsForButtonBar(Composite parent) {
57 createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, true);
60 public void create() {
63 if (dialogLocation != null)
64 getShell().setLocation(dialogLocation);
66 if (dialogSize != null)
67 getShell().setSize(dialogSize);
69 getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
70 getButton(IDialogConstants.CLOSE_ID).setFocus();
74 * (non-Javadoc) Method declared on Dialog.
76 protected Control createDialogArea(Composite parent) {
77 Composite outer = (Composite) super.createDialogArea(parent);
78 Text text = new Text(outer, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL | SWT.NO_FOCUS | SWT.H_SCROLL);
79 text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
80 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
81 gridData.grabExcessVerticalSpace = true;
82 gridData.grabExcessHorizontalSpace = true;
83 text.setLayoutData(gridData);
84 text.setText(getLogSummary());
88 private String getLogSummary() {
89 StringWriter out = new StringWriter();
90 PrintWriter writer = new PrintWriter(out);
91 if (logFile.length() > LogReader.MAX_FILE_LENGTH) {
92 readLargeFileWithMonitor(writer);
94 readFileWithMonitor(writer);
97 return out.toString();
103 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
105 protected void buttonPressed(int buttonId) {
106 if (buttonId == IDialogConstants.CLOSE_ID) {
110 super.buttonPressed(buttonId);
113 //--------------- configuration handling --------------
115 * Stores the current state in the dialog settings.
119 private void storeSettings() {
120 writeConfiguration();
124 * Returns the dialog settings object used to share state between several
125 * event detail dialogs.
127 * @return the dialog settings to be used
129 private IDialogSettings getDialogSettings() {
130 IDialogSettings settings = Activator.getDefault().getDialogSettings();
131 dialogSettings = settings.getSection(getClass().getName());
132 if (dialogSettings == null)
133 dialogSettings = settings.addNewSection(getClass().getName());
134 return dialogSettings;
138 * Initializes itself from the dialog settings with the same state as at the
139 * previous invocation.
141 private void readConfiguration() {
142 IDialogSettings s = getDialogSettings();
144 int x = s.getInt("x"); //$NON-NLS-1$
145 int y = s.getInt("y"); //$NON-NLS-1$
146 dialogLocation = new Point(x, y);
147 x = s.getInt("width"); //$NON-NLS-1$
148 y = s.getInt("height"); //$NON-NLS-1$
149 dialogSize = new Point(x, y);
150 } catch (NumberFormatException e) {
151 dialogLocation = null;
156 private void writeConfiguration() {
157 IDialogSettings s = getDialogSettings();
158 Point location = getShell().getLocation();
159 s.put("x", location.x); //$NON-NLS-1$
160 s.put("y", location.y); //$NON-NLS-1$
161 Point size = getShell().getSize();
162 s.put("width", size.x); //$NON-NLS-1$
163 s.put("height", size.y); //$NON-NLS-1$
166 // reading file within MAX_FILE_LENGTH size
167 void readFile(PrintWriter writer) throws FileNotFoundException, IOException {
168 try (BufferedReader bReader = new BufferedReader(new FileReader(logFile))) {
169 while (bReader.ready())
170 writer.println(bReader.readLine());
174 // reading large files
175 void readLargeFile(PrintWriter writer) throws FileNotFoundException, IOException {
176 RandomAccessFile random = null;
177 boolean hasStarted = false;
179 random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
180 random.seek(logFile.length() - LogReader.MAX_FILE_LENGTH);
182 String line = random.readLine();
186 if (line.length() == 0)
188 if (!hasStarted && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
191 writer.println(line);
198 } catch (IOException e1) { // do nothing
203 private void readLargeFileWithMonitor(final PrintWriter writer) {
204 IRunnableWithProgress runnable = new IRunnableWithProgress() {
205 public void run(IProgressMonitor monitor) {
206 monitor.beginTask(Messages.OpenLogDialog_message, IProgressMonitor.UNKNOWN);
208 readLargeFile(writer);
209 } catch (IOException e) {
210 writer.println(Messages.OpenLogDialog_cannotDisplay);
214 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
216 dialog.run(true, true, runnable);
217 } catch (InvocationTargetException e) { // do nothing
218 } catch (InterruptedException e) { // do nothing
222 private void readFileWithMonitor(final PrintWriter writer) {
223 IRunnableWithProgress runnable = new IRunnableWithProgress() {
224 public void run(IProgressMonitor monitor) {
225 monitor.beginTask(Messages.OpenLogDialog_message, IProgressMonitor.UNKNOWN);
228 } catch (IOException e) {
229 writer.println(Messages.OpenLogDialog_cannotDisplay);
233 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
235 dialog.run(true, true, runnable);
236 } catch (InvocationTargetException e) { // do nothing
237 } catch (InterruptedException e) { // do nothing