Fun with StyledText

So, in order to make a label on a GUI more interesting, I decided to use SWT’s StyledText. To start off with, I had a look at this tutorial:
Getting Your Feet Wet With the StyledText Widget

It certainly is more flexible than a normal label, but I wasn’t sure about the requirement to specify the character ranges within your text to which a particular style range should be applied.

I decided that, instead of creating one large StyledText widget and working out which text ranges within it should have which StyleRange associated with them, I would instead create an overall container composite, and populate it with multiple StyledTexts. Each StyledText would have a particular StyleRange applied to the whole of its text.

In order to help with the creation of multiple StyledTexts, I created a factory class. I had a requirement for a bold, blue header font, and a normal black body font. My factory class looked as follows:

private static StyleRange createHeaderStyleRange(Display display)  {  
StyleRange styleRange = new StyleRange();  
styleRange.foreground = display.getSystemColor(SWT.COLOR_BLUE);  
styleRange.fontStyle = SWT.BOLD;  
return styleRange;
}

public static StyledText createHeaderText(Display display, Composite parent, String text)  {  
StyledText styledText = new StyledText(parent, SWT.NONE | SWT.WRAP);  
if (text == null)  {    
  text = "";  
}  
styledText.setText(text);  
StyleRange headerStyleRange = createHeaderStyleRange(display);  
headerStyleRange.start = 0;  
headerStyleRange.length = text.length();  
styledText.setStyleRange(headerStyleRange);  
return styledText;
}

// Similar for a body text, but without the need to set a style range

So yeah, it worked for me. I’m a newbie to this control though, so if people know of better strategies for using StyleText, please let me know. 🙂

Advertisements

Sorting JFace table viewers by column

As with a lot of RCP topics, the Vogella tutorial on JFace table viewers is highly reccommended. The advanced section is found at http://www.vogella.de/articles/EclipseJFaceTableAdvanced/article.html.

The task which I had to tackle was sorting a table viewer based on the values in one of its columns. I followed the approach detailed in the Vogella tutorial. This entailed two main steps:

1) Define your own ViewerComparator. I adapted the MyViewerComparator class defined in the Vogella tutorial. The viewer comparator has methods on its interface to get the current sort direction, and to get/set the column with which we will carry out the sort.

2) Add a selection adaptor to the table viewer. In the selection adaptor, you get the column on which to sort the table, tell the viewer to carry out the sort, then refresh it.
Sample code from the tutorial to do this is:

SelectionAdapter selectionAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {  
  comparator.setColumn(index);  
  int dir = comparator.getDirection();  
  viewer.getTable().setSortDirection(dir);  
  viewer.refresh();
}

Setting SSL parameters for a SSL client

So I have a client application which needs to connect to an authenticated server. The authentication is SSL using certificates, so I need to specify the trust store and key store parameters with which I will connect. On my machine, I have the trust store and key store certificates which I can use to authenticate my identity.

I found the easiest way of setting these parameters for my client was to set the necessary parameters on my client’s VM. I did this as follows:

  System.setProperty("javax.net.ssl.truststore", truststore_location);  
  System.setProperty("javax.net.ssl.trustStoreType", type_of_truststore);  
  System.setProperty("javax.net.ssl.trustStorePassword", truststore_password);

  System.setProperty("javax.net.ssl.keyStore", keystore_location);  
  System.setProperty("javax.net.ssl.keyStoreType", type_of_keystore);  
  System.setProperty("javax.net.ssl.keyStorePassword", keystore_password);

Having set the properties on the client VM, it was then correctly set up to log in to the server.