[logback-dev] svn commit: r1452 - in logbackPDE/trunk/plugins/ConsolePlugin: . META-INF src/main/java/ch/qos/logback/eclipse/actions src/main/java/ch/qos/logback/eclipse/util src/main/java/ch/qos/logback/eclipse/views

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Mar 22 16:42:37 CET 2007


Author: seb
Date: Thu Mar 22 16:42:37 2007
New Revision: 1452

Added:
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/actions/OpenStackTraceAction.java
Modified:
   logbackPDE/trunk/plugins/ConsolePlugin/META-INF/MANIFEST.MF
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/util/EditorUtil.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/FilterContentProvider.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventContentProvider.java
   logbackPDE/trunk/plugins/ConsolePlugin/todo.txt

Log:
Resolved some of the discouraged dependencies
It is now possible to right-click on an event and, if it has a ThrowableInformation object, open eclipse's JavaStackTraceConsole, set its content with the LoggingEvent's stacktrace and show the console.


Modified: logbackPDE/trunk/plugins/ConsolePlugin/META-INF/MANIFEST.MF
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/META-INF/MANIFEST.MF	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/META-INF/MANIFEST.MF	Thu Mar 22 16:42:37 2007
@@ -13,11 +13,11 @@
 Bundle-Localization: plugin
 Require-Bundle: org.eclipse.ui,
  org.eclipse.core.runtime,
- org.eclipse.jface.text,
  org.eclipse.ui.console,
  org.eclipse.jdt.core,
- org.eclipse.jdt.ui,
  org.eclipse.core.resources,
- org.eclipse.ui.ide
+ org.eclipse.ui.ide,
+ org.eclipse.jdt.debug.ui,
+ org.eclipse.jface.text
 Eclipse-LazyStart: true
 Export-Package: ch.qos.logback.eclipse.views

Added: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/actions/OpenStackTraceAction.java
==============================================================================
--- (empty file)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/actions/OpenStackTraceAction.java	Thu Mar 22 16:42:37 2007
@@ -0,0 +1,43 @@
+package ch.qos.logback.eclipse.actions;
+
+import org.eclipse.jdt.internal.debug.ui.console.JavaStackTraceConsole;
+import org.eclipse.jface.action.Action;
+import org.eclipse.ui.console.ConsolePlugin;
+import org.eclipse.ui.console.IConsole;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.eclipse.views.LogbackView;
+
+public class OpenStackTraceAction extends Action {
+
+  private static final String NEW_LINE = System.getProperty("line.separator");
+
+  LogbackView view;
+  JavaStackTraceConsole console;
+
+  public OpenStackTraceAction(LogbackView view, String text) {
+    super(text);
+    this.view = view;
+    console = new JavaStackTraceConsole();
+    ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
+  }
+
+  @Override
+  public void run() {
+    LoggingEvent event = view.getSelectedEvent();
+    if (event == null || event.getThrowableInformation() == null) {
+      return;
+    }
+
+    StringBuffer buf = new StringBuffer();
+    String[] lines = event.getThrowableInformation().getThrowableStrRep();
+    for (int i = 0; i < lines.length; i++) {
+      buf.append(lines[i]).append(NEW_LINE);
+    }
+
+    console.clearConsole();
+    console.getDocument().set(buf.toString());
+    ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
+  }
+
+}

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/util/EditorUtil.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/util/EditorUtil.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/util/EditorUtil.java	Thu Mar 22 16:42:37 2007
@@ -7,10 +7,14 @@
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
 import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.internal.core.JavaProject;
