Project

General

Profile

Download (8.84 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2003, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11

    
12
package org.eclipse.pde.internal.runtime.logview;
13

    
14
import java.io.*;
15
import java.lang.reflect.InvocationTargetException;
16

    
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.jface.dialogs.*;
19
import org.eclipse.jface.operation.IRunnableWithProgress;
20
import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
21
import org.eclipse.pde.internal.runtime.PDERuntimePlugin;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.graphics.Point;
24
import org.eclipse.swt.layout.GridData;
25
import org.eclipse.swt.widgets.*;
26

    
27
/**
28
 * Displays the error log in non-Win32 platforms - see bug 55314.
29
 */
30
public final class OpenLogDialog extends TrayDialog {
31
    // input log file
32
    private File logFile;
33
    // location/size configuration
34
    private IDialogSettings dialogSettings;
35
    private Point dialogLocation;
36
    private Point dialogSize;
37
    private int DEFAULT_WIDTH = 750;
38
    private int DEFAULT_HEIGHT = 800;
39

    
40
    public OpenLogDialog(Shell parentShell, File logFile) {
41
        super(parentShell);
42
        this.logFile = logFile;
43
        setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN /*| SWT.MODELESS*/);
44

    
45
    }
46

    
47
    /*
48
     * (non-Javadoc) Method declared on Window.
49
     */
50
    protected void configureShell(Shell newShell) {
51
        super.configureShell(newShell);
52
        newShell.setText(PDERuntimeMessages.get().OpenLogDialog_title); 
53
        readConfiguration();
54
    }
55

    
56
    /*
57
     * (non-Javadoc) Method declared on Dialog.
58
     */
59
    protected void createButtonsForButtonBar(Composite parent) {
60
        createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.get().CLOSE_LABEL,
61
                true);
62
    }
63

    
64
    public void create() {
65
        super.create();
66
        // dialog location
67
        if (dialogLocation != null)
68
            getShell().setLocation(dialogLocation);
69
        // dialog size
70
        if (dialogSize != null)
71
            getShell().setSize(dialogSize);
72
        else
73
            getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
74
        getButton(IDialogConstants.CLOSE_ID).setFocus();
75
    }
76

    
77
    /*
78
     * (non-Javadoc) Method declared on Dialog.
79
     */
80
    protected Control createDialogArea(Composite parent) {
81
        Composite outer = (Composite) super.createDialogArea(parent);
82
        Text text = new Text(outer, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL
83
                | SWT.NO_FOCUS | SWT.H_SCROLL);
84
        text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
85
        GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
86
                | GridData.VERTICAL_ALIGN_FILL);
87
        gridData.grabExcessVerticalSpace = true;
88
        gridData.grabExcessHorizontalSpace = true;
89
        text.setLayoutData(gridData);
90
        text.setText(getLogSummary());
91
        return outer;
92
    }
93

    
94
    private String getLogSummary() {
95
        StringWriter out = new StringWriter();
96
        PrintWriter writer = new PrintWriter(out);
97
        if (logFile.length() > LogReader.MAX_FILE_LENGTH) {
98
            readLargeFileWithMonitor(writer);
99
        } else {
100
            readFileWithMonitor(writer);
101
        }
102
        writer.close();
103
        return out.toString();
104
    }
105

    
106
    /*
107
     * (non-Javadoc)
108
     * 
109
     * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
110
     */
111
    protected void buttonPressed(int buttonId) {
112
        if (buttonId == IDialogConstants.CLOSE_ID) {
113
            storeSettings();
114
            close();
115
        }
116
        super.buttonPressed(buttonId);
117
    }
118

    
119
    //--------------- configuration handling --------------
120
    /**
121
     * Stores the current state in the dialog settings.
122
     * 
123
     * @since 2.0
124
     */
125
    private void storeSettings() {
126
        writeConfiguration();
127
    }
128

    
129
    /**
130
     * Returns the dialog settings object used to share state between several
131
     * event detail dialogs.
132
     * 
133
     * @return the dialog settings to be used
134
     */
135
    private IDialogSettings getDialogSettings() {
136
        IDialogSettings settings = PDERuntimePlugin.getDefault().getDialogSettings();
137
        dialogSettings = settings.getSection(getClass().getName());
138
        if (dialogSettings == null)
139
            dialogSettings = settings.addNewSection(getClass().getName());
140
        return dialogSettings;
141
    }
