Eclipse Forms Programming Guide.docx
《Eclipse Forms Programming Guide.docx》由会员分享,可在线阅读,更多相关《Eclipse Forms Programming Guide.docx(42页珍藏版)》请在冰豆网上搜索。
EclipseFormsProgrammingGuide
EclipseFormsProgrammingGuide
Initialcreation:
21February2004
Addedtablewrapsample:
22February2004
Introduction
ThisdocumenthasbeenwrittentohelpyouusethenewEclipse3.0featurecalled'EclipseForms'.ThecontentwilleventuallymovetoEclipsehelp.Inthemeantime,youcanstartexperimentingusingexamplesshownbelowandcodeinEclipse3.0integrationbuilds.
EclipseFormsisaplug-inthatexposesasetofcustomswidgetsandothersupportingclassesthatwerebeforeusedasinternaltoPDEandUpdatecomponents.Theyprovideforcreatingpolished,'Web-like'UIsbymodestlyextendingSWTand/ormanipulatingstylebits,colors,fontsandotherpropertiesinordertogetthedesiredbehavior.ThistechnologywasusedthroughoutPDEmulti-pageeditorsandwillnowbeavailableaspublicAPIinEclipse3.0.
ThedocumentrepresentsthestateofAPIatthetimeofwriting.ItispossiblethatsomeaspectsoftheAPIwillslightlychangeuntilfinalrelease.Weanticipateonlymechanicalnamechangesratherthanmorefundamentalchanges.
Problemdefinition
WhenEclipseplatformwasdesigned,thereweretwodistinctcontextinwhichSWTwidgetscouldappear:
∙Intraditionaldialogs(messageboxes,dialogs,wizards,preferencepages,propertypages)
∙Incontentareas(viewsandeditors)
ControlsindialogstypicallyuseddefaultcolorsandfontsasprovidedbytheoperatingsystemandwerelayedoutusingGridLayoutin9outof10cases.Thegoalwastolooklikeadialogandfillthedialogspaceinbothdirections.
Controlsinviewsandeditorswere'stretchy'i.e.theyfilledtheentirecontentarea.Stretchycontrolscanscrolltheircontent(trees,tables,textareas,listsetc.).Thecolorsandfontsusedherewerethoseprovidedbythesystemforuseincontentarea(thesecolorscanbeobtainedfromtheDisplayusingSWT.LIST_BACKGROUNDorSWT.LIST_FOREGROUNDkeys).
Whatwasmissingwasthethirdoptionwherewidgetsyouwouldnormallyseeinthedialogcontextarecreatedinviewsoreditors(whereyouwouldexpect'stretchy'controls).Severalproblemsneededtobesolved:
1.SWTcontrolslikebuttons,labels,compositesetc.lookstrangeinthecontentareasas-is(withthedefaultdialogbackground)
2.CompositeswithGridLayoutinthecontentareaareproblematicbecausetheyarenotscrolled.Therefore,itiseasytogetthecontrolstocliporstartridingoneanother.
3.Controlsinthecontentareaarenormallywrappedtofilltheprovidedwidth.WrappingisveryhardtoaccomplishwiththeexistingSWTlayouts.
4.Whenproblems1-3arefixed,theresultlooksverymuchlikeformsinHTMLbrowsers,andanaturalleapistoaddhyperlinksandmixthemwithothercontrols.
EclipseFormsweredesignedtosolvetheseproblems.Theyprovide:
1.Aconceptofa'form'thatissuitableforinclusionintothecontentarea(editororview)
2.Atoolkitthatmanagescolors,hyperlinkgroupsandotheraspectsoftheform,aswellasserveasafactoryforanumberofSWTcontrols(thesecontrolsarecreatedandconfiguredtofitintotheformcontext)
3.AnewlayoutmanagerthatlaysoutcontrolsusingthealgorithmsimilartoHTMLtables
4.Customcontrolsdesignedtofitintotheform(hyperlink,imagehyperlink,scrollablecomposite,section).
5.Multi-pageeditorwhereeachpageisaform(asinPDE).
Setup
InordertobeabletouseEclipseForms,allyouneedtodoisadd'org.eclipse.ui.forms'plug-intoyourlistofrequiredplug-ins.Thisplug-insisRCP-friendlyinthatitdoesnothavedependencyonIDEplug-ins(itsonlydependencyisorg.eclipse.ui).
Examples
SourcecodeformostoftheexamplesshowninthisdocumentcanbefoundinEclipserepository(HEAD)asorg.eclipse.ui.forms.examples.Checkthemoutintotheworkspaceandlookinsidethe'src'directoryinaJavaorPDEperspective.
APIpackages
EclipseFormshasthefollowingpublic(API)packages:
∙org.eclipse.ui.forms-themainpackage
∙org.eclipse.ui.forms.editor-classesthatemployEclipseFormsontopoftheMultiPageEditorParttocreatemulti-pageeditorsasseeninPDE
∙org.eclipse.ui.forms.events-neweventscreatedtosupportcustomwidgets
∙org.eclipse.ui.forms.widgets-asetofcustomwidgetsandlayoutscreatedspecificallyforEclipseForms
GettingStarted
WewillstartplayingwithEclipseFormsbycreatinganemptyforminaview.Aswesaidabove,viewsarecontentareasthatrequireflexiblecontent.Hereisthecodesample:
packageorg.eclipse.ui.forms.examples.views;
importorg.eclipse.swt.widgets.Composite;
importorg.eclipse.ui.forms.widgets.*;
importorg.eclipse.ui.part.ViewPart;
publicclassFormViewextendsViewPart{
privateFormToolkittoolkit;
privateScrolledFormform;
/**
*Thisisacallbackthatwillallowus
*tocreatetheviewerandinitializeit.
*/
publicvoidcreatePartControl(Compositeparent){
toolkit=newFormToolkit(parent.getDisplay());
form=toolkit.createForm(parent);
form.setText("Hello,EclipseForms");
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Thisisacallbackthatwillallowus
*tocreatetheviewerandinitializeit.
*/
publicvoidcreatePartControl(Compositeparent){
toolkit=newFormToolkit(parent.getDisplay());
form=toolkit.createForm(parent);
form.setText("Hello,EclipseForms");
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Disposesthetoolkit.
*/
publicvoiddispose(){
toolkit.dispose();
super.dispose();
}
}
/*** Disposesthetoolkit. */ public void dispose(){toolkit.dispose();super.dispose();}}
org.eclipse.ui.forms.examples.views; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.forms.widgets.*; import org.eclipse.ui.part.ViewPart;
publicclassFormViewextendsViewPart{
privateFormToolkittoolkit;
privateScrolledFormform;
/**
*Thisisacallbackthatwillallowus
*tocreatetheviewerandinitializeit.
*/
publicvoidcreatePartControl(Compositeparent){
toolkit=newFormToolkit(parent.getDisplay());
form=toolkit.createForm(parent);
form.setText("Hello,EclipseForms");
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Thisisacallbackthatwillallowus
*tocreatetheviewerandinitializeit.
*/
publicvoidcreatePartControl(Compositeparent){
toolkit=newFormToolkit(parent.getDisplay());
form=toolkit.createForm(parent);
form.setText("Hello,EclipseForms");
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Passingthefocusrequesttotheform.
*/
publicvoidsetFocus(){
form.setFocus();
}
/**
*Disposesthetoolkit.
*/
publicvoiddispose(){
toolkit.dispose();
super.dispose();
}
}
/*** Disposesthetoolkit. */ public void dispose(){toolkit.dispose();super.dispose();}}
Inthesnippetabove,wehavecreatedaviewbycreatinganinstanceofatoolkitfirst.Wethenusedthetoolkittocreateaform.Wesetthetitleoftheform.Itisimportantnottoforgetthattoolkitmanagesresourcesthatneedtobedisposedof(hencethe dispose method).
Whenweregisterthisviewwiththeworkbenchandrun,wegetthefollowing:
Theformwejustcreateddoesthefollowingthings:
∙Itrendersthetitlewesetusing setText
∙Itcanrenderabackgroundimagebehindthetitle
∙Thetitlewillwrapifthereisnospacetoshowitononeline
∙Theformwillintroducescrollbarsifthecontentcannotbeshownintheprovidedspace
Thelastiteminthelistneedssomeclarification.Insomeinstances,youwillwanttomaketheformbeachildofamorecomplexlayoutthatisitselfcapableofscrolling.Inthatcase,youmaywanttogetallthecapabilitiesofaform,butwithoutscrolling.Inthatcaseyouwouldusetheclass Form insteadof ScrolledForm (infact, ScrolledForm has-a Form asaninternalobjectanddelegatesmostofthemethodslike setText toit).
Addingcontent
Nowthatwehavetheformviewrunning,wecanstartaddingsomecontenttoit.Eclipseformshaveabodyandallthecontentshouldbecreatedthere:
publicvoidcreatePartControl(Compositeparent){
toolkit=newFormToolkit(parent.getDisplay());
form=toolkit.createForm(parent);
form.setText("Hello,EclipseForms");
GridLayoutlayout=newGridLayout();
form.getBody().setLayout(layout);
Hyperlinklink=toolkit.createHyperlink(form.getBody(),"Clickhere.",
SWT.WRAP);
link.addHyperlinkListener(newHyperlinkAdapter(){
publicvoidlinkActivated(HyperlinkEvente){
System.out.println("Linkactivated!
");
}
});
}
Noticethatwefirstsetlayoutonthebody,thenusedthebodyasaparenttocreateahyperlink.HyperlinkcontrolisoneofthecustomwidgetsinEclipseFormsandactslikealabelthatisclickable.Anewtypeoflistener(HyperlinkListener),itsdefaultimplementation(HyperlinkAdapter)andtheeventypearealldefinedinEclipseFormstosupportthehyperlink.HandlingeventsofthehyperlinkareverysimilartootherSWTwidgets,asshowninthecodeabove.Ourviewnowlookslikethis:
Notehowhyperlinkhasfocusrectanglepaintedaroundit.Whentheviewisactivated,focusistransferredtotheform,whichpassesittothefirstcontrolcapableofacceptingfocus,ourlinkinthiscase.
HyperlinkGroups
Formtoolkithashyperlinkgroupthateachhyperlinkcreatedbyitisaddedto.Hyperlinkgroupsserveseveralroles:
∙Theycapturecolors(bothnormal,hoverandactive)forall