Tuesday, November 25, 2008

How to add user preferences to your Google Gadget

In the previous post, we saw how to create a simple Google Gadget that reads the CNN.com Top Stories feeds and display them. We make it by default to display only 10 stories and to display the summaries, but what about if I want to see some more stories or less? or if I don't want to the see the summary, only the story headline? Well, there is a way for the user to customize the gadget , we just need to use User Preferences.

With user preferences we can set information in the gadget that is specific to the user. To have user preferences you just need to specify them inside the Module tag of the gadget. See the following example:

<UserPref name="show_summ" display_name="Show Summaries?" datatype="bool" default_value="false" required="true"/>


As you can see what you need to specify is the name of the preference, the display name, the data type, the default value and if it is required or not. The data type, the default value and required attributes are optional. The default value for data type is string and the default value for required is false.

Let's see how our CNN.com Top Stories gadget would look with preferences that will allow the user to set how many stories wants to see and if he/she wants to see the summary or not.
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs
title="CNN.com Top Stories"
title_url="http://www.cnn.com/"
author="Your name"
author_email="youremail@domain.com"
description="Gadget to see the current CNN.com top stories."
width="450"
height="350"
scrolling="true">
<Require feature="dynamic-height"/>
</ModulePrefs>
<UserPref name="num_entries" display_name="Number of Entries:" default_value="5"/>
<UserPref name="show_summ" display_name="Show Summaries?" datatype="bool" default_value="false"/>

<Content type="html">
<![CDATA[
<style>
div#main_container {
font-size:12px;
font-family:Arial;
margin: 5px;
text-align: left;
text-decoration: none;
}

div#main_container h2 {
background: url(http://i2.cdn.turner.com/cnn/.element/img/1.0/logo/cnn.logo.rss.gif) no-repeat left top;
display: block;
height: 33px;
text-indent: -100000;
width: 144px;
}

</style>
<script type="text/javascript">

// Get userprefs
var prefs = new _IG_Prefs();
var summary = prefs.getBool("show_summ");
var entries = prefs.getInt("num_entries");

// Function to dipslay content retrieved from XML feed
function displayContent() {

// If user wants to display more than 50 entries, display an error
// and set the value to 50, the max allowed.
if (entries > 50)
{
alert("You cannot display more than 50 entries.");
entries = 50;
}

var url = "http://rss.cnn.com/rss/edition.rss";

// Use the _IG_FetchFeedAsJSON() function to retrieve core feed data from
// the specified URL. Then combine the data with HTML markup for display in
// the gadget.
_IG_FetchFeedAsJSON(url, processFeed, entries, summary);

}

// Function to process the feed
function processFeed(feed) {
var contentDiv = _gel("main_container");

if (feed == null) {
contentDiv.innerHTML = "Invalid data.";
return;
}

// Start building HTML string that will be displayed in gadget.
var html = "";

// Access the fields in the feed
html += "<h2>" + feed.Title + "</h2>";
html += "<div>" + feed.Description + "</div><br>";

// Access the data for a given entry
if (feed.Entry) {
for (var i = 0; i < feed.Entry.length; i++) {
html += "<div>" + "<a target='_blank' href='" + feed.Entry[i].Link + "'>" + feed.Entry[i].Title + "</a> ";
// The feed entry Date field contains the timestamp in seconds
// since Jan. 1, 1970. To convert it to the milliseconds needed
// to initialize the JavaScript Date object with the correct date,
// multiply by 1000.
var milliseconds = (feed.Entry[i].Date) * 1000;
var date = new Date(milliseconds);
html += date.toLocaleDateString();
html += " ";
html += date.toLocaleTimeString();
if (summary == true) {
html += "<br><i>" + feed.Entry[i].Summary + "</i>";
}

html += "</div>";
}
}

contentDiv.innerHTML = html;

// Adjust gadget height according to contents
_IG_AdjustIFrameHeight();

}

_IG_RegisterOnloadHandler(displayContent);


</script>

<div id="main_container"></div>

]]>
</Content>
</Module>

As you saw in the code we need the following code to retrieve the user preferences:

var prefs = new _IG_Prefs();
var summary = prefs.getBool("show_summ");
var entries = prefs.getInt("num_entries");


There is a "get" method for each data type available. To get more information about user preferences click here.

Allowing the user to set up its preferences is really simple and it adds a lot value to the gadget functionality!

Friday, November 21, 2008

How to create a Google Gadget

Google Gadgets are a great tool, they allow you to have specific information in, what you could call, a small HTML page. You can export your gadgets to iGoogle, Google Desktop, Google Toolbar, to any web page and even you can create a Windows Vista Gadget based on them, isn't that great?

In this post, I am going to walk you through the creation of a simple gadget, but first let's talk about what Google Gadgets are.

