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