1 11 12 package org.eclipse.ui.internal.util; 13 14 import org.eclipse.jface.action.ContributionItem; 15 import org.eclipse.jface.action.IContributionManager; 16 import org.eclipse.jface.action.StatusLineLayoutData; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.CLabel; 19 import org.eclipse.swt.graphics.FontMetrics; 20 import org.eclipse.swt.graphics.GC; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Label; 24 25 28 public class StatusLineContributionItem extends ContributionItem { 29 30 public final static int DEFAULT_CHAR_WIDTH = 40; 31 32 private int charWidth; 33 34 private CLabel label; 35 36 41 private Composite statusLine = null; 42 43 private String text = Util.ZERO_LENGTH_STRING; 44 45 private int widthHint = -1; 46 47 private int heightHint = -1; 48 49 public StatusLineContributionItem(String id) { 50 this(id, DEFAULT_CHAR_WIDTH); 51 } 52 53 public StatusLineContributionItem(String id, int charWidth) { 54 super(id); 55 this.charWidth = charWidth; 56 setVisible(false); } 58 59 public void fill(Composite parent) { 60 statusLine = parent; 61 62 Label sep = new Label(parent, SWT.SEPARATOR); 63 label = new CLabel(statusLine, SWT.SHADOW_NONE); 64 65 if (widthHint < 0) { 66 GC gc = new GC(statusLine); 67 gc.setFont(statusLine.getFont()); 68 FontMetrics fm = gc.getFontMetrics(); 69 widthHint = fm.getAverageCharWidth() * charWidth; 70 heightHint = fm.getHeight(); 71 gc.dispose(); 72 } 73 74 StatusLineLayoutData data = new StatusLineLayoutData(); 75 data.widthHint = widthHint; 76 label.setLayoutData(data); 77 label.setText(text); 78 79 data = new StatusLineLayoutData(); 80 data.heightHint = heightHint; 81 sep.setLayoutData(data); 82 83 } 84 85 92 public Point getDisplayLocation() { 93 if ((label != null) && (statusLine != null)) { 94 return statusLine.toDisplay(label.getLocation()); 95 } 96 97 return null; 98 } 99 100 public String getText() { 101 return text; 102 } 103 104 public void setText(String text) { 105 if (text == null) { 106 throw new NullPointerException (); 107 } 108 109 this.text = text; 110 111 if (label != null && !label.isDisposed()) { 112 label.setText(this.text); 113 } 114 115 if (this.text.length() == 0) { 116 if (isVisible()) { 117 setVisible(false); 118 IContributionManager contributionManager = getParent(); 119 120 if (contributionManager != null) { 121 contributionManager.update(true); 122 } 123 } 124 } else { 125 if (!isVisible()) { 126 setVisible(true); 127 IContributionManager contributionManager = getParent(); 128 129 if (contributionManager != null) { 130 contributionManager.update(true); 131 } 132 } 133 } 134 } 135 } 136 | Popular Tags |