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

<channel>
	<title>Sergei Sizov - Software Engineer</title>
	<atom:link href="http://euromoby.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://euromoby.com</link>
	<description>Agile Software Development with Java and PHP</description>
	<lastBuildDate>Sun, 23 Aug 2009 23:31:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SystemTray Icon for Java Swing Application</title>
		<link>http://euromoby.com/2009/08/systemtray-icon-for-java-swing-application/</link>
		<comments>http://euromoby.com/2009/08/systemtray-icon-for-java-swing-application/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 23:23:51 +0000</pubDate>
		<dc:creator>Sergei Sizov</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://euromoby.com/?p=29</guid>
		<description><![CDATA[Sometimes we need our application to be launched hidden in background. Applications like Skype or ICQ are running in background and waiting for incoming messages. These applications both having their icon in the System Tray and you can easily activate them by double-clicking on their system tray icon. This tutorial shows how to create the [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need our application to be launched hidden in background. Applications like Skype or ICQ are running in background and waiting for incoming messages. These applications both having their icon in the <strong>System Tray</strong> and you can easily activate them by double-clicking on their system tray icon. This tutorial shows how to create the System Tray Icon for your <strong>Java Swing Application</strong>.</p>
<p><span id="more-29"></span>One problem, however, is that you need to have <strong>JRE 6</strong> installed. Previous versions of Java VM do not support System Tray.</p>
<h2>Creating SystemTray Icon</h2>
<p>Firstly, we need to have the following packages imported:</p>
<pre>import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;</pre>
<p>Let's declare a SystemTray variable</p>
<pre>TrayIcon trayIcon = null;</pre>
<p>Then we need to check if the SystemTray is supported.</p>
<pre>if (SystemTray.isSupported()) ...</pre>
<p>If it is, then we will create a new <strong>TrayIcon</strong>. To create the TrayIcon we need to specify the <strong>Icon</strong> (or you can use the standard Java Icon), application name and a PopupMenu to be shown on right click.</p>
<pre><span style="color: #339966;">// getting image to be used as icon for our TrayIcon</span></pre>
<pre>URL iconURL = getClass().getResource("/graphics/rss.png");
ImageIcon icon = new ImageIcon(iconURL);</pre>
<pre><span style="color: #339966;">// Creating popup menu to be shown on right click</span>
trayMenu = new PopupMenu();
<span style="color: #339966;">// Shows or hides main frame</span>
popupShowHide = new MenuItem("Show/Hide");
popupShowHide.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
   frame.setVisible(!frame.isVisible());
  }
});
 trayMenu.add(popupShowHide);
// close application
 MenuItem popupExit = new MenuItem("Exit");
 popupExit.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 closeApplication();
 }
 });
 trayMenu.add(popupExit);</pre>
<pre><span style="color: #339966;">// Create TrayIcon</span>
trayIcon = new TrayIcon(icon.getImage(), // Image
"My Application", // Caption Text
trayMenu // Popup Menu
);
<span style="color: #339966;">// shrink image to the size of the tray icon</span>
trayIcon.setImageAutoSize(true);

// instance of SystemTray
SystemTray systemTray = SystemTray.getSystemTray();

