1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
9 package net.sourceforge.phpdt.internal.debug.core.logview;
11 import java.io.BufferedReader;
12 import java.io.BufferedWriter;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStreamWriter;
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.lang.reflect.InvocationTargetException;
22 import java.text.Collator;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.Date;
30 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
31 import net.sourceforge.phpdt.internal.debug.core.PHPDegugCorePluginImages;
33 import org.eclipse.core.runtime.ILogListener;
34 import org.eclipse.core.runtime.IProgressMonitor;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.Path;
37 import org.eclipse.core.runtime.Platform;
38 import org.eclipse.jface.action.Action;
39 import org.eclipse.jface.action.IMenuListener;
40 import org.eclipse.jface.action.IMenuManager;
41 import org.eclipse.jface.action.IStatusLineManager;
42 import org.eclipse.jface.action.IToolBarManager;
43 import org.eclipse.jface.action.MenuManager;
44 import org.eclipse.jface.action.Separator;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
47 import org.eclipse.jface.operation.IRunnableWithProgress;
48 import org.eclipse.jface.viewers.ColumnPixelData;
49 import org.eclipse.jface.viewers.DoubleClickEvent;
50 import org.eclipse.jface.viewers.IDoubleClickListener;
51 import org.eclipse.jface.viewers.ISelection;
52 import org.eclipse.jface.viewers.ISelectionChangedListener;
53 import org.eclipse.jface.viewers.IStructuredSelection;
54 import org.eclipse.jface.viewers.ITreeViewerListener;
55 import org.eclipse.jface.viewers.SelectionChangedEvent;
56 import org.eclipse.jface.viewers.TableLayout;
57 import org.eclipse.jface.viewers.TableTreeViewer;
58 import org.eclipse.jface.viewers.TreeExpansionEvent;
59 import org.eclipse.jface.viewers.Viewer;
60 import org.eclipse.jface.viewers.ViewerSorter;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.custom.BusyIndicator;
63 import org.eclipse.swt.custom.TableTree;
64 import org.eclipse.swt.custom.TableTreeItem;
65 import org.eclipse.swt.dnd.Clipboard;
66 import org.eclipse.swt.dnd.TextTransfer;
67 import org.eclipse.swt.dnd.Transfer;
68 import org.eclipse.swt.events.DisposeEvent;
69 import org.eclipse.swt.events.DisposeListener;
70 import org.eclipse.swt.events.SelectionAdapter;
71 import org.eclipse.swt.events.SelectionEvent;
72 import org.eclipse.swt.graphics.Color;
73 import org.eclipse.swt.graphics.Font;
74 import org.eclipse.swt.graphics.FontData;
75 import org.eclipse.swt.graphics.Point;
76 import org.eclipse.swt.layout.GridData;
77 import org.eclipse.swt.layout.GridLayout;
78 import org.eclipse.swt.program.Program;
79 import org.eclipse.swt.widgets.Composite;
80 import org.eclipse.swt.widgets.Control;
81 import org.eclipse.swt.widgets.Display;
82 import org.eclipse.swt.widgets.Event;
83 import org.eclipse.swt.widgets.FileDialog;
84 import org.eclipse.swt.widgets.Listener;
85 import org.eclipse.swt.widgets.Menu;
86 import org.eclipse.swt.widgets.Shell;
87 import org.eclipse.swt.widgets.Table;
88 import org.eclipse.swt.widgets.TableColumn;
89 import org.eclipse.swt.widgets.TableItem;
90 import org.eclipse.swt.widgets.Text;
91 import org.eclipse.ui.IActionBars;
92 import org.eclipse.ui.IMemento;
93 import org.eclipse.ui.ISharedImages;
94 import org.eclipse.ui.IViewSite;
95 import org.eclipse.ui.IWorkbenchPage;
96 import org.eclipse.ui.PartInitException;
97 import org.eclipse.ui.PlatformUI;
98 import org.eclipse.ui.XMLMemento;
99 import org.eclipse.ui.actions.ActionFactory;
100 import org.eclipse.ui.part.ViewPart;
102 public class LogView extends ViewPart implements ILogListener {
103 public final static String ID_LOGVIEW = "net.sourceforge.phpdt.internal.debug.core.logview.LogView";
105 private TableTreeViewer tableTreeViewer;
107 private ArrayList logs = new ArrayList();
109 public static final String P_LOG_WARNING = "warning"; //$NON-NLS-1$
111 public static final String P_LOG_ERROR = "error"; //$NON-NLS-1$
113 public static final String P_LOG_INFO = "info"; //$NON-NLS-1$
115 public static final String P_LOG_LIMIT = "limit"; //$NON-NLS-1$
117 public static final String P_USE_LIMIT = "useLimit"; //$NON-NLS-1$
119 public static final String P_SHOW_ALL_SESSIONS = "allSessions"; //$NON-NLS-1$
121 private static final String P_COLUMN_1 = "column1"; //$NON-NLS-1$
123 private static final String P_COLUMN_2 = "column2"; //$NON-NLS-1$
125 private static final String P_COLUMN_3 = "column3"; //$NON-NLS-1$
127 private static final String P_COLUMN_4 = "column4"; //$NON-NLS-1$
129 public static final String P_ACTIVATE = "activate"; //$NON-NLS-1$
131 private int MESSAGE_ORDER = -1;
133 private int PLUGIN_ORDER = -1;
135 private int DATE_ORDER = -1;
137 public static byte MESSAGE = 0x0;
139 public static byte PLUGIN = 0x1;
141 public static byte DATE = 0x2;
143 private static int ASCENDING = 1;
145 private static int DESCENDING = -1;
147 private Action clearAction;
149 private Action copyAction;
151 private Action readLogAction;
153 private Action deleteLogAction;
155 private Action exportAction;
157 private Action importAction;
159 private Action activateViewAction;
161 private Action propertiesAction;
163 private Action viewLogAction;
165 private Action filterAction;
167 private Clipboard clipboard;
169 private IMemento memento;
171 private File inputFile;
173 private String directory;
175 private TableColumn column0;
177 private TableColumn column1;
179 private TableColumn column2;
181 private TableColumn column3;
183 private TableColumn column4;
185 private static Font boldFont;
187 private Comparator comparator;
189 private Collator collator;
192 private boolean canOpenTextShell;
194 private Text textLabel;
196 private Shell textShell;
198 private boolean firstEvent = true;
201 logs = new ArrayList();
202 inputFile = Platform.getLogFileLocation().toFile();
205 public void createPartControl(Composite parent) {
207 TableTree tableTree = new TableTree(parent, SWT.FULL_SELECTION);
208 tableTree.setLayoutData(new GridData(GridData.FILL_BOTH));
209 createColumns(tableTree.getTable());
210 createViewer(tableTree);
211 createPopupMenuManager(tableTree);
212 makeActions(tableTree.getTable());
214 Platform.addLogListener(this);
215 getSite().setSelectionProvider(tableTreeViewer);
216 clipboard = new Clipboard(tableTree.getDisplay());
217 // WorkbenchHelp.setHelp(tableTree, IHelpContextIds.LOG_VIEW);
218 tableTreeViewer.getTableTree().getTable().setToolTipText(""); //$NON-NLS-1$
223 private void initializeFonts() {
224 Font tableFont = tableTreeViewer.getTableTree().getFont();
225 FontData[] fontDataList = tableFont.getFontData();
227 if (fontDataList.length > 0)
228 fontData = fontDataList[0];
230 fontData = new FontData();
231 fontData.setStyle(SWT.BOLD);
232 boldFont = new Font(tableTreeViewer.getTableTree().getDisplay(), fontData);
236 * Set all rows where the tableTreeItem has children to have a <b>bold </b> font.
238 private void applyFonts() {
239 if (tableTreeViewer == null || tableTreeViewer.getTableTree().isDisposed())
241 int max = tableTreeViewer.getTableTree().getItemCount();
242 int index = 0, tableIndex = 0;
243 while (index < max) {
244 LogEntry entry = (LogEntry) tableTreeViewer.getElementAt(index);
247 if (entry.hasChildren()) {
248 tableTreeViewer.getTableTree().getItems()[index].setFont(boldFont);
249 tableIndex = applyChildFonts(entry, tableIndex);
251 tableTreeViewer.getTableTree().getItems()[index].setFont(tableTreeViewer.getTableTree().getFont());
258 private int applyChildFonts(LogEntry parent, int index) {
259 if (!tableTreeViewer.getExpandedState(parent) || !parent.hasChildren())
261 LogEntry[] children = getEntryChildren(parent);
262 for (int i = 0; i < children.length; i++) {
264 if (children[i].hasChildren()) {
265 TableItem tableItem = getTableItem(index);
266 if (tableItem != null) {
267 tableItem.setFont(boldFont);
269 index = applyChildFonts(children[i], index);
271 TableItem tableItem = getTableItem(index);
272 if (tableItem != null) {
273 tableItem.setFont(tableTreeViewer.getTableTree().getFont());
280 private LogEntry[] getEntryChildren(LogEntry parent) {
281 Object[] entryChildren = parent.getChildren(parent);
282 if (comparator != null)
283 Arrays.sort(entryChildren, comparator);
284 LogEntry[] children = new LogEntry[entryChildren.length];
285 System.arraycopy(entryChildren, 0, children, 0, entryChildren.length);
289 private TableItem getTableItem(int index) {
290 TableItem[] tableItems = tableTreeViewer.getTableTree().getTable().getItems();
291 if (index > tableItems.length - 1)
293 return tableItems[index];
296 private void fillToolBar() {
297 IActionBars bars = getViewSite().getActionBars();
298 bars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
299 IToolBarManager toolBarManager = bars.getToolBarManager();
300 toolBarManager.add(exportAction);
301 toolBarManager.add(importAction);
302 toolBarManager.add(new Separator());
303 toolBarManager.add(clearAction);
304 toolBarManager.add(deleteLogAction);
305 toolBarManager.add(viewLogAction);
306 toolBarManager.add(readLogAction);
307 toolBarManager.add(new Separator());
308 IMenuManager mgr = bars.getMenuManager();
309 mgr.add(filterAction);
310 mgr.add(new Separator());
311 mgr.add(activateViewAction);
314 private void createViewer(TableTree tableTree) {
315 tableTreeViewer = new TableTreeViewer(tableTree);
316 tableTreeViewer.setContentProvider(new LogViewContentProvider(this));
317 tableTreeViewer.setLabelProvider(new LogViewLabelProvider());
318 tableTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
319 public void selectionChanged(SelectionChangedEvent e) {
320 handleSelectionChanged(e.getSelection());
321 if (propertiesAction.isEnabled())
322 ((EventDetailsDialogAction) propertiesAction).resetSelection();
325 tableTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
326 public void doubleClick(DoubleClickEvent event) {
327 ((EventDetailsDialogAction) propertiesAction).setComparator(comparator);
328 propertiesAction.run();
331 tableTreeViewer.addTreeListener(new ITreeViewerListener() {
332 public void treeCollapsed(TreeExpansionEvent event) {
336 public void treeExpanded(TreeExpansionEvent event) {
341 tableTreeViewer.setInput(Platform.class);
344 private void createPopupMenuManager(TableTree tableTree) {
345 MenuManager popupMenuManager = new MenuManager();
346 IMenuListener listener = new IMenuListener() {
347 public void menuAboutToShow(IMenuManager mng) {
348 fillContextMenu(mng);
351 popupMenuManager.addMenuListener(listener);
352 popupMenuManager.setRemoveAllWhenShown(true);
353 Menu menu = popupMenuManager.createContextMenu(tableTree);
354 tableTree.setMenu(menu);
357 private void createColumns(Table table) {
358 column0 = new TableColumn(table, SWT.NULL);
359 column0.setText(""); //$NON-NLS-1$
360 column1 = new TableColumn(table, SWT.NULL);
361 column1.setText(PHPDebugCorePlugin.getResourceString("LogView.column.severity")); //$NON-NLS-1$
362 column2 = new TableColumn(table, SWT.NULL);
363 column2.setText(PHPDebugCorePlugin.getResourceString("LogView.column.message")); //$NON-NLS-1$
364 column2.addSelectionListener(new SelectionAdapter() {
365 public void widgetSelected(SelectionEvent e) {
367 ViewerSorter sorter = getViewerSorter(MESSAGE);
368 tableTreeViewer.setSorter(sorter);
369 collator = sorter.getCollator();
370 boolean isComparatorSet = ((EventDetailsDialogAction) propertiesAction).resetSelection(MESSAGE, MESSAGE_ORDER);
371 setComparator(MESSAGE);
372 if (!isComparatorSet)
373 ((EventDetailsDialogAction) propertiesAction).setComparator(comparator);
377 column3 = new TableColumn(table, SWT.NULL);
378 column3.setText(PHPDebugCorePlugin.getResourceString("LogView.column.plugin")); //$NON-NLS-1$
379 column3.addSelectionListener(new SelectionAdapter() {
380 public void widgetSelected(SelectionEvent e) {
382 ViewerSorter sorter = getViewerSorter(PLUGIN);
383 tableTreeViewer.setSorter(sorter);
384 collator = sorter.getCollator();
385 boolean isComparatorSet = ((EventDetailsDialogAction) propertiesAction).resetSelection(PLUGIN, PLUGIN_ORDER);
386 setComparator(PLUGIN);
387 if (!isComparatorSet)
388 ((EventDetailsDialogAction) propertiesAction).setComparator(comparator);
392 column4 = new TableColumn(table, SWT.NULL);
393 column4.setText(PHPDebugCorePlugin.getResourceString("LogView.column.date")); //$NON-NLS-1$
394 column4.addSelectionListener(new SelectionAdapter() {
395 public void widgetSelected(SelectionEvent e) {
396 if (DATE_ORDER == ASCENDING) {
397 DATE_ORDER = DESCENDING;
399 DATE_ORDER = ASCENDING;
401 ViewerSorter sorter = getViewerSorter(DATE);
402 tableTreeViewer.setSorter(sorter);
403 collator = sorter.getCollator();
404 boolean isComparatorSet = ((EventDetailsDialogAction) propertiesAction).resetSelection(DATE, DATE_ORDER);
406 if (!isComparatorSet)
407 ((EventDetailsDialogAction) propertiesAction).setComparator(comparator);
411 TableLayout tlayout = new TableLayout();
412 tlayout.addColumnData(new ColumnPixelData(21));
413 tlayout.addColumnData(new ColumnPixelData(memento.getInteger(P_COLUMN_1).intValue()));
414 tlayout.addColumnData(new ColumnPixelData(memento.getInteger(P_COLUMN_2).intValue()));
415 tlayout.addColumnData(new ColumnPixelData(memento.getInteger(P_COLUMN_3).intValue()));
416 tlayout.addColumnData(new ColumnPixelData(memento.getInteger(P_COLUMN_4).intValue()));
417 table.setLayout(tlayout);
418 table.setHeaderVisible(true);
421 private void makeActions(Table table) {
422 propertiesAction = new EventDetailsDialogAction(table.getShell(), tableTreeViewer);
423 propertiesAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_PROPERTIES);
424 propertiesAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_PROPERTIES_DISABLED);
425 propertiesAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.properties.tooltip")); //$NON-NLS-1$
426 propertiesAction.setEnabled(false);
427 clearAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.clear")) { //$NON-NLS-1$
432 clearAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_CLEAR);
433 clearAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_CLEAR_DISABLED);
434 clearAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.clear.tooltip")); //$NON-NLS-1$
435 clearAction.setText(PHPDebugCorePlugin.getResourceString("LogView.clear")); //$NON-NLS-1$
436 readLogAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.readLog.restore")) { //$NON-NLS-1$
438 inputFile = Platform.getLogFileLocation().toFile();
442 readLogAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.readLog.restore.tooltip")); //$NON-NLS-1$
443 readLogAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_READ_LOG);
444 readLogAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_READ_LOG_DISABLED);
445 deleteLogAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.delete")) { //$NON-NLS-1$
450 deleteLogAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.delete.tooltip")); //$NON-NLS-1$
451 deleteLogAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_REMOVE_LOG);
452 deleteLogAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_REMOVE_LOG_DISABLED);
453 deleteLogAction.setEnabled(inputFile.exists() && inputFile.equals(Platform.getLogFileLocation().toFile()));
454 copyAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.copy")) { //$NON-NLS-1$
456 copyToClipboard(tableTreeViewer.getSelection());
459 copyAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
460 filterAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.filter")) { //$NON-NLS-1$
465 filterAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.filter")); //$NON-NLS-1$
466 filterAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_FILTER);
467 filterAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_FILTER_DISABLED);
468 exportAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.export")) { //$NON-NLS-1$
473 exportAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.export.tooltip")); //$NON-NLS-1$
474 exportAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_EXPORT);
475 exportAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_EXPORT_DISABLED);
476 importAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.import")) { //$NON-NLS-1$
481 importAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.import.tooltip")); //$NON-NLS-1$
482 importAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_IMPORT);
483 importAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_IMPORT_DISABLED);
484 activateViewAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.activate")) { //$NON-NLS-1$
488 activateViewAction.setChecked(memento.getString(P_ACTIVATE).equals("true")); //$NON-NLS-1$
489 viewLogAction = new Action(PHPDebugCorePlugin.getResourceString("LogView.view.currentLog")) { //$NON-NLS-1$
491 if (inputFile.exists()) {
492 if (inputFile.length() > LogReader.MAX_FILE_LENGTH) {
493 OpenLogDialog openDialog = new OpenLogDialog(getViewSite().getShell(), inputFile);
497 boolean canLaunch = Program.launch(inputFile.getAbsolutePath());
499 Program p = Program.findProgram(".txt"); //$NON-NLS-1$
501 p.execute(inputFile.getAbsolutePath());
503 OpenLogDialog openDialog = new OpenLogDialog(getViewSite().getShell(), inputFile);
512 viewLogAction.setImageDescriptor(PHPDegugCorePluginImages.DESC_OPEN_LOG);
513 viewLogAction.setDisabledImageDescriptor(PHPDegugCorePluginImages.DESC_OPEN_LOG_DISABLED);
514 viewLogAction.setEnabled(inputFile.exists());
515 viewLogAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.view.currentLog.tooltip")); //$NON-NLS-1$
518 public void dispose() {
519 Platform.removeLogListener(this);
526 private void handleImport() {
527 FileDialog dialog = new FileDialog(getViewSite().getShell());
528 dialog.setFilterExtensions(new String[] { "*.log" }); //$NON-NLS-1$
529 if (directory != null)
530 dialog.setFilterPath(directory);
531 String path = dialog.open();
532 if (path != null && new Path(path).toFile().exists()) {
533 inputFile = new Path(path).toFile();
534 directory = inputFile.getParent();
535 IRunnableWithProgress op = new IRunnableWithProgress() {
536 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
537 monitor.beginTask(PHPDebugCorePlugin.getResourceString("LogView.operation.importing"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
541 ProgressMonitorDialog pmd = new ProgressMonitorDialog(getViewSite().getShell());
543 pmd.run(true, true, op);
544 } catch (InvocationTargetException e) {
545 } catch (InterruptedException e) {
547 readLogAction.setText(PHPDebugCorePlugin.getResourceString("LogView.readLog.reload")); //$NON-NLS-1$
548 readLogAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.readLog.reload")); //$NON-NLS-1$
550 resetDialogButtons();
555 private void handleExport() {
556 FileDialog dialog = new FileDialog(getViewSite().getShell(), SWT.SAVE);
557 dialog.setFilterExtensions(new String[] { "*.log" }); //$NON-NLS-1$
558 if (directory != null)
559 dialog.setFilterPath(directory);
560 String path = dialog.open();
562 if (!path.endsWith(".log")) //$NON-NLS-1$
563 path += ".log"; //$NON-NLS-1$
564 File outputFile = new Path(path).toFile();
565 directory = outputFile.getParent();
566 if (outputFile.exists()) {
567 String message = PHPDebugCorePlugin.getFormattedMessage("LogView.confirmOverwrite.message", //$NON-NLS-1$
568 outputFile.toString());
569 if (!MessageDialog.openQuestion(getViewSite().getShell(), exportAction.getText(), message))
572 copy(inputFile, outputFile);
576 private void copy(File inputFile, File outputFile) {
577 BufferedReader reader = null;
578 BufferedWriter writer = null;
580 reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8")); //$NON-NLS-1$
581 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); //$NON-NLS-1$
582 while (reader.ready()) {
583 writer.write(reader.readLine());
584 writer.write(System.getProperty("line.separator")); //$NON-NLS-1$
586 } catch (IOException e) {
593 } catch (IOException e1) {
598 private void handleFilter() {
599 FilterDialog dialog = new FilterDialog(PHPDebugCorePlugin.getActiveWorkbenchShell(), memento);
601 dialog.getShell().setText(PHPDebugCorePlugin.getResourceString("LogView.FilterDialog.title")); //$NON-NLS-1$
602 if (dialog.open() == FilterDialog.OK)
606 private void doDeleteLog() {
607 String title = PHPDebugCorePlugin.getResourceString("LogView.confirmDelete.title"); //$NON-NLS-1$
608 String message = PHPDebugCorePlugin.getResourceString("LogView.confirmDelete.message"); //$NON-NLS-1$
609 if (!MessageDialog.openConfirm(tableTreeViewer.getControl().getShell(), title, message))
611 if (inputFile.delete()) {
614 resetDialogButtons();
618 public void fillContextMenu(IMenuManager manager) {
619 manager.add(copyAction);
620 manager.add(new Separator());
621 manager.add(clearAction);
622 manager.add(deleteLogAction);
623 manager.add(viewLogAction);
624 manager.add(readLogAction);
625 manager.add(new Separator());
626 manager.add(exportAction);
627 manager.add(importAction);
628 manager.add(new Separator());
629 ((EventDetailsDialogAction) propertiesAction).setComparator(comparator);
630 manager.add(propertiesAction);
633 public LogEntry[] getLogs() {
634 return (LogEntry[]) logs.toArray(new LogEntry[logs.size()]);
637 protected void handleClear() {
638 BusyIndicator.showWhile(tableTreeViewer.getControl().getDisplay(), new Runnable() {
642 resetDialogButtons();
647 protected void reloadLog() {
648 IRunnableWithProgress op = new IRunnableWithProgress() {
649 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
650 monitor.beginTask(PHPDebugCorePlugin.getResourceString("LogView.operation.reloading"), //$NON-NLS-1$
651 IProgressMonitor.UNKNOWN);
655 ProgressMonitorDialog pmd = new ProgressMonitorDialog(getViewSite().getShell());
657 pmd.run(true, true, op);
658 } catch (InvocationTargetException e) {
659 } catch (InterruptedException e) {
661 readLogAction.setText(PHPDebugCorePlugin.getResourceString("LogView.readLog.restore")); //$NON-NLS-1$
662 readLogAction.setToolTipText(PHPDebugCorePlugin.getResourceString("LogView.readLog.restore")); //$NON-NLS-1$
664 resetDialogButtons();
668 private void readLogFile() {
670 if (!inputFile.exists())
672 if (inputFile.length() > LogReader.MAX_FILE_LENGTH)
673 LogReader.parseLargeFile(inputFile, logs, memento);
675 LogReader.parseLogFile(inputFile, logs, memento);
678 public void logging(IStatus status, String plugin) {
679 if (!inputFile.equals(Platform.getLogFileLocation().toFile()))
690 private void pushStatus(IStatus status) {
691 LogEntry entry = new LogEntry(status);
692 LogReader.addEntry(entry, logs, memento, true);
696 private void asyncRefresh() {
700 private void asyncRefresh(final boolean activate) {
701 final Control control = tableTreeViewer.getControl();
702 if (control.isDisposed())
704 Display display = control.getDisplay();
705 final ViewPart view = this;
706 if (display != null) {
707 display.asyncExec(new Runnable() {
709 if (!control.isDisposed()) {
710 tableTreeViewer.refresh();
711 deleteLogAction.setEnabled(inputFile.exists() && inputFile.equals(Platform.getLogFileLocation().toFile()));
712 viewLogAction.setEnabled(inputFile.exists());
713 if (activate && activateViewAction.isChecked()) {
714 IWorkbenchPage page = PHPDebugCorePlugin.getActivePage();
716 page.bringToTop(view);
725 public void setFocus() {
726 if (tableTreeViewer != null && !tableTreeViewer.getTableTree().isDisposed())
727 tableTreeViewer.getTableTree().getTable().setFocus();
730 private void handleSelectionChanged(ISelection selection) {
731 updateStatus(selection);
732 copyAction.setEnabled(!selection.isEmpty());
733 propertiesAction.setEnabled(!selection.isEmpty());
736 private void updateStatus(ISelection selection) {
737 IStatusLineManager status = getViewSite().getActionBars().getStatusLineManager();
738 if (selection.isEmpty())
739 status.setMessage(null);
741 LogEntry entry = (LogEntry) ((IStructuredSelection) selection).getFirstElement();
742 status.setMessage(((LogViewLabelProvider) tableTreeViewer.getLabelProvider()).getColumnText(entry, 2));
746 private void copyToClipboard(ISelection selection) {
747 StringWriter writer = new StringWriter();
748 PrintWriter pwriter = new PrintWriter(writer);
749 if (selection.isEmpty())
751 LogEntry entry = (LogEntry) ((IStructuredSelection) selection).getFirstElement();
752 entry.write(pwriter);
754 String textVersion = writer.toString();
758 } catch (IOException e) {
760 if (textVersion.trim().length() > 0) {
761 // set the clipboard contents
762 clipboard.setContents(new Object[] { textVersion }, new Transfer[] { TextTransfer.getInstance() });
766 public void init(IViewSite site, IMemento memento) throws PartInitException {
767 super.init(site, memento);
769 this.memento = XMLMemento.createWriteRoot("LOGVIEW"); //$NON-NLS-1$
771 this.memento = memento;
775 private void initializeMemento() {
776 if (memento.getString(P_USE_LIMIT) == null)
777 memento.putString(P_USE_LIMIT, "true"); //$NON-NLS-1$
778 if (memento.getInteger(P_LOG_LIMIT) == null)
779 memento.putInteger(P_LOG_LIMIT, 50);
780 if (memento.getString(P_LOG_INFO) == null)
781 memento.putString(P_LOG_INFO, "true"); //$NON-NLS-1$
782 if (memento.getString(P_LOG_WARNING) == null)
783 memento.putString(P_LOG_WARNING, "true"); //$NON-NLS-1$
784 if (memento.getString(P_LOG_ERROR) == null)
785 memento.putString(P_LOG_ERROR, "true"); //$NON-NLS-1$
786 if (memento.getString(P_SHOW_ALL_SESSIONS) == null)
787 memento.putString(P_SHOW_ALL_SESSIONS, "true"); //$NON-NLS-1$
788 Integer width = memento.getInteger(P_COLUMN_1);
789 if (width == null || width.intValue() == 0)
790 memento.putInteger(P_COLUMN_1, 20);
791 width = memento.getInteger(P_COLUMN_2);
792 if (width == null || width.intValue() == 0)
793 memento.putInteger(P_COLUMN_2, 300);
794 width = memento.getInteger(P_COLUMN_3);
795 if (width == null || width.intValue() == 0)
796 memento.putInteger(P_COLUMN_3, 150);
797 width = memento.getInteger(P_COLUMN_4);
798 if (width == null || width.intValue() == 0)
799 memento.putInteger(P_COLUMN_4, 150);
800 if (memento.getString(P_ACTIVATE) == null)
801 memento.putString(P_ACTIVATE, "true"); //$NON-NLS-1$
804 public void saveState(IMemento memento) {
805 if (this.memento == null || memento == null)
807 this.memento.putInteger(P_COLUMN_1, column1.getWidth());
808 this.memento.putInteger(P_COLUMN_2, column2.getWidth());
809 this.memento.putInteger(P_COLUMN_3, column3.getWidth());
810 this.memento.putInteger(P_COLUMN_4, column4.getWidth());
811 this.memento.putString(P_ACTIVATE, activateViewAction.isChecked() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
812 memento.putMemento(this.memento);
815 private void addMouseListeners() {
816 Listener tableListener = new Listener() {
817 public void handleEvent(Event e) {
831 int[] tableEvents = new int[] { SWT.MouseDown, SWT.MouseMove, SWT.MouseHover };
832 for (int i = 0; i < tableEvents.length; i++) {
833 tableTreeViewer.getTableTree().getTable().addListener(tableEvents[i], tableListener);
837 private void makeHoverShell() {
838 Control control = tableTreeViewer.getControl();
839 textShell = new Shell(control.getShell(), SWT.NO_FOCUS | SWT.ON_TOP);
840 Display display = textShell.getDisplay();
841 textShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
842 GridLayout layout = new GridLayout(1, false);
843 int border = ((control.getShell().getStyle() & SWT.NO_TRIM) == 0) ? 0 : 1;
844 layout.marginHeight = border;
845 layout.marginWidth = border;
846 textShell.setLayout(layout);
847 textShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
848 Composite shellComposite = new Composite(textShell, SWT.NONE);
849 layout = new GridLayout();
850 layout.marginHeight = 0;
851 layout.marginWidth = 0;
852 shellComposite.setLayout(layout);
853 shellComposite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
854 textLabel = new Text(shellComposite, SWT.WRAP | SWT.MULTI);
855 GridData gd = new GridData(GridData.FILL_BOTH);
857 gd.grabExcessHorizontalSpace = true;
858 textLabel.setLayoutData(gd);
859 Color c = control.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
860 textLabel.setBackground(c);
861 c = control.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
862 textLabel.setForeground(c);
863 textLabel.setEditable(false);
864 textShell.addDisposeListener(new DisposeListener() {
865 public void widgetDisposed(DisposeEvent e) {
866 onTextShellDispose(e);
871 void onTextShellDispose(DisposeEvent e) {
872 canOpenTextShell = true;
876 void onMouseDown(Event e) {
877 if (textShell != null && !textShell.isDisposed() && !textShell.isFocusControl()) {
879 canOpenTextShell = true;
883 void onMouseHover(Event e) {
884 if (!canOpenTextShell)
886 canOpenTextShell = false;
887 Point point = new Point(e.x, e.y);
888 TableTree table = tableTreeViewer.getTableTree();
889 TableTreeItem item = table.getItem(point);
892 String message = ((LogEntry) item.getData()).getStack();
896 textLabel.setText(message);
898 int y = point.y - (table.getItemHeight() * 2) - 20;
899 textShell.setLocation(table.toDisplay(x, y));
900 textShell.setSize(tableTreeViewer.getTableTree().getSize().x - x, 125);
905 void onMouseMove(Event e) {
906 if (textShell != null && !textShell.isDisposed()) {
908 canOpenTextShell = textShell.isDisposed() && e.x > column0.getWidth() && e.x < (column0.getWidth() + column1.getWidth());
910 canOpenTextShell = e.x > column0.getWidth() && e.x < (column0.getWidth() + column1.getWidth());
914 public Comparator getComparator() {
918 private void setComparator(byte sortType) {
919 if (sortType == DATE) {
920 comparator = new Comparator() {
921 public int compare(Object e1, Object e2) {
923 SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss.SS"); //$NON-NLS-1$
924 Date date1 = formatter.parse(((LogEntry) e1).getDate());
925 Date date2 = formatter.parse(((LogEntry) e2).getDate());
926 if (DATE_ORDER == ASCENDING)
927 return date1.before(date2) ? -1 : 1;
928 return date1.after(date2) ? -1 : 1;
929 } catch (ParseException e) {
934 } else if (sortType == PLUGIN) {
935 comparator = new Comparator() {
936 public int compare(Object e1, Object e2) {
937 LogEntry entry1 = (LogEntry) e1;
938 LogEntry entry2 = (LogEntry) e2;
939 return collator.compare(entry1.getPluginId(), entry2.getPluginId()) * PLUGIN_ORDER;
943 comparator = new Comparator() {
944 public int compare(Object e1, Object e2) {
945 LogEntry entry1 = (LogEntry) e1;
946 LogEntry entry2 = (LogEntry) e2;
947 return collator.compare(entry1.getMessage(), entry2.getMessage()) * MESSAGE_ORDER;
953 private ViewerSorter getViewerSorter(byte sortType) {
954 if (sortType == PLUGIN) {
955 return new ViewerSorter() {
956 public int compare(Viewer viewer, Object e1, Object e2) {
957 LogEntry entry1 = (LogEntry) e1;
958 LogEntry entry2 = (LogEntry) e2;
959 return super.compare(viewer, entry1.getPluginId(), entry2.getPluginId()) * PLUGIN_ORDER;
962 } else if (sortType == MESSAGE) {
963 return new ViewerSorter() {
964 public int compare(Viewer viewer, Object e1, Object e2) {
965 LogEntry entry1 = (LogEntry) e1;
966 LogEntry entry2 = (LogEntry) e2;
967 return super.compare(viewer, entry1.getMessage(), entry2.getMessage()) * MESSAGE_ORDER;
971 return new ViewerSorter() {
972 public int compare(Viewer viewer, Object e1, Object e2) {
974 SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss.SS"); //$NON-NLS-1$
975 Date date1 = formatter.parse(((LogEntry) e1).getDate());
976 Date date2 = formatter.parse(((LogEntry) e2).getDate());
977 if (DATE_ORDER == ASCENDING)
978 return date1.before(date2) ? -1 : 1;
979 return date1.after(date2) ? -1 : 1;
980 } catch (ParseException e) {
988 private void resetDialogButtons() {
989 ((EventDetailsDialogAction) propertiesAction).resetDialogButtons();