Google gadgets are small pieces of code created using XML, HTML, CSS and Javascript that allow you to display data. These gadgets can be embedded in certain applications allowing you to export your information more easily.

They are really easy to create, you just need basic knowledge of HTML and JavaScript and Google provides you with JavaScript APIs to you make your life easier, you can check them in their Legacy Gadgets API Developer's Guide

One of the greatest advantages about a gadget is that they can be used in so many places, but you manage only one source code, so if you need to apply a change to your gadget, you just do it and then all the places were the gadget is used would be updated with no new installation or update required.

Before we start coding, let's talk about a little bit about the gadget components. A Google Gadget is a XML file that contains one big tag called Module that encloses the gadget, then we have two main different components, the module preferences and the gadget content.

The module preferences are tags that you use to set the gadget information like title, author, width, height, etc. In our example, you will see this tag <Require feature="dynamic-height"/>, this tells the gadget parser that the height should be dynamically changed depending on the size of the content. This feature only works with iGoogle though.

The gadget content is where you do all the work, here you need to specify a type, mainly "html" if you want to have your HTML and JavaScript in your gadget or "url" if you want the gadget content to live on a remote web page referenced by a URL in the gadget spec. Inside this tag you can code JavaScript and HTML to layout your data, and CSS if you want to give some style to your content. You need to enclose your code into the tags ![CDATA[ and ]].

For this post, we are going to create a simple gadget that reads the CNN.com top stories feed and display them using the API called _IG_FetchFeedAsJSON to read the feed as JSON. You will see that this code is really simple and easy to follow.

To work with Google gadgets you need a proper editor, you could use notepad if you want at first, but then you would need a way to publish it. Google already provides a tool called Google Gadget Editor (GGE) which is a gadget itself, so you can embedded it in your iGoogle, which allows you to edit your gadget and preview it in the same editor if want to. You can add it to your iGoogle by clicking here.

Now that you have the GGE in your iGoogle page, copy the following code into your GGE.

<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs
title="CNN.com Top Stories"
title_url="http://www.cnn.com/"
author="Your name"
author_email="youremail@domain.com"
description="Gadget to see the current CNN.com top stories."
width="450"
height="350"
scrolling="true">
<Require feature="dynamic-height"/>
</ModulePrefs>

<Content type="html">
<![CDATA[
<style>
div#main_container {
font-size:12px;
font-family:Arial;
margin: 5px;
text-align: left;
text-decoration: none;
}

div#main_container h2 {
background: url(http://i2.cdn.turner.com/cnn/.element/img/1.0/logo/cnn.logo.rss.gif) no-repeat left top;
display: block;
height: 33px;
text-indent: -100000;
width: 144px;
}
</style>

<script type="text/javascript">

// Function to dipslay content retrieved from XML feed
function displayContent() {

var url = "http://rss.cnn.com/rss/edition.rss";
// Use the _IG_FetchFeedAsJSON() function to retrieve core feed data from
// the specified URL. Then combine the data with HTML markup for display in
// the gadget.
// Let's say we want to display only 10 entries and that we want to display the summary
var entries = 10;
var summary = true;
_IG_FetchFeedAsJSON(url, processFeed, entries, summary);
}

// Function to process the feed
function processFeed(feed) {
var contentDiv = _gel("main_container");

if (feed == null) {
contentDiv.innerHTML = "Invalid data.";
return;
}

// Start building HTML string that will be displayed in gadget.
var html = "";

// Access the fields in the feed
html += "<h2>" + feed.Title + "</h2>";
html += "<div>" + feed.Description + "</div><br>";

// Access the data for a given entry
if (feed.Entry) {
for (var i = 0; i < feed.Entry.length; i++) {
html += "<div>" + "<a target='_blank' href='" + feed.Entry[i].Link + "'>" + feed.Entry[i].Title + "</a> ";
// The feed entry Date field contains the timestamp in seconds
// since Jan. 1, 1970. To convert it to the milliseconds needed
// to initialize the JavaScript Date object with the correct date,
// multiply by 1000.
var milliseconds = (feed.Entry[i].Date) * 1000;
var date = new Date(milliseconds);
html += date.toLocaleDateString();
html += " ";
html += date.toLocaleTimeString();
html += "<br><i>" + feed.Entry[i].Summary + "</i>";
html += "</div>";
}
}
// Set the html into the main container
contentDiv.innerHTML = html;

// Adjust gadget height according to contents
_IG_AdjustIFrameHeight();
}
// Register the function displayContent in the onLoad handler so it is called when the gadget is first loaded
_IG_RegisterOnloadHandler(displayContent);

</script>

<div id="main_container"></div>

]]>
</Content>
</Module>

As you can see in the code, the function _IG_FetchFeedAsJSON receives as parameters the feed URL and the name of the callback function in charge of processing and displaying the data.

