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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.action;
import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.jface.util.Util;
/**
 * A contribution item to be used with status line managers.
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 * 
 * @since 3.4
 */
public class StatusLineContributionItem extends ContributionItem {