Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Building Google gadgets, Part 2: Working with the user interface

Learn techniques for creating a compelling user experience.

John Muchow (Technical.tutorials@gmail.com), Writer, 自由职业者
John Muchow has been immersed in software development since 1988, working in roles from developer to chief software architect to CTO. Since 2000, John has focused on mobile technology, and during this time has had the opportunity to work alongside many high profile businesses including Nokia, Sony Ericsson, Symbian, Sprint/Nextel, Sun Microsystems, among others. He is also the author of Core J2ME Technology a best-selling wireless developer book that is still published in several foreign languages. John writes about mobile technology, from the developer perspective, on his blog 360Mobile.us.

Summary:  Part 2 of the Building Google gadgets series introduces the advanced features of gadgets, including creating a tabbed user interface, drag and drop, and MiniMessages, and gets you started creating your own.

View more content in this series

Date:  22 May 2007
Level:  Intermediate PDF:  A4 and Letter (388 KB | 35 pages)Get Adobe® Reader®

Activity:  10055 views
Comments:  

MiniMessages

MiniMessages offer a broad array of options for communicating with your gadget user. For instance, you can use messages to show status information (for example, "Please wait..."), error messages (for example, "You must select an option..."), or a prompt for an action (for example, "Click here to win...").

Message types

The three types of messages are: dismissible, static, and timer. Table 5 describes each of these types.


Table 5. MiniMessage types
Message typeDescription
DismissibleA message that the user can dismiss or close
StaticA message that can only be dismissed programmatically
TimerA message that is dismissed after a specified number of seconds

Although there are just three message types, as you'll see, they offer a great deal of flexibility when working with messages. You can use the MiniMessage library to include most any type of message and functionality in your gadget.

Includes

As with both tabs and drag-and-drop functionality, you need to include a reference to the appropriate library in order to use messages. Listing 6 shows the required library declaration, <Require feature="minimessage"/>.


Listing 6. MiniMessage library
                    
<?xml version="1.0" encoding="UTF-8" ?> 
 <Module>
   <ModulePrefs title="Message Example">
     <Require feature="minimessage" /> 
   </ModulePrefs>
...

MiniMessage functions

Five message functions are available to create and dismiss messages. Table 6 shows each, along with a short description.


Table 6. MiniMessage functions
FunctionDescription
_IG_MiniMessage(moduleId, opt_container)Constructor
createDismissibleMessage(msg, opt_callback)Creates a message the user can dismiss
createStaticMessage(msg)Creates a message that can only be dismissed programmatically
createTimerMessage(msg, seconds, opt_callback)Creates a message that is dismissed after a specified number of seconds
dismissMessage(msg)Used to dismiss a static message

MiniMessages code

The best way to get a feel for the flexibility and options when working with messages is to see a few examples. Listing 7 creates five different types of messages. Following the code listing are the corresponding screen shots, which are in turn followed by line-by-line descriptions of the code.


Listing 7. MiniMessage examples
                    
1 <?xml version="1.0" encoding="UTF-8" ?> 
2 <Module>
3   <ModulePrefs title="Message examples" height="100">
4     <Require feature="minimessage" /> 
5   </ModulePrefs>
6   <Content type="html">
7   <![CDATA[ 
8  
9     <script type="text/javascript">
10       
11     // Constructor
12     var msg = new _IG_MiniMessage(__MODULE_ID__);
13 
14     // User dismissible message
15     msg.createDismissibleMessage("You can dismiss this by clicking the X");
16 
17     // User dismissible message, with custom colors
18     var msg1 = msg.createDismissibleMessage("Custom message colors.");
19     msg1.style.backgroundColor = "white";
20     msg1.style.color = "blue";
21 
22     // Message that creates a callback when dismissed
23     msg.createDismissibleMessage("Generates a callback...try it.", msgAck);
24 
25     // A timed message, shown for 20 seconds
26     msg.createTimerMessage("Timed message...", 20);
27 
28     // Static message that is programmatically dismissed
29     var msg2 = msg.createStaticMessage("Message dismissed programmatically.");
30 
31     // Call back function
32     function msgAck()
33     {
34       alert("Message acknowledged. This message and the 
                static message will now be dismissed!");
35       msg.dismissMessage(msg2);
36     }
37     
38     </script>
39   ]]> 
40   </Content>
41 </Module>

Screenshots

Figure 13 shows all the messages on the gadget. Notice there is one timer message with the text "Timed message...".


Figure 13. All messages
All messages

Figure 14 shows nothing more than the timed message no longer visible on the gadget. Remember, no user acknowledgement is required in this case. Timed messages are handy for such things as brief, temporary reminders (for example, "Don't forget the TPS reports!").


Figure 14. Timer message dismissed
Timer message dismissed

To demonstrate both a callback and programmatically dismissing a message, you click on the "X" to dismiss the message titled "Generates a callback...try it." When you dismiss the message, a callback is generated, and the alert shown in Figure 15 is displayed. Within the callback, you can also programmatically dismiss the last (static) message on the display. For the end result, look ahead to Figure 16. You'll see the code for accomplishing all these steps in the next section.


Figure 15. Callback alert
Callback alert

Upon clicking OK in the alert message, the two messages on the bottom of the gadget are dismissed. Figure 16 shows the two user-dismissible messages remaining.


Figure 16. Static message removed
Static message removed

Initialization

Now take a moment to review the code, starting at line 12. As when working with tabs and the drag-and-drop libraries, your first step is to instantiate a new object. With the variable msg now referencing a MiniMessage object, you can proceed to create various types of messages.

Creating messages

You begin by creating a user-dismissible message on line 15. The only parameter is the text to be displayed. You follow this message with another user-dismissible message. However, this time you customize the text foreground and background colors (see lines 18-20).

Line 23 introduces a user-dismissible message with a callback. Once the user opts to dismiss the message, the callback function msgAck() is invoked -- more on this function in a minute. Line 26 is a timer message; the second parameter specifies that the text will be displayed for 20 seconds.

The last message on line 29 is a static message, which by definition, is a message that can only be dismissed programmatically. Continue on to see how this message is handled.

Callback

Two things are left to address. First is the callback function referenced on line 23. Second, you need to add code to programmatically dismiss the static message declared on line 29. The function msgAck() handles both cases for you. msgAck() is called when the user dismisses the message defined on line 23. Inside this same function, you dismiss the static message through a call to msg.dismissMessage(msg2). At this point, there are just two messages remaining on the display, as shown in Figure 16.

If you'd like to learn more about working with messages, including code samples of additional message options, check out the Google Gadgets API Developer Guide. Please refer to the Resources section for a link to MiniMessage reference material.

9 of 13 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development, XML, Java technology
ArticleID=223552
TutorialTitle=Building Google gadgets, Part 2: Working with the user interface
publish-date=05222007
author1-email=Technical.tutorials@gmail.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.