| <html xmlns:v="urn:schemas-microsoft-com:vml" |
| xmlns:o="urn:schemas-microsoft-com:office:office" |
| xmlns:w="urn:schemas-microsoft-com:office:word" |
| xmlns="http://www.w3.org/TR/REC-html40"> |
| |
| <head> |
| <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> |
| <meta name=ProgId content=Word.Document> |
| <meta name=Generator content="Microsoft Word 9"> |
| <meta name=Originator content="Microsoft Word 9"> |
| <link rel=File-List href="./Writing%20Your%20Own%20Widget_files/filelist.xml"> |
| <link rel=OLE-Object-Data |
| href="./Writing%20Your%20Own%20Widget_files/oledata.mso"> |
| <!--[if !mso]> |
| <style> |
| v\:* {behavior:url(#default#VML);} |
| o\:* {behavior:url(#default#VML);} |
| w\:* {behavior:url(#default#VML);} |
| .shape {behavior:url(#default#VML);} |
| </style> |
| <![endif]--> |
| <title>Writing Your Own Widget</title> |
| <!--[if gte mso 9]><xml> |
| <o:DocumentProperties> |
| <o:Author>OTI EMPLOYEE</o:Author> |
| <o:LastAuthor>Dave Thomson</o:LastAuthor> |
| <o:Revision>12</o:Revision> |
| <o:TotalTime>2064</o:TotalTime> |
| <o:LastPrinted>2001-02-09T19:31:00Z</o:LastPrinted> |
| <o:Created>2001-06-04T02:12:00Z</o:Created> |
| <o:LastSaved>2001-06-04T18:05:00Z</o:LastSaved> |
| <o:Pages>30</o:Pages> |
| <o:Words>8616</o:Words> |
| <o:Characters>49116</o:Characters> |
| <o:Company>OBJECT TECHNOLOGY INTL</o:Company> |
| <o:Lines>409</o:Lines> |
| <o:Paragraphs>98</o:Paragraphs> |
| <o:CharactersWithSpaces>60317</o:CharactersWithSpaces> |
| <o:Version>9.2720</o:Version> |
| </o:DocumentProperties> |
| </xml><![endif]--><!--[if gte mso 9]><xml> |
| <w:WordDocument> |
| <w:HideSpellingErrors/> |
| <w:DoNotHyphenateCaps/> |
| <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> |
| <w:DisplayVerticalDrawingGridEvery>0</w:DisplayVerticalDrawingGridEvery> |
| <w:UseMarginsForDrawingGridOrigin/> |
| <w:Compatibility> |
| <w:UsePrinterMetrics/> |
| <w:WW6BorderRules/> |
| <w:FootnoteLayoutLikeWW8/> |
| <w:ShapeLayoutLikeWW8/> |
| <w:AlignTablesRowByRow/> |
| <w:ForgetLastTabAlignment/> |
| <w:LayoutRawTableWidth/> |
| <w:LayoutTableRowsApart/> |
| </w:Compatibility> |
| <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> |
| </w:WordDocument> |
| </xml><![endif]--> |
| <link rel=Stylesheet type="text/css" media=all |
| href="Writing%20Your%20Own%20Widget_files/default_style.css"> |
| <style> |
| <!-- |
| /* Style Definitions */ |
| p.MsoNormal, li.MsoNormal, div.MsoNormal |
| {mso-style-parent:""; |
| margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| h1 |
| {mso-style-next:Normal; |
| margin-top:12.0pt; |
| margin-right:0in; |
| margin-bottom:3.0pt; |
| margin-left:0in; |
| mso-pagination:widow-orphan; |
| page-break-after:avoid; |
| mso-outline-level:1; |
| font-size:21.0pt; |
| font-family:Arial; |
| color:windowtext; |
| mso-font-kerning:14.0pt; |
| font-weight:bold;} |
| h2 |
| {mso-style-next:Normal; |
| margin-top:12.0pt; |
| margin-right:0in; |
| margin-bottom:6.0pt; |
| margin-left:0in; |
| mso-line-height-alt:10.5pt; |
| mso-pagination:widow-orphan; |
| page-break-after:avoid; |
| mso-outline-level:2; |
| font-size:18.0pt; |
| font-family:Arial; |
| color:windowtext; |
| font-weight:bold;} |
| h3 |
| {mso-style-next:Normal; |
| margin-top:12.0pt; |
| margin-right:0in; |
| margin-bottom:3.0pt; |
| margin-left:0in; |
| mso-pagination:widow-orphan; |
| page-break-after:avoid; |
| mso-outline-level:3; |
| font-size:14.0pt; |
| font-family:Arial; |
| color:windowtext; |
| font-weight:bold;} |
| h4 |
| {mso-style-next:Normal; |
| margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| page-break-after:avoid; |
| mso-outline-level:4; |
| tab-stops:24.0pt 48.0pt 1.0in 96.0pt 120.0pt 2.0in 168.0pt 192.0pt 3.0in 240.0pt 264.0pt 4.0in 312.0pt 336.0pt 5.0in 384.0pt 408.0pt 6.0in 456.0pt 480.0pt 7.0in 528.0pt 552.0pt 8.0in 600.0pt 624.0pt 9.0in 672.0pt 696.0pt 10.0in 744.0pt 768.0pt; |
| layout-grid-mode:char; |
| font-size:18.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| color:windowtext; |
| font-weight:normal;} |
| p.MsoToc1, li.MsoToc1, div.MsoToc1 |
| {mso-style-next:Normal; |
| margin-top:.25in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext; |
| text-transform:uppercase; |
| font-weight:bold; |
| mso-bidi-font-weight:normal;} |
| p.MsoToc2, li.MsoToc2, div.MsoToc2 |
| {mso-style-next:Normal; |
| margin-top:12.0pt; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext; |
| font-weight:bold; |
| mso-bidi-font-weight:normal;} |
| p.MsoToc3, li.MsoToc3, div.MsoToc3 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:10.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc4, li.MsoToc4, div.MsoToc4 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:20.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc5, li.MsoToc5, div.MsoToc5 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:30.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc6, li.MsoToc6, div.MsoToc6 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:40.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc7, li.MsoToc7, div.MsoToc7 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:50.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc8, li.MsoToc8, div.MsoToc8 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:60.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoToc9, li.MsoToc9, div.MsoToc9 |
| {mso-style-next:Normal; |
| margin-top:0in; |
| margin-right:0in; |
| margin-bottom:0in; |
| margin-left:70.0pt; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoHeader, li.MsoHeader, div.MsoHeader |
| {margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:center 3.0in right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoFooter, li.MsoFooter, div.MsoFooter |
| {margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:center 3.0in right 6.0in; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:windowtext;} |
| p.MsoBodyText, li.MsoBodyText, div.MsoBodyText |
| {margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| font-size:12.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Times New Roman"; |
| mso-fareast-font-family:"Times New Roman"; |
| color:red;} |
| p.MsoBodyText2, li.MsoBodyText2, div.MsoBodyText2 |
| {margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in; |
| layout-grid-mode:char; |
| font-size:9.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Courier New"; |
| mso-fareast-font-family:"Times New Roman"; |
| mso-bidi-font-family:"Times New Roman"; |
| color:black;} |
| a:link, span.MsoHyperlink |
| {color:blue; |
| text-decoration:underline; |
| text-underline:single;} |
| a:visited, span.MsoHyperlinkFollowed |
| {color:purple; |
| text-decoration:underline; |
| text-underline:single;} |
| strong |
| {mso-bidi-font-weight:normal;} |
| em |
| {mso-bidi-font-style:normal;} |
| p |
| {margin-top:5.0pt; |
| margin-right:0in; |
| margin-bottom:5.0pt; |
| margin-left:0in; |
| mso-pagination:widow-orphan; |
| font-size:10.0pt; |
| font-family:Arial; |
| mso-fareast-font-family:"Times New Roman"; |
| color:black;} |
| code |
| {mso-ansi-font-size:8.5pt; |
| mso-bidi-font-size:8.5pt; |
| mso-ascii-font-family:"Courier New"; |
| mso-fareast-font-family:"Times New Roman"; |
| mso-hansi-font-family:"Courier New"; |
| mso-bidi-font-family:"Courier New";} |
| pre |
| {margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; |
| font-size:10.0pt; |
| font-family:"Courier New"; |
| mso-fareast-font-family:"Courier New"; |
| color:windowtext;} |
| p.Code, li.Code, div.Code |
| {mso-style-name:Code; |
| margin:0in; |
| margin-bottom:.0001pt; |
| mso-pagination:widow-orphan; |
| tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in 1.75in 2.0in 2.25in 2.5in 2.75in 3.0in 3.25in 3.5in 3.75in 4.0in 4.25in 4.5in 4.75in 5.0in 5.25in 5.5in 5.75in; |
| layout-grid-mode:char; |
| font-size:9.0pt; |
| mso-bidi-font-size:10.0pt; |
| font-family:"Courier New"; |
| mso-fareast-font-family:"Times New Roman"; |
| mso-bidi-font-family:"Times New Roman"; |
| color:windowtext;} |
| @page Section1 |
| {size:8.5in 11.0in; |
| margin:1.0in 1.25in 1.0in 1.25in; |
| mso-header-margin:.5in; |
| mso-footer-margin:.5in; |
| mso-title-page:yes; |
| mso-even-footer:url("./Writing%20Your%20Own%20Widget_files/header.htm") ef1; |
| mso-footer:url("./Writing%20Your%20Own%20Widget_files/header.htm") f1; |
| mso-paper-source:0;} |
| div.Section1 |
| {page:Section1;} |
| /* List Definitions */ |
| @list l0 |
| {mso-list-id:-2; |
| mso-list-type:simple; |
| mso-list-template-ids:-1;} |
| @list l0:level1 |
| {mso-level-start-at:0; |
| mso-level-text:*; |
| mso-level-tab-stop:none; |
| mso-level-number-position:left; |
| margin-left:0in; |
| text-indent:0in;} |
| @list l1 |
| {mso-list-id:174617761; |
| mso-list-type:simple; |
| mso-list-template-ids:67698689;} |
| @list l1:level1 |
| {mso-level-number-format:bullet; |
| mso-level-text:\F0B7; |
| mso-level-tab-stop:.25in; |
| mso-level-number-position:left; |
| margin-left:.25in; |
| text-indent:-.25in; |
| font-family:Symbol;} |
| @list l2 |
| {mso-list-id:1592273852; |
| mso-list-type:simple; |
| mso-list-template-ids:67698689;} |
| @list l2:level1 |
| {mso-level-number-format:bullet; |
| mso-level-text:\F0B7; |
| mso-level-tab-stop:.25in; |
| mso-level-number-position:left; |
| margin-left:.25in; |
| text-indent:-.25in; |
| font-family:Symbol;} |
| @list l0:level1 lfo2 |
| {mso-level-number-format:bullet; |
| mso-level-numbering:continue; |
| mso-level-text:\F0B7; |
| mso-level-tab-stop:none; |
| mso-level-number-position:left; |
| mso-level-legacy:yes; |
| mso-level-legacy-indent:.25in; |
| mso-level-legacy-space:0in; |
| margin-left:.25in; |
| text-indent:-.25in; |
| font-family:Symbol;} |
| ol |
| {margin-bottom:0in;} |
| ul |
| {margin-bottom:0in;} |
| --> |
| </style> |
| <!--[if gte mso 9]><xml> |
| <o:shapedefaults v:ext="edit" spidmax="1050"/> |
| </xml><![endif]--><!--[if gte mso 9]><xml> |
| <o:shapelayout v:ext="edit"> |
| <o:idmap v:ext="edit" data="1"/> |
| </o:shapelayout></xml><![endif]--> |
| </head> |
| |
| <body lang=EN-US link=blue vlink=purple style='tab-interval:.5in'> |
| |
| <div class=Section1> |
| |
| <p class=MsoNormal align=right style='margin-top:14.2pt;margin-right:0in; |
| margin-bottom:0in;margin-left:25.5pt;margin-bottom:.0001pt;text-align:right'><a |
| name="_Toc496069418"></a><a name="_Toc506634621"><span style='mso-bookmark: |
| _Toc496069418'><span style='font-size:8.0pt;mso-bidi-font-size:10.0pt'>Copyright |
| © 2001 Object Technology International, Inc.<o:p></o:p></span></span></a></p> |
| |
| <table border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%; |
| mso-cellspacing:0in;margin-left:25.5pt;mso-padding-alt:1.5pt 1.5pt 1.5pt 1.5pt'> |
| <tr> |
| <td valign=top style='background:#0080C0;padding:1.5pt 1.5pt 1.5pt 1.5pt'> |
| <p class=MsoNormal><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><b><span style='font-family:Arial; |
| color:white'> Eclipse Corner Article</span></b></span></span><span |
| style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'><span |
| style='mso-bidi-font-size:12.0pt'><o:p></o:p></span></span></span></p> |
| </td> |
| <span style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'></span></span> |
| </tr> |
| </table> |
| |
| <h1 style='margin-left:25.5pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><img width=120 height=86 id="_x0000_i1043" |
| src="Writing%20Your%20Own%20Widget_files/Idea.jpg" align=CENTER></span></span><span |
| style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'><span |
| style='font-size:8.0pt;mso-bidi-font-size:10.0pt'><o:p></o:p></span></span></span></h1> |
| |
| <h1 align=center style='margin-left:25.5pt;text-align:center'><span |
| style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'>Creating |
| Your Own Widgets using SWT</span></span></h1> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;text-indent:.5in'><span |
| style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'><b><![if !supportEmptyParas]> <![endif]><o:p></o:p></b></span></span></p> |
| |
| <p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in; |
| margin-left:61.5pt;margin-bottom:.0001pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><b>Summary</b> <br> |
| When writing applications, you typically use the standard widgets provided by |
| SWT. On occasion, you will need to create your own custom widgets. For example, |
| you might want to add a new type of widget not provided by the standard |
| widgets, or extend the functionality of an existing widget.<span |
| style="mso-spacerun: yes"> </span>This article explains the different SWT |
| extension strategies and shows you how to use them. </span></span></p> |
| |
| <p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in; |
| margin-left:61.5pt;margin-bottom:.0001pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><b><![if !supportEmptyParas]> <![endif]><o:p></o:p></b></span></span></p> |
| |
| <p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in; |
| margin-left:61.5pt;margin-bottom:.0001pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><b>By Steve Northover & Carolyn MacLeod, |
| OTI<br> |
| </b></span></span><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><span style='font-size:10.0pt'>March 22, |
| 2001</span><b><o:p></o:p></b></span></span></p> |
| |
| <p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in; |
| margin-left:61.5pt;margin-bottom:.0001pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'><b><span style='font-size:8.0pt;mso-bidi-font-size: |
| 10.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></b></span></span></p> |
| |
| |
| <div class=MsoNormal align=center style='margin-left:25.5pt;text-align:center'><span |
| style='mso-bookmark:_Toc506634621'><span style='mso-bookmark:_Toc496069418'> |
| |
| <hr size=2 width="100%" align=center> |
| |
| </span></span></div> |
| |
| <b><span style='font-size:21.0pt;font-family:Arial;mso-fareast-font-family: |
| "Times New Roman";mso-font-kerning:14.0pt;mso-ansi-language:EN-US;mso-fareast-language: |
| EN-US;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'> |
| </span></b> |
| |
| <h1 style='margin-left:25.5pt'><span style='mso-bookmark:_Toc506634621'><span |
| style='mso-bookmark:_Toc496069418'>Creating Your Own Widgets</span></span></h1> |
| |
| <h2 style='margin-left:25.5pt'><a name="_Toc496069419"></a><a |
| name="_Toc496069776"></a><a name="_Toc506634622"><span style='mso-bookmark: |
| _Toc496069776'><span style='mso-bookmark:_Toc496069419'>Overview</span></span></a></h2> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>When writing applications, you |
| typically use the standard widgets provided by SWT. On occasion, you will need |
| to create your own custom widgets. There are several reasons that you might |
| want to do this:</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>To add a new type of widget not provided by the |
| standard widgets</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>To extend the functionality of an existing widget</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Custom widgets are |
| created by subclassing in the existing widget class hierarchy.</p> |
| |
| <h2 style='margin-left:25.5pt'><a name="_Toc506634623">Portability Issues</a></h2> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>It is very important to think |
| about portability before writing a custom widget. SWT can be extended in the |
| following ways:</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Write a new widget that is 100% Java™ portable</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Extend an existing widget in a 100% Java portable |
| manner</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Write a new widget that wraps an existing native widget |
| not portable</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Extend an existing widget by calling natives not |
| portable</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>In addition, a combination of |
| these can be used on different platforms:</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Write a new widget that wraps an existing native widget |
| on one platform, but is 100% Java portable on other platforms</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>Extend an existing widget by calling natives on one |
| platform, but call 100% Java portable code on other platforms</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>This of course involves |
| implementing the widget twice using native calls on the one platform and |
| portable code on the others while maintaining the same API for both.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>Each SWT platform is shipped with |
| both a shared library (for example, a DLL on Windows®) and a jar (for the Java |
| class files). The shared library contains all of the native function required |
| for SWT, but it was not meant to be a complete set of the functions available |
| on the platform. Thus to expose native function or native widgets that were not |
| exposed by SWT, you need to write your own shared library. If you are using a |
| combination of native code on one platform and portable code on another, make |
| sure you call your shared library on the platform with the native widget, and |
| your jar on the platform with the portable widget.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>One final note: SWTs interface |
| to its shared libraries is internal SWT code. It was not meant to provide a framework |
| for applications to access all possible native function on all platforms that |
| would be a daunting task. One of the purposes of this document is to show how |
| you can integrate C code with SWT, not model the operating system. As such, the |
| approach taken to writing natives in this document is different from the |
| approach taken by SWT.<br clear=all style='mso-special-character:line-break; |
| page-break-before:always'> |
| </p> |
| |
| <h2 style='margin-left:25.5pt;mso-list:skip'><a name="_Toc496069420"></a><a |
| name="_Toc496069777"></a><a name="_Toc506634624"><span style='mso-bookmark: |
| _Toc496069777'><span style='mso-bookmark:_Toc496069420'>Writing Portable |
| Widgets</span></span></a></h2> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>The SWT library |
| provides two widget classes that are typically used as the basis for a custom |
| 100% Java portable widget:</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]><i style='mso-bidi-font-style:normal'>Canvas</i> - to |
| create basic widgets</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]><i style='mso-bidi-font-style:normal'>Composite</i> - |
| to create compound widgets</p> |
| |
| <h3 style='margin-left:25.5pt'><a name="_Toc496069421"></a><a |
| name="_Toc496069778"></a><a name="_Toc506634625"><span style='mso-bookmark: |
| _Toc496069778'><span style='mso-bookmark:_Toc496069421'>Basic Widgets</span></span></a></h3> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Basic widgets do |
| not contain any other widgets, and are not built from any other widgets. Basic |
| widgets draw themselves. An example of a basic widget is <i style='mso-bidi-font-style: |
| normal'>Button</i>. Another example is <i style='mso-bidi-font-style:normal'>Text</i>. |
| To create a custom basic widget, subclass <i style='mso-bidi-font-style:normal'>Canvas</i>.</p> |
| |
| <h3 style='margin-left:25.5pt;mso-list:skip'><a name="_Toc496069422"></a><a |
| name="_Toc496069779"></a><a name="_Toc506634626"><span style='mso-bookmark: |
| _Toc496069779'><span style='mso-bookmark:_Toc496069422'>Compound Widgets</span></span></a></h3> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Compound widgets |
| contain other widgets, and/or are composed of other widgets. An example of a |
| compound widget is <i style='mso-bidi-font-style:normal'>Combo</i>. It contains |
| a <i style='mso-bidi-font-style:normal'>Text</i>, a <i style='mso-bidi-font-style: |
| normal'>Button</i> and a <i style='mso-bidi-font-style:normal'>List</i>. |
| Another example is <i style='mso-bidi-font-style:normal'>Group</i>. It can contain |
| any number of children. To create a custom compound widget, subclass <i |
| style='mso-bidi-font-style:normal'>Composite</i>.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>The astute reader may have |
| noticed that <i style='mso-bidi-font-style:normal'>Canvas</i> is actually a |
| subclass of <i style='mso-bidi-font-style:normal'>Composite</i>. This is an |
| artifact of the underlying implementation. We treat <i style='mso-bidi-font-style: |
| normal'>Canvas</i> as something you draw on and <i style='mso-bidi-font-style: |
| normal'>Composite</i> as something that has children. Therefore the rule for |
| deciding which class to subclass is this: If your widget has or will have |
| children, subclass <i style='mso-bidi-font-style:normal'>Composite</i>. If your |
| widget does not have and never will have children, subclass <i |
| style='mso-bidi-font-style:normal'>Canvas</i>.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'>Note also that we do not |
| distinguish between a compound widget that is intended to contain and lay out |
| children, and one that is merely composed of other widgets. Both will be a |
| subclass of <i style='mso-bidi-font-style:normal'>Composite</i>, and as such we |
| are describing implementation, rather than type, inheritance. When writing 100% |
| Java portable widgets, we can think of <i style='mso-bidi-font-style:normal'>Composite |
| </i>as the portable entry point into the SWT class hierarchy for all compound |
| widgets, and <i style='mso-bidi-font-style:normal'>Canvas </i>as the portable |
| entry point into the SWT class hierarchy for all basic widgets, regardless of |
| widget type.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <b><span style='font-size:18.0pt;font-family:Arial;mso-fareast-font-family: |
| "Times New Roman";mso-ansi-language:EN-US;mso-fareast-language:EN-US; |
| mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'> |
| </span></b> |
| |
| <h2 style='margin-left:25.5pt'><a name="_Toc496069423"></a><a |
| name="_Toc496069780"><span style='mso-bookmark:_Toc496069423'>Basic Widget |
| Example</span></a><span style='mso-bookmark:_Toc496069423'></span><span |
| style='mso-bookmark:_Toc496069780'></span><span style='font-size:12.0pt; |
| mso-bidi-font-size:18.0pt;font-family:"Times New Roman";font-weight:normal'><o:p></o:p></span></h2> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Imagine we are |
| building an application where we need a widget that displays an image with a |
| line of text to the right, something like this:</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><!--[if gte vml 1]><o:wrapblock><v:shapetype |
| id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" |
| path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> |
| <v:stroke joinstyle="miter"/> |
| <v:formulas> |
| <v:f eqn="if lineDrawn pixelLineWidth 0"/> |
| <v:f eqn="sum @0 1 0"/> |
| <v:f eqn="sum 0 0 @1"/> |
| <v:f eqn="prod @2 1 2"/> |
| <v:f eqn="prod @3 21600 pixelWidth"/> |
| <v:f eqn="prod @3 21600 pixelHeight"/> |
| <v:f eqn="sum @0 0 1"/> |
| <v:f eqn="prod @6 1 2"/> |
| <v:f eqn="prod @7 21600 pixelWidth"/> |
| <v:f eqn="sum @8 21600 0"/> |
| <v:f eqn="prod @7 21600 pixelHeight"/> |
| <v:f eqn="sum @10 21600 0"/> |
| </v:formulas> |
| <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> |
| <o:lock v:ext="edit" aspectratio="t"/> |
| </v:shapetype><v:shape id="_x0000_s1033" type="#_x0000_t75" style='position:absolute; |
| left:0;text-align:left;margin-left:154.8pt;margin-top:-14.25pt;width:51.75pt; |
| height:17.25pt;z-index:3;mso-position-horizontal-relative:text; |
| mso-position-vertical-relative:text' o:allowincell="f"> |
| <v:imagedata src="./Writing%20Your%20Own%20Widget_files/image001.png" |
| o:title=""/> |
| <w:wrap type="topAndBottom" anchorx="page"/> |
| </v:shape><![if gte mso 9]><o:OLEObject Type="Embed" ProgID="PBrush" |
| ShapeID="_x0000_s1033" DrawAspect="Content" ObjectID="_1053168690"> |
| </o:OLEObject> |
| <![endif]><![endif]--><![if !vml]><span style='mso-ignore:vglayout'> |
| <table cellpadding=0 cellspacing=0 align=left> |
| <tr> |
| <td width=206 height=0></td> |
| </tr> |
| <tr> |
| <td></td> |
| <td><img width=69 height=23 |
| src="./Writing%20Your%20Own%20Widget_files/image002.jpg" v:shapes="_x0000_s1033"></td> |
| </tr> |
| </table> |
| </span><![endif]><!--[if gte vml 1]></o:wrapblock><![endif]--><br |
| style='mso-ignore:vglayout' clear=ALL> |
| <![if !supportEmptyParas]> <![endif]><o:p></o:p><p></p> |
| |
| <br style='mso-ignore:vglayout' clear=ALL> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Since we plan to |
| draw both the image and the text, we subclass <i style='mso-bidi-font-style: |
| normal'>Canvas</i>.</p> |
| |
| <p style='margin-top:0in;margin-right:0in;margin-bottom:0in;margin-left:25.5pt; |
| margin-bottom:.0001pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>import</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> org.eclipse.swt.*;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>import</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> |
| org.eclipse.swt.graphics.*;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>import</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> org.eclipse.swt.widgets.*;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>import</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> org.eclipse.swt.events.*;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>class</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> PictureLabel </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>extends</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Canvas {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Image image;<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>String text;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Our widget needs to |
| be created. To do this, we must write at least one constructor. Because widgets |
| in SWT cannot be created without a parent, the constructor must take at least |
| one argument that is the parent. The convention in SWT is to have a constructor |
| with two arguments, <b style='mso-bidi-font-weight:normal'>parent</b> and <b |
| style='mso-bidi-font-weight:normal'>style</b>. Style bits are used to control |
| the look of widgets. Neither the parent nor the style bits can be changed after |
| the widget is created. Your widget can use style bits too.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>PictureLabel(Composite parent, </span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>int</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| style) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>super</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>(parent, style);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>The parent of any |
| widget must be a <i style='mso-bidi-font-style:normal'>Composite</i>. The style |
| is an integer, where some bits are already used by the system. For example, <b |
| style='mso-bidi-font-weight:normal'><span style='font-size:10.0pt'>SWT.BORDER</span></b> |
| will cause a <i style='mso-bidi-font-style:normal'>Canvas</i> to have a border.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Next we need to |
| initialize our widget. The convention in SWT is to do all initialization in the |
| constructor. Certainly, any initialization that requires the parent or the |
| style bits must be done here. We have decided that our <i style='mso-bidi-font-style: |
| normal'>PictureLabel</i> widget will default to a white background, so we need |
| to add a <i style='mso-bidi-font-style:normal'>Color</i> field, allocate a <i |
| style='mso-bidi-font-style:normal'>Color</i>, and initialize the background.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>class</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> PictureLabel </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>extends</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Canvas {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Image image;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>String text;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Color white;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>PictureLabel(Composite parent, </span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>int</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| style) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>super</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>(parent, style);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>white = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| Color(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>, 255, 255, 255);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>setBackground(white);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Colors are graphics |
| resources that must be disposed. How can we dispose of the white color that we |
| allocated? We add a dispose listener. Every widget provides notification when |
| it is destroyed. We add the dispose listener in the constructor.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>addDisposeListener(</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| DisposeListener() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> |
| widgetDisposed(DisposeEvent e) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 4'> </span>white.dispose();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>});<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><b |
| style='mso-bidi-font-weight:normal'>Note:</b> Do not just override dispose() to |
| release the color. This only works in the case where dispose is actually sent |
| to the widget. When the shell is disposed this does not happen, so overriding |
| dispose will leak the color. To ensure that your widget is informed of an event |
| no matter how it was generated, add an event listener instead of overriding |
| methods that generate events. </p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Our widget is |
| created and initialized, and it can be destroyed without leaking graphics |
| resources. Now it needs some functionality. We need to draw the image and the |
| text, and this will require another listener: the paint listener. Implementing |
| a widget often requires adding many listeners. We could implement the listener |
| interfaces as part of our new widget class, but that would make the interface |
| methods public in our class. Instead, the SWT convention is to use anonymous inner |
| classes to forward the functionality to non-public methods of the same name. |
| For consistency, we will rewrite the dispose listener to follow this |
| convention, moving the color dispose code into the <b style='mso-bidi-font-weight: |
| normal'>widgetDisposed</b> method. We write the paint listener the same way.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>addDisposeListener(</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| DisposeListener() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> |
| widgetDisposed(DisposeEvent e) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 4'> </span>PictureLabel.</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>this</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'>.widgetDisposed(e);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>});<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>addPaintListener(</span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| PaintListener() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> paintControl(PaintEvent e) |
| {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 4'> </span>PictureLabel.</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>this</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'>.paintControl(e);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>});<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>By choosing the |
| same names, we have the option of easily implementing the interfaces if we |
| decide to do so later. Here is the <b style='mso-bidi-font-weight:normal'>paintControl</b> |
| method to draw the widget.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> paintControl(PaintEvent e) |
| {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>GC gc = e.gc;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> x = 1;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (image != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>gc.drawImage(image, x, 1); <o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>x = image.getBounds().width + 5;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (text != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>gc.drawString(text, x, 1);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p style='margin-top:0in;margin-right:0in;margin-bottom:0in;margin-left:25.5pt; |
| margin-bottom:.0001pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Now we can draw the |
| image and the text, but we need to let the user set them. So we write set and |
| get methods for each of them.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Image getImage() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>return</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> image;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> setImage(Image image) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>.image = image;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>redraw();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> String getText() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>return</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> text;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> setText(String text) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>.text = text;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>redraw();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>The get methods are |
| trivial. They simply answer the fields. The set methods set the fields and then |
| redraw the widget to show the change. The easiest way to do this is to damage |
| the widget by calling <b style='mso-bidi-font-weight:normal'>redraw()</b>, |
| which queues a paint event for the widget. This approach has the advantage that |
| setting both the image and the text will cause only one paint event because |
| multiple paints are collapsed in the event queue.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>We are not done |
| yet. Our widget does not know its preferred size. This information is needed in |
| order to lay out the widget. In our case, the best size is simply the size of |
| the text plus the size of the image, plus a little bit of space in between. |
| Also, we will add a 1 pixel margin all the way around.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>To return the |
| preferred size of the widget, we must implement the <b style='mso-bidi-font-weight: |
| normal'>computeSize</b> method. The <b style='mso-bidi-font-weight:normal'>computeSize</b> |
| method can be quite complicated. Its job is to calculate the preferred size of |
| the widget based on the current contents. The simplest implementation ignores |
| the arguments and just computes the size.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Point computeSize(</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> wHint, </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> hHint, </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>boolean</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> changed) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> width = 0, height = 0;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (image != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>Rectangle bounds = image.getBounds();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>width = bounds.width + 5;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>height = bounds.height;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (text != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>GC gc = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| GC(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>Point extent = gc.stringExtent(text);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>gc.dispose();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>width += extent.x;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>height = Math.max(height, extent.y);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>return</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>new</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Point(width + 2, height + |
| 2);<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>What are <b |
| style='mso-bidi-font-weight:normal'>wHint</b>, <b style='mso-bidi-font-weight: |
| normal'>hHint</b>, and <b style='mso-bidi-font-weight:normal'>changed</b>? The |
| hint arguments allow you to ask a widget questions such as Given a particular |
| width, how high does the widget need to be to show all of the contents? For |
| example, a word-wrapping <i style='mso-bidi-font-style:normal'>Label</i> widget |
| might be asked this. To indicate that the client does not care about a |
| particular hint, the special value <b style='mso-bidi-font-weight:normal'><span |
| style='font-size:10.0pt'>SWT.DEFAULT</span></b> is used. The following example |
| asks a label for its preferred size given a width of 100 pixels: </p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Point extent = label.computeSize(100, </span><span |
| style='font-size:8.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>SWT.DEFAULT</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>, </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>false</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>For our <i |
| style='mso-bidi-font-style:normal'>PictureLabel</i> widget, we could be fancy |
| and stack the image over the text when the width is too small, and/or wrap the text |
| in order to meet a width request, but for simplicity we have decided not to do |
| so. Still, we need to honour the hints. So, our widget will clip. The easiest |
| way to do this is to perform the calculation and then filter the results.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Point computeSize(</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> wHint, </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> hHint, </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>boolean</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> changed) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>int</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> width = 0, height = 0;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (image != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>Rectangle bounds = image.getBounds();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>width = bounds.width + 5;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>height = bounds.height;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (text != </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>GC gc = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| GC(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>Point extent = gc.stringExtent(text);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>gc.dispose();<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>width += extent.x;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>height = Math.max(height, extent.y);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (wHint != </span><span |
| style='font-size:8.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>SWT.DEFAULT</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) width = wHint;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>if</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> (hHint != </span><span |
| style='font-size:8.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>SWT.DEFAULT</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>) height = hHint;<span |
| style='mso-tab-count:2'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>return</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>new</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Point(width + 2, height + |
| 2);<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Notice that we do |
| not return the hint sizes exactly as specified. We have added the 1-pixel |
| border. Why do we do this? All widgets have a <i style='mso-bidi-font-style: |
| normal'>client area</i> and <i style='mso-bidi-font-style:normal'>trim</i>. The |
| hint parameters specify the desired size of the client area. We must set the |
| size of the widget so that the size of the client area is the same as the hint, |
| so the size we return from <b style='mso-bidi-font-weight:normal'>computeSize</b> |
| must include the trim.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>What about the <b |
| style='mso-bidi-font-weight:normal'>changed</b> flag? This is used in |
| conjunction with SWT layout managers and is ignored for basic widgets. This |
| will be discussed when we talk about compound widgets.</p> |
| |
| <span style='font-size:18.0pt;font-family:Arial;mso-fareast-font-family:"Times New Roman"; |
| mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA; |
| mso-bidi-font-weight:bold'><br clear=all style='page-break-before:always'> |
| </span> |
| |
| <h2 style='margin-left:25.5pt;mso-list:skip'><a name="_Toc496069424"></a><a |
| name="_Toc496069781"></a><a name="_Ref506190201"></a><a name="_Toc506634627"><span |
| style='mso-bookmark:_Ref506190201'><span style='mso-bookmark:_Toc496069781'><span |
| style='mso-bookmark:_Toc496069424'>Compound Widget Example</span></span></span></a></h2> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Now we will recode |
| the <i style='mso-bidi-font-style:normal'>PictureLabel</i> widget as a compound |
| widget. Note that this section assumes that you have read the basic widget |
| example section. This time the widget will be implemented using two <i |
| style='mso-bidi-font-style:normal'>Label</i> children: one to display the |
| image, and one to display the text. Since we are using other widgets to |
| implement our widget, we subclass <i style='mso-bidi-font-style:normal'>Composite</i>.</p> |
| |
| <p class=Code style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>class</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> PictureLabel </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>extends</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> Composite {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Label image, text;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>Color white;<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 1'> </span>PictureLabel(Composite parent, </span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>int</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| style) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>super</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>(parent, style);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>white = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| Color(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>null</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>, 255, 255, 255);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>image = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| Label(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>, 0);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>text = </span><span style='font-size:9.0pt;mso-bidi-font-size: |
| 10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| Label(</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family: |
| "Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>this</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'>, 0);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>setBackground(white);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>image.setBackground(white);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>text.setBackground(white);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>addDisposeListener(</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| DisposeListener() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> |
| widgetDisposed(DisposeEvent e) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 4'> </span>PictureLabel.</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>this</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'>.widgetDisposed(e);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>});<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=Code style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>As well as |
| initializing the graphics resources in the constructor, we need to create the child |
| widgets and set their background color. A common mistake is to create the child |
| widgets as children of the parent. This would make them peers of our widget. |
| Instead, make sure to create them as children of <b style='mso-bidi-font-weight: |
| normal'>this</b>. The dispose listener frees the color, as before.</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>Now that we have |
| handled creation and destruction, we need to lay out the children. There are |
| two possibilities:</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>position the children when the widget is resized</p> |
| |
| <p class=MsoNormal style='margin-left:43.5pt;text-indent:-.25in;mso-list:l0 level1 lfo2'><![if !supportLists]><span |
| style='font-family:Symbol'>·<span style='font:7.0pt "Times New Roman"'> |
| </span></span><![endif]>use a layout manager</p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>We will implement |
| both here for comparison.</p> |
| |
| <h3 style='margin-left:25.5pt;mso-list:skip'><a name="_Toc496069425"></a><a |
| name="_Toc496069782"></a><a name="_Toc506634628"><span style='mso-bookmark: |
| _Toc496069782'><span style='mso-bookmark:_Toc496069425'>Positioning Children on |
| Resize</span></span></a></h3> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip'>First, we will |
| position the children when the widget is resized. We need to add a resize |
| listener.</p> |
| |
| <p class=Code style='margin-left:25.5pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>addControlListener(</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>new</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'> |
| ControlAdapter() {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span></span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:navy'>public</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> </span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:navy'>void</span><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'> |
| controlResized(ControlEvent e) {<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 4'> </span>PictureLabel.</span><span style='font-size:9.0pt; |
| mso-bidi-font-size:10.0pt;font-family:"Courier New";mso-bidi-font-family:"Times New Roman"; |
| color:navy'>this</span><span style='font-size:9.0pt;mso-bidi-font-size:10.0pt; |
| font-family:"Courier New";mso-bidi-font-family:"Times New Roman";color:black'>.controlResized(e);<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 3'> </span>}<o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count: |
| 2'> </span>});<span style='mso-tab-count:1'> </span><o:p></o:p></span></p> |
| |
| <p class=MsoNormal style='margin-left:25.5pt;mso-list:skip;tab-stops:.25in .5in .75in 1.0in 1.25in 1.5in'><span |
| style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:"Courier New"; |
| mso-bidi-font-family:"Times New Roman";color:black'><span style='mso-tab-count:<
|