【JfaceTextFramework学习笔记之三】大纲视图

本文探讨了文本编辑器如何提供大纲视图,包括如何获取适配器、实现大纲页面接口、创建视图和更新内容,以及如何响应选择变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1-通用的 TextEditor 由于不知道要编辑的文本结构,所以不能提供大纲视图

 

2-

public Object getAdapter(Class required) {
	if (IContentOutlinePage.class.equals(required)) {
		if (fOutlinePage == null) {
			fOutlinePage= new JavaContentOutlinePage(getDocumentProvider(), this);
			if (getEditorInput() != null)
				fOutlinePage.setInput(getEditorInput());
		}
		return fOutlinePage;
	}
	return super.getAdapter(required);
}
3-A content outliner page must implement IContentOutlinePage
This interface combines the ability to notify selection change 
listeners (ISelectionProvider) with the behavior of being a page 
in a view (IPage).  通常在该视图中使用JFace的 TreeViewer来显示文本结构
4- 编辑器的EditInput也要传入大概视图中的Viewer
public void createControl(Composite parent) {

	super.createControl(parent);

	TreeViewer viewer= getTreeViewer();
	viewer.setContentProvider(new ContentProvider());
	viewer.setLabelProvider(new LabelProvider());
	viewer.addSelectionChangedListener(this);

	if (fInput != null)
		viewer.setInput(fInput);
}

The tree viewer creation is inherited from ContentOutlinePage

The standard label provider is used.

The content provider is provided inside JavaContentOutlinePage

and is responsible for parsing the editor input into individual

segments whenever it changes.

	
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			...
			if (newInput != null) {
				IDocument document= fDocumentProvider.getDocument(newInput);
				if (document != null) {
					document.addPositionCategory(SEGMENTS);
					document.addPositionUpdater(fPositionUpdater);
					parse(document);
				}
			}
		}
5-

When the selection changes, the selected segment is retrieved. 

 

Its offsets are used to set the highlight range in the editor.

public void selectionChanged(SelectionChangedEvent event) {

	super.selectionChanged(event);

	ISelection selection= event.getSelection();
	if (selection.isEmpty())
		fTextEditor.resetHighlightRange();
	else {
		Segment segment= (Segment) ((IStructuredSelection) selection).getFirstElement();
		int start= segment.position.getOffset();
		int length= segment.position.getLength();
		try {
			fTextEditor.setHighlightRange(start, length, true);
		} catch (IllegalArgumentException x) {
			fTextEditor.resetHighlightRange();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值