|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
DataWindows Highlighting Selected Text in PowerBuilder DataWindow
Techniques for simulating text formatting
Aug. 17, 2006 08:45 AM
Let's suppose that you have a search window where a user can enter a search string and get the list of text items matching this pattern. Ideally, the portions of text that meet the search criteria should be highlighted, for example, by text formatting. It could look like Figure 1.
A Sample DataWindow Then we can either add some text data in our DataWindow through the Rows/Data menu command in the DataWindow Painter, or load data from some external source in script.
Selecting Part of the Text If there are three rows in the DataWindow:
PowerBuilder rules the world. And the string pattern is the 'rule,' we want our rows to look like this:
PowerBuilder rules the world. We can't just highlight a portion of the DataWindow column, but we can break the column into three parts: a part before the selection, the selection, and the part after the selection. Let's add another string columnsearch to the result set of our DataWindow to store the string pattern. Instead of the original column text, add three computed fields. The expression for the first computed field is:
if( isNull(search), text, Let's call it "prefix". The expression for the second computed field is: if(pos(text, search, 1) = 0, '', search) Let's call it "matched." The expression for the third computed field is:
if( isNull(search), '', We call it "suffix." Put them side-by-side and size them to the width of original text column. Set the font color for the "matched" to red. Now you can delete the text column. As a result we get the picture shown in Figure 3 in preview mode. Well done, they're red! But there are nasty gaps between our computed fields and they don't look like one DataWindow column. To properly size and place our computed fields we check on the Slide left property for the matched and suffix computed fields (click the left mouse button on a computed field, Properties, Position tab, Slide groupbox, check the left checkbox). Put your DataWindow in preview: And here it is - text formatting inside a DataWindow column! So, we have simulated the selection of a portion of text in a DataWindow item just by adding an extra column to the DataWindow result set, creating three computed fields instead of the original column. No script at all! (see Figure 4)
Selecting Several Parts mid( text, , 1) Then we'll be able to select any characters of the text column by setting the font color of the desired computed fields. We could get a string such as: PowerBuilder rules the world. To properly set the font color for our computed fields we can use a technique described by Buck Woolley in his article "Not Your Father's DataWindow" (PBDJ volume 8 issue 7). In short, the essence of this technique is packing parameters of many DataWindow objects in string columns. In our case we have one such parameter for every computed field: selected or not. In our example DataWindow the length of the text column is 50 characters so we'll have 50 computed fields. Let's add a string column called "select" to the result set of our DataWindow where the packed parameters will be placed. It also has a length of 50. For the colorful text mentioned above, the "select" column will contain the string: "0101010101010010100101001010000000<...>" and so on to the end of string. OK, so far so good, but how can we create our computed fields? Can you imagine creating 50 (or even more) computed fields manually? This would be madness! Well, it's time for coding.
Selection Service Object Second, we need a function that selects the given portion of the text. The arguments for this function are the number of the first selected character, the length of the selection, and the row number in the DataWindow. Let's call it of_select(). The code for this function is shown in Listing 2. Finally, we need a function that clears a selection in a particular row. The code for this function of_clear() is shown in listing 3.
Conclusion Reader Feedback: Page 1 of 1
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||