Sie sind auf Seite 1von 28

2004 IBM Corporation

Kai-Uwe Mtzel
IBM OTI Labs Zurich, Switzerland
Text Editors
and
How to Implement Your Own
2004 IBM Corporation
Example: J ava Editor
2004 IBM Corporation
J ava Editor
functionality
text coloring, current line highlighting, multi-column vertical ruler,
overview ruler, error squiggles, different kind of hovers, dynamically
updated outliner, content assist, bracket matching, find/replace,
annotation navigation,
variety conceptually different functions
nested control flows
interleaving control flows
2004 IBM Corporation
Overview
defining a text editor
the basics of text editors
connecting to the menu bar and tool bar
adding actions to the text editor
adding syntax highlighting
available configuration options
architecture
outlook
2004 IBM Corporation
Defining a text editor
<extension poi nt =" or g. ecl i pse. ui . edi t or s">
<editor
i d=" or g. ecl i pse. edi t or . exampl e"
name="Exampl e Edi t or "
ext ensi ons=" expl "
def aul t ="t r ue"
i con="i cons/ exampl e. gi f "
cl ass=" or g. ecl i pse. edi t or . i nt er nal . Exampl eEdi t or ">
</editor>
</extension>
can be opened on files with the expl extension
lives in the workbench not yet connected to the tool or menu bar
works on documents
ExtendedTextEditor
AbstractTextEditor
ITextEditor
2004 IBM Corporation
What are documents?
Documents store text and provide support for
line information
text manipulation
document change listeners
customizable position management
search
customizable partition management
document partition change listeners
2004 IBM Corporation
IDocument and its implementation
IDocument
IDocumentListener
IPositionUpdater
IDocumentPartitioner
IDocumentPartitioningListener
ConfigurableLineTracker
SequentialRewriteStore
GapTextStore
DefaultLineTracker
AbstractDocument
ITextStore
ILineTracker
2004 IBM Corporation
Recurring abstraction layers
Concept
Default Strategy for
Default Implementation
Default
Implementation
of Concept
Strategy
configurable with
r
e
p
l
a
c
a
b
l
e
r
e
p
l
a
c
a
b
l
e
2004 IBM Corporation
Where do documents come from?
each editor is connected to a document provider
document providers can be shared between editors
document provider
maps editor inputs onto documents and annotation models
tracks and communicates changes to the editor inputs into editor
understandable events (I El ement ChangeLi st ener )
translates changes of the documents and annotation models into
changes of the editor input (save)
manages dirty state, modification stamps, encoding
provides uniform access to editor inputs and their underlying elements
i nt er f ace I Text Edi t or ext ends I Edi t or Par t {

I Document Pr ovi der getDocumentProvider( ) ;


}
2004 IBM Corporation
Available document providers
St or ageDocument Pr ovi der
specialized on I St or ageEdi t or I nput
Text Fi l eDocument Pr ovi der
specialized on I Fi l eEdi t or I nput
replaces Fi l eDocument Pr ovi der
thin layer on top of file buffers (Fi l eBuf f er s)
file buffers assume most of the responsibility of document providers in
an editor independent way
<extension poi nt =" or g. ecl i pse. ui . edi t or s. document Pr ovi der s" >
<provider
cl ass=" or g. ecl i pse. ui . edi t or s. t ext . Text Fi l eDocument Pr ovi der "
i nput Types=" or g. ecl i pse. ui . I St or ageEdi t or I nput "
i d=" or g. ecl i pse. ui . edi t or s. t ext . St or ageDocument Pr ovi der " >
</provider>
</extension>
2004 IBM Corporation
Basic setup
publ i c cl ass Abst r act Text Edi t or i mpl ement s I El ement St at eLi st ener {
publ i c voi d setInput( I Edi t or I nput i nput ) {
releaseInput( ) ;
I Document Pr ovi der p= getDocumentProvider( i nput ) ;
p. addElementStateListener( t hi s) ;
p. connect( i nput ) ;
setDocument( p. getDocument( i nput ) ) ;
setAnnotationModel( p. getAnnotationModel( i nput ) ) ;
}
publ i c voi d elementDeleted( Obj ect el ement ) {
i f ( isInput( el ement ) )
}
publ i c voi d elementDirtyStateChanged( Obj ect el ement ) {}

}
2004 IBM Corporation
Connecting to the workbench
editor action bar contributor
connects editors with the tool and
menu bar of the workbench
editor action bar contributors are shared
on a per editor type base
<extension poi nt =" or g. ecl i pse. ui . edi t or s" >
<editor
i d=" or g. ecl i pse. edi t or . exampl e"
name=" Exampl e Edi t or "
ext ensi ons=" expl "
def aul t =" t r ue"
i con=" i cons/ exampl e. gi f "
cont r i but or Cl ass=" or g. ecl i pse. edi t or . i nt er nal . Exampl eAct i onCont r i but or "
cl ass=" or g. ecl i pse. edi t or . i nt er nal . Exampl eEdi t or " >
</editor>
</extension>
BasicTextEditorActionContributor
EditorActionBarContributor
IEditorActionBarContributor
2004 IBM Corporation
ExampleActionContributor
publ i c cl ass Exampl eAct i onCont r i but or
ext ends Basi cText Edi t or Act i onCont r i but or {
publ i c voi d setActiveEditor( I Edi t or Par t par t ) {
super . setActiveEditor( par t ) ;
i f ( ! ( par t i nst anceof I Text Edi t or ) ) return;
I Act i onBar s act i onBar s= getActionBars( ) ;
i f ( act i onBar s == nul l ) return;
I Text Edi t or edi t or = ( I Text Edi t or ) par t ;
act i onBar s. setGlobalActionHandler(
I DEAct i onFact or y. ADD_TASK. getId( ) ,
getAction( edi t or , I DEAct i onFact or y. ADD_TASK. getId( ) ) ) ;
act i onBar s. setGlobalActionHandler(
I DEAct i onFact or y. BOOKMARK. getId( ) ,
getAction( edi t or , I DEAct i onFact or y. BOOKMARK. getId( ) ) ) ;
}
}
2004 IBM Corporation
Adding actions
override Basi cText Edi t or Act i onCont r i but or . cont r i but eToMenu( )
contribute to extension point or g. ecl i pse. ui . edi t or Act i ons
contribute to extension point or g. ecl i pse. ui . popupMenus
"automatic actions": selection and post selection listeners
I Text Edi t or . set Act i on( St r i ng i d, I Act i on act i on)
id.. command id or RulerDoubleClick or RulerClick
actions are automatically registered with the key binding service
publ i c voi d contributeToMenu( I MenuManager menu) {
I MenuManager m= menu. findMenuUsingPath( I Wor kbenchAct i onConst ant s. M_EDI T) ;
m. appendToGroup( I Wor kbenchAct i onConst ant s. FI ND_EXT, getFindAction( ) ) ;
}
2004 IBM Corporation
AbstractTextEditor action management
override Abst r act Text Edi t or . cr eat eAct i ons( )
for context menu additions override
edi t or Cont ext MenuAbout ToShow( )
r ul er Cont ext MenuAbout ToShow( )
pr ot ect ed voi d createActions( ) {
super . createActions( ) ;
Act i on act i on= new ExampleAction( ) ;
act i on. setHelpContextId( " or g. ecl i pse. edi t or . exampl e. act i on. cont ext " ) ;
act i on. setActionDefinitionId( " or g. ecl i pse. edi t or . exampl e. command" ) ;
setAction( " or g. ecl i pse. edi t or . exampl e. command" , act i on) ;
markAsSelectionDependentAction( " or g. ecl i pse. edi t or . exampl e. command" , t r ue) ;
markAsContentDependentAction( " or g. ecl i pse. edi t or . exampl e. command" , t r ue) ;
}
2004 IBM Corporation
Inside AbstractTextEditor
Abst r act Text Edi t or uses a Sour ceVi ewer as implementation
customization via Sour ceVi ewer Conf i gur at i on
publ i c cl ass Exampl eEdi t or ext ends Ext endedText Edi t or {
pr ot ect ed voi d initializeEditor( ) {
super . initializeEditor( ) ;
setSourceViewerConfiguration( new ExampleSourceViewerConfiguration( ) ) ;
}

}
publ i c abst r act cl ass Abst r act Exampl eEdi t or {
publ i c voi d createPartControl( Composi t e par ent ) {
f Sour ceVi ewer = new SourceViewer( par ent , ) ;
}

}
2004 IBM Corporation
Adding an annotation hover
publ i c cl ass Exampl eSour ceVi ewer Conf i gur at i on
ext ends Sour ceVi ewer Conf i gur at i on {
publ i c I Annot at i onHover getAnnotationHover( I Sour ceVi ewer vi ewer ) {
r et ur n new ExampleAnnotationHover( ) ;
}
}

