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

来源:互联网 发布:淘宝 0.0.0.1 编辑:程序博客网 时间:2024/06/02 12:23

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();}}}
原创粉丝点击