In the callback function what we do first is to check if the JSON response we received is valid, if it isn't then we just display a message saying that the data is invalid. If the response is valid then we go through the JSON properties to retrieve the data and display it using HTML and CSS.

As you can see this particular API is really easy to use and understand, you just need some basic knowledge of Javascript and that is it!

Now that you have the code in your GGE, you can publish it to your iGoogle so you can see how the dynamic height works depending on the content. Also you can publish it to a web page. Just click on the File menu and select the Publish option. You will get a warning saying that we did not set the Thumbnail preference, but that is not going to affect the code itself, since the Thumbnail is used just to display the gadget Thumbnail in the Google Gadget Directory, so people know how the gadget looks like.

I hope this helps! Post your comments and/or questions!

Monday, April 14, 2008

Working with Enumerations in Java

An enumeration type is a Java type, which all values for the type are known when the type is defined. It is a special kind of class, with an instance that represents each value of the enumeration.

An instance of the enum class is called an enum constant, we will be referring to this term along the article.

There are some interesting things a developer must know when working with enums:

  • When declaring an enumeration, it is like declaring a class but with two exceptions:
    - The keyword enum is used instead of class,
    - Before declaring any class members, an enum must declare first all its enum constants.

  • All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

  • All enum are implicitly Serializable and Comparable.

  • Enums can have fields, methods and nested types, including nested enums.

  • Two static methods are automatically generated by the compiler:
    - values: returns an array of the enum constansts
    - valueOf(String name): returns the enum constant for a given name.

  • An enum type is not allowed to override the finalize method from Object.

  • Enum instances may never be finalized.

  • Nested enums can have any access modifier, while a top-level enum is public or package accessible.

  • Enums are implicitly static.

  • An enum cannot be declared abstract, but can declare abstract methods. Also, it cannot be declared final.

  • An enum constant declaration cannot have modifiers applied to it, except for annotations.

  • Restrictions of enum constructors:
    - All enum constructors are private.
    - An enum constructor cannot explicitly invoke a superclass constructor.
    - An enum constructor cannot use a non-constant static field of the enum.

  • Enum constants can have constant-specific behavior.

  • Some useful properties are set for enums:
    - The method clone is overriden to be declared final and to throw CloneNotSupported Exception.
    - The hasCode and equals method are overriden to be declared final.
    - The compareTo method is implemented and defined so that enum constants have a natural ordering based on their order of declaration (the first declared enum constant has the lowest position in the ordering).

  • The toString method returns the name of the enum constant.


When using an enum will depend on how sophisticated is the behavior of type, because if the behavior is too sophisticated, it is more likely the application might need to specialize that behavior. Enums are suggested for simple types like card suits or the days of the week.

Friday, February 22, 2008

Professional Networking

Summary

“Many of the successful people in the world say that they owe their success not only to their own skills but to all the people that help them achieve their goals, others say that it is not enough being smart, you have to be surrounded by smart people. Given this, you can know how important is to have the right people near you, keeping a good network of friends, partners and clients can help achieve your goals faster and more efficiently. Nowadays there are lots of tools to help you manage your networks, you could take advantage of several options that they offer to their users. We are going to focus on these advantages and in the importance of having a solid professional network.”

Keywords: professional networks, potential customers, colleagues, friends, profile, business, visibility.

1 Introduction

