Text and Dynamic Text

Text components are similar to Label components. They provide much more flexibility than Labels, in that they allow HTML formatting inside of the definition for the text. They also allow for some special tags that will display data from a JavaScript environment. This becomes important if we want to add AJAX or dynamic DOM manipulation to our report. It is even possible to put in images without using the BIRT Image component and anchor tags.

Text and Dynamic Text

For the following example we will build on to our Hello World report by adding in a list that will be formatted with HTML. The items will be Item 1, Item 2, and Item 3 respectively. Item 1 will be set to bold using HTML, whereas Item 2 and Item 3 will be left alone. For the purpose of this example, you do not need to be familiar with HTML as I will walk you through the steps. A detailed tutorial on HTML is beyond of the scope of this book.

  1. Open HelloWorld.rptdesign from the Classic_Cars_BIRT_Reports project.
  2. From the Palette, drag a Text object over to the Report Design pane, just below the Hello World label. The Text element icon is illustrated at the beginning of this section.
    Text and Dynamic Text
  3. The Edit Text Item dialog box will pop up. Here we can edit the type of text we want to appear, control the HTML formatting of the text, and get access to quick menus that will allow us to see a small sample of HTML code we can embed in a text object. At the very top of the dialog box, change the type of text from Auto to HTML.
    Text and Dynamic Text
  4. Go ahead and play around with the different types of formatting menus here; a drop-down menu with the name Formatting appears. Change the option from Formatting to Lists. Press the<DL> button that appears to the right of the drop-down list. We can see the editor will automatically create both the opening and closing tags for the list.
    Text and Dynamic Text
  5. Now click on the <LI> button. Do this three times. In between the<LI> and</LI> tags, put the following labels in order:
    • Item 1
    • Item 2
    • Item 3
    Text and Dynamic Text
  6. Now, using mouse, highlight the Item 1 text between the first<LI> and</LI> tags. With Item 1 highlighted, click on the<B> button at the top of the Edit Text Item dialog. This will automatically surround the Item 1 text with the tags<B> and</B>.
    Text and Dynamic Text
  7. Move the cursor below the</DL> tag. Select the Dynamic Text tag from the drop-down list. Click on the<VALUE-OF> button, which will insert a<VALUE-OF> and</VALUE-OF> tag where the cursor is. The VALUE-OF tag is a special BIRT tag that will insert dynamic text based on any number of values such as JavaScript results or data values at the time of the report generation.
    Text and Dynamic Text
  8. In between the<VALUE-OF> and</VALUE-OF> tags, type in new Date();. This is a small piece of JavaScript code that will retrieve the current date.
  9. We will also want to format this date. Move the mouse cursor to inside of the first<VALUE-OF> tag, just to the right of VALUE-OF.
  10. Now under the<VALUE-OF> tag on the top, with the drop-down box next to it, open the drop-down box and select Format DateTime.
    Text and Dynamic Text
  11. Select the date format that reads "MMM DD, YYYY".
    Text and Dynamic Text
  12. Click OK.
  13. We will see that a format was inserted next to the VALUE-OF tag. Go ahead and click OK.
    Text and Dynamic Text
  14. Now click Preview. We can see that the Item 1 text is now in bold, and the place where the VALUE-OF tag was located now contains the current date and time.
Text and Dynamic Text

We can go back and change the formatting to use any standard Java formatting expression. In the example, instead of using the format dialog, we can change it using the format="YYYY-MM-DD", VALUE-OF tag attribute in the Edit dialog, and the date will be displayed as shown in the following screenshot:

Text and Dynamic Text

We will notice two different date formats. One is from our VALUE-OF expression. The other is located in an area called the Master Page, which we will discuss later.

Of course, with some understanding of HTML there is a lot more we can do with this. The VALUE-OF tag also gives us a lot of flexibility once data becomes part of the picture, which we will see later on. With a little understanding of JavaScript, we can make some really dynamic things happen. With the combination of HTML and JavaScript, we can add some report interactivity.

The Dynamic Text component is very similar to the Text component using the VALUE-OF tag, except that it uses JavaScript, BIRT report objects, and CLOBS to display its text. For the time being, we needn't go into much detail about this as it will be covered in depth in the Practical Example building reports for Bugzilla chapter. For now we will go through a simple exercise to demonstrate how the Dynamic Text component can be used with JavaScript. In the following exercise, we will take a simple phrase and, using the JavaScript function to convert to upper case, we will display the text in all caps. A discussion on JavaScript and JavaScript programming is outside the scope of this book. For this exercise, one needn't know JavaScript; one just needs to understand that we are taking a string and converting it to uppercase.

  1. Open the HelloWorld.rptdesign from the Classic_Cars_ BIRT_Reports project.
  2. From the Palette, drag a Dynamic Text object to the Report Design pane, just below the Hello World label or the Text object from the previous exercise.
    Text and Dynamic Text
  3. The Expression Builder will pop up.
    Text and Dynamic Text
  4. We will notice this looks different than the Edit Text Item dialog from the previous exercise. The Expression Builder is a dialog that will be used a lot in BIRT. It is used to edit small, short JavaScript expressions that will usually return a single value. When we cover scripting in more depth, we will discuss the differences between the Expression Editor and the Script Editor Pane used in the Report Designer. Type in the following text, including the quotes: "Good-Bye World".
    Text and Dynamic Text
  5. At the bottom of the Expression Editor, we will notice three different list boxes. These are used as a quick reference for JavaScript objects and methods that can be used in an expression. Put a period at the end of the "Good-Bye World" text. Then, under the Category list box at the bottom of the Expression Editor, choose Native JavaScript Functions.
  6. A whole list of items will appear under the Sub-Category listbox. These are types of objects and methods we can work with. As "Good-Bye World" is a text string, select the String item.
    Text and Dynamic Text
  7. Now we have a list of methods we can use with the string "Good-Bye World". Make sure the cursor is after the period that was added after the "Good-Bye World" text. In the third list box, find and double-click on the toUpperCase() : String item. When we do that, we will notice that the method will get added to the place where the cursor is located.
    Text and Dynamic Text
  8. Click OK.
  9. Preview the report.
Text and Dynamic Text

In the finished report, the string "Good-Bye World" now reads GOOD-BYE WORLD. This is a useful component when one needs to perform operations on data values, perform calculations, and do a few other things that would require dynamic, calculation-driven text. There are properties of components that can do similar things such as Maps and Highlights that can perform dynamic text substitutions, enumerate data values, and change formatting of text based on certain conditions. We will discuss these elements in depth later in the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset