<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Kah - The Developer</title>
	<atom:link href="http://kahdev.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kahdev.wordpress.com</link>
	<description>The world of development ...</description>
	<lastBuildDate>Tue, 10 Jan 2012 16:18:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kahdev.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Kah - The Developer</title>
		<link>http://kahdev.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kahdev.wordpress.com/osd.xml" title="Kah - The Developer" />
	<atom:link rel='hub' href='http://kahdev.wordpress.com/?pushpress=hub'/>
		<item>
		<title>[Android] Downloading and Displaying RSS Content</title>
		<link>http://kahdev.wordpress.com/2011/12/13/android/</link>
		<comments>http://kahdev.wordpress.com/2011/12/13/android/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 12:18:38 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=627</guid>
		<description><![CDATA[RSS feeds list the latest content added to a site. The sample in this post demonstrates how to obtain RSS content and display the latest item. It will cover: Downloading the RSS Extract an item Display the content The sample code can be downloaded from the GitHub repository. Downloading the RSS To download RSS content, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=627&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>RSS feeds list the latest content added to a site. The sample in this post demonstrates how to obtain RSS content and display the latest item. It will cover:</p>
<ul>
<li><a href="#download">Downloading the RSS</a></li>
<li><a href="#extract">Extract an item</a></li>
<li><a>Display the content</a></li>
</ul>
<p>The sample code can be downloaded from the <a href="https://github.com/kahgoh/Android-Downloading-and-Displaying-RSS-Content" target="_blank">GitHub repository</a>.</p>
<p><!-- more --><br />
<a name="download"></a><br />
<h3>Downloading the RSS</h3>
<p>To download RSS content, the app must first have the <a href="http://developer.android.com/reference/android/Manifest.permission.html#INTERNET" target="_blank">INTERNET</a> permission. Declaring this in the manifest:</p>
<p><pre class="brush: xml;">
...
&lt;manifest ...&gt;
    ...
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
...
&lt;/manifest&gt;
</pre></p>
<p>The demo activity contains only a <a href="http://developer.android.com/reference/android/webkit/WebView.html" target="_href">WebView</a>, so it may as well download the content when the activity starts:</p>
<p><pre class="brush: java;">
protected void onStart() {
    super.onStart();

    // Downloading the RSS feed needs to be done on a separate thread.
    Thread downloadThread = new Thread(new Runnable() {

        public void run() {
            try {
                updateView(getLatestContent(retrieveRssDocument()));
            } catch (Exception e) {
                Log.e(&quot;Content Retriever&quot;, e.getLocalizedMessage(), e);
            }
        }
    });

    downloadThread.start();
}
</pre></p>
<p>Note that because obtaining data over HTTP can be slow, it must be done on a separate thread. Failure to do so may result in a <a href="http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html" target="_blank">NetworkOnMainThreadException</a> (at least for API 11 anyway).</p>
<p>To download the content, create an <a href="http://developer.android.com/reference/java/net/URL.html" target="_blank">URL</a> based on the location of the RSS content and call its <a href="http://developer.android.com/reference/java/net/URL.html#openConnection()" target="_blank">openConnection</a> method and extract the content from an <a href="http://developer.android.com/reference/java/io/InputStream.html" target="_blank">InputStream</a>. Since RSS uses the XML format, the content is read into a <a href="http://developer.android.com/reference/org/w3c/dom/Document.html" target="_blank">Document</a>:</p>
<p><pre class="brush: java;">
private Document retrieveRssDocument() throws IOException,
        ParserConfigurationException, SAXException {

    URL url = new URL(source);
    URLConnection connection = url.openConnection();
    InputStream inStream = connection.getInputStream();

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        return builder.parse(new BufferedInputStream(inStream));
    } finally {
        inStream.close();
    }
}
</pre></p>
<p><a name="extract"></a><br />
<h3>Extract an item</h3>
<p>RSS uses the XML format and its specification can be found on the <a href="http://www.rssboard.org/rss-specification" target="_blank">RSS Advisory Board</a>. Here is small sample of RSS content:<br />
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;rss version=&quot;2.0&quot;&gt;
   &lt;channel&gt;
      &lt;title&gt;...(Name for RSS channel)...&lt;/title&gt;
      &lt;link&gt;...(Link to the site)...&lt;/link&gt;
      &lt;description&gt;...(description for the channel)...&lt;/description&gt;

      &lt;item&gt;
         &lt;title&gt;...(item title)...&lt;/title&gt;
         &lt;link&gt;...(URL to the story)...&lt;/link&gt;
         &lt;description&gt;...(sample content)...&lt;/description&gt;
         &lt;pubDate&gt;...(when the item was published)...&lt;/pubDate&gt;
         &lt;guid&gt;...(string to identify the item)...&lt;/guid&gt;
      &lt;/item&gt;

      ...(possibly more items)...
   &lt;/channel&gt;
&lt;/rss&gt;
</pre></p>
<p>An _item_ represents the content that has been added, such as a post, comment or article. Earlier, the content has already been read into a <a href="http://developer.android.com/reference/org/w3c/dom/Document.html" target="_blank">Document</a>. From this, we can extract the _description_ of the first _item_:</p>
<p><pre class="brush: java;">
private String getLatestContent(Document document) throws IOException,
        ParserConfigurationException, SAXException {

    NodeList nodeList = document.getElementsByTagName(&quot;item&quot;);
    int length = nodeList.getLength();
    if (length &gt; 0) {
        return getDescriptionContent(nodeList.item(0));
    }

    return null;
}

private String getDescriptionContent(Node root) {
    if (root instanceof Element) {
        Element asElement = (Element) root;
        if (asElement.getTagName().equalsIgnoreCase(&quot;description&quot;)) {
            return asElement.getTextContent();
        }
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i &lt; children.getLength(); i++) {
        Node node = children.item(i);
        String result = getDescriptionContent(node);
        if (result != null) {
            return result;
        }
    }
    return null;
}
</pre></p>
<p><a name="display"></a><br />
<h3>Display the content</h3>
<p>Finally, displaying the content is as simple as setting the content of a view! The description of the items from some RSS feeds may contain HTML. For a <a href="http://developer.android.com/reference/android/webkit/WebView.html" target="_blank">WebView</a>, the content may be loaded like this:</p>
<p><pre class="brush: plain;">
private void updateView(final String content) {
    final WebView view = (WebView) findViewById(R.id.content);
    view.loadData(&quot;&lt;html&gt;&lt;body&gt;&quot; + content + &quot;&lt;/body&gt;&lt;/html&gt;&quot;,
            &quot;text/html&quot;, null);
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/627/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/627/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/627/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=627&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/12/13/android/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>
	</item>
		<item>
		<title>[Java] Specifying the column widths of a JTable as percentages</title>
		<link>http://kahdev.wordpress.com/2011/10/30/java-specifying-the-column-widths-of-a-jtable-as-percentages/</link>
		<comments>http://kahdev.wordpress.com/2011/10/30/java-specifying-the-column-widths-of-a-jtable-as-percentages/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 08:41:25 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Swing]]></category>
		<category><![CDATA[JTable]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=618</guid>
		<description><![CDATA[The widths of the columns of a JTable are specified through TableColumn.setWidth. To set the widths of the columns as percentage, set the widths of column in proportion to each other. For example: The percentage can be specified in the range from 0 to 1 (with 0 being 0% and 1 being 100%) or the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=618&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The widths of the columns of a <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JTable.html" target="_blank">JTable</a> are specified through <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/table/TableColumn.html#setWidth(int)" target="_blank">TableColumn.setWidth</a>. To set the widths of the columns as percentage, set the widths of column in proportion to each other. For example:</p>
<p><span id="more-618"></span></p>
<p><pre class="brush: java;">
private static JTable createTable() {
    JTable table = new JTable(1, 3);
    TableColumnModel model = table.getColumnModel();

    model.getColumn(0).setHeaderValue(&quot;20 %&quot;);
    model.getColumn(1).setHeaderValue(&quot;30 %&quot;);
    model.getColumn(2).setHeaderValue(&quot;50 %&quot;);

    setWidthAsPercentages(table, 0.20, 0.30, 0.50);
    TableModel tableModel = table.getModel();

    tableModel.setValueAt(&quot;First Column&quot;, 0, 0);
    tableModel.setValueAt(&quot;Second Column&quot;, 0, 1);
    tableModel.setValueAt(&quot;Third Column&quot;, 0, 2);
}

/**
 * Set the width of the columns as percentages.
 * 
 * @param table
 *            the {@link JTable} whose columns will be set
 * @param percentages
 *            the widths of the columns as percentages; note: this method
 *            does NOT verify that all percentages add up to 100% and for
 *            the columns to appear properly, it is recommended that the
 *            widths for ALL columns be specified
 */
private static void setWidthAsPercentages(JTable table,
        double... percentages) {
    final double factor = 10000;

    TableColumnModel model = table.getColumnModel();
    for (int columnIndex = 0; columnIndex &lt; percentages.length; columnIndex++) {
        TableColumn column = model.getColumn(columnIndex);
        column.setPreferredWidth((int) (percentages[columnIndex] * factor));
    }
}
</pre></p>
<p>The percentage can be specified in the range from 0 to 1 (with 0 being 0% and 1 being 100%) or the range from 0 to 100. Note that the above implementation of <b>setWidthAsPercentages</b> relies that the widths for <b>ALL</b> columns are specified. It also relies a value of <b>factor</b> that is sufficiently large to guarantee that the total of the widths is greater than the width of the table. If the sum of the column widths is less than the table&#8217;s initial width, some of the columns will be expanded. Yet, when the total is greater than the table&#8217;s widths, the columns widths are reduced in the required fashion.</p>
<p>This means you would need to ensure that the initial width of the table is less than 10000 and if changes are made later, this value may need to be revised. If the table is being placed in a <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html" target="_blank">JScrollPane</a>, you could use <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getPreferredScrollableViewportSize()" target="_blank">getPreferredScrollableViewportSize</a> instead:</p>
<p><pre class="brush: plain;">
    ...
    final double factor = table.getPreferredScrollableViewportSize().getWidth();
    ...
</pre></p>
<p>Interesting, a factor that is too large can cause the table headings to be rendererd incorrectly. When factor is changed to <a href="http://download.oracle.com/javase/7/docs/api/java/lang/Double.html#MAX_VALUE">Double.MAX_VALUE</a>, the table is rendered like this when it is first loaded:</p>
<p><img class="aligncenter size-full wp-image-620" title="Table widths using Double.MAX_VALUE" src="http://kahdev.files.wordpress.com/2011/10/table_widths.png?w=630" alt=""   /></p>
<p>Admittedly, I have do yet fully understand why this happens when (however, I think there could be an overflow when Java is performing the calculations for laying out the table). When this happens, you can get the headings to appear by moving the mouse over them. When using the view port size, beware that the default viewport size happens to be 450 x 400 and is coded into the <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JTable.html" target="_blank">JTable</a>&#8216;s <b>initializeLocalVars</b> method (at least for Oracle Java) &#8211; so it is possible for the value to differ between different Java implementations.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/618/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=618&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/10/30/java-specifying-the-column-widths-of-a-jtable-as-percentages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/10/table_widths.png" medium="image">
			<media:title type="html">Table widths using Double.MAX_VALUE</media:title>
		</media:content>
	</item>
		<item>
		<title>[Java] Getting Combo Box Renderers to Look Consistent with Other Combo Boxes</title>
		<link>http://kahdev.wordpress.com/2011/09/20/java-getting-combo-box-renderers-to-look-consistent-with-other-combo-boxes/</link>
		<comments>http://kahdev.wordpress.com/2011/09/20/java-getting-combo-box-renderers-to-look-consistent-with-other-combo-boxes/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 12:29:21 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Swing]]></category>
		<category><![CDATA[JComboBox]]></category>
		<category><![CDATA[ListCellRenderer]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=606</guid>
		<description><![CDATA[One way of controlling the text in a JComboBox is to provide a custom ListCellRenderer. I have seen implementations that subclass a concrete implementation (such as DefaultListCellRenderer or the BasicComboBoxRenderer). Generally though, I find that the combo boxes using such renderers can appear inconsistent with another combo box that may not have a renderer set [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=606&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One way of controlling the text in a <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html" target="_blank">JComboBox</a> is to provide a custom <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/ListCellRenderer.html" target="_blank">ListCellRenderer</a>. I have seen implementations that subclass a concrete implementation (such as <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListCellRenderer.html" target="_blank">DefaultListCellRenderer</a> or the <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicComboBoxRenderer.html" target="_blank">BasicComboBoxRenderer</a>). Generally though, I find that the combo boxes using such renderers can appear inconsistent with another combo box that may not have a renderer set or one that extends a different implementation. To illustrate the inconsistency that could happen, I prepared a small test program that you can download from my <a href="https://github.com/kahgoh/java-list-cell-renderers-experiment" target="_blank">Github repository</a>. It displays a number of combo boxes that are based on different renderers. Just running it produces the following dialog in Windows 7:</p>
<p><span id="more-606"></span></p>
<p><img class="aligncenter size-full wp-image-610" src="http://kahdev.files.wordpress.com/2011/09/system_lnf_renderer.jpg?w=630"   /></p>
<p>For reference, the following table describes the types of renderers that are displayed:</p>
<table>
<tbody>
<tr>
<th>Combo Box</th>
<th>Renderer</th>
</tr>
<tr>
<td>Unchanged</td>
<td>No custom renderer. This is how the combo box would normally appear if renderer is set.</td>
</tr>
<tr>
<td>Basic</td>
<td>A custom renderer that is based on <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicComboBoxRenderer.html" target="_blank">BasicComboBoxRenderer</a>.</td>
</tr>
<tr>
<td>Substance</td>
<td>A custom renderer that is based on <a href="https://java.net/projects/substance/" target="_blank">Substance&#8217;s</a> default list cell renderer (<a href="http://www.jarvana.com/jarvana/view/org/java/net/substance/substance/6.0/substance-6.0-javadoc.jar!/org/pushingpixels/substance/api/renderers/SubstanceDefaultListCellRenderer.html" target="_blank">SubstanceDefaultListCellRenderer</a>).</td>
</tr>
<tr>
<td>Static Default</td>
<td>Based on <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListCellRenderer.html" target="_blank">DefaultListCellRenderer</a>.</td>
</tr>
<tr>
<td>Refreshing Default</td>
<td>A renderer that is also based on <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListCellRenderer.html" target="_blank">DefaultListCellRenderer</a>, but the renderer is reset each time the look and feel is changed.</td>
</tr>
<tr>
<td>Static Delegate</td>
<td>A custom renderer that will delegates to the implementation that would normally be used in a combo box.</td>
</tr>
<tr>
<td>Refreshing Delegate</td>
<td>A custom renderer that also delegates to an implementation that would normally be used in a combo box. However, it will also update the renderer that it delegates to when the look and feel is changed.</td>
</tr>
</tbody>
</table>
<p>The <i>Unchanged</i> combo box is the reference and would be how the combo boxes would normally appear, if you did not change the renderer. It can be seen that the <i>Basic</i> combo box  appears to with a different background, whereas the <i>Substance</i> appears to indent its contents. Next, the results of changing to one of the <a href="http://java.net/projects/substance/" target="_blank">Substance</a> look and feels:</p>
<p><a name="dynamic_shot"><br />
<img class="aligncenter size-full wp-image-610" src="http://kahdev.files.wordpress.com/2011/09/substance_lnf_renderer1.jpg?w=630" /><br />
</a></p>
<p>Now the <i>Substance</i> and <i>Refreshing Delegate</i> renderers appear consistent with the unchanged and the <i>Static Delegate</i> does not appear to have the same indent (more on the delegate renderers later). From these, it should be obvious that changing the renderers will effect how the combo boxes will appear on the screen. If you had a custom render that extended one of base implementations (e.g. <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListCellRenderer.html" target="_blank">, <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicComboBoxRenderer.html" target="_blank">BasicComboBoxRenderer</a> or <a href="http://www.jarvana.com/jarvana/view/org/java/net/substance/substance/6.0/substance-6.0-javadoc.jar!/org/pushingpixels/substance/api/renderers/SubstanceDefaultListCellRenderer.html" target="_blank">SubstanceDefaultListCellRenderer</a>, you might have to change it to extend a different implementation when you change the application&#8217;s look and feel and makes it harder to reuse the renderer across different applications that use different look and feels.</p>
<h2>Avoiding a Custom Renderer</h2>
<p>Normally, a <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html" target="_blank">JComboBox</a> will get its renderer through an UI object that implements <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/ComboBoxUI.html" target="_blank">ComboBoxUI</a>(see <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#setUI(javax.swing.plaf.ComboBoxUI)" target="_blank">JComboBox.setUI()</a>). In most cases, this is also a subclass of <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/ComboBoxUI.html" target="_blank">BasicComboBoxUI</a>, which provides the renderer through <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicComboBoxUI.html#createRenderer()" target="_blank">createRenderer()</a>. By overriding this method, different look and feels can provide their own renderers. But, according to the Javadocs for  <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicComboBoxUI.html#createRenderer()" target="_blank">createRenderer()</a>, it is used only &#8220;<i>if a renderer has not been explicitly set with <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#setRenderer(javax.swing.ListCellRenderer)" target="_blank">setRenderer</a></i>&#8220;. So, one way of achieving a consistent look is to never to change the renderer. In this case, you would have to override the object&#8217;s <a href="http://download.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()" target="_blank">toString()</a> method to control the text in the combo box. If you want to put objects from an external library into a combo box, you could &#8220;wrap&#8221; them into one that you define (of course, you could also consider extending the class of the object and override <a href="http://download.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()" target="_blank">toString()</a>, but this will only work if the class of the object is not final). For example, the look and feel combo box in the above examples uses this method. The following listing is an extract that shows how it is implemented:</p>
<p><pre class="brush: java;">
private static JComboBox&lt;?&gt; lnfComboBox() {
    final JComboBox&lt;LnfLoader&gt; skinSelector = new JComboBox&lt;LnfLoader&gt;();

    // ...
    for (LookAndFeelInfo lookAndFeel : UIManager.getInstalledLookAndFeels()) {
        LnfLoader item = standard(lookAndFeel);
        skinSelector.addItem(item);
        // ...
    }

    // ...
    
    for (SkinInfo skin : skins.values()) {
        LnfLoader item = substance(skin);
        skinSelector.addItem(item);
        // ...
    }

    // ...
    return skinSelector
}

// ...
private static LnfLoader standard(final LookAndFeelInfo lookAndFeel) {
    return new LnfLoader() {

        // ... 
        public String toString() {
            return lookAndFeel.getName();
        }
    };
}

// ...
private static LnfLoader substance(final SkinInfo skin) {
    return new LnfLoader() {

        // ... 
        public String toString() {
            return skin.getDisplayName();
        }
    };
}

// ...
</pre></p>
<p>Of course this only works if you want to control only the text in the combo box. If you want to change other aspects (such as the background or the icon), you will still need to provide your own renderer and some people may prefer to change the renderer instead. Next, we will have a look at the <i>Static Delegate</i> and the <i>Refreshing Delegate</i> renderers and how they help to produce a consistent looking combo box.</p>
<h2>The Static Delegate Renderer</h2>
<p>This renderer works by creating a custom renderer that translates the object into text and uses an original renderer to render the contents. In using this approach, it will appear as if the original renderer was being used. This is how the renderer is created and implemented:</p>
<p><pre class="brush: java;">
public void installRenderer(JComboBox&lt;Fruit&gt; comboBox) {
    final ListCellRenderer&lt;? super Object&gt; original = new JComboBox&lt;Object&gt;()
            .getRenderer();
    comboBox.setRenderer(new ListCellRenderer&lt;Fruit&gt;() {

        public Component getListCellRendererComponent(
                JList&lt;? extends Fruit&gt; list, Fruit value,
                int index, boolean isSelected, boolean cellHasFocus) {

            return original.getListCellRendererComponent(list,
                    nameOf(value), index, isSelected, cellHasFocus);
        }
    });
}
</pre></p>
<p>This approach works in cases where the renderer is created and set <b><i>after</i></b> the look and feel is set <b><i>and</i></b> does not change. In one of the above <a href="#dynamic_shot">screenshots</a> the renderer produces an inconsistent look, but it was produced by starting the application with a Windows look and feel and then was changed to <a href="https://java.net/projects/substance/" target="_blank">Substance</a>. This happens because the combo box&#8217;s UI class provides <a href="https://java.net/projects/substance/" target="_blank">Substance&#8217;s</a> renderer, which chooses to render its contents differently to the standard ones. However, if the demo was modified to start with <a href="https://java.net/projects/substance/" target="_blank">Substance</a> instead, then <i>Static Delegate</i> will delegate to a <a href="https://java.net/projects/substance/" target="_blank">Substance</a> renderer:</p>
<p><img class="aligncenter size-full wp-image-610" src="http://kahdev.files.wordpress.com/2011/09/substance_lnf_renderer2.jpg?w=630" /></p>
<h2>The Refreshing Delegate Renderer</h2>
<p>The <i>Refreshing Delegate</i> solves the problem of the changing look and feel by updating the delegate renderer when ever the look and feel is changed:</p>
<p><pre class="brush: java;">
public class DelegatingRenderer implements ListCellRenderer&lt;Fruit&gt; {

	// ...
	public static void install(JComboBox&lt;Fruit&gt; comboBox) {
		DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
		renderer.initialise();
		comboBox.setRenderer(renderer);
	}

    // ...
	private final JComboBox&lt;Fruit&gt; comboBox;

	// ...
	private ListCellRenderer&lt;? super Object&gt; delegate;

	// ...
	private DelegatingRenderer(JComboBox&lt;Fruit&gt; comboBox) {
		this.comboBox = comboBox;
	}

	// ...
	private void initialise() {
		delegate = new JComboBox&lt;Object&gt;().getRenderer();
		comboBox.addPropertyChangeListener(&quot;UI&quot;, new PropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent evt) {
				delegate = new JComboBox&lt;Object&gt;().getRenderer();
			}
		});
	}

	// ...
	public Component getListCellRendererComponent(JList&lt;? extends Fruit&gt; list,
			Fruit value, int index, boolean isSelected, boolean cellHasFocus) {

		return delegate.getListCellRendererComponent(list, value.getName(),
				index, isSelected, cellHasFocus);
	}
}
</pre></p>
<p>The <a href="http://download.oracle.com/javase/7/docs/api/" target="_blank">PropertyChangeListener</a> listens for changes to the UI property. This is property that is fired when the new combo box&#8217;s UI is set (which is what happens when changing the look and feel). When triggered, it simply updates the delegate. In doing so, it will ensure that combo box will appear as if the renderer had not been changed and can also be reused, without modification, in another application that may have a different look and feel.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/606/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=606&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/09/20/java-getting-combo-box-renderers-to-look-consistent-with-other-combo-boxes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/09/system_lnf_renderer.jpg" medium="image" />

		<media:content url="http://kahdev.files.wordpress.com/2011/09/substance_lnf_renderer1.jpg" medium="image" />

		<media:content url="http://kahdev.files.wordpress.com/2011/09/substance_lnf_renderer2.jpg" medium="image" />
	</item>
		<item>
		<title>[Android] Using Frame Animation to Recreate the Flashing Arrow</title>
		<link>http://kahdev.wordpress.com/2011/07/27/android-using-frame-animation-to-recreate-the-flashing-arrow/</link>
		<comments>http://kahdev.wordpress.com/2011/07/27/android-using-frame-animation-to-recreate-the-flashing-arrow/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 12:51:29 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Frame Animation]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=599</guid>
		<description><![CDATA[Previously, I wrote about creating a simple animation of a flashing arrow using timers to hide and display a view at regular intervals (see A Simple Way To Make View Flash on Top of Another View). Another way to do this is to use Frame Animation, which is created by displaying a series of frames. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=599&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Previously, I wrote about creating a simple animation of a flashing arrow using timers to hide and display a view at regular intervals (see <a href="http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/" target="_blank">A Simple Way To Make View Flash on Top of Another View</a>). Another way to do this is to use Frame Animation, which is created by displaying a series of frames. Frame animation is actually covered in the Android <a href="http://developer.android.com/guide/topics/graphics/view-animation.html" target="_blank">Dev Guide</a>, under <i><a href="http://developer.android.com/guide/topics/graphics/view-animation.html" target="_blank">View Animation</a></i>. Here, using the same steps as described in the guide, the animation of the flashing arrow from the previous <a href="http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/" target="_blank">post</a> is recreated.</p>
<p><span id="more-599"></span></p>
<p>First, we need to place the contents of each of the frames in a <a href="http://developer.android.com/guide/topics/resources/drawable-resource.html" target="_blank">Drawable resource</a>. This animation requires only two frames &#8211; one to show the arrow and another to completely hide it. The image for the first frame is easily taken from the example from last <a href="http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/" target="_blank">post</a>. Using an image editor, the second frame is created from a copy of the first frame by completely erasing its contents. The resulting images can then be added as image resources.</p>
<p>With images in place, the <a href="http://developer.android.com/guide/topics/graphics/view-animation.html" target="_blank">Frame Animation</a>, is defined in an <a href="http://developer.android.com/guide/topics/resources/animation-resource.html#Frame" target="_blank">frame animation resource</a> XML file. The file is stored in the drawable resource (i.e. &#8220;res/drawable&#8221;) directory.</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!-- Definition for the animation of a flashing up arrow. --&gt;
&lt;animation-list 
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; 
    android:id=&quot;@+id/flashingArrow&quot;
    android:oneshot=&quot;false&quot;&gt;

    &lt;!-- First frame - up arrow image for 1000 milliseconds 
        (1 second) --&gt;
    &lt;item 
        android:drawable=&quot;@drawable/uparrow&quot; 
        android:duration=&quot;1000&quot; /&gt;

    &lt;!-- Second frame - an empty image for another 1000 milliseconds 
        (or 1 second) --&gt;
    &lt;item 
        android:drawable=&quot;@drawable/empty&quot; 
        android:duration=&quot;1000&quot; /&gt;
&lt;/animation-list&gt;
</pre>  </p>
<p>The <b>oneshot</b> attribute in the <b>&lt;animation-list&gt;</b> tag is set to <i>false</i> so that the animation is repeated. If it was set to <i>true</i>, the animation would run only once. The <b>&lt;item&gt;</b> tags define each frame. Note that they refer to another image resource and that the duration are specified in milliseconds.</p>
<p>If the main layout is defined in an XML layout, the frame animation resource can referenced just like any other drawable resource. The main layout XML from the previous tutorial can be updated so that the <a href="http://developer.android.com/reference/android/widget/ImageView.html" target="_target"><b>ImageView</b></a> holds the animation instead of the arrow image. With the animation was defined in a file called <i>flashing.xml</i>, the layout XML would now contain something like this:<br />
<pre class="brush: xml;">
...
&lt;ImageView
    ... (attributes for the ImageView)
    android:src=&quot;@drawable/flashing&quot;&gt;
&lt;/ImageView&gt;
...
</pre></p>
<p>To start the animation in the <a href="http://developer.android.com/reference/android/app/Activity.html" target="_blank"><b>Activity</b></a>, get a hold of the <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html" target="_blank"><b>AnimationDrawable</b></a> from the <a href="http://developer.android.com/reference/android/widget/ImageView.html" target="_blank"><b>ImageView</b></a> and call the <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html#start()" target="_blank"><b>start</b></a>. Use the <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html#stop()" target="_blank"><b>stop</b></a> method to stop the animation:<br />
<pre class="brush: java;">
...
final AnimationDrawable animation = (AnimationDrawable) arrowView.getDrawable();

if (arrowView.getVisibility() != View.VISIBLE) {
    arrowView.setVisibility(View.VISIBLE);
}

if (animation.isRunning()) {
    animation.stop();
} else {
    animation.start();
}
...
</pre></p>
<p>The source code for my example can be downloaded from my <a href="https://github.com/kahgoh/Android-Flashing-Frame-Animation" target="_blank">GitHub repository</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/599/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/599/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/599/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=599&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/07/27/android-using-frame-animation-to-recreate-the-flashing-arrow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>
	</item>
		<item>
		<title>[Android] A Simple Way To Make View Flash on Top of Another View</title>
		<link>http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/</link>
		<comments>http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 13:49:14 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=588</guid>
		<description><![CDATA[[Android] Simple Flashing a View on Top of Another View Flashing an image is a simple animation that can be achieved by laying an image on top of another and toggling the visibility at regular intervals. In Android, a View can be placed on top of another View, with a FrameLayout. Views can also be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=588&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>[Android] Simple Flashing a View on Top of Another View</p>
<p>Flashing an image is a simple animation that can be achieved by laying an image on top of another and toggling the visibility at regular intervals. In Android, a <a href="http://developer.android.com/reference/android/view/View.html" target="_blank">View</a> can be placed on top of another <b>View</b>, with a <a href="http://developer.android.com/reference/android/widget/FrameLayout.html" target="_blank"><b>FrameLayout</b></a>. <b>Views</b> can also be hidden and displayed by changing its visibility, through <a href="http://developer.android.com/reference/android/view/View.html#setVisibility(int)" target="_blank">setVisibility</a>. To demonstrate this method, I&#8217;ve prepared a small example of an arrow that flashes over a rocket when the rocket is clicked (the rocket and arrow clipart were obtained from <a href="http://www.openclipart.org/" target="_blank">Open Clip Art Library</a>). The source is available from my <a href="https://github.com/kahgoh/Android-Flashing-View-Demo" target="_blank">Github repository</a>. </p>
<p><span id="more-588"></span></p>
<p><img src="http://kahdev.files.wordpress.com/2011/06/rocket1.png?w=630" alt="" title="Rocket"   class="aligncenter size-full wp-image-591" /></p>
<p>The main layout, defined by <b>rocket.xml</b>, contains the <b>FrameLayout</b>:</p>
<p><pre class="brush: xml;">
&lt;FrameLayout 
    android:id=&quot;@+id/frame&quot; 
    android:layout_height=&quot;wrap_content&quot; 
    android:layout_width=&quot;match_parent&quot;&gt;
	&lt;ImageView 
	    android:id=&quot;@+id/rocketImage&quot;
	    android:layout_gravity=&quot;center_horizontal&quot; 
	    android:layout_height=&quot;wrap_content&quot; 
	    android:src=&quot;@drawable/rocket&quot; 
	    android:layout_width=&quot;wrap_content&quot;&gt;
	&lt;/ImageView&gt;
	&lt;ImageView
        android:id=&quot;@+id/uparrowImage&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:visibility=&quot;invisible&quot;
        android:src=&quot;@drawable/uparrow&quot;&gt;
	&lt;/ImageView&gt;
&lt;/FrameLayout&gt;
</pre></p>
<p>By positioning mentioning the <b>rocketImage</b> before the <b>uparrowImage</b>, the image of the arrow will be placed on top of the rocket. However, the XML also sets the arrow to be invisible, so the arrow will not be visible when this view is displayed.</p>
<p>To perform the flashing, the visibility of the arrow is toggled at regular intervals. The changing of the visibility status should be done on the UI thread (see the article <a href="http://developer.android.com/resources/articles/painless-threading.html" target="_blank">Painless Threading</a>). Since there is a defined interval between changing the visibility, I use <a href="http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable,%20long)"><b>postDelayed</b></a> as follows:</p>
<p><pre class="brush: java;">
    // ...

	private final OnClickListener trigger = new OnClickListener() {

		@Override
		public void onClick(View v) {
			arrowView.removeCallbacks(flashTask);
			flashTask.reset();
			postFlashTask();
		}
	};

    // ...

    private View arrowView;

    // ...

	/**
	 * Reposts the task to toggle the visibility after short interval.
	 */
	private void postFlashTask() {
		arrowView.postDelayed(flashTask, 250);
	}

	/**
	 * This is the {@link Runnable} that will toggle the visibility of the
	 * arrow. This can be configured to flash up a certain number of times.
	 */
	private class FlashTask implements Runnable {

		/**
		 * The number of times to flash before stopping.
		 */
		private int countDown = 0;

		/**
		 * Resets the {@link Runnable}, ready to flash the arrow a certain
		 * number of times again.
		 */
		public void reset() {
			countDown = 3;
		}

		@Override
		public void run() {
			if (arrowView.getVisibility() == View.VISIBLE) {
				arrowView.setVisibility(View.INVISIBLE);
				countDown--;
				if (countDown &gt; 0) {
					// Can continue flashing.
					postFlashTask();
				}
			} else {
				arrowView.setVisibility(View.VISIBLE);

				// The arrow should should always finish in the invisible state.
				postFlashTask();
			}
		}
	}

    // ...
</pre></p>
<p>That is all there is to in this technique! Another possibility is to use <del><a href="http://developer.android.com/reference/android/view/animation/AlphaAnimation.html" target="_blank">AlphaAnimation</a></del> frame animation as described in the <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html" target="_blank">View Animation section of the developer guide</a> and version 3 introduces <a href="http://developer.android.com/guide/topics/graphics/animation.html" target="_blank">Property Animation</a>, but I&#8217;ll leave that for another time. With the technique illustrated in this tutorial, you are not limited to <b>ImageViews</b> &#8211; you can also do this with other types of views. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/588/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=588&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/06/24/android-a-simple-way-to-make-view-flash-on-top-of-another-view/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/06/rocket1.png" medium="image">
			<media:title type="html">Rocket</media:title>
		</media:content>
	</item>
		<item>
		<title>[KVM] Installing Virtio drivers in a KVM Windows guest VM</title>
		<link>http://kahdev.wordpress.com/2011/05/30/using-virtio-drivers-in-a-kvm-windows-guest-vm/</link>
		<comments>http://kahdev.wordpress.com/2011/05/30/using-virtio-drivers-in-a-kvm-windows-guest-vm/#comments</comments>
		<pubDate>Mon, 30 May 2011 15:07:09 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[KVM]]></category>
		<category><![CDATA[Kvm]]></category>
		<category><![CDATA[Virtio]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=556</guid>
		<description><![CDATA[KVM provides a Virtio interface for the virtual hard disk and NIC. To use them in a Windows guest VM, the drivers from Fedora (you only need the ISO file) must first be installed into Windows. To install them in a Windows guest VM, it must also be started with these interfaces so that Windows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=556&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>KVM provides a Virtio interface for the virtual hard disk and NIC. To use them in a Windows guest VM, the drivers from <a href="http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/" target="_blank">Fedora</a> (you only need the ISO file) must first be installed into Windows. To install them in a Windows guest VM, it must also be started with these interfaces so that Windows can detect them. While the Windows guest could be started with the Virtio NIC without any problems, we can not start with the Windows image (i.e. the virtual hard disk image where Windows is installed on your guest VM) using the Virtio hard disk interface (until the driver is installed, Windows will not know how to use the Virtio hard disk interface). The easiest solution to this problem is to create another virtual disk that can use Virtio:</p>
<p><pre class="brush: plain;">
qemu-img create -f qcow2 &lt;image name&gt; &lt;size&gt;
</pre></p>
<p><span id="more-556"></span> Now, start the Windows VM with the Virtio interfaces:<br />
<pre class="brush: plain;">
qemu-kvm \
    -hda &lt;path to your Windows guest image&gt; \
    -drive file=&lt;your guest image file&gt;,if=virtio \
    -drive file=&lt;path to Virtio driver ISO&gt;,media=cdrom,index=1 \
    -net nic,model=virtio \
    -net user \
    -boot d \
    -vga std \
    -m 1024
</pre></p>
<p>When Windows is loaded in your guest VM, go in to the <i>Device Manager</i>. Under <i>Other Devices</i>, there should be two entries &#8211; one for the SCSI controller to the the temporary virtual hard drive and the other for the Virtio network card.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/device_manager_before.jpg?w=630" alt="" title="Device manager, before Virtio drivers are installed."   class="aligncenter size-full wp-image-558" /></p>
<p>On one of the items, right click on it and select <i>Properties</i>, go to the <i>Driver</i> tab. You should now see the following:</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/driver_properties.jpg?w=630" alt="" title="Driver Properties Dialog"   class="aligncenter size-full wp-image-559" /></p>
<p>Click on <i>Update Driver</i>. In the next dialog, choose to specify a location and install the Red Hat Virtio drivers. The dialogs for installing the drivers will vary, depending on the installed version of Windows. It should be fairly easy to work how to install the drivers, but installing the drivers for Windows 7 and Windows XP are here if you need them (<a href="#windows7"> skip to instructions for Windows 7</a>). </p>
<h2>Installing the Drivers on Windows XP</h2>
<p>For Windows XP, choose <i>Install from a list or specific location</i> on the first prompt.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windowsxp_install1.jpg?w=630" alt="" title="Windows XP Driver Install 1"   class="aligncenter size-full wp-image-565" /></p>
<p>On the next set of options, choose <i>Search for the best driver in these locations</i> and ensure that <i>Search removable media</i> is ticked and click on <i>Next</i> again. </p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windowsxp_install2.jpg?w=630" alt="" title="Windows XP Driver Install 2"   class="aligncenter size-full wp-image-567" /></p>
<p>When installing the hard disk controller, you might be prompted, at this point, to choose a driver. At this point, choose the one in the <i>wxp</i> directory. </p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windowsxp_hdd_install_driver_select.jpg?w=630" alt="" title="Windows XP Driver Version Selection"   class="aligncenter size-full wp-image-566" /></p>
<p>Eventually, you will probably be warned that you are installing an unsigned driver. Select <i>Continue Anyway</i> to install the driver.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windowsxp_install3.jpg?w=630" alt="" title="Windows XP Driver Install 3"   class="aligncenter size-full wp-image-568" /></p>
<p>When installation is complete, you should get a confirmation, similar to the following:</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windowsxp_install4.jpg?w=630" alt="" title="Windows XP Install 4"   class="aligncenter size-full wp-image-570" /></p>
<p>After repeating the steps for the other driver, you use the Virtio interface on your Windows hard disk image (see <a href="#after">After Installing the Drivers&#8230;</a>).</p>
<p><a name="windows7" id="windows7"></a><br />
<h2>Installing the Drivers on Windows 7</h2>
<p>For Windows 7, choose <i>Browse my computer for driver software</i>.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windows7_install1.jpg?w=630" alt="" title="Windows 7 Install 1"   class="aligncenter size-full wp-image-571" /></p>
<p>On the next screen, choose the drive for your CDROM. You should be able to let Windows search the CD for the drive.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windows7_install2.jpg?w=630" alt="" title="Windows 7 Install 2"   class="aligncenter size-full wp-image-572" /></p>
<p>When prompted, confirm that you want to install the driver. </p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windows7_install3.jpg?w=630" alt="" title="Windows 7 Install 3"   class="aligncenter size-full wp-image-573" /></p>
<p>There will be a confirmation that the driver was installed, similar to this one:</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/windows7_install4.jpg?w=630" alt="" title="Windows 7 Install 4"   class="aligncenter size-full wp-image-574" /></p>
<p>Close the dialog and repeat the steps for the other driver.</p>
<p><a name="after" id="after"></a><br />
<h2>After Installing the Drivers&#8230;</h2>
<p>After both drivers are installed, the device manager should contain a <i>Red Hat VirtIO</i> device for the SCSI controller and another one for the ethernet adapter.</p>
<p><img src="http://kahdev.files.wordpress.com/2011/05/device_manager_after.jpg?w=630" alt="" title="Device Manager After Virtio Install"   class="aligncenter size-full wp-image-560" /></p>
<p>Next time you start the guest VM, you can use the Virtio interface for the Windows image:</p>
<p><pre class="brush: plain;">
qemu-kvm \
    -drive file=&lt;path to your Windows guest image&gt;,if=virtio \
    -net nic,model=virtio \
    -net user \
    -vga std \
    -m 1024 
</pre></p>
<p>Your Windows VM is now using the Virtio interfaces for your Windows virtual hard drive and NIC. If you wish to know more about Virtio and how it works, I suggest having a read of the article <a href="http://www.ibm.com/developerworks/linux/library/l-virtio/index.html?ca=dgr-lnxw01Viriodth-LX&amp;S_TACT=105AGX59&amp;S_CMP=grlnxw03" target="_blank">Virtio: An I/O virtualization framework for Linux</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/556/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/556/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=556&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/05/30/using-virtio-drivers-in-a-kvm-windows-guest-vm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/device_manager_before.jpg" medium="image">
			<media:title type="html">Device manager, before Virtio drivers are installed.</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/driver_properties.jpg" medium="image">
			<media:title type="html">Driver Properties Dialog</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windowsxp_install1.jpg" medium="image">
			<media:title type="html">Windows XP Driver Install 1</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windowsxp_install2.jpg" medium="image">
			<media:title type="html">Windows XP Driver Install 2</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windowsxp_hdd_install_driver_select.jpg" medium="image">
			<media:title type="html">Windows XP Driver Version Selection</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windowsxp_install3.jpg" medium="image">
			<media:title type="html">Windows XP Driver Install 3</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windowsxp_install4.jpg" medium="image">
			<media:title type="html">Windows XP Install 4</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windows7_install1.jpg" medium="image">
			<media:title type="html">Windows 7 Install 1</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windows7_install2.jpg" medium="image">
			<media:title type="html">Windows 7 Install 2</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windows7_install3.jpg" medium="image">
			<media:title type="html">Windows 7 Install 3</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/windows7_install4.jpg" medium="image">
			<media:title type="html">Windows 7 Install 4</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/05/device_manager_after.jpg" medium="image">
			<media:title type="html">Device Manager After Virtio Install</media:title>
		</media:content>
	</item>
		<item>
		<title>[Python] Creating Custom Cookies for HTTP Requests</title>
		<link>http://kahdev.wordpress.com/2011/05/14/544/</link>
		<comments>http://kahdev.wordpress.com/2011/05/14/544/#comments</comments>
		<pubDate>Sat, 14 May 2011 15:28:00 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=544</guid>
		<description><![CDATA[One way of adding a custom cookie is to directly specify the &#34;cookie&#34; field in your request header. For example: Here, two cookies are set in the request (&#34;something&#34; and &#34;source&#34;). Notice that the cookies are fully specified in the string, instead of using urllib.urlencode. Another way of adding cookies is to create Cookie objects [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=544&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One way of adding a custom cookie is to directly specify the &quot;cookie&quot; field in your request header. For example:</p>
<p><pre class="brush: python;"> 
request.add_header(&quot;Cookie&quot;, &quot;something=test+1+2+3; source=script&quot;)
</pre> </p>
<p>
Here, two cookies are set in the request (&quot;something&quot; and &quot;source&quot;). Notice that the cookies are fully specified in the string, instead of using <a href="http://docs.python.org/library/urllib#urllib.urlencode" target="_blank"><b>urllib.urlencode</b></a>. Another way of adding cookies is to create <a href="http://docs.python.org/library/cookielib.html#cookie-objects" target="_blank"><b>Cookie</b></a> objects and add them to your <a href="http://docs.python.org/library/cookielib.html#cookiejar-and-filecookiejar-objects" target="_blank"><b>CookieJar</b></a>. Before sending your request, add the cookies by using <a href="http://docs.python.org/library/cookielib.html#cookielib.CookieJar.add_cookie_header" target="_blank"><b>add_cookie_header</b></a>.<br />
<span id="more-544"></span><br />
An example for Python <b>2.7</b>:
</p>
<p><pre class="brush: python;">
from cookielib import Cookie
from cookielib import CookieJar
from urllib2 import Request
import urllib2
import urllib

'''
    Makes a cookie with provided name and value.
'''
def makeCookie(name, value):
    return Cookie(
        version=0, 
        name=name, 
        value=value,
        port=None, 
        port_specified=False,
        domain=&quot;kahdev.bur.st&quot;, 
        domain_specified=True, 
        domain_initial_dot=False,
        path=&quot;/&quot;, 
        path_specified=True,
        secure=False,
        expires=None,
        discard=False,
        comment=None,
        comment_url=None,
        rest=None
    )

# Create a cookie jar to store our custom cookies.
jar = CookieJar()

# Generate a request to make use of these cookies.
request = Request(url=&quot;http://kahdev.bur.st/python/cookies/receiver.php&quot;)

# Use makeCookie to generate a cookie and add it to the cookie jar.
jar.set_cookie(makeCookie(&quot;name&quot;, &quot;kahdev&quot;))
jar.set_cookie(makeCookie(&quot;where&quot;, &quot;here&quot;))

# Add the cookies from the jar to the request.
jar.add_cookie_header(request)

# Now, let us try open and read.
opener = urllib2.build_opener()
f = opener.open(request)

print &quot;Server responds with: &quot;
print f.read()
</pre></p>
<p>
The above source can also be downloaded from my <a href="https://github.com/kahgoh/Python-Custom-Cookies-Demo" target="_blank">Github repository</a>. If you run it, you should see the following output:<br />
<pre class="brush: plain;">
# python cookies.py
Server responds with: 
Cookies:
    [where] = [here] 
    [name] = [kahdev] 
</pre>
</p>
<p>
Note that the 2.7 <a href="http://docs.python.org/library/cookielib.html#cookielib.Cookie">documentation for <b>Cookie</b></a> indicates that you should not need to be doing this often:</p>
<blockquote><p>
It is not expected that users of cookielib construct their own Cookie instances. Instead, if necessary, call make_cookies() on a CookieJar instance.
</p></blockquote>
<p>However, the method <a href="http://docs.python.org/library/cookielib.html#cookielib.CookieJar.make_cookies" target="_blank"><b>make_cookies</b></a> is for <b>extracting</b> cookies from HTTP responses.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/544/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=544&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/05/14/544/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>
	</item>
		<item>
		<title>[Python] Using Basic HTTP Authentication</title>
		<link>http://kahdev.wordpress.com/2011/04/16/python-using-basic-http-authentication/</link>
		<comments>http://kahdev.wordpress.com/2011/04/16/python-using-basic-http-authentication/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 02:19:04 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Basic HTTP Authentication]]></category>
		<category><![CDATA[urllib2]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=538</guid>
		<description><![CDATA[To use urllib2 to access content that requires HTTP authentication, you need to create an OpenerDirector with a HTTPBasicAuthHandler. With HTTPBasicAuthHandler, add the authentication details by calling its add_password method. It takes in four parameters &#8211; realm, uri, user and passwd. The user and passwd are, quite obviously, the user name and password. The uri [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=538&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To use urllib2 to access content that requires HTTP authentication, you need to create an <a href="http://docs.python.org/library/urllib2.html#urllib2.OpenerDirector" target="_blank">OpenerDirector</a> with a <a href="http://docs.python.org/library/urllib2.html#urllib2.HTTPBasicAuthHandler">HTTPBasicAuthHandler</a>. With <a href="http://docs.python.org/library/urllib2.html#urllib2.HTTPBasicAuthHandler">HTTPBasicAuthHandler</a>, add the authentication details by calling its <a href="http://docs.python.org/library/urllib2.html#urllib2.HTTPPasswordMgr.add_password" target="_blank">add_password</a> method. It takes in four parameters &#8211; <i>realm</i>, <i>uri</i>, <i>user</i> and <i>passwd</i>.<br />
<span id="more-538"></span></p>
<p>The <i>user</i> and <i>passwd</i> are, quite obviously, the user name and password. The <i>uri</i> specifies the location where the user name and password applies. If there is only one location, this is usually specified as a string. However, if it can also be applied to more locations, you specify the locations as a sequence. The <i>realm</i> is the name of protected area where the authentication is applied to. You can get the realm from your browser, by trying to access the protected area.</p>
<p>With the handler created, build an opener that uses this handler. Then, use the opener to access the protected site or webpage. For example:<br />
<pre class="brush: python;">
import urllib2

authHandler = urllib2.HTTPBasicAuthHandler()
    
authHandler.add_password(
    realm=&quot;Protected Area&quot;,
    uri=&quot;http://local.intranet&quot;,
    # If there were multiple URIs then we can specify then like this:
    # uri = [&quot;site1&quot;, &quot;site2&quot;]

    user=&quot;user&quot;, 
    passwd=&quot;my_password&quot;)
opener = urllib2.build_opener(authHandler)
    
content = opener.open(&quot;http://192.168.1.254&quot;)
print content.read()
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/538/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/538/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/538/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=538&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/04/16/python-using-basic-http-authentication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>
	</item>
		<item>
		<title>[Android] Using Projects to Separate Content Provider from Application Code</title>
		<link>http://kahdev.wordpress.com/2011/01/29/android-using-projects-to-separate-content-provider-from-application-code/</link>
		<comments>http://kahdev.wordpress.com/2011/01/29/android-using-projects-to-separate-content-provider-from-application-code/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 04:38:38 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=527</guid>
		<description><![CDATA[Rather than have one massive project containing the code, it is sometimes better to organise your separate your code out into projects. In this example, a workspace containing two applications sharing the same content provider is separated out into four separate projects.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=527&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Content providers can be used to store and share data between two or more applications. The applications are usually independent of each other and they do not need to know the content provider stores and retrieves its data. Instead of lumping all of them together in one project, I generally find it easier to work with when they are separated out into their own individual projects. In this example (downloadable from <a href="http://www.box.net/shared/ict75mx1yy" target="_blank">here</a>), this is separated in to four projects:<br />
<span id="more-527"></span></p>
<ol>
<li><b>CategoryEditor</b> &#8211; Contains a <a href="http://developer.android.com/reference/android/app/ListActivity.html">ListActivity</a> for editing the Categories.</li>
<li><b>CategoryField</b> &#8211; Contains another activity that displays the Categories in a combo box.</li>
<li><b>CategoryProviderApi</b> &#8211; Contains an API for other projects to make use of the content provider.</li>
<li><b>CategoryProviderImpl</b> &#8211; Contains the content provider implementation.</li>
</ol>
<p>The reason for this separation is because the activities in <b>CategoryEditor</b> and <b>CategoryField</b> do not need to know the implementation details of the content provider &#8211; how it stores its data and how it retrieves it. All they require is the information contained in <b>CategoryProviderApi</b>. In fact, <b>CategoryProviderApi</b> contains only a single class that provides constants for the other applications to access the content provider.</p>
<p>To use this kind of structure, the <b>CategoryProviderApi</b> must first be marked as a library. This is done via the Android properties:<br />
<img class="aligncenter size-full wp-image-532" title="Setting an Android Project as a Library" src="http://kahdev.files.wordpress.com/2011/01/set_project_as_library.png?w=630&#038;h=498" alt="" width="630" height="498" /></p>
<p>Next, the other projects are referencing this project (including <b>CategoryProviderImpl</b>). In the Android properties in each of the projects, they need to add <b>CategoryProviderApi</b><br />
<img class="aligncenter size-full wp-image-533" title="Adding an Android Library to a Project" src="http://kahdev.files.wordpress.com/2011/01/add_project_library.png?w=630" alt=""   /></p>
<p>We also have to declare the content provider in an <i>AndroidManifest.xml</i> file. With the projects separated like this, we could still place this information inside the manifest for <b>CategoryProviderImpl</b>. The manifest would look something like this:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:versionCode=&quot;1&quot;
      android:versionName=&quot;1.0&quot; package=&quot;org.kah.provider&quot;&gt;
	&lt;application&gt;
		&lt;provider android:authorities=&quot;org.kah.categories&quot;
			android:name=&quot;TasksProvider&quot;&gt;
		&lt;/provider&gt;
	&lt;/application&gt;
&lt;/manifest&gt; 
</pre></p>
<p>However, this would make <b>CategoryProviderImpl</b> look like another application and you would have to install this into the emulator or phone separately from the other applications. </p>
<p>Alternatively, we could make <b>CategoryProviderImpl</b> another library and have one of the project&#8217;s manifest declare the provider. However, this would require the application declaring the content provider to be installed first. Furthermore, if the user chooses to uninstall the application declaring the content provider, than the other application will no longer have access to the same content provider. Unfortunately, I&#8217;m not aware of any other solutions that do not have this draw backs. Please, feel free to leave a comment if you do know of a better way or suggestions.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/527/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/527/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=527&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2011/01/29/android-using-projects-to-separate-content-provider-from-application-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/01/set_project_as_library.png" medium="image">
			<media:title type="html">Setting an Android Project as a Library</media:title>
		</media:content>

		<media:content url="http://kahdev.files.wordpress.com/2011/01/add_project_library.png" medium="image">
			<media:title type="html">Adding an Android Library to a Project</media:title>
		</media:content>
	</item>
		<item>
		<title>[Maven] Placing Third Party Libraries in your own Repository</title>
		<link>http://kahdev.wordpress.com/2010/12/25/maven-placing-third-paty-libraries-in-your-own-repository/</link>
		<comments>http://kahdev.wordpress.com/2010/12/25/maven-placing-third-paty-libraries-in-your-own-repository/#comments</comments>
		<pubDate>Sat, 25 Dec 2010 05:50:43 +0000</pubDate>
		<dc:creator>kahgoh</dc:creator>
				<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://kahdev.wordpress.com/?p=512</guid>
		<description><![CDATA[Although the public repositories for Maven contains a vast collection of third party libraries. Despite this, there are some libraries that you would be able to find in them. Fortunately, you can place the library into your local repository or set up your own internal repository. If you are setting up a large internal repository, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=512&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Although the public repositories for <a href="http://maven.apache.org" target="_blank"><i>Maven</i></a> contains a vast collection of third party libraries. Despite this, there are some libraries that you would be able to find in them. Fortunately, you can place the library into your local repository or set up your own internal repository. If you are setting up a large internal repository, you might consider using a repository manager, such as <a href="http://archiva.apache.org/" target="_blank"><i>Archiva</i></a>. However, if you do not want to install one, you can set your own internal repository or place it directly in your private repository.<br />
<span id="more-512"></span></p>
<h2>Installing to your personal local repository</h2>
<p>Use the <a href="http://maven.apache.org/plugins/maven-install-plugin/" target="_blank"><i>install</i></a> plugin to install the files directly into the personal local repository (usually, <i>~/.m2/repository</i> or <i>c:\Documents and Settings\&lt;username&gt;\.m2\repository</i>). Use the <a href="http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html" target="_blank"><i>install-file</i></a> goal to place specific files from third party libraries. The structure of the command:</p>
<p><pre class="brush: plain;">
mvn install:install-file \
    -Dfile=&lt;the file to install&gt; \
    -DartifactId=&lt;id for the file&gt; \
    -DgroupId=&lt;group id&gt; \
    -Dpackaging=&lt;package type (e.g. jar, war, ear)&gt; \
    -Dversion=&lt;version of file&gt;
</pre></p>
<p>The above command assumes that your personal local repository is in the default path (as mentioned before, either <i>~/.m2/repository</i> or <i>c:\Documents and Settings\&lt;username&gt;\.m2\repository</i>). If it is in a different location, you need to provide it in the <i>localRepositoryPath</i> parameter. This can also be used to place the artifact in an internal repository.</p>
<p>If the artifact has any dependencies that may not be directly referenced by the your project, you will also have to add them into the repository. You should also edit the generated pom to include the dependency.</p>
<h2>Deploying the artifact to the repository</h2>
<p>The <a href="http://maven.apache.org/plugins/maven-deploy-plugin/" target="_blank"><i>deploy</i></a> can also be used to add artifacts to a repository. Unlike the <i>install</i> plugin, it does not assume a default location for the respository. Instead, you must specify the repository location, using the <i>url</i> parameter.</p>
<p><pre class="brush: plain;">
mvn deploy:deploy-file \
    -Dfile=&lt;file to deploy&gt; \
    -DartifactId=&lt;id for the artifact&gt; \
    -DgroupId=&lt;group id&gt; \
    -Dpackaging=&lt;the type of packaging (e.g. jar, war, ear)&gt; \
    -Dversion=&lt;version of artifact&gt; \
    -Durl=&lt;url of repository&gt;
</pre></p>
<p>It is important to note that <i>url</i> parameter is expecting a full url path, including the protocol. If the repository is a location within the file system, you should specify it with <i>file://&lt;file path&gt;</i>. The <i>localRepositoryPath</i> parameter for <i>install</i> already expects a file system path and does not require the protocol to be specified. If the artifact has any dependencies on any other artifacts, you will need to edit the pom generated for the artifact.</p>
<h2>Manually adding the artifact to the repository</h2>
<p>When you add artifacts using either the <i>install</i> or <i>deploy</i> plugins, these steps are performed. To add the artifact manually, you must first set up the appropriate directory structure in the repository. The directory structure is related to the group id of the artifact and is set up in a way that is similar to how directory structure to represent Java packages. For example, if the group id was <i>org.example.project</i>, the directory structure would look like this:</p>
<p><pre class="brush: plain;">
repository root
|
+- org
 |
 +- example
  |
  +- project
</pre></p>
<p>For group ids that do not use the dot notation, you would only need a single directory to represent the group id. At end of the directory structure (e.g. the <i>project</i> directory in the above example), you will need to create a directory for each of the artifacts. The name of the directory will correspond to the artifact id. Inside the artifact directory, you also need to create a directory per version of the artifact. For example, if you were adding version 1.0.1 of an artifact with the id <i>sample</i> in the above example, your directory structure would now be:</p>
<p><pre class="brush: plain;">
repository root
|
+- org
 |
 +- example
  |
  +- project
   |
   +- sample
    |
    +- 1.0.1
</pre></p>
<p>The artifact is placed in the directory representing the version. However, its file name needs to be in the following format:</p>
<p><pre class="brush: plain;">
&lt;artifact id&gt;-&lt;version&gt;.&lt;file extension&gt;
</pre></p>
<p>Following on from the example, if the artifact was a jar file, the file name would be <i>sample-1.0.1.jar</i>. Finally, set up a pom file for the artifact. The file name for the pom should follow the same format (e.g. <i>sample-1.0.1.pom</i>). </p>
<p>Once the pom is set up, you should be able to start pulling the artifact from the repository. As mentioned before, the <i>install</i> and <i>deploy</i> plugins will do all this for you. However, they also generate the hashes  for the poms and the artifacts, a few other files. If you wish you could also manually create these files, but is not really necessary to start using the artifact from the repository.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kahdev.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kahdev.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kahdev.wordpress.com/512/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kahdev.wordpress.com&amp;blog=4034716&amp;post=512&amp;subd=kahdev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kahdev.wordpress.com/2010/12/25/maven-placing-third-paty-libraries-in-your-own-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">kahgoh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