Recently, the number of sites dedicated to social networking has been growing. Sites like Hi5 (http://www.hi5.com/) and Facebook (http://www.facebook.com/) have become really popular between Internet users of all ages, popularity that grows thanks to the fact that people can keep in touch with friends, meet new people, share pictures, hobbies and stories among other things.

Along with social networking sites, the popularity of sites for professional networking has been growing as well; one example is LinkedIn (http://www.linkedin.com), a professional networking site with thousands of users from all over the world.

This type of sites have all the same advantages as social networking sites, but adding their own features and focusing mainly in professional people who wants to do business and keep in touch with colleagues and potential customers.

In this article, we would be focusing on the main advantages of being part of a professional network and how this type of network can help your career.

2 Main Advantages

A professional network offers a lot of advantages to all the people who are part of it and now you can find several sites in the Internet that work as tools, allowing professionals to create their networks easily.

Some of the main advantages and features these sites offer are explained as follow.

2.1 Keep in touch

The biggest advantage of a professional network site is that users can easily keep in touch with friends, colleagues or partners.

Keep in touch with all the people you have known along your career is sometimes impossible, addresses and phone numbers change. Lost contact is sometimes inevitable, so having a site that gives you the ability to connect with those you lost contact over time is just a great advantage.

On the other hand, you can keep your contact list in one place, knowing that it always will be up-to-date, since each contact information is updated by the actual contact letting you have reliable information almost all the time.

“Relationships are meaningful, in every aspect of life including career management. Networking is the key to discovery of our career selves and you never know, you might just make some friends and continue to find your path.” Katie Metcalfe (LinkedIn user)

2.2 Become visible

Publishing your profile, having it up-to-date with relevant information about your education, experience and skills will make you visible to your network. Colleagues, classmates and partners will be able to reconnect with you and people that don’t know you will be able to get to know you better and even get interested in what you have to offer.

Having a well-built profile will allow you to get job offers and business opportunities allowing you to grow your network and your business.

Your profile will help you to promote yourself, to market your experience and brand your business.

2.3 Contact potential customers

If you own a business with services to offer, you can use your network to promote your services and find potential customers.

In a professional network site you can contact potential customers directly or by getting recommended by your actual customers or partners, getting a lot of benefits from something as easy as that.

“I have always been taught when selling to try to find common ground with potential customer. Haveing a business network in common can be a big help.” Louis D'Esposito (LinkedIn user)

2.4 Share ideas

Most of these sites offer features that allow their users to share ideas or things that other people might find interesting.

Some have forums, blogs or question and answers modules promoting the transfer of knowledge. These features allow their users to contact people who are experts in certain areas or even become experts themselves by sharing their answers and knowledge with others, winning recognition for their expertise.

“The knowledge that is freely shared both on the site and between connections off site is priceless. We can reach out to an almost unlimited number of great minds and share ideas, solve problems and do business.” Sheilah Etheridge (LinkedIn user)

When professionals publish their articles or answer questions related to their fields, they are demonstrating that they have experience and expertise in a certain area, allowing others to get to know them and even maybe becoming a “go to guy” in that specific area creating a lot of opportunities for themselves and why not?, helping others with their knowledge.

2.5 Connect with international professionals

“To me, the biggest advantage is making international contacts that I otherwise wouldn't make. I appreciate the diverse insights and perspectives, and who knows how these contacts might pay off in the future? In business and in networking, there's rarely such a thing as incidental contact. Relationships are meaningful.” Tom Field (LinkedIn user)

Since these sites are based on the Internet, they are accessible to everybody in the world, creating a network of a big number of users that you have access to. This gives the advantage that one user can easily connect to another no matter if the other user lives in a country in the other side of the world. Also, it allows getting different experiences from different professionals around the globe.

2.6 Recommendations

Some of the professional networking sites offer the possibility of managing recommendations; users can give recommendations to their connections or get recommended.

This type of features gives the user the chance to help others by giving their recommendations, which can be accounted on looking for a new job, a new business partner or a potential customer since recommendations add more completion and credibility to your profile.

2.7 Hiring people

Since users of professional networking sites manage a public profile where they describe their education, experience and skills, recruiters can use these sites to look for potential candidates for the job positions they are offering.

The good thing is that these recruiters can use the site to contact these potential candidates directly or through their connections. In addition to this, since these sites are used by a lot of people around the world, recruiters have access to a bigger number of potential candidates.

Besides, if the users have a completed profile, just by reading their profiles, recruiters can get to know their potential candidates before even contact them, sometimes saving time in phone calls and emails.

2.8 Finding a job

Some professional networking sites offer the opportunity for companies to promote their business and to public their job openings allowing professionals to apply to these jobs directly in the site.

Having a completed profile and several recommendations will give the professional more chances to be selected for a job. You could be contacted directly by the recruiter; you might have been recommended by someone or be discovered through your participation in forums or blogs. It is all a matter of how much effort you put when you publish your profile or how much advantage you take of the features the site offers.

2.9 Quality of business

This type of sites let you get leads on possible customers that are actually looking for a product or service that you could provide. Most of the time, these customers are referred by someone you know well, someone who is a mutual acquaintance of you and the prospect customer, which is something good for your business because it lets you save time in looking for new clients, spending less money in advertising and getting more profit since you are not investing resources in a prospect that is less probable would do business with you.

3 Conclusions

Professional networking sites are great tools to keep in touch with colleagues, clients and partners, since they offer a lot of advantages that make things easier inside a network. However the use you do of these features is completely up to you. You can be an active user that takes advantage of all features and make them work for you by branding yourself, promoting your business and getting recognized for your expertise or you can be a user with an active account that is never updated and therefore not visible inside the network.

Professional networking sites can create a lot of opportunities for you if you know how to take advantage of them. These opportunities can be used in the present or in the future, the bigger your network the bigger the number of opportunities you will have.

"Knowing the right people" is one of the keys to success in today's world, especially because you need to work with partners to grow your business more quickly, so being part of a well developed and broad professional network is more important than ever. Keeping a large number of "business friends" is critical for your career, therefore the importance of professional networking tools.

If you are an entrepreneur promoting your services or someone looking for a new job, these tools can really help you achieve your goals, but it is always up to you.