Board logo

标题: Eclipse 任务列表(2)记号和任务列表 [打印本页]

作者: look_w    时间: 2018-7-18 08:20     标题: Eclipse 任务列表(2)记号和任务列表

从用户的观点看,Eclipse 支持有两个问题:XM 有自己的项目重建逻辑和错误报告逻辑。最终,这两个问题表现为 XM 忽略了 Eclipse 的资源管理。
我准备在本系列的后两篇文章中讨论构建过程,现在主要解决错误报告的问题。
记号Eclipse 为构建人员和编译人员提供了任务列表和问题列表来报告错误,如图 1 所示。当用户双击其中的错误项时,编辑器就会打开有问题的文件。不幸的是,编写 XM 插件的第一个版本时,我没有找到如何添加列表项的文档,所以忽略了它。结果,该插件有自己的控制台,但不支持双击。
图 1. 任务和问题列表最后发现,向标准列表中添加消息并不难,但是不能直接添加。一开始,我试图寻找一个任务列表对象,但是没有发现添加列表项的方法。最后发现,无法添加或者至少无法        直接添加列表项。要添加错误消息,需要在资源上创建一个        记号(接口 IMarker)。从列表中删除一个消息,也要从资源上去掉记号。列表会自动更新翻译记号的变化。      
createMarker() 方法用于创建记号。该方法以记号 ID 作为参数。平台中定义了以几种标准的记号 ID:      
定义插件专用的记号是一种不错的选择。新记号的 ID 在         plugin.xml 文件(与 Eclipse 中的其他声明一样)重定义。清单 1 显示了一个记号声明,定义了记号 ID(        org.eclipse.core.resources.markers)的一个扩展。它还声明了新的记号,这些记号分别从         problemmarker(显示在问题列表中)和         textmarker(为了记录行号)中继承而来。将记号声明为持久的是为了在会话之间保存这些记号。      
清单 1. 记号声明
1
2
3
4
5
6
7
<extension id="marker"
           name="XM Message"
           point="org.eclipse.core.resources.markers">
   <super type="org.eclipse.core.resources.problemmarker"/>
   <super type="org.eclipse.core.resources.textmarker"/>
   <persistent value="true"/>
</extension>




用户可以根据不同的条件过滤消息,比如问题的类型(警告、错误)、优先级和记号 ID。定义插件专用的记号可以帮助用户对插件消息应用专门的过滤规则。
警告:Eclipse 有可能过滤掉插件消息。如果没有看到 XM 的任何消息,应该看看这些消息是不是过滤掉了。要改变过滤器,请在任务列表或问题列表中单击过滤器图标,一定要选中 XM 记号。
了解其中的窍门之后,集成到 XM 中并不难。从一开始,就通过 Messenger 接口将用户界面抽象化了。Messenger 定义了核心需要报告错误或进程信息的方法。为了支持方法列表,只需要编写新的 Messager 实现来创建适当的记号,如清单 2 所示。注意,        begin() 方法将删除所有的记号,以便在构建之前清除问题列表。      
清单 2. 记号的 Messenger 实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.ananas.xm.eclipse;
import java.text.MessageFormat;
import org.eclipse.ui.IWorkbench;
import org.ananas.xm.core.Filename;
import org.ananas.xm.core.Location;
import org.ananas.xm.core.Messenger;
import org.eclipse.ui.IWorkbenchPage;
import org.ananas.xm.core.XMException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.views.markers.MarkerViewUtil;
public class MessengerTaskList
  implements Messenger, EclipseConstants
{
  private IProject project = null;
  private IWorkbench workbench = null;
  private boolean noMarkerSoFar = true;
  private static class ShowMarkerView
    implements Runnable
  {
    private IWorkbench workbench;
    private IMarker marker;
    public ShowMarkerView(IWorkbench workbench,IMarker marker)
    {
      this.workbench = workbench;
      this.marker = marker;
    }
    public void run()
    {
      IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
      if(window == null)
      {
        IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
        if(windows != null && windows.length > 0)
           window = windows[0];
        else
           return;
      }
      IWorkbenchPage page = window.getActivePage();
      if(page != null)
        MarkerViewUtil.showMarker(page,marker,true);
    }
  }
  public MessengerTaskList(IWorkbench workbench,IProject project)
  {
    if(null == project || null == workbench)
      throw
      new NullPointerException("null argument in TaskListMessenger constructor");
    this.project = project;
    this.workbench = workbench;
  }
  protected void addMarker(String msg,Location location,
  int severity,int priority)
    throws XMException
  {
    IResource resource = null;
    if(null == location || location.equals(Location.UNKNOWN))
      resource = project;
    else
      resource = (IResource)location.getFilename().asPlatformSpecific();
    try
    {
      IMarker marker = resource.createMarker(MARKER_ID);
      if(null != location &&
      Location.UNKNOWN_POSITION != location.getLine())
         marker.setAttribute(IMarker.LINE_NUMBER,location.getLine());
      if(null != msg)
         marker.setAttribute(IMarker.MESSAGE,msg);
      marker.setAttribute(IMarker.SEVERITY,severity);
      marker.setAttribute(IMarker.PRIORITY,priority);
      if(noMarkerSoFar)
         showMarkerView(marker);
      else
         noMarkerSoFar = false;
    }
    catch(CoreException e)
    {
     throw new XMException(e,location);
    }
  }
  public void error(XMException x)
    throws XMException
  {
    addMarker(x.getMessage(),x.getLocation(),
    IMarker.SEVERITY_ERROR,IMarker.PRIORITY_NORMAL);
  }
  public void fatal(XMException x)
    throws XMException
  {
    addMarker(x.getMessage(),x.getLocation(),
    IMarker.SEVERITY_ERROR,IMarker.PRIORITY_HIGH);
  }
  public void warning(XMException x)
    throws XMException
  {
    addMarker(x.getMessage(),x.getLocation(),
    IMarker.SEVERITY_WARNING,IMarker.PRIORITY_LOW);
  }
  public boolean progress(Filename sourceFile,Filename resultFile)
  {
    return true;
  }
  public void info(String msg,Location location)
    throws XMException
  {
    addMarker(msg,location,IMarker.SEVERITY_INFO,IMarker.PRIORITY_NORMAL);
  }
  public void info(String pattern,Object[] arguments,Location location)
    throws XMException
  {
    info(MessageFormat.format(pattern,arguments),location);
  }
  public void begin(String source,String target)
    throws XMException
  {
    try
    {
      project.deleteMarkers(MARKER_ID,true,IResource.DEPTH_INFINITE);
      noMarkerSoFar =  true;
    }
    catch(CoreException e)
    {
      throw new XMException(e);
    }
  }
  public void end()
  {
  }
  protected void showMarkerView(IMarker marker)
  {
    Display display = Display.getCurrent();
    if(display == null)
      display = Display.getDefault();
    ShowMarkerView showMarkerView = new ShowMarkerView(workbench,marker);
    display.syncExec(showMarkerView);
  }
}






欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0