-import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
@@ -21,7 +25,6 @@
 
 public class EditorUtil {
 
-  @SuppressWarnings("restriction")
   public static void openInEditor(LoggingEvent event) {
     IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
 
@@ -31,12 +34,12 @@
       project = projects[i];
       if (project.isOpen()) {
         try {
-          JavaProject javaProject = new JavaProject(project, null);
+          IJavaProject javaProject = JavaCore.create(project);
           String className = getClassName(event);
           if (className == null) {
             return;
           }
-          type = JavaModelUtil.findType(javaProject, className);
+          type = findType(javaProject, className);
           if (type != null) {
             String path = type.getPath().toString();
             if (path.startsWith("/" + project.getName())) {
@@ -86,4 +89,73 @@
       e.printStackTrace();
     }
   }
+
+  public static IType findType(IJavaProject jproject, String fullyQualifiedName)
+      throws JavaModelException {
+    IType type = jproject.findType(fullyQualifiedName);
+    if (type != null)
+      return type;
+    IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
+    for (int i = 0; i < roots.length; i++) {
+      IPackageFragmentRoot root = roots[i];
+      type = findType(root, fullyQualifiedName);
+      if (type != null && type.exists())
+        return type;
+    }
+    return null;
+  }
+
+  private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName)
+      throws JavaModelException {
+    IJavaElement[] children = root.getChildren();
+    for (int i = 0; i < children.length; i++) {
+      IJavaElement element = children[i];
+      if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
+        IPackageFragment pack = (IPackageFragment) element;
+        if (!fullyQualifiedName.startsWith(pack.getElementName()))
+          continue;
+        IType type = findType(pack, fullyQualifiedName);
+        if (type != null && type.exists())
+          return type;
+      }
+    }
+    return null;
+  }
+
+  private static IType findType(IPackageFragment pack, String fullyQualifiedName)
+      throws JavaModelException {
+    ICompilationUnit[] cus = pack.getCompilationUnits();
+    for (int i = 0; i < cus.length; i++) {
+      ICompilationUnit unit = cus[i];
+      IType type = findType(unit, fullyQualifiedName);
+      if (type != null && type.exists())
+        return type;
+    }
+    return null;
+  }
+
+  private static IType findType(ICompilationUnit cu, String fullyQualifiedName)
+      throws JavaModelException {
+    IType[] types = cu.getAllTypes();
+    for (int i = 0; i < types.length; i++) {
+      IType type = types[i];
+      if (getFullyQualifiedName(type).equals(fullyQualifiedName))
+        return type;
+    }
+    return null;
+  }
+
+  public static String getFullyQualifiedName(IType type) {
+    try {
+      if (type.isBinary() && !type.isAnonymous()) {
+        IType declaringType = type.getDeclaringType();
+        if (declaringType != null) {
+          return getFullyQualifiedName(declaringType) + '.' + type.getElementName();
+        }
+      }
+    } catch (JavaModelException e) {
+      // ignore
+    }
+    return type.getFullyQualifiedName('.');
+  }
 }

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/FilterContentProvider.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/FilterContentProvider.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/FilterContentProvider.java	Thu Mar 22 16:42:37 2007
@@ -11,21 +11,21 @@
 import ch.qos.logback.core.filter.EvaluatorFilter;
 import ch.qos.logback.eclipse.model.EventFilter;
 
-public class FilterContentProvider extends LabelProvider 
-implements ITableLabelProvider, IStructuredContentProvider {
+public class FilterContentProvider extends LabelProvider implements ITableLabelProvider,
+    IStructuredContentProvider {
 
   private static FilterContentProvider provider = new FilterContentProvider();
-  
+
   private TableViewer viewer;
-  
+
   public static FilterContentProvider getProvider() {
     return provider;
   }
-  
+
   public Object[] getElements(Object inputElement) {
     return EventFilter.getAllFilters().toArray();
   }
-  
+
   public void setViewer(TableViewer viewer) {
     this.viewer = viewer;
   }
@@ -33,7 +33,7 @@
   public void dispose() {
   }
 
-  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 
+  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
   }
 
   public Image getColumnImage(Object element, int columnIndex) {
@@ -41,8 +41,8 @@
   }
 
   public String getColumnText(Object element, int columnIndex) {
-    EvaluatorFilter filter = (EvaluatorFilter)element;
-    JaninoEventEvaluator eval = (JaninoEventEvaluator)filter.getEvaluator();
+    EvaluatorFilter filter = (EvaluatorFilter) element;
+    JaninoEventEvaluator eval = (JaninoEventEvaluator) filter.getEvaluator();
     switch (columnIndex) {
     case 0:
       return eval.getExpression();
@@ -51,18 +51,18 @@
     case 2:
       return filter.getOnMismatch().toString();
     }
-    
+
     return null;
   }
-  
+
   public void remove(EvaluatorFilter filter) {
     EventFilter.remove(filter);
     viewer.remove(filter);
     viewer.refresh();
   }
-  
+
   public EvaluatorFilter createNewFilter() {
-    EvaluatorFilter current =  new EvaluatorFilter();
+    EvaluatorFilter current = new EvaluatorFilter();
     current.setContext(EventFilter.getContext());
     current.setName("evaluator filter");
     JaninoEventEvaluator eval = new JaninoEventEvaluator();

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	Thu Mar 22 16:42:37 2007
@@ -3,8 +3,11 @@
 import java.net.URL;
 
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IMenuListener;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
@@ -13,12 +16,14 @@
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IMemento;
 import org.eclipse.ui.ISharedImages;
 import org.eclipse.ui.IViewSite;
+import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.part.ViewPart;
@@ -28,6 +33,7 @@
 import ch.qos.logback.eclipse.PluginLogger;
 import ch.qos.logback.eclipse.actions.LogbackFilterAction;
 import ch.qos.logback.eclipse.actions.LogbackPreferencesAction;
+import ch.qos.logback.eclipse.actions.OpenStackTraceAction;
 import ch.qos.logback.eclipse.model.EventFilter;
 import ch.qos.logback.eclipse.model.LoggingEventManager;
 import ch.qos.logback.eclipse.reciever.SimpleSocketServer;
@@ -43,42 +49,43 @@
 public class LogbackView extends ViewPart {
 
   public static final String ID = "ch.qos.logback.eclipse.views.LogbackView";
-  
+
   private TableViewer viewer;
   private TableColumn textColumn;
-  
+
   private LoggingEventContentProvider provider;
   private LoggingEventLabelProvider labelProvider;
-  
+
   private Action clearConsoleAction;
   private Action autoScrollAction;
-  
+
   private SimpleSocketServer server;
   private Thread serverThread;
   private LogbackPreferencesAction preferencesAction;
   private LogbackFilterAction filterAction;
+  private OpenStackTraceAction openStackTraceAction;
 
   private IMemento memento;
-  
+
   public LogbackView() {
   }
 
   public void createPartControl(Composite parent) {
-    viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE
-        | SWT.FULL_SELECTION | SWT.VIRTUAL);
-   
+    viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION
+        | SWT.VIRTUAL);
+
     final Table table = viewer.getTable();
-    
+
     textColumn = new TableColumn(table, SWT.LEFT);
     textColumn.setText("Event");
     textColumn.setWidth(2000);
-    
+
     viewer.setItemCount(0);
 
     provider = new LoggingEventContentProvider(viewer);
     LoggingEventManager.getManager().addLoggingEventManagerListener(provider);
     viewer.setContentProvider(provider);
-    
+
     labelProvider = new LoggingEventLabelProvider();
     if (memento != null) {
       labelProvider.init(memento);
@@ -96,6 +103,7 @@
 
     makeActions();
     contributeToActionBars();
+    createContextMenu();
   }
 
   private void contributeToActionBars() {
@@ -113,7 +121,7 @@
 
     filterAction = new LogbackFilterAction(viewer);
     menu.add(filterAction);
-    
+
     clearConsoleAction = new Action() {
       @Override
       public void run() {
@@ -127,15 +135,20 @@
     clearConsoleAction.setToolTipText("Clear the console");
     clearConsoleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
         .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
-    
+
     autoScrollAction = new Action() {
       @Override
       public void run() {
         super.run();
         provider.toggleAutoScroll();
         updateText();
+        updateButton();
+      }
+
+      private void updateButton() {
+        // TODO change the icon when autoscroll is toggled
       }
-      
+
       private void updateText() {
         if (provider.getAutoScroll()) {
           this.setText("Turn off auto scroll");
@@ -152,11 +165,13 @@
     ImageDescriptor imgDesc = ImageDescriptor.createFromURL(url);
     autoScrollAction.setImageDescriptor(imgDesc);
 
+    openStackTraceAction = new OpenStackTraceAction(this, "Open stacktrace...");
+
   }
 
   private void launchSocketServer(Integer port) {
     server = new SimpleSocketServer();
-    if (port !=  null) {
+    if (port != null) {
       server.setPort(port.intValue());
     }
     serverThread = new Thread(server);
@@ -201,23 +216,60 @@
     serverThread = new Thread(server);
     serverThread.start();
   }
-  
+
   public void saveState(IMemento memento) {
     super.saveState(memento);
     EventFilter.saveState(memento);
     labelProvider.saveState(memento);
     server.saveState(memento);
   }
-  
+
   public void init(IViewSite site, IMemento memento) throws PartInitException {
     super.init(site, memento);
     this.memento = memento;
     initServer();
   }
-  
+
   public Viewer getViewer() {
-    //used in tests
+    // used in tests
     return viewer;
   }
-  
+
+  private void createContextMenu() {
+    MenuManager menuMgr = new MenuManager("#PopupMenu");
+    menuMgr.setRemoveAllWhenShown(true);
+    menuMgr.addMenuListener(new IMenuListener() {
+      public void menuAboutToShow(IMenuManager m) {
+        LogbackView.this.fillContextMenu(m);
+      }
+    });
+    Menu menu = menuMgr.createContextMenu(viewer.getControl());
+    viewer.getControl().setMenu(menu);
+    getSite().registerContextMenu(menuMgr, viewer);
+  }
+
+  private void fillContextMenu(IMenuManager menuMgr) {
+    boolean isEmpty = viewer.getSelection().isEmpty();
+    boolean enable = false;
+    if (!isEmpty) {
+      IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
+      LoggingEvent event = (LoggingEvent) selection.iterator().next();
+      if (event.getThrowableInformation() != null) {
+        enable = true;
+      }
+    }
+
+    openStackTraceAction.setEnabled(enable);
+    menuMgr.add(openStackTraceAction);
+    menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
+  }
+
+  public LoggingEvent getSelectedEvent() {
+    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
+    if (selection.size() > 0) {
+      return (LoggingEvent) selection.iterator().next();
+    }
+    return null;
+  }
+
 }
\ No newline at end of file

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventContentProvider.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventContentProvider.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventContentProvider.java	Thu Mar 22 16:42:37 2007
@@ -14,7 +14,8 @@
  * 
  * @author S&eacute;bastien Pennec
  */
-public class LoggingEventContentProvider implements LoggingEventManagerListener, ILazyContentProvider {
+public class LoggingEventContentProvider implements LoggingEventManagerListener,
+    ILazyContentProvider {
 
   private TableViewer viewer;
   private boolean autoScroll = true;
@@ -39,23 +40,23 @@
   }
 
   private void updateViewer(final LoggingEventManagerEvent event) {
-    //we should *not* add an element to the viewer here!
+    // we should *not* add an element to the viewer here!
     int count = LoggingEventManager.getManager().getEventCount();
     viewer.setItemCount(count);
     if (event.getItemsAdded().length > 0 && autoScroll) {
-      viewer.getTable().showItem(viewer.getTable().getItem(count -1));
+      viewer.getTable().showItem(viewer.getTable().getItem(count - 1));
     }
   }
 
   public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
     this.viewer = (TableViewer) viewer;
   }
-  
+
   public void toggleAutoScroll() {
     autoScroll = !autoScroll;
     int size = LoggingEventManager.getManager().getEventCount();
     if (autoScroll && size > 0) {
-      viewer.getTable().showItem(viewer.getTable().getItem(size -1));
+      viewer.getTable().showItem(viewer.getTable().getItem(size - 1));
     }
   }
 
@@ -64,11 +65,10 @@
   }
 
   public void dispose() {
-    //do nothing
+    // do nothing
   }
-  
+
   public void updateElement(int index) {
-    System.out.println("update element: " + index);
     viewer.replace(LoggingEventManager.getManager().getEvent(index), index);
   }
 }

Modified: logbackPDE/trunk/plugins/ConsolePlugin/todo.txt
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/todo.txt	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/todo.txt	Thu Mar 22 16:42:37 2007
@@ -6,11 +6,14 @@
 
 Stack traces with links
 
+Allow the user to choose Font and font size
+
 Color certains lines. Based on keywords and colors?
 
-Click on a log line and display the java class that requested the log
+Change the image when the autoScroll button is pressed
 
 DONE:
+Click on a log line and display the java class that requested the log
 Allow the user to change the socket's port
 Make the viewer always display the latest lines added
 Filter logs: threshold



More information about the logback-dev mailing list