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.pde.internal.runtime.logview;
011:
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileNotFoundException;
015: import java.io.FileReader;
016: import java.io.IOException;
017: import java.io.PrintWriter;
018: import java.io.RandomAccessFile;
019: import java.io.StringWriter;
020: import java.lang.reflect.InvocationTargetException;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.eclipse.jface.dialogs.IDialogConstants;
024: import org.eclipse.jface.dialogs.IDialogSettings;
025: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
026: import org.eclipse.jface.dialogs.TrayDialog;
027: import org.eclipse.jface.operation.IRunnableWithProgress;
028: import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
029: import org.eclipse.pde.internal.runtime.PDERuntimePlugin;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.graphics.Point;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Shell;
036: import org.eclipse.swt.widgets.Text;
037:
038: /**
039: * Displays the error log in non-Win32 platforms - see bug 55314.
040: */
041: public final class OpenLogDialog extends TrayDialog {
042: // input log file
043: private File logFile;
044: // location/size configuration
045: private IDialogSettings dialogSettings;
046: private Point dialogLocation;
047: private Point dialogSize;
048: private int DEFAULT_WIDTH = 750;
049: private int DEFAULT_HEIGHT = 800;
050:
051: public OpenLogDialog(Shell parentShell, File logFile) {
052: super (parentShell);
053: this .logFile = logFile;
054: setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN
055: | SWT.MODELESS);
056:
057: }
058:
059: /*
060: * (non-Javadoc) Method declared on Window.
061: */
062: protected void configureShell(Shell newShell) {
063: super .configureShell(newShell);
064: newShell.setText(PDERuntimeMessages.OpenLogDialog_title);
065: readConfiguration();
066: }
067:
068: /*
069: * (non-Javadoc) Method declared on Dialog.
070: */
071: protected void createButtonsForButtonBar(Composite parent) {
072: createButton(parent, IDialogConstants.CLOSE_ID,
073: IDialogConstants.CLOSE_LABEL, true);
074: }
075:
076: public void create() {
077: super .create();
078: // dialog location
079: if (dialogLocation != null)
080: getShell().setLocation(dialogLocation);
081: // dialog size
082: if (dialogSize != null)
083: getShell().setSize(dialogSize);
084: else
085: getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
086: getButton(IDialogConstants.CLOSE_ID).setFocus();
087: }
088:
089: /*
090: * (non-Javadoc) Method declared on Dialog.
091: */
092: protected Control createDialogArea(Composite parent) {
093: Composite outer = (Composite) super .createDialogArea(parent);
094: Text text = new Text(outer, SWT.MULTI | SWT.BORDER
095: | SWT.READ_ONLY | SWT.V_SCROLL | SWT.NO_FOCUS
096: | SWT.H_SCROLL);
097: text.setBackground(parent.getDisplay().getSystemColor(
098: SWT.COLOR_LIST_BACKGROUND));
099: GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
100: | GridData.VERTICAL_ALIGN_FILL);
101: gridData.grabExcessVerticalSpace = true;
102: gridData.grabExcessHorizontalSpace = true;
103: text.setLayoutData(gridData);
104: text.setText(getLogSummary());
105: return outer;
106: }
107:
108: private String getLogSummary() {
109: StringWriter out = new StringWriter();
110: PrintWriter writer = new PrintWriter(out);
111: if (logFile.length() > LogReader.MAX_FILE_LENGTH) {
112: readLargeFileWithMonitor(writer);
113: } else {
114: readFileWithMonitor(writer);
115: }
116: writer.close();
117: return out.toString();
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
124: */
125: protected void buttonPressed(int buttonId) {
126: if (buttonId == IDialogConstants.CLOSE_ID) {
127: storeSettings();
128: close();
129: }
130: super .buttonPressed(buttonId);
131: }
132:
133: //--------------- configuration handling --------------
134: /**
135: * Stores the current state in the dialog settings.
136: *
137: * @since 2.0
138: */
139: private void storeSettings() {
140: writeConfiguration();
141: }
142:
143: /**
144: * Returns the dialog settings object used to share state between several
145: * event detail dialogs.
146: *
147: * @return the dialog settings to be used
148: */
149: private IDialogSettings getDialogSettings() {
150: IDialogSettings settings = PDERuntimePlugin.getDefault()
151: .getDialogSettings();
152: dialogSettings = settings.getSection(getClass().getName());
153: if (dialogSettings == null)
154: dialogSettings = settings.addNewSection(getClass()
155: .getName());
156: return dialogSettings;
157: }
158:
159: /**
160: * Initializes itself from the dialog settings with the same state as at the
161: * previous invocation.
162: */
163: private void readConfiguration() {
164: IDialogSettings s = getDialogSettings();
165: try {
166: int x = s.getInt("x"); //$NON-NLS-1$
167: int y = s.getInt("y"); //$NON-NLS-1$
168: dialogLocation = new Point(x, y);
169: x = s.getInt("width"); //$NON-NLS-1$
170: y = s.getInt("height"); //$NON-NLS-1$
171: dialogSize = new Point(x, y);
172: } catch (NumberFormatException e) {
173: dialogLocation = null;
174: dialogSize = null;
175: }
176: }
177:
178: private void writeConfiguration() {
179: IDialogSettings s = getDialogSettings();
180: Point location = getShell().getLocation();
181: s.put("x", location.x); //$NON-NLS-1$
182: s.put("y", location.y); //$NON-NLS-1$
183: Point size = getShell().getSize();
184: s.put("width", size.x); //$NON-NLS-1$
185: s.put("height", size.y); //$NON-NLS-1$
186: }
187:
188: // reading file within MAX_FILE_LENGTH size
189: private void readFile(PrintWriter writer)
190: throws FileNotFoundException, IOException {
191: BufferedReader bReader = new BufferedReader(new FileReader(
192: logFile));
193: while (bReader.ready())
194: writer.println(bReader.readLine());
195: }
196:
197: // reading large files
198: private void readLargeFile(PrintWriter writer)
199: throws FileNotFoundException, IOException {
200: RandomAccessFile random = null;
201: boolean hasStarted = false;
202: try {
203: random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
204: random.seek(logFile.length() - LogReader.MAX_FILE_LENGTH);
205: for (;;) {
206: String line = random.readLine();
207: if (line == null)
208: break;
209: line = line.trim();
210: if (line.length() == 0)
211: continue;
212: if (!hasStarted
213: && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
214: hasStarted = true;
215: if (hasStarted)
216: writer.println(line);
217: continue;
218: }
219: } finally {
220: try {
221: if (random != null)
222: random.close();
223: } catch (IOException e1) {
224: }
225: }
226: }
227:
228: private void readLargeFileWithMonitor(final PrintWriter writer) {
229: IRunnableWithProgress runnable = new IRunnableWithProgress() {
230: public void run(IProgressMonitor monitor)
231: throws InvocationTargetException,
232: InterruptedException {
233: monitor.beginTask(
234: PDERuntimeMessages.OpenLogDialog_message,
235: IProgressMonitor.UNKNOWN);
236: try {
237: readLargeFile(writer);
238: } catch (IOException e) {
239: writer
240: .println(PDERuntimeMessages.OpenLogDialog_cannotDisplay);
241: }
242: }
243: };
244: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
245: getParentShell());
246: try {
247: dialog.run(true, true, runnable);
248: } catch (InvocationTargetException e) {
249: } catch (InterruptedException e) {
250: }
251: }
252:
253: private void readFileWithMonitor(final PrintWriter writer) {
254: IRunnableWithProgress runnable = new IRunnableWithProgress() {
255: public void run(IProgressMonitor monitor)
256: throws InvocationTargetException,
257: InterruptedException {
258: monitor.beginTask(
259: PDERuntimeMessages.OpenLogDialog_message,
260: IProgressMonitor.UNKNOWN);
261: try {
262: readFile(writer);
263: } catch (IOException e) {
264: writer
265: .println(PDERuntimeMessages.OpenLogDialog_cannotDisplay);
266: }
267: }
268: };
269: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
270: getParentShell());
271: try {
272: dialog.run(true, true, runnable);
273: } catch (InvocationTargetException e) {
274: } catch (InterruptedException e) {
275: }
276: }
277: }
|