001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 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.ui.launcher;
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.ui.PDEPlugin;
029: import org.eclipse.pde.internal.ui.PDEUIMessages;
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(PDEUIMessages.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() > LaunchListener.MAX_FILE_LENGTH) {
112: readLargeFileWithMonitor(writer);
113: } else {
114: readFileWithMonitor(writer);
115: }
116: writer.close();
117: return out.toString();
118: }
119:
120: // reading file within MAX_FILE_LENGTH size
121: private void readFile(PrintWriter writer)
122: throws FileNotFoundException, IOException {
123: BufferedReader bReader = new BufferedReader(new FileReader(
124: logFile));
125: while (bReader.ready())
126: writer.println(bReader.readLine());
127: }
128:
129: // reading large files
130: private void readLargeFile(PrintWriter writer)
131: throws FileNotFoundException, IOException {
132: RandomAccessFile random = null;
133: boolean hasStarted = false;
134: try {
135: random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
136: random.seek(logFile.length()
137: - LaunchListener.MAX_FILE_LENGTH);
138: for (;;) {
139: String line = random.readLine();
140: if (line == null)
141: break;
142: line = line.trim();
143: if (line.length() == 0)
144: continue;
145: if (!hasStarted
146: && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
147: hasStarted = true;
148: if (hasStarted)
149: writer.println(line);
150: continue;
151: }
152: } finally {
153: try {
154: if (random != null)
155: random.close();
156: } catch (IOException e1) {
157: }
158: }
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
165: */
166: protected void buttonPressed(int buttonId) {
167: if (buttonId == IDialogConstants.CLOSE_ID) {
168: storeSettings();
169: close();
170: }
171: super .buttonPressed(buttonId);
172: }
173:
174: private void readLargeFileWithMonitor(final PrintWriter writer) {
175: IRunnableWithProgress runnable = new IRunnableWithProgress() {
176: public void run(IProgressMonitor monitor)
177: throws InvocationTargetException,
178: InterruptedException {
179: monitor.beginTask(PDEUIMessages.OpenLogDialog_message,
180: IProgressMonitor.UNKNOWN);
181: try {
182: readLargeFile(writer);
183: } catch (IOException e) {
184: writer
185: .println(PDEUIMessages.OpenLogDialog_cannotDisplay);
186: }
187: }
188: };
189: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
190: getParentShell());
191: try {
192: dialog.run(true, true, runnable);
193: } catch (InvocationTargetException e) {
194: } catch (InterruptedException e) {
195: }
196: }
197:
198: private void readFileWithMonitor(final PrintWriter writer) {
199: IRunnableWithProgress runnable = new IRunnableWithProgress() {
200: public void run(IProgressMonitor monitor)
201: throws InvocationTargetException,
202: InterruptedException {
203: monitor.beginTask(PDEUIMessages.OpenLogDialog_message,
204: IProgressMonitor.UNKNOWN);
205: try {
206: readFile(writer);
207: } catch (IOException e) {
208: writer
209: .println(PDEUIMessages.OpenLogDialog_cannotDisplay);
210: }
211: }
212: };
213: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
214: getParentShell());
215: try {
216: dialog.run(true, true, runnable);
217: } catch (InvocationTargetException e) {
218: } catch (InterruptedException e) {
219: }
220: }
221:
222: //--------------- configuration handling --------------
223: /**
224: * Stores the current state in the dialog settings.
225: *
226: * @since 2.0
227: */
228: private void storeSettings() {
229: writeConfiguration();
230: }
231:
232: /**
233: * Returns the dialog settings object used to share state between several
234: * event detail dialogs.
235: *
236: * @return the dialog settings to be used
237: */
238: private IDialogSettings getDialogSettings() {
239: IDialogSettings settings = PDEPlugin.getDefault()
240: .getDialogSettings();
241: dialogSettings = settings.getSection(getClass().getName());
242: if (dialogSettings == null)
243: dialogSettings = settings.addNewSection(getClass()
244: .getName());
245: return dialogSettings;
246: }
247:
248: /**
249: * Initializes itself from the dialog settings with the same state as at the
250: * previous invocation.
251: */
252: private void readConfiguration() {
253: IDialogSettings s = getDialogSettings();
254: try {
255: int x = s.getInt("x"); //$NON-NLS-1$
256: int y = s.getInt("y"); //$NON-NLS-1$
257: dialogLocation = new Point(x, y);
258: x = s.getInt("width"); //$NON-NLS-1$
259: y = s.getInt("height"); //$NON-NLS-1$
260: dialogSize = new Point(x, y);
261: } catch (NumberFormatException e) {
262: dialogLocation = null;
263: dialogSize = null;
264: }
265: }
266:
267: private void writeConfiguration() {
268: IDialogSettings s = getDialogSettings();
269: Point location = getShell().getLocation();
270: s.put("x", location.x); //$NON-NLS-1$
271: s.put("y", location.y); //$NON-NLS-1$
272: Point size = getShell().getSize();
273: s.put("width", size.x); //$NON-NLS-1$
274: s.put("height", size.y); //$NON-NLS-1$
275: }
276: }
|