📄 basecombopaintingstrategy.java
字号:
package com.swtplus.widgets.combo.internal;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import com.swtplus.internal.TextUtils;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.combo.IComboLabelProvider;
public class BaseComboPaintingStrategy implements IComboPaintingStrategy {
private PCombo pcombo;
private int minWidth = 15;
private Point textExtent = new Point(0,0);
private int margin = 2;
private int spacing = 4;
private int rightExtraMargin = 3;
private IComboLabelProvider labelProvider;
private String text ="";
private Image image;
public BaseComboPaintingStrategy(IComboLabelProvider labelProvider){
this.labelProvider = labelProvider;
}
public Point computeSize(int wHint, int hHint, boolean changed) {
Point p = new Point(wHint,hHint);
if (wHint == SWT.DEFAULT){
p.x = margin;
if (image != null){
p.x += image.getBounds().width + spacing;
}
p.x += textExtent.x + rightExtraMargin + margin;
}
{
int imageHeight = 0;
int textHeight = 0;
if (image != null){
imageHeight = image.getBounds().height;
}
textHeight = margin + textExtent.y + margin;
p.y = Math.max(imageHeight,textHeight);
}
if (p.x < minWidth)
p.x = minWidth;
return p;
}
public void paint(GC gc, boolean focused,Rectangle bounds) {
gc.fillRectangle(bounds);
if (focused){
gc.setBackground(pcombo.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.fillRectangle(margin-1,margin-1,bounds.width - (2*(margin -1)),bounds.height - (2*(margin-1)));
gc.setForeground(pcombo.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
}
int textX = margin;
if (image != null){
int y = (bounds.height - image.getBounds().height)/2;
if ((bounds.height - image.getBounds().height) % 2 != 0)
y++;
gc.drawImage(image,margin,y);
textX += image.getBounds().width + spacing;
}
int textWidth = bounds.width - textX - margin;
String shortText = TextUtils.getShortString(gc,text,textWidth);
int textY = (bounds.height - textExtent.y)/2;
gc.drawString(shortText,textX +1,textY,true);
}
public void valueChanged(Object newValue) {
text = labelProvider.getText(newValue);
if (text == null)
text = "";
image = labelProvider.getImage(newValue);
GC gc = new GC(pcombo);
textExtent = gc.stringExtent(text);
gc.dispose();
}
public void setPCombo(PCombo pcombo) {
this.pcombo = pcombo;
}
public void dispose(){
labelProvider.dispose();
}
public int getTextXStartPosition(){
int textX = margin;
if (image != null){
textX += image.getBounds().width + spacing;
}
return textX;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -