Bug 297054 [Contributions] extension of org.eclipse.jface.action.StatusLineContributionItem that should not "guess" contribution width
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729) Build Identifier: 20090920-1017 Current implementation of org.eclipse.jface.action.StatusLineContributionItem uses a DEFAULT_CHAR_WIDTH to "guess" the needed size (which will fail e.g. if many uppercase letters are used). Also, the size is fix once the instance is created. A much better and more dynamic implementation, however, would be a Contribution item that calculates its needed size based on the actual text contained (thus, grabbing exactly the size it needs for the text). This can be easily achieved by creating a new Contribution Item class that differs from StatusLineContributionItem in just the fill method. Below is a suggestion for the new method: public void fill(Composite parent) { statusLine = parent; Label sep = new Label(parent, SWT.SEPARATOR); label = new CLabel(statusLine, SWT.SHADOW_NONE); label.setText(text); // compute the size of the label to get the width hint for the contribution int widthHint = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; StatusLineLayoutData data = new StatusLineLayoutData(); data.widthHint = widthHint; label.setLayoutData(data); data = new StatusLineLayoutData(); data.heightHint = heightHint; sep.setLayoutData(data); } Reproducible: Always
bundles/org.eclipse.jface/src/org/eclipse/jface/action/StatusLineContributionItem.java
this(id, DEFAULT_CHAR_WIDTH);
	}
	/**
	 * Creates a status line contribution item with the given id that displays
	 * the given number of characters.
	 * 
	 * @param id
	 *            the contribution item's id, or <code>null</code> if it is to
	 *            have no id
	 * @param charWidth
	 *            the number of characters to display
	 */
	public StatusLineContributionItem(String id, int charWidth) {
		super(id);
		this.charWidth = charWidth;
		setVisible(false); 
	}
	public void fill(Composite parent) {
		statusLine = parent;
		Label sep = new Label(parent, SWT.SEPARATOR);
		label = new CLabel(statusLine, SWT.SHADOW_NONE);
		if (widthHint < 0) {
			GC gc = new GC(statusLine);
			gc.setFont(statusLine.getFont());
			FontMetrics fm = gc.getFontMetrics();
			widthHint = fm.getAverageCharWidth() * charWidth;
			heightHint = fm.getHeight();
			gc.dispose();
		}
		StatusLineLayoutData data = new StatusLineLayoutData();