1 11 12 package org.eclipse.ui.texteditor; 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.custom.CLabel; 16 import org.eclipse.swt.events.DisposeEvent; 17 import org.eclipse.swt.events.DisposeListener; 18 import org.eclipse.swt.events.MouseAdapter; 19 import org.eclipse.swt.events.MouseEvent; 20 import org.eclipse.swt.events.MouseListener; 21 import org.eclipse.swt.graphics.GC; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.swt.widgets.Label; 26 27 import org.eclipse.jface.action.ContributionItem; 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.action.StatusLineLayoutData; 30 import org.eclipse.jface.resource.JFaceColors; 31 32 36 public class StatusLineContributionItem extends ContributionItem implements IStatusField, IStatusFieldExtension { 37 38 42 private class Listener extends MouseAdapter { 43 46 public void mouseDoubleClick(MouseEvent e) { 47 if (fActionHandler != null && fActionHandler.isEnabled()) 48 fActionHandler.run(); 49 } 50 } 51 52 56 private static final int INDENT= 3; 57 61 static final int DEFAULT_WIDTH_IN_CHARS= 14; 62 66 private int fFixedWidth= -1; 67 71 private int fFixedHeight= -1; 72 73 private String fText; 74 75 private Image fImage; 76 80 private String fErrorText; 81 85 private Image fErrorImage; 86 90 private String fToolTipText; 91 95 private int fWidthInChars; 96 97 private CLabel fLabel; 98 102 private IAction fActionHandler; 103 107 private MouseListener fMouseListener; 108 109 110 115 public StatusLineContributionItem(String id) { 116 this(id, true, DEFAULT_WIDTH_IN_CHARS); 117 } 118 119 127 public StatusLineContributionItem(String id, boolean visible, int widthInChars) { 128 super(id); 129 setVisible(visible); 130 fWidthInChars= widthInChars; 131 } 132 133 136 public void setText(String text) { 137 fText= text; 138 updateMessageLabel(); 139 } 140 141 144 public void setImage(Image image) { 145 fImage= image; 146 updateMessageLabel(); 147 } 148 149 153 public void setErrorText(String text) { 154 fErrorText= text; 155 updateMessageLabel(); 156 } 157 158 162 public void setErrorImage(Image image) { 163 fErrorImage= image; 164 updateMessageLabel(); 165 } 166 167 171 public void setToolTipText(String string) { 172 fToolTipText= string; 173 updateMessageLabel(); 174 } 175 176 179 public void fill(Composite parent) { 180 181 Label sep= new Label(parent, SWT.SEPARATOR); 182 fLabel= new CLabel(parent, SWT.SHADOW_NONE); 183 184 fLabel.addDisposeListener(new DisposeListener() { 185 public void widgetDisposed(DisposeEvent e) { 186 fMouseListener= null; 187 } 188 }); 189 if (fActionHandler != null) { 190 fMouseListener= new Listener(); 191 fLabel.addMouseListener(fMouseListener); 192 } 193 194 StatusLineLayoutData data= new StatusLineLayoutData(); 195 data.widthHint= getWidthHint(parent); 196 fLabel.setLayoutData(data); 197 198 data= new StatusLineLayoutData(); 199 data.heightHint= getHeightHint(parent); 200 sep.setLayoutData(data); 201 202 updateMessageLabel(); 203 } 204 205 public void setActionHandler(IAction actionHandler) { 206 if (fActionHandler != null && actionHandler == null && fMouseListener != null) { 207 if (!fLabel.isDisposed()) 208 fLabel.removeMouseListener(fMouseListener); 209 fMouseListener= null; 210 } 211 212 fActionHandler= actionHandler; 213 214 if (fLabel != null && !fLabel.isDisposed() && fMouseListener == null && fActionHandler != null) { 215 fMouseListener= new Listener(); 216 fLabel.addMouseListener(fMouseListener); 217 } 218 } 219 220 227 private int getWidthHint(Composite control) { 228 if (fFixedWidth < 0) { 229 GC gc= new GC(control); 230 gc.setFont(control.getFont()); 231 fFixedWidth= gc.getFontMetrics().getAverageCharWidth() * fWidthInChars; 232 fFixedWidth += INDENT * 2; 233 gc.dispose(); 234 } 235 return fFixedWidth; 236 } 237 238 245 private int getHeightHint(Composite control) { 246 if (fFixedHeight < 0) { 247 GC gc= new GC(control); 248 gc.setFont(control.getFont()); 249 fFixedHeight= gc.getFontMetrics().getHeight(); 250 gc.dispose(); 251 } 252 return fFixedHeight; 253 } 254 255 260 private void updateMessageLabel() { 261 if (fLabel != null && !fLabel.isDisposed()) { 262 Display display= fLabel.getDisplay(); 263 if ((fErrorText != null && fErrorText.length() > 0) || fErrorImage != null) { 264 fLabel.setForeground(JFaceColors.getErrorText(display)); 265 fLabel.setText(fErrorText); 266 fLabel.setImage(fErrorImage); 267 if (fToolTipText != null) 268 fLabel.setToolTipText(fToolTipText); 269 else if (fErrorText.length() > fWidthInChars) 270 fLabel.setToolTipText(fErrorText); 271 else 272 fLabel.setToolTipText(null); 273 } 274 else { 275 fLabel.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND)); 276 fLabel.setText(fText); 277 fLabel.setImage(fImage); 278 if (fToolTipText != null) 279 fLabel.setToolTipText(fToolTipText); 280 else if (fText != null && fText.length() > fWidthInChars) 281 fLabel.setToolTipText(fText); 282 else 283 fLabel.setToolTipText(null); 284 } 285 } 286 } 287 } 288 289 | Popular Tags |