<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>info.michael-simons.eu &#187; Code Snippets</title>
	<atom:link href="http://info.michael-simons.eu/tag/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://info.michael-simons.eu</link>
	<description>Just another nerd blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 10:26:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Get the uptime of your Java VM</title>
		<link>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/</link>
		<comments>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:26:00 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=657</guid>
		<description><![CDATA[You don&#8217;t need JConsole or similar for just displaying the approximate uptime of your application respectively your Java Virtual Machine: import java.lang.management.ManagementFactory; &#160; public class Demo &#123; public static void main&#40;String... args&#41; &#123; final long uptime = ManagementFactory.getRuntimeMXBean&#40;&#41;.getUptime&#40;&#41;; System.out.println&#40;String.format&#40;&#34;Up for %dms&#34;, uptime&#41;&#41;; &#125; &#125; If you use Joda-Time (and you should if you have anything [...]]]></description>
			<content:encoded><![CDATA[<p>You don&#8217;t need JConsole or similar for just displaying the approximate uptime of your application respectively your Java Virtual Machine:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.management.ManagementFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> uptime <span style="color: #339933;">=</span> ManagementFactory.<span style="color: #006633;">getRuntimeMXBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getUptime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Up for %dms&quot;</span>, uptime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you use <a href="http://joda-time.sourceforge.net/">Joda-Time</a> (and you should if you have anything to do with date/datetime processing), you can format it nicely like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.management.ManagementFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.MessageFormat</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.Period</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.PeriodType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.format.PeriodFormatter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.format.PeriodFormatterBuilder</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> Period vmUptime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Period<span style="color: #009900;">&#40;</span>ManagementFactory.<span style="color: #006633;">getRuntimeMXBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getUptime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">normalizedStandard</span><span style="color: #009900;">&#40;</span>PeriodType.<span style="color: #006633;">yearDayTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">final</span> PeriodFormatter pf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PeriodFormatterBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">printZeroAlways</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">appendDays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendLiteral</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">MessageFormat</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{0,choice,0# days, |1# day, |2# days, }&quot;</span>, vmUptime.<span style="color: #006633;">getDays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">minimumPrintedDigits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">appendHours</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendLiteral</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendMinutes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">toFormatter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Up for %s&quot;</span>, pf.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>vmUptime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You also have a nice example of the often unknown <a href="http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html">MessageFormat</a>.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=657&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_657" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a better PathMatcher for Spring 3</title>
		<link>http://info.michael-simons.eu/2011/03/09/creating-a-better-pathmatcher-for-spring-3/</link>
		<comments>http://info.michael-simons.eu/2011/03/09/creating-a-better-pathmatcher-for-spring-3/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 08:14:35 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[URL Mapping]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=507</guid>
		<description><![CDATA[Spring 3 has excellent support for mapping URLs to @Controller methods through the @RequestMapping annotation. This works quite well and i especially like the fact having the mapping right next to the method and not in some other config file like routes.rb. My goal was to have urls like http://foobar.com/resource, http://foobar.com/resource.html, http://foobar.com/resource.zip etc. This is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springframework.org/">Spring 3</a> has excellent support for mapping URLs to @Controller methods through the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping">@RequestMapping</a> annotation. This works quite well and i especially like the fact having the mapping right next to the method and not in some other config file like routes.rb.</p>
<p>My goal was to have urls like <em>http://foobar.com/resource</em>, <em>http://foobar.com/resource.html</em>, <em>http://foobar.com/resource.zip</em> etc. This is no problem at all thanks to the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multiple-representations"> ContentNegotiatingViewResolver</a>.</p>
<p>The solution has only one draw back: The format is not known to the controller. Yes, this shouldn&#8217;t be a controller concern in most cases but what if you have a format that you don&#8217;t want to be available to all users? Maybe an nice zip download of your resources? Handling authentication in a view? I don&#8217;t think so.</p>
<p>So my first attempt looked like this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RequestMapping<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/resource.{format}&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> resource<span style="color: #009900;">&#40;</span>
		<span style="color: #000000; font-weight: bold;">final</span> @PathVariable <span style="color: #003399;">String</span> format,
		<span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest request,
		<span style="color: #000000; font-weight: bold;">final</span> Model model
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>That didn&#8217;t work because it wouldn&#8217;t work for the default text/html resource http://foobar.com/resource so i added</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RequestMapping<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/resource&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> resource<span style="color: #009900;">&#40;</span>
		<span style="color: #000000; font-weight: bold;">final</span> Model model
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html'</span>, model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That worked for http://foobar.com/resource but not for http://foobar.com/resource.zip… &#8220;format&#8221; was always html. Hmmm…</p>
<p>After much googling and reading StackOverflow.com i found the &#8220;useDefaultSuffixPattern&#8221; option on <em>org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping</em>. If set to true (which is the default) the mapping &#8220;/resource/&#8221; will also map to &#8220;/resource/&#8221; and &#8220;/resource.*&#8221;. Although both useful i tried disabling it through my spring-cfg.xml like</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;order&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;useDefaultSuffixPattern&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Enter the next problem: First, i didn&#8217;t work either. Second, all urls where mapped twice. Without the default suffix pattern and with. I spend 2 hours trying to locate the place where the spring config was loaded twice. In the end it was that one line that caused me trouble:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mvc:annotation-driven</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>That tag enables a lot of stuff in Spring 3, like the @Controller programming model and many other <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-annotation-driven">goodies</a>. What it also does is establishing an AnnotationHandlerMapping that cannot be overwritten. So the next thing i did was browsing through the Spring sources to see what it does and redid with Spring beans in my config file (code follows later).</p>
<p>With that implemented, my urls still didn&#8217;t work, for all cases the URL without .{format} was called.</p>
<p>As i was already deep down in the Spring sources i had a look at the default path parser and matcher called <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/">AntPathMatcher</a>. There is nothing wrong with the parsing code but the &#8220;getPatternComparator&#8221; method that &#8220;Given a full path, returns a Comparator suitable for sorting patterns in order of explicitness.&#8221; had some flaws, at least for my use case.</p>
<p>It sorts the patterns by explicitness and that explicitness is (among others) defined by how many placeholders for path variables are present. So my &#8220;/resource&#8221; is more explicit that &#8220;/resource.{format}&#8221;. With that in mind, i extend the path matcher like so:</p>
<p><script src="https://gist.github.com/861852.js?file=ExtendedAntPathMatcher.java"></script></p>
<p>This PathMatcher delegates most of his methods to the default AntPathMatcher but overwrites the getPatternComparator. If you have a look at the sources you&#8217;ll see that it is also partly copied. In the last else branch you&#8217;ll see that i sort both patterns by length, strip the default suffix (.*) and check wether the longer pattern starts with the other one. If it does i check wether the difference is just a .{format} (hardcoded). If that&#8217;s true, than the pattern with the format suffix is more explicit. Otherwise, i&#8217;ll use the default algorithm.</p>
<p>To get this to work, you cannot use the mvc:annotation-driven tag as the PathMatcher is a property of the AnnotationMappingHandler which in turn cannot be overwritten. So to get the same functionality like in Spring 3.0.5 with my PathMatcher use </p>
<p><script src="https://gist.github.com/861863.js?file=gistfile1.xml"></script></p>
<p>As you can see i left the useDefaultSuffixPattern option enabled as it works very well with my PathMatcher and i didn&#8217;t want to care about mapping &#8220;/resource&#8221;, &#8220;/resource/&#8221; etc…</p>
<p>I really hope that the gists will save someone some time. I cannot imagine that i&#8217;m the only one having this kind of requirement. The solution is really simple but the way to it was not that easy.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=507&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_507" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/03/09/creating-a-better-pathmatcher-for-spring-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Oracle &#8220;sleep&#8221; procedure: DBMS_LOCK.SLEEP</title>
		<link>http://info.michael-simons.eu/2010/09/07/oracle-sleep-procedure-dbms_lock-sleep/</link>
		<comments>http://info.michael-simons.eu/2010/09/07/oracle-sleep-procedure-dbms_lock-sleep/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 09:00:01 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[DBMS_LOCK]]></category>
		<category><![CDATA[PL/SQL]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=440</guid>
		<description><![CDATA[There&#8217;s a nice little &#8220;sleep&#8221; procedure in Oracle: A procedure that stops the execution of the current thread for n seconds. Strangely, this method can be called in SQL*Plus like so: EXEC dbms_lock.sleep&#40;10&#41;; but not in another stored procedure or function like so CREATE OR REPLACE PROCEDURE FOOBAR AS BEGIN DBMS_LOCK.SLEEP&#40;1&#41;; END; / To use [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a nice little &#8220;sleep&#8221; procedure in Oracle: A procedure that stops the execution of the current thread for n seconds.</p>
<p>Strangely, this method can be called in SQL*Plus like so:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">EXEC</span> dbms_lock<span style="color: #66cc66;">.</span>sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>but not in another stored procedure or function like so</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> FOOBAR <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
  DBMS_LOCK<span style="color: #66cc66;">.</span>SLEEP<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">END</span>;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>To use &#8220;sleep&#8221; in your procedures or functions, login as administrator to your database (or ask you admin to to so) and create the following objects:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> sleep<span style="color: #66cc66;">&#40;</span>seconds <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
  DBMS_LOCK<span style="color: #66cc66;">.</span>SLEEP<span style="color: #66cc66;">&#40;</span>seconds<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">END</span>;
<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> public synonym sleep <span style="color: #993333; font-weight: bold;">FOR</span> sleep;
&nbsp;
<span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">EXECUTE</span> <span style="color: #993333; font-weight: bold;">ON</span> sleep <span style="color: #993333; font-weight: bold;">TO</span> public;</pre></div></div>

<p>and in your procedure just use &#8220;sleep&#8221;.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=440&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_440" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2010/09/07/oracle-sleep-procedure-dbms_lock-sleep/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to get UIDefaults in Java</title>
		<link>http://info.michael-simons.eu/2010/09/06/how-to-get-uidefaults-in-java/</link>
		<comments>http://info.michael-simons.eu/2010/09/06/how-to-get-uidefaults-in-java/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 09:28:34 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=428</guid>
		<description><![CDATA[If you&#8217;re loocking for Javas UIDefaults, use the UIManager class. This snippet gives you all installed UIDefaults: UIDefaults defaults = UIManager.getDefaults&#40;&#41;; for&#40;Enumeration e = defaults.keys&#40;&#41;; e.hasMoreElements&#40;&#41;; &#41;&#123; String key = e.nextElement&#40;&#41;.toString&#40;&#41;; System.out.println&#40;key + &#34; = &#34; + defaults.get&#40;key&#41;&#41;; &#125; Share This]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re loocking for Javas UIDefaults, use the UIManager class. This snippet gives you all installed UIDefaults:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">UIDefaults</span> defaults <span style="color: #339933;">=</span> <span style="color: #003399;">UIManager</span>.<span style="color: #006633;">getDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		 
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Enumeration</span> e <span style="color: #339933;">=</span> defaults.<span style="color: #006633;">keys</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> e.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> e.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; = &quot;</span> <span style="color: #339933;">+</span> defaults.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	         
<span style="color: #009900;">&#125;</span></pre></div></div>

<p class="akst_link"><a href="http://info.michael-simons.eu/?p=428&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_428" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2010/09/06/how-to-get-uidefaults-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>J2SE: Cut/Copy/Paste Helper</title>
		<link>http://info.michael-simons.eu/2010/07/09/j2se-cutcopypaste-helper/</link>
		<comments>http://info.michael-simons.eu/2010/07/09/j2se-cutcopypaste-helper/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 07:39:31 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[J2SE]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=413</guid>
		<description><![CDATA[You wouldn&#8217;t think that having a standard edit menü with Cut, Copy and Paste buttons would be much of a problem in the J2SE world, especially regarding the fact that most standard Swing components have TransferHandlers that support the 3 operations with the standard keyboard shortcuts. First try was to user TransferHandler.getCopyAction() etc. and create [...]]]></description>
			<content:encoded><![CDATA[<p>You wouldn&#8217;t think that having a standard edit menü with Cut, Copy and Paste buttons would be much of a problem in the J2SE world, especially regarding the fact that most standard Swing components have TransferHandlers that support the 3 operations with the standard keyboard shortcuts. </p>
<p>First try was to user TransferHandler.getCopyAction() etc. and create menuitems from the actions. The actions will actually be called but they use the source of the action event to determine the component whose contents should be transfered.</p>
<p>After some searching i found this <a href="http://weblogs.java.net/blog/2006/08/07/cut-copy-and-paste">tutorial</a>. The idea is to have a global property change handler on the global KeyboardFocusManager that tracks the current component that has the focus. The handler than updates the 3 actions with the new target.</p>
<p>I have changed the code a little bit. I removed the methods for adding special client parameters to determine whether cut/copy/paste should be enabled completely. Instead i check whether the component has a TransferHandler installed and if so, whether that handler supports the current action. By doing so my actions have the same behavior like the ones that are the swing components have.</p>
<p>The following code can be used to create menuitems or buttons that mimic the default cut/copy/paste actions on Swing components:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Component</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.KeyboardFocusManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Toolkit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.datatransfer.Clipboard</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.ActionEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.InputEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.KeyEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.PropertyChangeEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.PropertyChangeListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.AbstractAction</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JComponent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.KeyStroke</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.TransferHandler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> CutCopyPasteHelper <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> FocusedAction CUT_INSTANCE<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> FocusedAction COPY_INSTANCE<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> FocusedAction PASTE_INSTANCE<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Clipboard</span> CLIPBOARD<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>FocusedAction<span style="color: #339933;">&gt;</span> FOCUSED_ACTIONS <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>FocusedAction<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Clipboard</span> clipboard<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			clipboard <span style="color: #339933;">=</span> <span style="color: #003399;">Toolkit</span>.<span style="color: #006633;">getDefaultToolkit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSystemClipboard</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SecurityException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Don't have access to the clipboard, create a new one</span>
			clipboard <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Clipboard</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sandboxed Clipboard&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		CLIPBOARD <span style="color: #339933;">=</span> clipboard<span style="color: #339933;">;</span>
&nbsp;
		FOCUSED_ACTIONS.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>CUT_INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CutAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		FOCUSED_ACTIONS.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>COPY_INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CopyAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		FOCUSED_ACTIONS.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>PASTE_INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PasteAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		KeyboardFocusManager.<span style="color: #006633;">getCurrentKeyboardFocusManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addPropertyChangeListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PropertyChangeHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Clipboard</span> getClipboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> CLIPBOARD<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns an action to perform a cut operation.
	 *
	 * @return the cut action
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Action</span> getCutAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> CUT_INSTANCE<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns an action to perform a copy operation.
	 *
	 * @return the copy action
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Action</span> getCopyAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> COPY_INSTANCE<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns an action to perform a paste operation.
	 *
	 * @return the paste action
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Action</span> getPasteAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> PASTE_INSTANCE<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> updateActions<span style="color: #009900;">&#40;</span><span style="color: #003399;">Component</span> focusedComponent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>FocusedAction action <span style="color: #339933;">:</span> FOCUSED_ACTIONS<span style="color: #009900;">&#41;</span>						
			action.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>focusedComponent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>					
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> PropertyChangeHandler <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">PropertyChangeListener</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> propertyChange<span style="color: #009900;">&#40;</span><span style="color: #003399;">PropertyChangeEvent</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getPropertyName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;permanentFocusOwner&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				updateActions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Component</span><span style="color: #009900;">&#41;</span>e.<span style="color: #006633;">getNewValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> CutCopyPasteHelper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> FocusedAction <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">AbstractAction</span> <span style="color: #009900;">&#123;</span>        
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 9134168335741527777L<span style="color: #339933;">;</span>		
		<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">JComponent</span> focusedComponent<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> FocusedAction<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
&nbsp;
		<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #003399;">Component</span> permanentFocusOwner<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">focusedComponent</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>permanentFocusOwner <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">JComponent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">JComponent</span><span style="color: #009900;">&#41;</span>permanentFocusOwner <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> 			
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">checkTarget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">JComponent</span> getFocusedComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> focusedComponent<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> actionSupported<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> actionMap, <span style="color: #000066; font-weight: bold;">int</span> action<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>actionMap <span style="color: #339933;">&amp;</span> action<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> checkTarget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> export<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> exportAction, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">JComponent</span> component<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Clipboard</span> clipboard <span style="color: #339933;">=</span> getClipboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		component.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">exportToClipboard</span><span style="color: #009900;">&#40;</span>component, clipboard, exportAction<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> CopyAction <span style="color: #000000; font-weight: bold;">extends</span> FocusedAction <span style="color: #009900;">&#123;</span>		
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1765740238724009255L<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> CopyAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Kopieren&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACTION_COMMAND_KEY, <span style="color: #0000ff;">&quot;a6f9f030-c408-4531-857f-dd3aad7b43f6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACCELERATOR_KEY, <span style="color: #003399;">KeyStroke</span>.<span style="color: #006633;">getKeyStroke</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">VK_C</span>, <span style="color: #003399;">InputEvent</span>.<span style="color: #006633;">CTRL_DOWN_MASK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
			export<span style="color: #009900;">&#40;</span>TransferHandler.<span style="color: #006633;">COPY</span>, focusedComponent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkTarget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span>focusedComponent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> actionSupported<span style="color: #009900;">&#40;</span>focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSourceActions</span><span style="color: #009900;">&#40;</span>focusedComponent<span style="color: #009900;">&#41;</span>, TransferHandler.<span style="color: #006633;">COPY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> CutAction <span style="color: #000000; font-weight: bold;">extends</span> FocusedAction <span style="color: #009900;">&#123;</span>		
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1765740238724009255L<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> CutAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ausschneiden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACTION_COMMAND_KEY, <span style="color: #0000ff;">&quot;aa33f95a-c741-407a-a0ca-0c3aedd33c4d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACCELERATOR_KEY, <span style="color: #003399;">KeyStroke</span>.<span style="color: #006633;">getKeyStroke</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">VK_X</span>, <span style="color: #003399;">InputEvent</span>.<span style="color: #006633;">CTRL_DOWN_MASK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
			export<span style="color: #009900;">&#40;</span>TransferHandler.<span style="color: #006633;">MOVE</span>, focusedComponent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkTarget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span>focusedComponent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> actionSupported<span style="color: #009900;">&#40;</span>focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSourceActions</span><span style="color: #009900;">&#40;</span>focusedComponent<span style="color: #009900;">&#41;</span>, TransferHandler.<span style="color: #006633;">MOVE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> PasteAction <span style="color: #000000; font-weight: bold;">extends</span> FocusedAction <span style="color: #009900;">&#123;</span>		
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>2117467682323608417L<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> PasteAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Einfügen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACTION_COMMAND_KEY, <span style="color: #0000ff;">&quot;2676413d-8b9e-4d64-b59a-800db6747d80&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">putValue</span><span style="color: #009900;">&#40;</span>ACCELERATOR_KEY, <span style="color: #003399;">KeyStroke</span>.<span style="color: #006633;">getKeyStroke</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">VK_V</span>, <span style="color: #003399;">InputEvent</span>.<span style="color: #006633;">CTRL_DOWN_MASK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkTarget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span>focusedComponent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> focusedComponent.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">canImport</span><span style="color: #009900;">&#40;</span>focusedComponent, getClipboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAvailableDataFlavors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Clipboard</span> clipboard <span style="color: #339933;">=</span> getClipboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">JComponent</span> target <span style="color: #339933;">=</span> getFocusedComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			target.<span style="color: #006633;">getTransferHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">importData</span><span style="color: #009900;">&#40;</span>target, clipboard.<span style="color: #006633;">getContents</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What&#8217;s missing: The actions aren&#8217;t localized and they use Ctrl+? on every system. One could use the default Swing bundles to determine the correct modifier.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=413&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_413" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2010/07/09/j2se-cutcopypaste-helper/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Oracle: Drop table if exists replacement</title>
		<link>http://info.michael-simons.eu/2010/02/16/oracle-drop-table-if-exists-replacement/</link>
		<comments>http://info.michael-simons.eu/2010/02/16/oracle-drop-table-if-exists-replacement/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 11:18:32 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=395</guid>
		<description><![CDATA[Mysql has a nice &#8220;if exists&#8221; addition to the drop table statement. If the table to be dropped does not exists, it doesn&#8217;t raise an exception but only creates a warning. In Oracle RDMBS you can emulate this behavior like so: BEGIN EXECUTE immediate 'drop table INSERT_TABLE_NAME_HERE'; EXCEPTION WHEN others THEN IF SQLCODE != -942 [...]]]></description>
			<content:encoded><![CDATA[<p>Mysql has a nice &#8220;if exists&#8221; addition to the drop table statement. If the table to be dropped does not exists, it doesn&#8217;t raise an exception but only creates a warning.</p>
<p>In Oracle RDMBS you can emulate this behavior like so:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">BEGIN</span> <span style="color: #993333; font-weight: bold;">EXECUTE</span> immediate <span style="color: #ff0000;">'drop table INSERT_TABLE_NAME_HERE'</span>; EXCEPTION <span style="color: #993333; font-weight: bold;">WHEN</span> others <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">IF</span> SQLCODE !<span style="color: #66cc66;">=</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">942</span> <span style="color: #993333; font-weight: bold;">THEN</span> RAISE; <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">IF</span>; <span style="color: #993333; font-weight: bold;">END</span>;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>Ugly, but it works very well.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=395&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_395" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2010/02/16/oracle-drop-table-if-exists-replacement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An iterable array</title>
		<link>http://info.michael-simons.eu/2010/01/12/an-iterable-array/</link>
		<comments>http://info.michael-simons.eu/2010/01/12/an-iterable-array/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 13:10:58 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=386</guid>
		<description><![CDATA[Java has the nice Iterable interface (since Java 5, i guess) that allows object oriented loops like List&#60;String&#62; strings = new ArrayList&#60;String&#62;&#40;&#41;; for&#40;String string : strings&#41; System.out.println&#40;string&#41;; but guess what, a simple array is not iterable&#8230; In case you need one, feel free to use this one: package ac.simons; &#160; import java.util.Iterator; &#160; public class [...]]]></description>
			<content:encoded><![CDATA[<p>Java has the nice Iterable interface (since Java 5, i guess) that allows object oriented loops like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> strings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> string <span style="color: #339933;">:</span> strings<span style="color: #009900;">&#41;</span> 
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>but guess what, a simple array is not iterable&#8230; </p>
<p>In case you need one, feel free to use this one:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">ac.simons</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IterableArray<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">implements</span> Iterable<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayIteratorImpl <span style="color: #000000; font-weight: bold;">implements</span> Iterator<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> position <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> data <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">position</span> <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> T next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> data<span style="color: #009900;">&#91;</span>position<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> T<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> IterableArray<span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">data</span> <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Iterator<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> iterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIteratorImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p class="akst_link"><a href="http://info.michael-simons.eu/?p=386&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_386" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2010/01/12/an-iterable-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Today: Fun with Unicode, Regex and Java.</title>
		<link>http://info.michael-simons.eu/2009/11/03/today-fun-with-unicode-regex-and-java/</link>
		<comments>http://info.michael-simons.eu/2009/11/03/today-fun-with-unicode-regex-and-java/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:32:11 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=374</guid>
		<description><![CDATA[Some would say, i have 3 problems private final static Pattern placeholder = Pattern.compile&#40;&#34;#\\{(\\w+?)\\}&#34;&#41;; won&#8217;t match &#8220;Mot#{ö}rhead&#8221; for example. To replace the word character \w you either need the list of possible unicodeblocks like [\p{InLatin}&#124;\p{InEtc}] (you get the codes for the blocks through &#8220;Character.UnicodeBlock.forName&#8221; or you&#8217;re lazy like me and just use the dot: private [...]]]></description>
			<content:encoded><![CDATA[<p>Some would say, i have 3 problems <img src='http://info.michael-simons.eu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Pattern placeholder <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#<span style="color: #000099; font-weight: bold;">\\</span>{(<span style="color: #000099; font-weight: bold;">\\</span>w+?)<span style="color: #000099; font-weight: bold;">\\</span>}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>won&#8217;t match &#8220;Mot#{ö}rhead&#8221; for example. </p>
<p>To replace the word character \w you either need the list of possible unicodeblocks like [\p{InLatin}|\p{InEtc}] (you get the codes for the blocks through &#8220;Character.UnicodeBlock.forName&#8221; or you&#8217;re lazy like me and just use the dot:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Pattern placeholder <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#<span style="color: #000099; font-weight: bold;">\\</span>{(.+?)<span style="color: #000099; font-weight: bold;">\\</span>}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Oh what a day&#8230; :/</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=374&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_374" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2009/11/03/today-fun-with-unicode-regex-and-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redcloth / Textilize :hard_breaks is broken in Rails 2.3.4</title>
		<link>http://info.michael-simons.eu/2009/09/24/redcloth-textilize-hard_breaks-is-broken-in-rails-2-3-4/</link>
		<comments>http://info.michael-simons.eu/2009/09/24/redcloth-textilize-hard_breaks-is-broken-in-rails-2-3-4/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 18:59:46 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=368</guid>
		<description><![CDATA[The default behavior for quite a long time was :hard_breaks in the textilize helper method in rails. Hard Breaks means: All line breaks are turned into &#60;br /&#62;&#8217;s. Somebody changed the textilize helper in &#8220;actionpack-2.3.4/lib/action_view/helpers/text_helper.rb&#8221; in 2.3.4 and added the ability to pass some options but broke the default behavior here: def textilize&#40;text, *options&#41; options [...]]]></description>
			<content:encoded><![CDATA[<p>The default behavior for quite a long time was :hard_breaks in the textilize helper method in rails. </p>
<p>Hard Breaks means: All line breaks are turned into &lt;br /&gt;&#8217;s. </p>
<p>Somebody changed the textilize helper in &#8220;actionpack-2.3.4/lib/action_view/helpers/text_helper.rb&#8221; in 2.3.4 and added the ability to pass some options but broke the default behavior here:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> textilize<span style="color:#006600; font-weight:bold;">&#40;</span>text, <span style="color:#006600; font-weight:bold;">*</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
        options <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:hard_breaks</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#008000; font-style:italic;"># ...</span></pre></div></div>

<p>Options will never be null.</p>
<p>I fixed this by monkey patching the module through the following code in config/initializers/textilizepatch.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActionView
  <span style="color:#9966CC; font-weight:bold;">module</span> Helpers
    <span style="color:#9966CC; font-weight:bold;">module</span> TextHelper
      <span style="color:#9966CC; font-weight:bold;">def</span> textilize<span style="color:#006600; font-weight:bold;">&#40;</span>text, <span style="color:#006600; font-weight:bold;">*</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
        options = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:hard_breaks</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options == <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">||</span> options.<span style="color:#9900CC;">size</span> == <span style="color:#006666;">0</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> text.<span style="color:#9900CC;">blank</span>?
          <span style="color:#996600;">&quot;&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          textilized = RedCloth.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text, options<span style="color:#006600; font-weight:bold;">&#41;</span>
          textilized.<span style="color:#9900CC;">to_html</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Such changes should be tested… *grml*</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=368&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_368" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2009/09/24/redcloth-textilize-hard_breaks-is-broken-in-rails-2-3-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logging in Rails outside a controller</title>
		<link>http://info.michael-simons.eu/2009/08/25/logging-in-rails-outside-a-controller/</link>
		<comments>http://info.michael-simons.eu/2009/08/25/logging-in-rails-outside-a-controller/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 05:25:43 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/2009/08/25/logging-in-rails-outside-a-controller/</guid>
		<description><![CDATA[You can use RAILS_DEFAULT_LOGGER.log &#34;foobar&#34; # or Rails.logger.log &#34;blah&#34; outside a controller for logging. Share This]]></description>
			<content:encoded><![CDATA[<p>You can use</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">RAILS_DEFAULT_LOGGER.<span style="color:#9900CC;">log</span> <span style="color:#996600;">&quot;foobar&quot;</span>
<span style="color:#008000; font-style:italic;"># or</span>
Rails.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">log</span> <span style="color:#996600;">&quot;blah&quot;</span></pre></div></div>

<p>outside a controller for logging.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=364&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_364" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2009/08/25/logging-in-rails-outside-a-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.107 seconds -->

