1 11 12 package org.eclipse.pde.internal.ui.launcher; 13 14 import java.io.BufferedReader ; 15 import java.io.File ; 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 ; 23 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.dialogs.IDialogSettings; 27 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 28 import org.eclipse.jface.dialogs.TrayDialog; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 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; 39 40 43 public final class OpenLogDialog extends TrayDialog { 44 private File logFile; 46 private IDialogSettings dialogSettings; 48 private Point dialogLocation; 49 private Point dialogSize; 50 private int DEFAULT_WIDTH = 750; 51 private int DEFAULT_HEIGHT = 800; 52 53 public OpenLogDialog(Shell parentShell, File logFile) { 54 super(parentShell); 55 this.logFile = logFile; 56 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN | SWT.MODELESS); 57 58 } 59 60 63 protected void configureShell(Shell newShell) { 64 super.configureShell(newShell); 65 newShell.setText(PDEUIMessages.OpenLogDialog_title); 66 readConfiguration(); 67 } 68 69 72 protected void createButtonsForButtonBar(Composite parent) { 73 createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, 74 true); 75 } 76 77 public void create() { 78 super.create(); 79 if (dialogLocation != null) 81 getShell().setLocation(dialogLocation); 82 if (dialogSize != null) 84 getShell().setSize(dialogSize); 85 else 86 getShell().setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 87 getButton(IDialogConstants.CLOSE_ID).setFocus(); 88 } 89 90 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()); 104 return outer; 105 } 106 107 private String getLogSummary() { 108 StringWriter out = new StringWriter (); 109 PrintWriter writer = new PrintWriter (out); 110 if (logFile.length() > LaunchListener.MAX_FILE_LENGTH) { 111 readLargeFileWithMonitor(writer); 112 } else { 113 readFileWithMonitor(writer); 114 } 115 writer.close(); 116 return out.toString(); 117 } 118 119 private void readFile(PrintWriter writer) throws FileNotFoundException , IOException { 121 BufferedReader bReader = new BufferedReader (new FileReader (logFile)); 122 while (bReader.ready()) 123 writer.println(bReader.readLine()); 124 } 125 126 private void readLargeFile(PrintWriter writer) throws FileNotFoundException , 128 IOException { 129 RandomAccessFile random = null; 130 boolean hasStarted = false; 131 try { 132 random = new RandomAccessFile (logFile, "r"); random.seek(logFile.length() - LaunchListener.MAX_FILE_LENGTH); 134 for (;;) { 135 String line = random.readLine(); 136 if (line == null) 137 break; 138 line = line.trim(); 139 if (line.length() == 0) 140 continue; 141 if (!hasStarted 142 && (line.startsWith("!ENTRY") || line.startsWith("!SESSION"))) hasStarted = true; 144 if (hasStarted) 145 writer.println(line); 146 continue; 147 } 148 } finally { 149 try { 150 if (random != null) 151 random.close(); 152 } catch (IOException e1) { 153 } 154 } 155 } 156 157 162 protected void buttonPressed(int buttonId) { 163 if (buttonId == IDialogConstants.CLOSE_ID) { 164 storeSettings(); 165 close(); 166 } 167 super.buttonPressed(buttonId); 168 } 169 170 private void readLargeFileWithMonitor(final PrintWriter writer) { 171 IRunnableWithProgress runnable = new IRunnableWithProgress() { 172 public void run(IProgressMonitor monitor) throws InvocationTargetException , 173 InterruptedException { 174 monitor 175 .beginTask( 176 PDEUIMessages.OpenLogDialog_message, IProgressMonitor.UNKNOWN); 177 try { 178 readLargeFile(writer); 179 } catch (IOException e) { 180 writer.println(PDEUIMessages.OpenLogDialog_cannotDisplay); 181 } 182 } 183 }; 184 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell()); 185 try { 186 dialog.run(true, true, runnable); 187 } catch (InvocationTargetException e) { 188 } catch (InterruptedException e) { 189 } 190 } 191 192 private void readFileWithMonitor(final PrintWriter writer) { 193 IRunnableWithProgress runnable = new IRunnableWithProgress() { 194 public void run(IProgressMonitor monitor) throws InvocationTargetException , 195 InterruptedException { 196 monitor 197 .beginTask( 198 PDEUIMessages.OpenLogDialog_message, IProgressMonitor.UNKNOWN); 199 try { 200 readFile(writer); 201 } catch (IOException e) { 202 writer.println(PDEUIMessages.OpenLogDialog_cannotDisplay); 203 } 204 } 205 }; 206 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getParentShell()); 207 try { 208 dialog.run(true, true, runnable); 209 } catch (InvocationTargetException e) { 210 } catch (InterruptedException e) { 211 } 212 } 213 214 220 private void storeSettings() { 221 writeConfiguration(); 222 } 223 224 230 private IDialogSettings getDialogSettings() { 231 IDialogSettings settings = PDEPlugin.getDefault().getDialogSettings(); 232 dialogSettings = settings.getSection(getClass().getName()); 233 if (dialogSettings == null) 234 dialogSettings = settings.addNewSection(getClass().getName()); 235 return dialogSettings; 236 } 237 238 242 private void readConfiguration() { 243 IDialogSettings s = getDialogSettings(); 244 try { 245 int x = s.getInt("x"); int y = s.getInt("y"); dialogLocation = new Point(x, y); 248 x = s.getInt("width"); y = s.getInt("height"); dialogSize = new Point(x, y); 251 } catch (NumberFormatException e) { 252 dialogLocation = null; 253 dialogSize = null; 254 } 255 } 256 257 private void writeConfiguration() { 258 IDialogSettings s = getDialogSettings(); 259 Point location = getShell().getLocation(); 260 s.put("x", location.x); s.put("y", location.y); Point size = getShell().getSize(); 263 s.put("width", size.x); s.put("height", size.y); } 266 } 267 | Popular Tags |