Create a commercialquality Eclipse IDE Part 2 The user interface.docx
- 文档编号:15467917
- 上传时间:2023-07-04
- 格式:DOCX
- 页数:31
- 大小:168.05KB
Create a commercialquality Eclipse IDE Part 2 The user interface.docx
《Create a commercialquality Eclipse IDE Part 2 The user interface.docx》由会员分享,可在线阅读,更多相关《Create a commercialquality Eclipse IDE Part 2 The user interface.docx(31页珍藏版)》请在冰点文库上搜索。
CreateacommercialqualityEclipseIDEPart2Theuserinterface
Createacommercial-qualityEclipseIDE,Part2:
Theuserinterface
PrashantDeva,Founder,PlacidSystems
Summary:
Thistutorial--secondinthis"Createacommercial-qualityEclipseIDE"series--showshowtocreatetheUIoftheIDE.ItexaminestheeditoruserinterfaceframeworkthatEclipseoffers,aswellastheSourceViewerConfigurationclassandallthevariousclassesrelatedtoit,usedtoimplementandconfigureyourIDE'sUI.
Beforeyoustart
Aboutthisseries
This"Createacommercial-qualityEclipseIDE"seriesdemonstrateswhatittakestocreateintegrateddevelopmentenvironments(IDEs)asEclipseplug-insforanyexistingprogramminglanguagesoryourown.ItwalksyouthroughthetwomostimportantpartsoftheIDE--thecoreandtheuserinterface(UI)--andtakesadetailedlookatthechallengesassociatedwithdesigningandimplementingthem.
ThisseriesusesANTLRStudioIDEasacasestudyandexaminesitsinternalstohelpyouunderstandwhatittakestocreateaprofessionalcommercial-levelIDE.CodesampleshelpyoufollowtheconceptsandunderstandhowtousetheminyourownIDE.
Aboutthistutorial
Part1 ofthisseriesintroducesthearchitectureofanIDEandshowshowtocreatetheIDE'scorelayer.ThissecondinstallmentshowshowtoimplementtheUIcomponent.You'lllearnthevariousUI-relatedobjectsinEclipseandhowtouseandextendthosetoprovidespecificfunctionalityinyourIDE.ThetutorialcontainsmanycodesamplesforeasycopyingintoyourIDE'scodetogetthatfunctionalityworkinginyourIDErightaway.
Prerequisites
Thistutorialassumesabasicknowledgeofcreatingplug-insforEclipseandusingtheEclipse Plug-inDevelopmentEnvironment (PDE).
Systemrequirements
Torunthecodesamplesinthistutorial,youneedacopyoftheEclipseSoftwareDevelopmentKit(SDK)runningJava™VirtualMachine(JVM)V1.4orlater.
Basiceditorframework
What'sthefirstthingthatcomestomindwhenyouthinkofaUIforanIDE?
Theeditor!
Infact,everythingconcerningtheUIofanIDErevolvesaroundtheeditor.ThisisparticularlytruewithEclipseplug-insbecausebasicUIfunctionality--theworkbench,toolbars,etc.--arealreadyimplemented.YousimplyhavetospecializeeverythingrelatedtoyourIDE.
We'llbeginbylookingatthearchitectureofatexteditorinsideEclipse.Eclipsedividestheconceptofatexteditorintotwoparts:
thedocumentandtheviewer.Whilethe document holdstheactualcontentoftheeditor,the viewer isresponsibleforhandlingthecontentdisplay.ThisniceModel-View-Controller(MVC)-styleseparationallowsyoutoseparatecodethatmanipulatesthecontentoftheeditorfromthepartthatmanipulatesitsdisplay.Forexample,theparser/lexerrunsinthebackgroundthreadandcommunicatesonlywiththedocument,whilethesyntaxhighlighttercancommunicateonlywiththeviewerwithoutconcerningitselfwiththedocument.
Thedocument
Thedocumentrepresentsthe"model"partofthetexteditorframework.Itcontainstheactualtextualcontentoftheeditor.Itdoesnotconcernitselfwiththedisplayofthetextinanyway.
Adocumentallowsyoutosettheactualtextualcontentofaneditorandcontainsmethodstomanipulatethecontent.Itisdefinedbythe org.eclipse.jface.text.IDocument interface.Formanipulatingdocuments,the IDocument interfaceprovidesthe replace()method:
voidreplace(intoffset,intlength,Stringtext)throwsBadLocationException
Themethodtakestheoffsetinthedocumenttoreplace,alongwiththelengthoftexttoreplaceandthetexttoreplaceitwith.Notethattheoffsetsstartat0.Listing1showsexamplesofusingthe replace() methodtomanipulatethecontentofthedocument.
Listing1.Replacemethodexamples
document.set("Hello");//setthetextofthedocumentto'Hello'
document.replace(document.length()-1,0"World");
//Addstheword'World'tothedocument
System.out.println(document.get());//Prints'HelloWorld'
document.replace(1,4,"i");//replaces'ello'with'i'
System.out.println(document.get());//Prints'HiWorld'
Thedocumentalsoprovidesabstractionfor lines.Itdoesthiswiththehelpof positions,whicharelikestickerstoaportionoftextinthedocument.Afteryouattachapositionobjecttoarangeoftext,theobjectupdatesitselfautomaticallywhiletheusereditsthetextofthedocument.Forexample,ifyouassignapositionatoffset20andtheuseraddsacharactertothedocumentatoffset19,thepositionobjectupdatesitselftoposition21.
Oneinterestingpartinthedesignofthe IDocument interfaceisthesupportformultithreadedaccess.Notethatnoneoftheimplementationsof IDocument allowformulti-threadedaccess.Thus,youmusttakecareofsynchronizingalltheaccesstothedocumentyourself.Butthedesignersof IDocument thoughtaboutthisandtriedtomakethingsjustabiteasierforus.
Thedesignof IDocument ismadetobe"fail-fast."Itthrowsa BadLocationException exceptionwheneveryoutrytoaccesstextfromitthatisoutsidetheboundsofthedocument.Becausethisisacheckedexception,youmustcreateahandlerforiteverywhereyouwanttoaccessanytextfromthedocument.Inthisway,youcancatcherrorsinamultithreadedprogrameasilybecauseifanythreadattemptstoaccesstextoutsidethebounds,itfailswithanexception--thus,effectivelynotifyingyouthatyoumissedsynchronizingsomethingsomewhere.Thisfunctionalityalsoallowsyoutocatchbugsmuchmoreeasilyandearlierinthedevelopmentprocessthanifyouweretosilentlylettheoperationfail.
Backtotop
Theviewer
Theviewerisresponsibleordisplayingthecontentofthedocumentandisdefinedbythe org.eclipse.jface.text.ITextViewerinterface.The TextViewer isbuiltontopoftheStandardWidgetToolkit(SWT) StyledText widget.The Viewer and DocumentclassesarepresenttoprovideanMVCseparationontopofthe StyledText widget.
Now,whilethe TextViewer isdesignedasagenericimplementationtohandleanykindoftext,Eclipseprovidesaspecialsubclassofitspecificallyfordisplayingsourcecode.Thissubclassiscalledthe SourceViewer,anditisimplementedbytheorg.eclipse.jface.text.source.SourceViewer class,whichistheclassyouwilluseasyourtextviewerforthisseries.
AlltheUI-relatedactivitiesforyoureditor,suchassyntaxhighlighting,texthovers,andtextfolding,arehandledinthesourcevieweritself.A SourceViewer usesaverticalrulerattheleftofthetextwidgettodisplayallthewarningsanda +/- iconforfolding,etc.,whenyouopenaJavafile.Italsousesanoverviewrulertotherightofthetextwidgettodisplayalltheerrorandwarningmarkersinthedocumentsoyoucannavigatetothemquickly.
SourceViewer alsousesa SourceViewerConfiguration class,whichallowsyoutoselectivelyplugincustomizableUIbehaviortofityourneeds.You'llseethe SourceViewerConfiguration classindetaillater.
Backtotop
Thetexteditor
TheprevioustwoclassesarepartoftheSWT/JFaceframework,whichEclipseuses.Theorg.eclipse.ui.editors.text.TextEditor classtiesthedocumentandtheviewertogetherandinsertsEclipse-specificfunctionality.Thus,whilethevieweranddocumentabstractionsarepresentintheJFaceframework,the TextEditor ispartofthecoreEclipseUItextframework.The TextEditor implementsthe org.eclipse.ui.IEditorPart class,which--asthenameimplies--denotestotheEclipseWorkbenchthatitispartoftheworkbenchandisaneditor.AneditorisassociatedwithanIEditorInput class,whichdefinestheprotocolofeditorinput.
Forourpurposes,yousimplyneedtosubclassthe TextEditor inyourplug-inandoverridethemethodstocustomizeitaccordingtoyourplug-in'sneeds.Usingthe TextEditor,youcanspecifythesubclassesof IDocument and ITextViewer thatyouuseinyourplug-in,alongwithafewotherclassesdiscussedlater.
Touseatexteditorinyourplug-in,youmustspecifyyoursubclassof TextEditor intheplugin.xmlfileusingtheorg.eclipse.ui.editors extensionpoint,asshowninListing2.
Listing2.SpecifytheTextEditorsubclass
id="my.ide.editor.MyEditor" class="my.ide.ui.MyEditor" extensions="c" icon="icons/fileIcon.gif" name="MyEditor" /> Attributes: ∙id --Auniquenameusedtoidentifythiseditor ∙class --Theactualsubclassof org.eclipse.ui.IEditorPart thatyou'reusinginyourplug-in ∙extension --Thefileextensionforfiletypesforwhichyouareusingthiseditor ∙icon --Theiconusedtodisplaythisfileinvariousviews,suchasNavigatorandPackageExplorer ∙name --Anameforthiseditortype The TextEditor classisresponsibleforinstantiatingandconfiguring SourceViewer.Thus,ifyou'reusingsomecustomsubclassof SourceViewer orwanttodosomethingspecialwhilethesourceviewerisbeinginitialized,youmustoverridethecreateSourceViewer() methodinyoursubclassof TextEditor.Here'show: 1.Createasubclassof TextEditor. 2.Overridethe createSourceViewerMethod() method. 3.Createyourcustominstanceof SourceViewer. 4.Call getSourceViewerDecorationSupport toensurethatdecorationsupporthasbeencreatedandconfiguredfortheviewer. 5.Writeanycustomconfigurationcodeyouwant. 6.Returntheinstanceof SourceViewer. Listing3demonstratethisprocess. Listing3.OverridethecreateSourceViewer()method publicclassMyEditorextendsTextEditor { @Override protectedISourceViewercreateSourceViewer(Compositeparent,IVerticalRulerruler, intstyles) { ISourceViewerviewer=newMySourceViewer(parent,ruler,getOverviewRuler(), isO
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Create commercialquality Eclipse IDE Part The user interface
链接地址:https://www.bingdoc.com/p-15467917.html