{"id":450,"date":"2007-12-12T14:34:12","date_gmt":"2007-12-12T20:34:12","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/archives\/000450"},"modified":"2008-04-01T16:22:45","modified_gmt":"2008-04-01T22:22:45","slug":"gwt-mini-pattern-configuration-reader","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/450","title":{"rendered":"GWT Mini Pattern: Configuration Reader"},"content":{"rendered":"<p>I&#8217;ve written about <a href=\"http:\/\/www.mooreds.com\/wordpress\/archives\/000397\">configuration options to GWT widgets before<\/a>.  Basically, the idea is that you have some configuration that lets the widget change behavior without redeploying or compiling code.  In fact, nontechnical users (like designers) can change configuration, if it&#8217;s documented.  These are the ways I know to specify configuration:<\/p>\n<p><strong>Meta tags<\/strong><\/p>\n<p>In the page:<br \/>\n<code \/><\/p>\n<pre>&lt;meta content=\"bar\" name=\"foo\" id=\"foo\" \/&gt;<\/pre>\n<p><em>Updated 4\/1\/2008, to have id in the meta tag attbute.<\/em><\/p>\n<p>In your code:<br \/>\n<code \/><\/p>\n<pre>String var = DOM.getElementProperty(DOM.getElementById(\"foo\"), \"content\");<\/pre>\n<p>You can also request only Boolean and Int:<br \/>\n<code \/><\/p>\n<pre>String var = DOM.getElementPropertyInt(DOM.getElementById(\"foo\"), \"content\");<\/pre>\n<p>This is simple, all browsers ignore these tags, and you can place them anywhere in a page and get them read.  However, attributes can only contain strings.<\/p>\n<p><strong>Hidden spans<\/strong><\/p>\n<p>In the page:<\/p>\n<p><code \/><\/p>\n<pre>&lt;span id=\"foo\" style=\"display: none\"&gt;bar&lt;\/span&gt;<\/pre>\n<p>In your code:<\/p>\n<p><code \/><\/p>\n<pre>String var = DOM.getInnerHTML(DOM.getElementById(\"foo\"));<\/pre>\n<p>This is a good choice if you want configuration to be more structured than what a tag attribute can provide--you can parse the 'var' string further.<\/p>\n<p><strong>Javascript using jsni<\/strong><\/p>\n<p><code \/><\/p>\n<pre>&lt;script type=\"text\/javascript\"&gt;\r\nvar ID = 'bar';\r\n&lt;\/script&gt;<\/pre>\n<p>And in your code:<\/p>\n<p><code \/><\/p>\n<pre>private native String getIDAsString()\/*-{\r\nif ($wnd.ID == undefined) {\r\nreturn \"\";\r\n} else {\r\nreturn $wnd.ID;\r\n}\r\n}-*\/;\r\n\r\nprivate Integer getID() {\r\nInteger id = new Integer(-1);\r\ntry {\r\nid = Integer.valueOf(getIDAsString());\r\n} catch (NumberFormatException ignore) {}\r\nreturn id;\r\n}\r\n\r\nInteger foo = getID();<\/pre>\n<p>This method is great when you have variables that are used by other javascript components that you'd like to leverage for your GWT component, or if you have a really complex configuration that is multiple levels in structure.<\/p>\n<p><strong>Javascript using dictionary<\/strong><\/p>\n<p>(As outlined <a href=\"http:\/\/google-web-toolkit.googlecode.com\/svn\/javadoc\/1.4\/com\/google\/gwt\/i18n\/client\/Dictionary.html\">here<\/a> and <a href=\"http:\/\/dev-answers.blogspot.com\/2007\/01\/how-to-give-initialisation-parameters.html\">here<\/a>.)<br \/>\nIn the page:<\/p>\n<p><code \/><\/p>\n<pre>&lt;script type=\"text\/javascript\"&gt;\r\nvar dictionary = {\r\nfoo: \"bar\"\r\n};\r\n&lt;\/script&gt;<\/pre>\n<p>In your code:<br \/>\n<code \/><\/p>\n<pre>Dictionary theme = Dictionary.getDictionary(\"dictionary\");\r\nString bar = theme.get(\"foo\");<\/pre>\n<p>This is good for simple hash datastructures.  I haven't tried it, but from the documentation, it doesn't look like it supports anything past String key value pairs, and your module must inherit from com.google.gwt.i18n.I18N.<\/p>\n<p>Regardless of your configuration method, I've found myself using a ConfigReader to isolate this logic.  It looks something like this:<\/p>\n<p><code \/><\/p>\n<pre>public class ConfigReader{\r\n\r\nprivate final boolean opt1;\r\nprivate final String opt2;\r\n\r\npublic ConfigReader(){\r\n\/\/ init opt1 and opt2 using one of the four methods above.\r\n}\r\n\r\npublic boolean isOpt1() {\r\nreturn opt1;\r\n}\r\n\r\npublic String getOpt2() {\r\nreturn opt2;\r\n}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve written about configuration options to GWT widgets before. Basically, the idea is that you have some configuration that lets the widget change behavior without redeploying or compiling code. In [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,30],"tags":[],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-gwt","category-gwt-mini-patterns"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}