17.7. Adding Contributions to the Status LineIn Chapter 6, you added a presence indicator to Hyperbola's status line. You may have noticed that as parts are activatedfor example, activating the Contacts view, then a chat editorthis status line indicator disappears. This happens because the indicator was added to the shared message area of the status line (refer to Chapter 6 for an overview of the areas in the status line). A better approach is to add the indicator to the contribution area so that it remains visible across part switches. Actions or contribution items can be added to the status line in the ActionBarAdvisor method fillStatusLine(). To maintain some stability in the status line, you should add contributions to pre-defined named groups that are defined on StatusLineManager such as BEGIN_GROUP, MIDDLE_GROUP, and END_GROUP. This enables aligning global contributions to the right or left of the contribution area. Status line contributions are very similar to the ControlContributions classes used in the last section. Instead of adding a control to a toolbar, they add controls to a compositethe status line is really just a fancy composite. The sample code for this chapter includes a custom contribution item called StatusLineContribution, which uses a CLabel control to show its contents. A CLabel is a custom SWT control that displays both an image and a label together. The fill(Composite) method in StatusLineContribution is called to add the contribution to the status line. You can add just about anything you like at this point. Here, we use two CLabels, the first to show a separator and the other to show an icon followed by text: org.eclipsercp.hyperbola/StatusLineContribution
public void fill(Composite parent) {
Label separator = new Label(parent, SWT.SEPARATOR);
label = new CLabel(parent, SWT.SHADOW_NONE);
GC gc = new GC(parent);
gc.setFont(parent.getFont());
FontMetrics fm = gc.getFontMetrics();
Point extent = gc.textExtent(text);
if (widthHint > 0)
widthHint = fm.getAverageCharWidth() * widthHint;
else
widthHint = extent.x;
heightHint = fm.getHeight();
gc.dispose();
StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
statusLineLayoutData.widthHint = widthHint;
statusLineLayoutData.heightHint = heightHint;
label.setLayoutData(statusLineLayoutData);
label.setText(text);
label.setImage(image);
...
}The StatusLineLayoutData is a special layout used to tell the status line how much room is needed for the contribution area. These areas cannot be resized dynamically, so you must commit to a size when they are created. To add the contribution to the status line, create the contribution in the ActionBarAdvisor method makeActions() and add it as follows: org.eclipsercp.hyperbola/ApplicationActionBarAdvisor
protected void fillStatusLine(IStatusLineManager statusLine) {
statusLine.add(statusContribution);
} |