]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message.ui/src/org/simantics/message/ui/OpenLogDialog.java
Workaround to fix performance problems when opening log view
[simantics/platform.git] / bundles / org.simantics.message.ui / src / org / simantics / message / ui / OpenLogDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.ui;
13
14 import java.io.*;
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.*;
23
24 /**
25  * Displays the error log in non-Win32 platforms - see bug 55314.
26  */
27 public final class OpenLogDialog extends TrayDialog {
28         // input log file
29         private File logFile;
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;
36
37         public OpenLogDialog(Shell parentShell, File logFile) {
38                 super(parentShell);
39                 this.logFile = logFile;
40                 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN | SWT.MODELESS);
41
42         }
43
44         /*
45          * (non-Javadoc) Method declared on Window.
46          */
47         protected void configureShell(Shell newShell) {
48                 super.configureShell(newShell);
49                 newShell.setText(Messages.OpenLogDialog_title);
50                 readConfiguration();
51         }
52
53         /*
54          * (non-Javadoc) Method declared on Dialog.
55          */
56         protected void createButtonsForButtonBar(Composite parent) {
57                 createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, true);
58         }
59
60         public void create() {
61                 super.create();
62                 // dialog location
63                 if (dialogLocation != null)
64                         getShell().setLocation(dialogLocation);
65                 // dialog size
66                 if (dialogSize != null)
67                         getShell().setSize(dialogSize);
68                 else
69                         getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
70                 getButton(IDialogConstants.CLOSE_ID).setFocus();
71         }
72
73         /*
74          * (non-Javadoc) Method declared on Dialog.
75          */
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());
85                 return outer;
86         }
87
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);
93                 } else {
94                         readFileWithMonitor(writer);
95                 }
96                 writer.close();
97                 return out.toString();
98         }
99
100         /*
101          * (non-Javadoc)
102          * 
103          * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
104          */
105         protected void buttonPressed(int buttonId) {
106                 if (buttonId == IDialogConstants.CLOSE_ID) {
107                         storeSettings();
108                         close();
109                 }
110                 super.buttonPressed(buttonId);
111         }
112
113         //--------------- configuration handling --------------
114         /**
115          * Stores the current state in the dialog settings.
116          * 
117          * @since 2.0
118          */
119         private void storeSettings() {
120                 writeConfiguration();
121         }
122
123         /**
124          * Returns the dialog settings object used to share state between several
125          * event detail dialogs.
126          * 
127          * @return the dialog settings to be used
128          */
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;
135         }
136
137         /**
138          * Initializes itself from the dialog settings with the same state as at the
139          * previous invocation.
140          */
141         private void readConfiguration() {
142                 IDialogSettings s = getDialogSettings();
143                 try {
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;
152                         dialogSize = null;
153                 }
154         }
155
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$
164         }
165
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());
171                 }
172         }
173
174         // reading large files
175         void readLargeFile(PrintWriter writer) throws FileNotFoundException, IOException {
176                 RandomAccessFile random = null;
177                 boolean hasStarted = false;
178                 try {
179                         random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
180                         random.seek(logFile.length() - LogReader.MAX_FILE_LENGTH);
181                         for (;;) {
182                                 String line = random.readLine();
183                                 if (line == null)
184                                         break;
185                                 line = line.trim();
186                                 if (line.length() == 0)
187                                         continue;
188                                 if (!hasStarted && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
189                                         hasStarted = true;
190                                 if (hasStarted)
191                                         writer.println(line);
192                                 continue;
193                         }
194                 } finally {
195                         try {
196                                 if (random != null)
197                                         random.close();
198                         } catch (IOException e1) { // do nothing
199                         }
200                 }
201         }
202
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);
207                                 try {
208                                         readLargeFile(writer);
209                                 } catch (IOException e) {
210                                         writer.println(Messages.OpenLogDialog_cannotDisplay);
211                                 }
212                         }
213                 };
214                 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
215                 try {
216                         dialog.run(true, true, runnable);
217                 } catch (InvocationTargetException e) { // do nothing
218                 } catch (InterruptedException e) { // do nothing
219                 }
220         }
221
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);
226                                 try {
227                                         readFile(writer);
228                                 } catch (IOException e) {
229                                         writer.println(Messages.OpenLogDialog_cannotDisplay);
230                                 }
231                         }
232                 };
233                 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
234                 try {
235                         dialog.run(true, true, runnable);
236                 } catch (InvocationTargetException e) { // do nothing
237                 } catch (InterruptedException e) { // do nothing
238                 }
239         }
240 }