142

    
143
    /**
144
     * Initializes itself from the dialog settings with the same state as at the
145
     * previous invocation.
146
     */
147
    private void readConfiguration() {
148
        IDialogSettings s = getDialogSettings();
149
        try {
150
            int x = s.getInt("x"); //$NON-NLS-1$
151
            int y = s.getInt("y"); //$NON-NLS-1$
152
            dialogLocation = new Point(x, y);
153
            x = s.getInt("width"); //$NON-NLS-1$
154
            y = s.getInt("height"); //$NON-NLS-1$
155
            dialogSize = new Point(x, y);
156
        } catch (NumberFormatException e) {
157
            dialogLocation = null;
158
            dialogSize = null;
159
        }
160
    }
161

    
162
    private void writeConfiguration() {
163
        IDialogSettings s = getDialogSettings();
164
        Point location = getShell().getLocation();
165
        s.put("x", location.x); //$NON-NLS-1$
166
        s.put("y", location.y); //$NON-NLS-1$
167
        Point size = getShell().getSize();
168
        s.put("width", size.x); //$NON-NLS-1$
169
        s.put("height", size.y); //$NON-NLS-1$
170
    }
171

    
172
    // reading file within MAX_FILE_LENGTH size
173
    private void readFile(PrintWriter writer) throws FileNotFoundException, IOException {
174
        BufferedReader bReader = new BufferedReader(new FileReader(logFile));
175
        while (bReader.ready())
176
            writer.println(bReader.readLine());
177
    }
178

    
179
    // reading large files
180
    private void readLargeFile(PrintWriter writer) throws FileNotFoundException,
181
            IOException {
182
        RandomAccessFile random = null;
183
        boolean hasStarted = false;
184
        try {
185
            random = new RandomAccessFile(logFile, "r"); //$NON-NLS-1$
186
            random.seek(logFile.length() - LogReader.MAX_FILE_LENGTH);
187
            for (;;) {
188
                String line = random.readLine();
189
                if (line == null)
190
                    break;
191
                line = line.trim();
192
                if (line.length() == 0)
193
                    continue;
194
                if (!hasStarted
195
                        && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) //$NON-NLS-1$ //$NON-NLS-2$
196
                    hasStarted = true;
197
                if (hasStarted)
198
                    writer.println(line);
199
                continue;
200
            }
201
        } finally {
202
            try {
203
                if (random != null)
204
                    random.close();
205
            } catch (IOException e1) {
206
            }
207
        }
208
    }
209

    
210
    private void readLargeFileWithMonitor(final PrintWriter writer) {
211
        IRunnableWithProgress runnable = new IRunnableWithProgress() {
212
            public void run(IProgressMonitor monitor) throws InvocationTargetException,
213
                    InterruptedException {
214
                monitor
215
                        .beginTask(
216
                                PDERuntimeMessages.get().OpenLogDialog_message, IProgressMonitor.UNKNOWN); 
217
                try {
218
                    readLargeFile(writer);
219
                } catch (IOException e) {
220
                    writer.println(PDERuntimeMessages.get().OpenLogDialog_cannotDisplay); 
221
                }
222
            }
223
        };
224
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
225
        try {
226
            dialog.run(true, true, runnable);
227
        } catch (InvocationTargetException e) {
228
        } catch (InterruptedException e) {
229
        }
230
    }
231

    
232
    private void readFileWithMonitor(final PrintWriter writer) {
233
        IRunnableWithProgress runnable = new IRunnableWithProgress() {
234
            public void run(IProgressMonitor monitor) throws InvocationTargetException,
235
                    InterruptedException {
236
                monitor
237
                        .beginTask(
238
                                PDERuntimeMessages.get().OpenLogDialog_message, IProgressMonitor.UNKNOWN); 
239
                try {
240
                    readFile(writer);
241
                } catch (IOException e) {
242
                    writer.println(PDERuntimeMessages.get().OpenLogDialog_cannotDisplay); 
243
                }
244
            }
245
        };
246
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell());
247
        try {
248
            dialog.run(true, true, runnable);
249
        } catch (InvocationTargetException e) {
250
        } catch (InterruptedException e) {
251
        }
252
    }
253
}
(11-11/12)