publ i c cl ass Exampl eAnnot at i onHover i mpl ement s I Annot at i onHover {


pr i vat e Li st getAnnotations( I Sour ceVi ewer vi ewer , i nt l i neNumber ) {
I Annot at i onModel model = vi ewer . getAnnotationModel( ) ;
I Document document = vi ewer . getDocument( ) ;
r et ur n getAnnotationsAtLine( model , document , l i neNumber ) ;
}
publ i c St r i ng getHoverInfo( I Sour ceVi ewer vi ewer , i nt l i neNumber ) {
Li st annot at i ons= getAnnotations( vi ewer , l i neNumber ) ;
r et ur n printAnnotations( annot at i ons) ;
}

}
2004 IBM Corporation
Adding syntax coloring
publ i c cl ass Exampl eSour ceVi ewer Conf i gur at i on {

I Pr esent at i onReconci l er getPresentationReconciler( I Sour ceVi ewer vi ewer ) {


r et ur n IPresentationReconciler;
}
}
IPresentationReconciler
PresentationReconciler
IPresentationDamager
IPresentationRepairer
DefaultDamagerRepairer ITokenScanner
2004 IBM Corporation
Adding syntax coloring
publ i c cl ass Exampl eSour ceVi ewer Conf i gur at i on {

I Pr esent at i onReconci l er getPresentationReconciler( I Sour ceVi ewer vi ewer ) {


Pr esent at i onReconci l er r econci l er = new Pr esent at i onReconci l er ( ) ;
Def aul t Damager Repai r er df l t = new Def aul t Damager Repai r er ( ITokenScanner) ;
r econci l er . set Damager ( df l t , I Document . DEFAULT_CONTENT_TYPE) ;
r econci l er . set Repai r er ( df l t , I Document . DEFAULT_CONTENT_TYPE) ;
r et ur n r econci l er ;
}
}
ITokenScanner
RuleBasedScanner
BufferedRuleBasedScanner
IRule
WordRule NumberRule PredicateRule
.
2004 IBM Corporation
Adding syntax coloring
publ i c cl ass Exampl eSour ceVi ewer Conf i gur at i on {

pr i vat e I TokenScanner createTokenScanner( ) {


Rul eBasedScanner scanner = new RuleBasedScanner( ) ;
scanner . setRules( createRules( ) ) ;
r et ur n scanner ;
}
pr i vat e I Rul e[ ] createRules( ) {
I Token t okenA= new Token( new TextAttribute( getBlueColor( ) ) ;
I Token t okenB= new Token( new TextAttribute( getGrayColor( ) ) ;
r et ur n new I Rul e[ ] {
new PatternRule( " >" , " <" , t okenA, ' \ \ ' , f al se) ,
new EndOfLineRule( " - - " , t okenB)
};
}
}
2004 IBM Corporation
Adding syntax coloring
BUT multi-line "><" tokens are not correctly colored
Def aul t Damager Repai r er relies on partitioning information of
the document to be presented: The damager only damages a single
line unless it detects changes in the document partitioning.
When using the Def aul t Damager Repai r er together with rules
intended to span multiple lines, the multi-line regions must be
reflected as document partitions.
2004 IBM Corporation
Partitioning and source viewers
partitioning is a semantic view onto the document
each partition has a content type
each character of a document belongs to a partition
documents support multiple partitionings
partitioning is always up-to-date
allows for customizing viewer behavior based on content types
specify damager/repairers per content type
specify text hover per content type

aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
2004 IBM Corporation
Partitioning a document
document creation and setup is managed by the file buffer manager
participate in the document setup process of the file buffer manager
<extension poi nt =" or g. ecl i pse. cor e. f i l ebuf f er s. document Set up" >
<participant
ext ensi ons=" expl "
cl ass=" or g. ecl i pse. edi t or . i nt er nal . Exampl eDocument Set up">
</participant>
</extension>
2004 IBM Corporation
Partitioning a document
publ i c cl ass Exampl eDocument Set up i mpl ement s I Document Set upPar t i ci pant {
publ i c st at i c f i nal St r i ng PARTI TI ONI NG= " or g. ecl i pse. edi t or ";
publ i c st at i c f i nal St r i ng EXPL= " or g. ecl i pse. edi t or . expl " ;
publ i c st at i c f i nal St r i ng[ ] TYPES= new St r i ng[ ] {
I Document . DEFAULT_CONTENT_TYPE, EXPL};
publ i c voi d setup( I Document document ) {
I Document Par t i t i oner p;
p= new DefaultPartitioner( createJavaPartitionScanner( ) , TYPES) ;
document . setDocumentPartitioner( PARTI TI ONI NG, p) ;
p. connect( document ) ;
}
pr i vat e I Par t i t i onTokenScanner createJavaPartitionScanner( ) {
Rul eBasedPar t i t i onScanner scanner = new RuleBasedPartitionScanner( ) ;
scanner . setPredicateRules( new IPredicateRule[ ] {
new PatternRule( " >" , " <" , new Token( EXPL) , ' \ \ ' , f al se) }) ;
r et ur n scanner ;
}
}
2004 IBM Corporation
Revisiting syntax coloring
Adapt Exampl eSour ceVi ewer Conf i gur at i on to available
partitioning
I Pr esent at i onReconci l er getPresentationReconciler( I Sour ceVi ewer vi ewer ) {
Pr esent at i onReconci l er r econci l er = new PresentationReconciler( ) ;
r econci l er . setDocumentPartitioning( Exampl eDocument Set up. PARTI TI ONI NG) ;
Def aul t Damager Repai r er dr ;
dr = new DefaultDamagerRepairer( createTokenScannerForEXPL( ) ) ;
r econci l er . setDamager( dr , Exampl eDocument Set up. EXPL) ;
r econci l er . setRepairer( dr , Exampl eDocument Set up. EXPL) ;
dr = new DefaultDamagerRepairer( createTokenScannerForDefault( ) ) ;
r econci l er . setDamager( dr , I Document . DEFAULT_CONTENT_TYPE) ;
r econci l er . setRepairer( dr , I Document . DEFAULT_CONTENT_TYPE) ;
r et ur n r econci l er ;
}
2004 IBM Corporation
Default configuration scope
Sour ceVi ewer Conf i gur at i on
vertical ruler annotation hover, auto indent strategy, content assist,
content formatter, double clicking, prefixing, shifting, information
presenter, overview ruler annotation hover, presentation reconicler,
model reconciler, text hover, undo manager
Abst r act /Ext endedText Edi t or
menu ids, key binding scopes, preference stores, source viewer
configuration, annotation presentation, vertical ruler columns, change
ruler column, line number ruler, overview ruler
the set of pr ot ect ed methods
2004 IBM Corporation
Overview of all architectural layers
Document Infrastructure
Document Infrastructure
File Buffers
File Buffers
Source Viewer Framework
Source Viewer Framework
Text Editor Framework
Text Editor Framework
File Based Text Editor Framework
File Based Text Editor Framework
Eclipse Java Editor
Eclipse Java Editor
Eclipse Default Text Editor
Eclipse Default Text Editor
2004 IBM Corporation
Outlook
will appear in the default configuration scope
linked positions
templates
folding
see poster "Editor centric Workbench" for general direction

Das könnte Ihnen auch gefallen