<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Can I change this later??? » Programming</title>
	
	<link>http://www.vainolo.com</link>
	<description>My thoughts, ideas.</description>
	<lastBuildDate>Wed, 30 May 2012 22:10:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/vainolo/categories/programming" /><feedburner:info uri="vainolo/categories/programming" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Enabling Select-All Action in a GEF Editor</title>
		<link>http://feedproxy.google.com/~r/vainolo/categories/programming/~3/gBzISvzpRnw/</link>
		<comments>http://www.vainolo.com/2012/05/17/enabling-select-all-action-in-a-gef-editor/#comments</comments>
		<pubDate>Wed, 16 May 2012 21:19:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[GEF]]></category>
		<category><![CDATA[Select All]]></category>

		<guid isPermaLink="false">http://www.vainolo.com/?p=1226</guid>
		<description><![CDATA[Today I was working on a model using my OPM GEF editor, and wanted to select all elements of the model&#8230; but for some strange reason the &#8220;Select All&#8221; action in the &#8220;Edit&#8221; menu was disabled&#8230; So I started to investigate. First, the &#8220;Select All&#8221; action should be similar to other eclipse standard actions, like &#8220;Undo&#8221; and &#8220;Delete&#8221;, so I expected some treatment similar to these actions &#8211; a SelectAllRetargetAction or something similar. There is a SelectAllAction class in the GEF framework, but I was not sure how to plug it in. First, I saw that this action was already &#8230; <a class="more-link" href="http://www.vainolo.com/2012/05/17/enabling-select-all-action-in-a-gef-editor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I was working on a model using my OPM GEF editor, and wanted to select all elements of the model&#8230; but for some strange reason the &#8220;Select All&#8221; action in the &#8220;Edit&#8221; menu was disabled&#8230; So I started to investigate.</p>
<p>First, the &#8220;Select All&#8221; action should be similar to other eclipse standard actions, like &#8220;Undo&#8221; and &#8220;Delete&#8221;, so I expected some treatment similar to these actions &#8211; a <code>SelectAllRetargetAction</code> or something similar. There is a <code>SelectAllAction</code> class in the GEF framework, but I was not sure how to plug it in. First, I saw that this action was already registered in the <code>actionRegistry</code> in <code>GraphicalEditor.createActions()</code> function. So why was it disabled? More code reading showed me that also the undo and redo actions are registered there, but I also had to create for them a retargetable action in the <code>ActionBarContributor</code> of my editor. So I added this code to the <code>OPMGraphicalEditorActionBarContributor.buildActions()</code> method (the last line):</p>
<pre class="brush: java; first-line: 20; title: ; notranslate">
	@Override
	protected void buildActions() {
		addRetargetAction(new UndoRetargetAction());
		addRetargetAction(new RedoRetargetAction());
		addRetargetAction(new DeleteRetargetAction());
		addRetargetAction(new RetargetAction(GEFActionConstants.TOGGLE_GRID_VISIBILITY,
				GEFMessages.ToggleGrid_Label,
				IAction.AS_CHECK_BOX));
		addRetargetAction(new RetargetAction(GEFActionConstants.TOGGLE_SNAP_TO_GEOMETRY,
				GEFMessages.ToggleSnapToGeometry_Label,
				IAction.AS_CHECK_BOX));
		addRetargetAction(new RetargetAction(GEFActionConstants.SELECT_ALL, GEFMessages.SelectAllAction_Label));
	}
</pre>
<p>The &#8220;Select All&#8221; menu item became available and the action worked, even with the expected &#8220;Ctrl+A&#8221; keyboard accelerator. But the editor showed me that the <code>GEFActionConstants.SELECT_ALL</code> constant is deprecated, and that I should use the <code>ActionFactory.SELECT_ALL.getId()</code>&#8230; I scratched my head a bit&#8230; I had a new lead on my problem, but where to search for clues? Obviously, the <code>org.eclipse.gef.examples.logic</code> project! And there it was: the <code>ActionBarContributor.declareGlobalActionKeys()</code> function calls <code>addGlobalActionKey(ActionFactory.SELECT_ALL.getId())</code>. In two minutes I removed my old line and added this one to my contributor class, and there it was again, the &#8220;Select All&#8221; action worked, and the code was clean of warnings.</p>
<p>Seems strange to me that although the action was already registered, only when I added here the action became available. The name of the function (and it&#8217;s location) is also confusing: why the <code>Bar</code> in <code>ActionBarContributor</code>? And another nice thing in the code of this class is the warning in the class comment:</p>
<pre class="brush: java; title: ; notranslate">
/**
 * Contributes actions to the workbench. !!Warning: This class is subject to
 * change.
 */
</pre>
<p>But the &#8220;Select All&#8221; action works, and that is what matters.</p>
<p>Related posts:</p><ol>
<li><a href='http://www.vainolo.com/2011/08/01/creating-a-gef-editor-%e2%80%93-part-12-enable-save-action-on-the-editor/' rel='bookmark' title='Creating a GEF Editor – Part 12: Enable Save Action on the Editor'>Creating a GEF Editor – Part 12: Enable Save Action on the Editor</a></li>
<li><a href='http://www.vainolo.com/2011/09/04/creating-an-opm-gef-editor-%e2%80%93-part-18-snapping-to-grid-and-to-geometry/' rel='bookmark' title='Creating an OPM GEF Editor – Part 18: Snapping to Grid and to Geometry'>Creating an OPM GEF Editor – Part 18: Snapping to Grid and to Geometry</a></li>
<li><a href='http://www.vainolo.com/2011/07/10/creating-a-gef-editor-%e2%80%93-part-8-delete-undo-and-redo/' rel='bookmark' title='Creating a GEF Editor – Part 8: Delete, Undo and Redo'>Creating a GEF Editor – Part 8: Delete, Undo and Redo</a></li>
</ol>
<p><a href="http://feedads.g.doubleclick.net/~a/ZDv0yCu09O3Ts358L6aGrFvxegM/0/da"><img src="http://feedads.g.doubleclick.net/~a/ZDv0yCu09O3Ts358L6aGrFvxegM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ZDv0yCu09O3Ts358L6aGrFvxegM/1/da"><img src="http://feedads.g.doubleclick.net/~a/ZDv0yCu09O3Ts358L6aGrFvxegM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/vainolo/categories/programming/~4/gBzISvzpRnw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vainolo.com/2012/05/17/enabling-select-all-action-in-a-gef-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vainolo.com/2012/05/17/enabling-select-all-action-in-a-gef-editor/</feedburner:origLink></item>
		<item>
		<title>Extracting the Contents of a Zip File in Java</title>
		<link>http://feedproxy.google.com/~r/vainolo/categories/programming/~3/4AyceUfBH34/</link>
		<comments>http://www.vainolo.com/2012/05/08/1173/#comments</comments>
		<pubDate>Tue, 08 May 2012 12:25:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://www.vainolo.com/?p=1173</guid>
		<description><![CDATA[The following short program shows how to extract all of the files that are inside a Zip file. Java provides us with a number of classes to work with zip files, and their use is pretty straightforward. To extract the files to their original directories, I also had to create the directory in the file system. Some thoughts on my implementation: First, I do no parameter validation or error checking. So what can happen? here are a few examples: The input file may not exist. The input file exists but is not a zip file. The output directory does not &#8230; <a class="more-link" href="http://www.vainolo.com/2012/05/08/1173/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The following short program shows how to extract all of the files that are inside a Zip file. <a class="zem_slink" title="Java (programming language)" href="http://www.oracle.com/technetwork/java/" rel="homepage">Java</a> provides us with a number of classes to work with zip files, and their use is pretty straightforward. To extract the files to their original directories, I also had to create the directory in the file system.</p>
<script>document.write('<link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"/>')

document.write('<div id=\"gist-2633812\" class=\"gist\">\n\n        <div class=\"gist-file\">\n          <div class=\"gist-data gist-syntax\">\n              <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'><span class=\"cm\">/*******************************************************************************<\/span><\/div><div class=\'line\' id=\'LC2\'><span class=\"cm\"> * Copyright (c) 2012 Arieh &#39;Vainolo&#39; Bibliowicz<\/span><\/div><div class=\'line\' id=\'LC3\'><span class=\"cm\"> * You can use this code for educational purposes. For any other uses<\/span><\/div><div class=\'line\' id=\'LC4\'><span class=\"cm\"> * please contact me: vainolo@gmail.com<\/span><\/div><div class=\'line\' id=\'LC5\'><span class=\"cm\"> *******************************************************************************/<\/span><\/div><div class=\'line\' id=\'LC6\'><span class=\"kn\">package<\/span> <span class=\"n\">com<\/span><span class=\"o\">.<\/span><span class=\"na\">vainolo<\/span><span class=\"o\">.<\/span><span class=\"na\">examples<\/span><span class=\"o\">.<\/span><span class=\"na\">file<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC7\'><br/><\/div><div class=\'line\' id=\'LC8\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.BufferedInputStream<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC9\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.BufferedOutputStream<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC10\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.File<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC11\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.FileOutputStream<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC12\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.IOException<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC13\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.util.Enumeration<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC14\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.util.zip.ZipEntry<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC15\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.util.zip.ZipFile<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC16\'><br/><\/div><div class=\'line\' id=\'LC17\'><span class=\"cm\">/**<\/span><\/div><div class=\'line\' id=\'LC18\'><span class=\"cm\"> * Extract a zip file to a folder. No error checking or argument validation is done.<\/span><\/div><div class=\'line\' id=\'LC19\'><span class=\"cm\"> * <\/span><\/div><div class=\'line\' id=\'LC20\'><span class=\"cm\"> * @author vainolo<\/span><\/div><div class=\'line\' id=\'LC21\'><span class=\"cm\"> */<\/span><\/div><div class=\'line\' id=\'LC22\'><span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">ZipExtracter<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC23\'>	<span class=\"kd\">public<\/span> <span class=\"kd\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">openZipFile<\/span><span class=\"o\">(<\/span><span class=\"n\">String<\/span> <span class=\"n\">zipFilename<\/span><span class=\"o\">,<\/span> <span class=\"n\">String<\/span> <span class=\"n\">destinationDirname<\/span><span class=\"o\">)<\/span> <span class=\"kd\">throws<\/span> <span class=\"n\">IOException<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC24\'>		<span class=\"kt\">byte<\/span><span class=\"o\">[]<\/span> <span class=\"n\">buffer<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"kt\">byte<\/span><span class=\"o\">[<\/span><span class=\"mi\">1024<\/span><span class=\"o\">];<\/span><\/div><div class=\'line\' id=\'LC25\'>		<span class=\"kt\">int<\/span> <span class=\"n\">bytesRead<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC26\'>		<span class=\"n\">File<\/span> <span class=\"n\">zipFile<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">File<\/span><span class=\"o\">(<\/span><span class=\"n\">zipFilename<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC27\'>		<span class=\"n\">File<\/span> <span class=\"n\">destinationDir<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">File<\/span><span class=\"o\">(<\/span><span class=\"n\">destinationDirname<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC28\'><br/><\/div><div class=\'line\' id=\'LC29\'>		<span class=\"n\">ZipFile<\/span> <span class=\"n\">zip<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">ZipFile<\/span><span class=\"o\">(<\/span><span class=\"n\">zipFile<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC30\'>		<span class=\"n\">Enumeration<\/span><span class=\"o\">&lt;?<\/span> <span class=\"kd\">extends<\/span> <span class=\"n\">ZipEntry<\/span><span class=\"o\">&gt;<\/span> <span class=\"n\">zipEntries<\/span> <span class=\"o\">=<\/span> <span class=\"n\">zip<\/span><span class=\"o\">.<\/span><span class=\"na\">entries<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC31\'>		<span class=\"k\">while<\/span> <span class=\"o\">(<\/span><span class=\"n\">zipEntries<\/span><span class=\"o\">.<\/span><span class=\"na\">hasMoreElements<\/span><span class=\"o\">())<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC32\'>			<span class=\"n\">ZipEntry<\/span> <span class=\"n\">entry<\/span> <span class=\"o\">=<\/span> <span class=\"n\">zipEntries<\/span><span class=\"o\">.<\/span><span class=\"na\">nextElement<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC33\'>			<span class=\"k\">if<\/span> <span class=\"o\">(<\/span><span class=\"n\">entry<\/span><span class=\"o\">.<\/span><span class=\"na\">isDirectory<\/span><span class=\"o\">())<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC34\'>				<span class=\"n\">File<\/span> <span class=\"n\">newDir<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">File<\/span><span class=\"o\">(<\/span><span class=\"n\">destinationDir<\/span><span class=\"o\">,<\/span> <span class=\"n\">entry<\/span><span class=\"o\">.<\/span><span class=\"na\">getName<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC35\'>				<span class=\"n\">newDir<\/span><span class=\"o\">.<\/span><span class=\"na\">mkdirs<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC36\'>			<span class=\"o\">}<\/span> <span class=\"k\">else<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC37\'>				<span class=\"n\">BufferedInputStream<\/span> <span class=\"n\">inputStream<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">BufferedInputStream<\/span><span class=\"o\">(<\/span><span class=\"n\">zip<\/span><span class=\"o\">.<\/span><span class=\"na\">getInputStream<\/span><span class=\"o\">(<\/span><span class=\"n\">entry<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC38\'>				<span class=\"n\">File<\/span> <span class=\"n\">outputFile<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">File<\/span><span class=\"o\">(<\/span><span class=\"n\">destinationDir<\/span><span class=\"o\">,<\/span> <span class=\"n\">entry<\/span><span class=\"o\">.<\/span><span class=\"na\">getName<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC39\'>				<span class=\"n\">BufferedOutputStream<\/span> <span class=\"n\">outputStream<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">BufferedOutputStream<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">FileOutputStream<\/span><span class=\"o\">(<\/span><span class=\"n\">outputFile<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC40\'>				<span class=\"k\">while<\/span> <span class=\"o\">((<\/span><span class=\"n\">bytesRead<\/span> <span class=\"o\">=<\/span> <span class=\"n\">inputStream<\/span><span class=\"o\">.<\/span><span class=\"na\">read<\/span><span class=\"o\">(<\/span><span class=\"n\">buffer<\/span><span class=\"o\">))<\/span> <span class=\"o\">!=<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC41\'>					<span class=\"n\">outputStream<\/span><span class=\"o\">.<\/span><span class=\"na\">write<\/span><span class=\"o\">(<\/span><span class=\"n\">buffer<\/span><span class=\"o\">,<\/span> <span class=\"mi\">0<\/span><span class=\"o\">,<\/span> <span class=\"n\">bytesRead<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC42\'>				<span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC43\'>				<span class=\"n\">inputStream<\/span><span class=\"o\">.<\/span><span class=\"na\">close<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC44\'>				<span class=\"n\">outputStream<\/span><span class=\"o\">.<\/span><span class=\"na\">close<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC45\'>			<span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC46\'>		<span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC47\'>	<span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC48\'><span class=\"o\">}<\/span><\/div><\/pre><\/div>\n          <\/div>\n\n          <div class=\"gist-meta\">\n            <a href=\"https://gist.github.com/raw/2633812/5e997fd28ea55738d2dffdf39f24c63061e82924/ZipExtracter.java\" style=\"float:right;\">view raw<\/a>\n            <a href=\"https://gist.github.com/2633812#file_zip_extracter.java\" style=\"float:right;margin-right:10px;color:#666\">ZipExtracter.java<\/a>\n            <a href=\"https://gist.github.com/2633812\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n          <\/div>\n        <\/div>\n<\/div>\n')
</script><div style='margin-bottom:1em;padding:0;'><noscript><code><pre style='overflow:auto;margin:0;padding:0;border:1px solid #DDD;'>/*******************************************************************************
 * Copyright (c) 2012 Arieh 'Vainolo' Bibliowicz
 * You can use this code for educational purposes. For any other uses
 * please contact me: vainolo@gmail.com
 *******************************************************************************/
package com.vainolo.examples.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Extract a zip file to a folder. No error checking or argument validation is done.
 * 
 * @author vainolo
 */
public class ZipExtracter {
	public static void openZipFile(String zipFilename, String destinationDirname) throws IOException {
		byte[] buffer = new byte[1024];
		int bytesRead = 0;
		File zipFile = new File(zipFilename);
		File destinationDir = new File(destinationDirname);

		ZipFile zip = new ZipFile(zipFile);
		Enumeration&lt;? extends ZipEntry&gt; zipEntries = zip.entries();
		while (zipEntries.hasMoreElements()) {
			ZipEntry entry = zipEntries.nextElement();
			if (entry.isDirectory()) {
				File newDir = new File(destinationDir, entry.getName());
				newDir.mkdirs();
			} else {
				BufferedInputStream inputStream = new BufferedInputStream(zip.getInputStream(entry));
				File outputFile = new File(destinationDir, entry.getName());
				BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
				while ((bytesRead = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, bytesRead);
				}
				inputStream.close();
				outputStream.close();
			}
		}
	}
}</pre></code></noscript></div>
<p>Some thoughts on my implementation:</p>
<p>First, I do no parameter validation or error checking. So what can happen? here are a few examples:</p>
<ul>
<li>The input file may not exist.</li>
<li>The input file exists but is not a zip file.</li>
<li>The output directory does not exist.</li>
<li>Problems may occur in the middle of the process of reading from the input file or writing to the file system (for example if your output file system is a network disk and your cat just ate the network cable).</li>
</ul>
<p>As you can see, many things can go wrong (and will), so a GOOD implementation (not a simple example) should check for these problems and deal with them as best as it can. I personally don&#8217;t like the way Java forces you to catch all exceptions, because it makes code harder to read. But in real systems you cannot have this luxury.</p>
<p>Second, the implementation assumes that the <code>Enumeration</code> returned by the <code><a class="zem_slink" title="ZIP (file format)" href="http://en.wikipedia.org/wiki/ZIP_%28file_format%29" rel="wikipedia">ZipFile</a></code> is ordered &#8211; directories before the files in the directories. I didn&#8217;t find any comments about this in the documentation of <code>ZipFile</code>&#8230; maybe it&#8217;s part of the zip standard.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=9eacdb41-0c83-466a-9349-075a1f972e36" alt="Enhanced by Zemanta" /></a></div>
<p>Related posts:</p><ol>
<li><a href='http://www.vainolo.com/2012/05/07/java-examples-repository-first-example-copy-a-file-from-a-url-to-a-local-path/' rel='bookmark' title='Java Examples Repository &#8211; First Example: Copy a File from a URL to a Local Path'>Java Examples Repository &#8211; First Example: Copy a File from a URL to a Local Path</a></li>
<li><a href='http://www.vainolo.com/2011/06/22/creating-a-gef-editor-%e2%80%93-part-5-loading-the-model-from-an-emf-file/' rel='bookmark' title='Creating a GEF Editor – Part 5: Loading the Model from an EMF File'>Creating a GEF Editor – Part 5: Loading the Model from an EMF File</a></li>
<li><a href='http://www.vainolo.com/2011/05/24/the-quest-for-the-perfect-java-graph-framework/' rel='bookmark' title='The Quest for the Perfect Java Graph Framework'>The Quest for the Perfect Java Graph Framework</a></li>
</ol>
<p><a href="http://feedads.g.doubleclick.net/~a/ckNu8QLFMCBYNjm97DBMd6LmJwY/0/da"><img src="http://feedads.g.doubleclick.net/~a/ckNu8QLFMCBYNjm97DBMd6LmJwY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ckNu8QLFMCBYNjm97DBMd6LmJwY/1/da"><img src="http://feedads.g.doubleclick.net/~a/ckNu8QLFMCBYNjm97DBMd6LmJwY/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/vainolo/categories/programming/~4/4AyceUfBH34" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vainolo.com/2012/05/08/1173/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vainolo.com/2012/05/08/1173/</feedburner:origLink></item>
		<item>
		<title>Java Examples Repository – First Example: Copy a File from a URL to a Local Path</title>
		<link>http://feedproxy.google.com/~r/vainolo/categories/programming/~3/Pz_x1-TdK14/</link>
		<comments>http://www.vainolo.com/2012/05/07/java-examples-repository-first-example-copy-a-file-from-a-url-to-a-local-path/#comments</comments>
		<pubDate>Sun, 06 May 2012 21:42:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[network]]></category>

		<guid isPermaLink="false">http://www.vainolo.com/?p=1166</guid>
		<description><![CDATA[There are programming tasks that I don&#8217;t do a lot, and they are forgotten in the depths of my brain, never to be seen again. That is why I try to store all of the code I have written so that I can fetch examples when needed (and now that github exists, why not share it with the world?). So my first example in the new public repository is this: how to copy a file (maybe binary) from a URL to a local path. The code is pretty simple: The code is available on the Vainosamples repository. Oh, and if &#8230; <a class="more-link" href="http://www.vainolo.com/2012/05/07/java-examples-repository-first-example-copy-a-file-from-a-url-to-a-local-path/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are programming tasks that I don&#8217;t do a lot, and they are forgotten in the depths of my brain, never to be seen again. That is why I try to store all of the code I have written so that I can fetch examples when needed (and now that <a href="github.com">github</a> exists, why not share it with the world?).<br />
So my first example in the new public repository is this: how to copy a file (maybe binary) from a URL to a local path. The code is pretty simple:</p>
<pre class="brush: java; title: ; notranslate">
/*******************************************************************************
 * Copyright (c) 2012 Arieh 'Vainolo' Bibliowicz
 * You can use this code for educational purposes. For any other uses
 * please contact me: vainolo@gmail.com
 *******************************************************************************/
package com.vainolo.examples.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class NetworkFileCopier {
	public static File copyFileFromWeb(String address, String filePath) throws Exception {
		byte[] buffer = new byte[1024];
		int bytesRead;

		URL url = new URL(address);
		BufferedInputStream inputStream = null;
		BufferedOutputStream outputStream = null;
		URLConnection connection = url.openConnection();
		// If you need to use a proxy for your connection, the URL class has another openConnection method.
		// For example, to connect to my local SOCKS proxy I can use:
		// url.openConnection(new Proxy(Proxy.Type.SOCKS, newInetSocketAddress(&quot;localhost&quot;, 5555)));
		inputStream = new BufferedInputStream(connection.getInputStream());
		File f = new File(filePath);
		outputStream = new BufferedOutputStream(new FileOutputStream(f));
		while ((bytesRead = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, bytesRead);
		}
		inputStream.close();
		outputStream.close();
		return f;
	}
}
</pre>
<p>The code is available on the <a href="https://github.com/vainolo/Vainosamples">Vainosamples repository</a>. Oh, and if you have a better implementation, please do tell me!</p>
<p>Related posts:</p><ol>
<li><a href='http://www.vainolo.com/2011/02/14/learning-jung-java-universal-networkgraph-framework/' rel='bookmark' title='Learning JUNG &#8211; Java Universal Network/Graph Framework'>Learning JUNG &#8211; Java Universal Network/Graph Framework</a></li>
<li><a href='http://www.vainolo.com/2011/04/10/jgraphx-another-graph-framework/' rel='bookmark' title='JGraph &#8211; Another Java Graph Framework'>JGraph &#8211; Another Java Graph Framework</a></li>
<li><a href='http://www.vainolo.com/2011/06/22/creating-a-gef-editor-%e2%80%93-part-5-loading-the-model-from-an-emf-file/' rel='bookmark' title='Creating a GEF Editor – Part 5: Loading the Model from an EMF File'>Creating a GEF Editor – Part 5: Loading the Model from an EMF File</a></li>
</ol>
<p><a href="http://feedads.g.doubleclick.net/~a/P1zaxNbe9Jd-L_hg5rX33LGMK_8/0/da"><img src="http://feedads.g.doubleclick.net/~a/P1zaxNbe9Jd-L_hg5rX33LGMK_8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/P1zaxNbe9Jd-L_hg5rX33LGMK_8/1/da"><img src="http://feedads.g.doubleclick.net/~a/P1zaxNbe9Jd-L_hg5rX33LGMK_8/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/vainolo/categories/programming/~4/Pz_x1-TdK14" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vainolo.com/2012/05/07/java-examples-repository-first-example-copy-a-file-from-a-url-to-a-local-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vainolo.com/2012/05/07/java-examples-repository-first-example-copy-a-file-from-a-url-to-a-local-path/</feedburner:origLink></item>
		<item>
		<title>Factory Method Design Pattern – Sequence Diagrams</title>
		<link>http://feedproxy.google.com/~r/vainolo/categories/programming/~3/rxoraNw3INY/</link>
		<comments>http://www.vainolo.com/2012/05/02/factory-method-design-pattern-uml-modeling/#comments</comments>
		<pubDate>Wed, 02 May 2012 09:26:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Factory method pattern]]></category>
		<category><![CDATA[Sequence diagram]]></category>

		<guid isPermaLink="false">http://www.vainolo.com/?p=1147</guid>
		<description><![CDATA[As defined by the GOF, the Factory Method Design Pattern is used to &#8221;Define an interface for creating an object, but let the classes which implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses&#8221;. Factory methods can be used in place of constructors to create objects, doing so in a more elegant and controllable way (Joshua Bloch said in an interview that only classes that were designed for subclassing should have constructors). In its most simplest form, a factory method simply replaces the constructor: And we can show how this works using &#8230; <a class="more-link" href="http://www.vainolo.com/2012/05/02/factory-method-design-pattern-uml-modeling/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As defined by the GOF, the <a class="zem_slink" title="Factory method pattern" href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="wikipedia">Factory Method</a> Design Pattern is used to &#8221;Define an interface for creating an object, but let the classes which implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses&#8221;. Factory methods can be used in place of constructors to create objects, doing so in a more elegant and controllable way (Joshua Bloch said in an <a href="http://www.artima.com/intv/blochP.html">interview </a>that only classes that were designed for subclassing should have constructors).</p>
<p>In its most simplest form, a factory method simply replaces the constructor:</p>
<p><a href="http://www.vainolo.com/wp-content/uploads/2012/05/Capture1.png"><img class="aligncenter size-full wp-image-1149" title="factory.1.class" src="http://www.vainolo.com/wp-content/uploads/2012/05/Capture1.png" alt="" width="307" height="79" /></a></p>
<p>And we can show how this works using a <a class="zem_slink" title="Sequence diagram" href="http://en.wikipedia.org/wiki/Sequence_diagram" rel="wikipedia">sequence diagram</a>:</p>
<p><a href="http://www.vainolo.com/wp-content/uploads/2012/05/Capture2.png"><img class="aligncenter size-full wp-image-1150" title="factory.1.sequence.diagram" src="http://www.vainolo.com/wp-content/uploads/2012/05/Capture2.png" alt="" width="372" height="141" /></a></p>
<p>We can obviously add more functionality to the <code>createProduct()</code> method, but this is trivial. A more complex example is to use polymorphism to get factories that can create different kinds of objects, all of which comply to the same abstraction. This is how it is described by the GOF (fairly overlapping the abstract factory pattern):</p>
<p><a href="http://www.vainolo.com/wp-content/uploads/2012/05/Capture3.png"><img class="aligncenter size-full wp-image-1152" title="factory.2.class" src="http://www.vainolo.com/wp-content/uploads/2012/05/Capture3.png" alt="" width="513" height="335" /></a></p>
<p>This implementation completely decouples the client from the implementation. The client is only aware of a <code>Creator</code> and an <code><a class="zem_slink" title="Abstraction (computer science)" href="http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29" rel="wikipedia">Abstraction</a></code> with complete disregard of their implementations. The creator is somehow available to the client (via <a class="zem_slink" title="Dependency injection" href="http://en.wikipedia.org/wiki/Dependency_injection" rel="wikipedia">dependency injection</a>). So now that we have a static model, is there a way to describe the dynamics of this diagram using a sequence diagram? Let&#8217;s try:</p>
<p><a href="http://www.vainolo.com/wp-content/uploads/2012/05/Capture5.png"><img class="aligncenter size-full wp-image-1154" title="factory.2.sequence.diagram" src="http://www.vainolo.com/wp-content/uploads/2012/05/Capture5.png" alt="" width="731" height="384" /></a></p>
<p>It turns out that to model the flow of operations I am actually modeling how polymorphism, late binding and all that stuff works. I guess that in this case, a sequence diagram is not only irrelevant, but actually confusing. But I had to try <img src='http://www.vainolo.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=519b0fb2-a826-4847-b4ca-26d398ba61b8" alt="Enhanced by Zemanta" /></a></div>
<p>Related posts:</p><ol>
<li><a href='http://www.vainolo.com/2012/04/29/singleton-design-pattern-sequence-diagram/' rel='bookmark' title='Singleton Design Pattern &#8211; Sequence Diagram'>Singleton Design Pattern &#8211; Sequence Diagram</a></li>
</ol>
<p><a href="http://feedads.g.doubleclick.net/~a/aw8SpeqhS4gqd4eo_GJ-LwNdMIo/0/da"><img src="http://feedads.g.doubleclick.net/~a/aw8SpeqhS4gqd4eo_GJ-LwNdMIo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/aw8SpeqhS4gqd4eo_GJ-LwNdMIo/1/da"><img src="http://feedads.g.doubleclick.net/~a/aw8SpeqhS4gqd4eo_GJ-LwNdMIo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/vainolo/categories/programming/~4/rxoraNw3INY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vainolo.com/2012/05/02/factory-method-design-pattern-uml-modeling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vainolo.com/2012/05/02/factory-method-design-pattern-uml-modeling/</feedburner:origLink></item>
		<item>
		<title>Creating an OPM GEF Editor – Part 21: Adding Keyboard Shortcuts</title>
		<link>http://feedproxy.google.com/~r/vainolo/categories/programming/~3/eKp0J1kZbu8/</link>
		<comments>http://www.vainolo.com/2012/04/30/creating-an-opm-gef-editor-part-21-adding-keyboard-shortcuts/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 10:37:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[GEF]]></category>
		<category><![CDATA[Keyboard shortcut]]></category>
		<category><![CDATA[opm]]></category>

		<guid isPermaLink="false">http://www.vainolo.com/?p=1126</guid>
		<description><![CDATA[Previous Tutorial: Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions Keyboard shortcuts are very useful for activating actions. There are many shortcuts that are common in all environments &#8211; and for better usability, enabling this shortcuts give the user a better user experience. In my case, I wanted to let the user edit the name of a thing (or state) using the F2 key, which is commonly used for this purpose. Adding keyboard shortcuts to GEF is fairly easy. You only need to define a &#60;code&#62;KeyHandler&#60;/code&#62; and attach it to the graphical viewer. &#8230; <a class="more-link" href="http://www.vainolo.com/2012/04/30/creating-an-opm-gef-editor-part-21-adding-keyboard-shortcuts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Previous Tutorial: <a title="Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions" href="http://www.vainolo.com/2011/09/19/creating-an-opm-gef-editor-%e2%80%93-part-20-creating-a-context-menu-and-adding-custom-actions/">Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions</a></p>
<p>Keyboard shortcuts are very useful for activating actions. There are many shortcuts that are common in all environments &#8211; and for better usability, enabling this shortcuts give the user a better user experience. In my case, I wanted to let the user edit the name of a thing (or state) using the F2 key, which is commonly used for this purpose.</p>
<p>Adding keyboard shortcuts to GEF is fairly easy. You only need to define a &lt;code&gt;KeyHandler&lt;/code&gt; and attach it to the graphical viewer. I defined two key shortcuts: <code>F2</code> for direct editing, and <code>F3</code> for my own <code>ResizeToContentsAction</code> action.</p>
<pre class="brush: java; first-line: 87; title: ; notranslate">
	private void configureKeyboardShortcuts() {
		getGraphicalViewer().getKeyHandler();&lt;br&gt; GraphicalViewerKeyHandler keyHandler = new GraphicalViewerKeyHandler(getGraphicalViewer())
		keyHandler.put(KeyStroke.getPressed(SWT.F2, 0), getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));
		keyHandler.put(KeyStroke.getPressed(SWT.F3, 0),
				getActionRegistry().getAction(ResizeToContentsAction.RESIZE_TO_CONTENTS_ID));
		getGraphicalViewer().setKeyHandler(keyHandler);
	}
</pre>
<p>This function is called from the <code>configureGraphicalViewer</code>. And that is all! You can find the source for this change in my github repository: <a href="https://github.com/vainolo/Object-Process-Programming">https://github.com/vainolo/Object-Process-Programming</a>.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=224f110a-e510-4a8c-a42e-468a3cbfce5a" alt="Enhanced by Zemanta" /></a></div>
<p>Related posts:</p><ol>
<li><a href='http://www.vainolo.com/2011/09/19/creating-an-opm-gef-editor-%e2%80%93-part-20-creating-a-context-menu-and-adding-custom-actions/' rel='bookmark' title='Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions'>Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions</a></li>
<li><a href='http://www.vainolo.com/2011/09/04/creating-an-opm-gef-editor-%e2%80%93-part-18-snapping-to-grid-and-to-geometry/' rel='bookmark' title='Creating an OPM GEF Editor – Part 18: Snapping to Grid and to Geometry'>Creating an OPM GEF Editor – Part 18: Snapping to Grid and to Geometry</a></li>
<li><a href='http://www.vainolo.com/2011/08/16/creating-an-opm-gef-editor-part-15-adding-structural-links/' rel='bookmark' title='Creating an OPM GEF Editor &#8211; Part 15: Adding Structural Links'>Creating an OPM GEF Editor &#8211; Part 15: Adding Structural Links</a></li>
</ol>
<p><a href="http://feedads.g.doubleclick.net/~a/Z-EKttCZTnRUnsNPA1Ot5VWxINs/0/da"><img src="http://feedads.g.doubleclick.net/~a/Z-EKttCZTnRUnsNPA1Ot5VWxINs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Z-EKttCZTnRUnsNPA1Ot5VWxINs/1/da"><img src="http://feedads.g.doubleclick.net/~a/Z-EKttCZTnRUnsNPA1Ot5VWxINs/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/vainolo/categories/programming/~4/eKp0J1kZbu8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vainolo.com/2012/04/30/creating-an-opm-gef-editor-part-21-adding-keyboard-shortcuts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vainolo.com/2012/04/30/creating-an-opm-gef-editor-part-21-adding-keyboard-shortcuts/</feedburner:origLink></item>
	</channel>
</rss>

