{"id":502,"date":"2009-03-13T08:49:08","date_gmt":"2009-03-13T07:49:08","guid":{"rendered":"http:\/\/www.sahits.ch\/blog\/?p=502"},"modified":"2017-12-24T21:42:27","modified_gmt":"2017-12-24T20:42:27","slug":"refactoring-into-a-component","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2009\/03\/13\/refactoring-into-a-component\/","title":{"rendered":"Refactoring into a Component"},"content":{"rendered":"<p>When you design a graphical user interface strait forward (meaning you don&#8217;t plan on the final product) you might sometime realise that you have components that would be useful in other GUIs as well. The refactoring of such a component can be tricky especially consearning the preserving of the layout (the optical impression to the user). Therefore this article should shed some light on how to wrap a simple Lable into its component.<br \/>\n<!--more--><br \/>\nFirst let us define a Shell with a grid layout with two columns.<\/p>\n<pre>\r\nsShell = new Shell();\r\nsShell.setText(\"Shell\");\r\nsShell.setBackground(new Color(null, 255, 0, 0));\r\nsShell.setLayout(new GridLayout(2,false));\r\nGridData gridData = new GridData(GridData.FILL_HORIZONTAL);\r\ngridData.horizontalAlignment = GridData.FILL;\r\nsShell.setLayoutData(gridData);\r\n<\/pre>\n<p>Now we add a Label to the shell and surround it with other Labels to let us see the cells:<\/p>\n<pre>\r\nnew Label(sShell,SWT.NULL).setText(\"---------------------------------------\");\r\nnew Label(sShell,SWT.NULL).setText(\"+++++++++++++++++++++++++++++++++++++++\");\r\nnew Label(sShell,SWT.NULL).setText(\"local label\");\r\nnew Label(sShell,SWT.NULL).setText(\"+++++++++++++++++++++++++++++++++++++++\");\r\nnew Label(sShell,SWT.NULL).setText(\"---------------------------------------\");\r\nnew Label(sShell,SWT.NULL).setText(\"+++++++++++++++++++++++++++++++++++++++\");\r\n<\/pre>\n<p>Now we create a Composite that holds the Label. This we realise with an inner class:<\/p>\n<pre>\r\npublic class LabelComposite extends Composite{\r\n\tpublic LabelComposite(Composite parent, int style) {\r\n\t\tsuper(parent, style);\r\n\t\tGridData gd = new GridData();\r\n\t\tgd.horizontalSpan=2;\r\n\t\tsetLayoutData(gd);\r\n\t\tGridLayout layout = new GridLayout(1,false);\r\n\t\tlayout.horizontalSpacing=0;\r\n\t\tlayout.verticalSpacing=0;\r\n\t\tlayout.marginWidth=0;\r\n\t\tlayout.marginHeight=0;\r\n\t\tsetLayout(layout);\r\n\t\tLabel lbl = new Label(this,SWT.NONE);\r\n\t\tlbl.setText(\"composite label...................................................................\");\r\n\t\tlbl.setBackground(new Color(null, 0, 255, 0));\r\n\t}\r\n}\r\n<\/pre>\n<p>As you see the inner Label contains only one Control but spans to cells.<br \/>\nNow we add the component into our shell and souround it with labels as above.<\/p>\n<pre>\r\nnew LabelComposite(sShell,SWT.NULL);\r\nnew Label(sShell,SWT.NULL).setText(\"---------------------------------------\");\r\nnew Label(sShell,SWT.NULL).setText(\"+++++++++++++++++++++++++++++++++++++++\");\r\n<\/pre>\n<p>If we run this shell the result looks like this:<br \/>\n<a href=\"http:\/\/www.sahits.ch\/blog\/wp-content\/uploads\/2009\/03\/compositelabel.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.sahits.ch\/blog\/wp-content\/uploads\/2009\/03\/compositelabel.jpg\" alt=\"compositelabel\" title=\"compositelabel\" width=\"491\" height=\"122\" class=\"alignleft size-full wp-image-504\" srcset=\"http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2009\/03\/compositelabel.jpg 491w, http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2009\/03\/compositelabel-300x74.jpg 300w\" sizes=\"auto, (max-width: 491px) 85vw, 491px\" \/><\/a><br \/>\nThe code to this example is available <a href='http:\/\/www.sahits.ch\/blog\/wp-content\/uploads\/2009\/03\/compositelayout.java'>here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you design a graphical user interface strait forward (meaning you don&#8217;t plan on the final product) you might sometime realise that you have components that would be useful in other GUIs as well. The refactoring of such a component can be tricky especially consearning the preserving of the layout (the optical impression to the &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2009\/03\/13\/refactoring-into-a-component\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eRefactoring into a Component\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,7,6],"tags":[109,110,98],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry","category-eclipse","category-java","category-programmieren","tag-component","tag-layout","tag-swt"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":3,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":2565,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/502\/revisions\/2565"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}