1 /*******************************************************************************
2 * Copyright (c) 2003, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.logview;
14 import java.io.BufferedReader;
16 import java.io.FileNotFoundException;
17 import java.io.FileReader;
18 import java.io.IOException;
19 import java.io.PrintWriter;
20 import java.io.RandomAccessFile;
21 import java.io.StringWriter;
22 import java.lang.reflect.InvocationTargetException;
24 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jface.dialogs.Dialog;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
41 * Displays the error log in non-Win32 platforms - see bug 55314.
43 public final class OpenLogDialog extends Dialog {
46 // location/size configuration
47 private IDialogSettings dialogSettings;
48 private Point dialogLocation;
49 private Point dialogSize;
50 private int DEFAULT_WIDTH = 750;
51 private int DEFAULT_HEIGHT = 800;
53 public OpenLogDialog(Shell parentShell, File logFile) {
55 this.logFile = logFile;
56 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN | SWT.MODELESS);
61 * (non-Javadoc) Method declared on Window.
63 protected void configureShell(Shell newShell) {
64 super.configureShell(newShell);
65 newShell.setText(PHPDebugCorePlugin.getResourceString("OpenLogDialog.title")); //$NON-NLS-1$
70 * (non-Javadoc) Method declared on Dialog.
72 protected void createButtonsForButtonBar(Composite parent) {
73 createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL,
77 public void create() {
80 if (dialogLocation != null)
81 getShell().setLocation(dialogLocation);
83 if (dialogSize != null)
84 getShell().setSize(dialogSize);
86 getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
87 getButton(IDialogConstants.CLOSE_ID).setFocus();
91 * (non-Javadoc) Method declared on Dialog.
93 protected Control createDialogArea(Composite parent) {
94 Composite outer = (Composite) super.createDialogArea(parent);
95 Text text = new Text(outer, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL
96 | SWT.NO_FOCUS | SWT.H_SCROLL);
97 text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
98 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
99 | GridData.VERTICAL_ALIGN_FILL);
100 gridData.grabExcessVerticalSpace = true;
101 gridData.grabExcessHorizontalSpace = true;
102 text.setLayoutData(gridData);
103 text.setText(getLogSummary());
107 private String getLogSummary() {
108 StringWriter out = new StringWriter();
109 PrintWriter writer = new PrintWriter(out);
110 if (logFile.length() > LogReader.MAX_FILE_LENGTH) {
111 readLargeFileWithMonitor(writer);
113 readFileWithMonitor(writer);
116 return out.toString();
122 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
124 protected void buttonPressed(int buttonId) {
125 if (buttonId == IDialogConstants.CLOSE_ID) {
129 super.buttonPressed(buttonId);
132 //--------------- configuration handling --------------
134 * Stores the current state in the dialog settings.
138 private void storeSettings() {
139 writeConfiguration();
143 * Returns the dialog settings object used to share state between several
144 * event detail dialogs.
146 * @return the dialog settings to be used
148 private IDialogSettings getDialogSettings() {
149 IDialogSettings settings = PHPDebugCorePlugin.getDefault().getDialogSettings();
150 dialogSettings = settings.getSection(getClass().getName());
151 if (dialogSettings == null)
152 dialogSettings = settings.addNewSection(getClass().getName());
153 return dialogSettings;
157 * Initializes itself from the dialog settings with the same state as at the
158 * previous invocation.
160 private void readConfiguration() {
161 IDialogSettings s = getDialogSettings();
163 int x = s.getInt("x"); //$NON-NLS-1$
164 int y = s.getInt("y"); //$NON-NLS-1$
165 dialogLocation = new Point(x, y);
166 x = s.getInt("width"); //$NON-NLS-1$
167 y = s.getInt("height"); //$NON-NLS-1$
168 dialogSize = new Point(x, y);
169 } catch (NumberFormatException e) {
170 dialogLocation = null;
175 private void writeConfiguration() {
176 IDialogSettings s = getDialogSettings();
177 Point location = getShell().getLocation();
178 s.put("x", location.x); //$NON-NLS-1$
179 s.put("y", location.y); //$NON-NLS-1$
180 Point size = getShell().getSize();
181 s.put("width", size.x); //$NON-NLS-1$
182 s.put("height", size.y); //$NON-NLS-1$
185 // reading file within MAX_FILE_LENGTH size
186 private void readFile(PrintWriter writer) throws FileNotFoundException, IOException {
187 BufferedReader bReader = new BufferedReader(new FileReader(logFile));
188 while (bReader.ready())
189 writer.println(bReader.readLine());
192 // reading large files
193 private void readLargeFile(PrintWriter writer) throws FileNotFoundException,
195 RandomAccessFile random = null;
196 boolean hasStarted = false;
198 random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
199 random.seek(logFile.length() - LogReader.MAX_FILE_LENGTH);
201 String line = random.readLine();
205 if (line.length() == 0)
208 && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
211 writer.println(line);
218 } catch (IOException e1) {
223 private void readLargeFileWithMonitor(final PrintWriter writer) {
224 IRunnableWithProgress runnable = new IRunnableWithProgress() {
225 public void run(IProgressMonitor monitor) throws InvocationTargetException,
226 InterruptedException {
230 .getResourceString("OpenLogDialog.message"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
232 readLargeFile(writer);
233 } catch (IOException e) {
234 writer.println(PHPDebugCorePlugin
235 .getResourceString("OpenLogDialog.cannotDisplay")); //$NON-NLS-1$
239 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
241 dialog.run(true, true, runnable);
242 } catch (InvocationTargetException e) {
243 } catch (InterruptedException e) {
247 private void readFileWithMonitor(final PrintWriter writer) {
248 IRunnableWithProgress runnable = new IRunnableWithProgress() {
249 public void run(IProgressMonitor monitor) throws InvocationTargetException,
250 InterruptedException {
254 .getResourceString("OpenLogDialog.message"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
257 } catch (IOException e) {
258 writer.println(PHPDebugCorePlugin
259 .getResourceString("OpenLogDialog.cannotDisplay")); //$NON-NLS-1$
263 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
265 dialog.run(true, true, runnable);
266 } catch (InvocationTargetException e) {
267 } catch (InterruptedException e) {