<span style="color: #339966;">// trying to add the TrayIcon</span>
try {
 systemTray.add(trayIcon);
} catch (AWTException ignore) {
 <span style="color: #339966;">// ignore</span>
}</pre>
<p>Also, you can catch the left double click on the TrayIcon. Say, for example you want to activate your application on left double click. Just add and ActionListener like this.</p>
<pre>trayIcon.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent event) {
  frame.setVisible(!frame.isVisible());
 }
});</pre>
<h2>SystemTray notifications</h2>
<p>SystemTray allows you to show notifications about some events that has occured.</p>
<pre>public void showInformation(String message) {

 trayIcon.displayMessage("My Application", <span style="color: #339966;">// Application Title</span>
 message, <span style="color: #339966;">// Message to be shown</span>
 MessageType.INFO <span style="color: #339966;">// Type of the message - INFO</span>
 );

}</pre>
<p>For more information visit java.sun.com website.</p>
]]></content:encoded>
			<wfw:commentRss>http://euromoby.com/2009/08/systemtray-icon-for-java-swing-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing XML with Java</title>
		<link>http://euromoby.com/2009/08/processing-xml-with-java/</link>
		<comments>http://euromoby.com/2009/08/processing-xml-with-java/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 23:44:52 +0000</pubDate>
		<dc:creator>Sergei Sizov</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://euromoby.com/?p=10</guid>
		<description><![CDATA[Short tutorial on how to read XML configuration files]]></description>
			<content:encoded><![CDATA[<p>The de-facto standard for computer communication is XML. Java provides good support for XML processing. This tutorial shows how to read XML configuration files with Java. The source code used in this tutorial is taken from my Java application <strong>SimpleRSS Reader</strong>. I use XML format to store structured text data and to save program configuration.</p>
<p><span id="more-10"></span></p>
<h2>Reading XML files</h2>
<p>Example XML file: channels.xml</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;channels&gt;
 &lt;channel&gt;
  &lt;url&gt;http://rss.dw-world.de/rdf/rss-en-world&lt;/url&gt;
  &lt;title&gt;Deutsche Welle: DW-WORLD.DE - World&lt;/title&gt;
  &lt;updatestatus&gt;OK&lt;/updatestatus&gt;
  &lt;updatetime&gt;29.7.2009 21:41:16&lt;/updatetime&gt;
 &lt;/channel&gt;
 &lt;channel&gt;
  &lt;url&gt;http://rss.dw-world.de/rdf/rss-en-eu&lt;/url&gt;
  &lt;title&gt;Deutsche Welle: DW-WORLD.DE - Europe&lt;/title&gt;
  &lt;updatestatus&gt;OK&lt;/updatestatus&gt;
  &lt;updatetime&gt;29.7.2009 21:41:15&lt;/updatetime&gt;
 &lt;/channel&gt;
&lt;/channels&gt;</pre>
<p>This is a configuration file that is used to store RSS channels. Each channel is represented by the <strong>Channel</strong> object that are stored in the channel<strong> ArrayList</strong>. When the application starts it parses the configuration file channels.xml and fills <strong>ArrayList&lt;Channel&gt;</strong> with <strong>Channel</strong> entities. This short tutorial is not about Collections, but there will be some extra code to make this example more interesting.</p>
<p>I use <strong>DocumentBuilder</strong> class to create <strong>DOM</strong> structure from XML. The DocumentBuilder is in <strong>javax.xml</strong> package, so to use it you need to have the following packages imported.</p>
<pre>import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;</pre>
<p>The following code parses an XML file and loads the configuration.</p>
<pre><span style="color: #339966;">// configuration XML file to store channels</span>
File channelsFile = new File(userHomeDir + fileSeparator + programDirName +
                    fileSeparator + "channels.xml");</pre>
<pre>try {</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// getting the instance of the DocumentBuilderFactory</span>
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
<span style="color: #339966;">// call factory method to get a new Document Builder instance</span>
DocumentBuilder db = dbf.newDocumentBuilder();</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// trying to parse the file</span>
Document doc = db.parse(channelsFile);
<span style="color: #339966;">// normalizing a file to merge all text nodes</span>
doc.getDocumentElement().normalize();</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// getting the list of &lt;channel&gt; nodes</span>
NodeList nodeList = doc.getElementsByTagName("channel");</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// loop through the node list</span>
for (int i = 0; i &lt; nodeList.getLength(); i++) {
<span style="color: #339966;">// getting a node</span>
Node channelNode = nodeList.item(i);</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// an element found?</span>
if (channelNode.getNodeType() == Node.ELEMENT_NODE) {</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// strings to store values</span>
String channelUrl = "";
String channelTitle = "";</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// convert node to element</span>
Element channelElement = (Element) channelNode;</pre>
<pre style="padding-left: 30px;">/<span style="color: #339966;">/ get Channel Url</span>
NodeList channelUrlList = channelElement.getElementsByTagName("url");
Element channelUrlElement = (Element) channelUrlList.item(0);
NodeList channelUrlTextList = channelUrlElement.getChildNodes();
channelUrl = ((Node) channelUrlTextList.item(0)).getNodeValue().trim();</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// getting the remaining parameters
// ...</span></pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// create a Channel instance</span>
Channel aChannel = new Channel(channelUrl);
<span style="color: #339966;">// configure the instance</span>
aChannel.setName(channelTitle);</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// add new Channel to ArrayList</span>
channelList.add(aChannel);</pre>
<pre>} catch (Exception e) {</pre>
<pre style="padding-left: 30px;"><span style="color: #339966;">// catch exceptions ...</span>
e.printStackTrace();</pre>
<pre>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://euromoby.com/2009/08/processing-xml-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
