📄 simplegroupstrategy.java
字号:
package com.swtplus.widgets.group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import com.swtplus.internal.TextUtils;
import com.swtplus.widgets.toggle.IToggleStrategy;
/**
* SimpleGroupStrategy adds a seperator to the normal PGroup's image and text.
* <p>
* <b>Styles:</b>
* <ul>
* <li>TOGGLE_LEFT</li>
* <li>IMAGE_RIGHT</li>
* <li>LINE_MIDDLE</li>
* <li>CENTER_VERTICALLY</li>
* </ul>
*
* @since 1.0
*
* @author chris
*/
public class SimpleGroupStrategy extends AbstractGroupStrategy {
/**
* Places the toggle on the left.
*/
public static final int TOGGLE_LEFT = 1 << 2;
/**
* Places the image on the right
*/
public static final int IMAGE_RIGHT = 1 << 4;
/**
* Paints the seperator line trailing after the text
* rather than on the bottom.
*/
public static final int LINE_MIDDLE = 1 << 5;
/**
* Centers vertically the image, text and toggle.
*/
public static final int CENTER_VERTICALLY = 1 << 8;
private int separatorHeight = 2;
private boolean imageLeft = true;
private boolean toggleLeft = false;
private boolean lineBottom = true;
private boolean centerVertically = false;
private int heightWithoutLine = 0;
private int lineMargin = 2;
private int lineBetweenSpacing = 8;
private int titleTextMargin = 0;
private int betweenSpacing = 6;
private int vMargin = 3;
private int hMargin = 3;
/**
* Creates a SimpleGroupStrategy with the given style without a toggle.
*
* @param style
*/
public SimpleGroupStrategy(int style) {
super(style);
if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
toggleLeft = true;
if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
imageLeft = false;
if ((style & LINE_MIDDLE) == LINE_MIDDLE)
lineBottom = false;
if ((style & CENTER_VERTICALLY) == CENTER_VERTICALLY)
centerVertically = true;
}
/**
* Creates a SimpleGroupStrategy with the given toggle and style.
*
* @param ts
* @param style
*/
public SimpleGroupStrategy(IToggleStrategy ts, int style){
super (ts, style);
if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
toggleLeft = true;
if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
imageLeft = false;
if ((style & LINE_MIDDLE) == LINE_MIDDLE)
lineBottom = false;
if ((style & CENTER_VERTICALLY) == CENTER_VERTICALLY)
centerVertically = true;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#paintGroup(org.eclipse.swt.graphics.GC)
*/
public void paintGroup(GC gc) {
gc.fillRectangle(0,0,getGroup().getSize().x,getTitleHeight());
if (getGroup().getImage() != null){
int imgX = 0,imgY = 0;
if (imageLeft){
imgX = hMargin;
if (toggleLeft && (getToggleStrategy() != null)){
imgX += getToggleStrategy().getSize().x + betweenSpacing;
}
} else {
imgX = getGroup().getSize().x - getGroup().getImage().getBounds().width - hMargin;
if (!toggleLeft && (getToggleStrategy() != null)){
imgX -= getToggleStrategy().getSize().x + betweenSpacing;
}
}
if (centerVertically){
imgY = (heightWithoutLine - getGroup().getImage().getBounds().height)/2;
} else {
imgY = heightWithoutLine - getGroup().getImage().getBounds().height - vMargin;
}
gc.drawImage(getGroup().getImage(),imgX,imgY);
}
int textX = hMargin;
if (imageLeft && (getGroup().getImage() != null)){
textX += getGroup().getImage().getBounds().width + betweenSpacing;
}
if (toggleLeft && (getToggleStrategy() != null)){
textX += getToggleStrategy().getSize().x + betweenSpacing;
}
int textWidth = getGroup().getSize().x - textX - hMargin;
if (!imageLeft && (getGroup().getImage() != null)){
textWidth -= getGroup().getImage().getBounds().width + betweenSpacing;
}
if (!toggleLeft && (getToggleStrategy() != null)){
textWidth -= getToggleStrategy().getSize().x + betweenSpacing;
}
int textY = 0;
if (centerVertically){
textY = (heightWithoutLine - getFontHeight())/2;
} else {
textY = heightWithoutLine - (getFontHeight() + (titleTextMargin) + (vMargin));
}
if (getToggleStrategy() != null){
int toggleHeight = getToggleStrategy().getSize().y;
int fontHeight = getFontHeight();
if (toggleHeight > fontHeight){
int toggleY = ((heightWithoutLine - toggleHeight)/2);
int difference = (toggleHeight - fontHeight) /2;
textY = toggleY + difference;
}
}
gc.drawString(TextUtils.getShortString(gc,getGroup().getText(),textWidth),textX,textY);
int x = 0,x2 = 0,y = 0;
if (lineBottom){
x = 0;
x2 = getGroup().getSize().x;
y = getTitleHeight() - separatorHeight - lineMargin;
} else {
Point p = gc.stringExtent(TextUtils.getShortString(gc,getGroup().getText(),textWidth));
x = textX + p.x + lineBetweenSpacing;
x2 = textX + p.x + betweenSpacing + (textWidth - p.x) - lineBetweenSpacing;
y = textY + (p.y /2);
}
if (x2 > x){
gc.setForeground(getGroup().getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
gc.drawLine(x,y,x2,y);
gc.setForeground(getGroup().getDisplay().getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
gc.drawLine(x,y + 1,x2,y + 1);
}
if (!getGroup().isExpanded()){
gc.setBackground(getGroup().getParent().getBackground());
} else {
// e.gc.setBackground(parent.getParent().getBackground());
}
gc.fillRectangle(0,getTitleHeight(),getGroup().getBounds().width,getGroup().getBounds().height);
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#resize(org.eclipse.swt.events.ControlEvent)
*/
public void resize(ControlEvent e) {
if (getToggleStrategy() != null){
int toggleY = 0;
if (centerVertically){
toggleY = (heightWithoutLine - getToggleStrategy().getSize().y)/2;
} else {
toggleY = (heightWithoutLine - getToggleStrategy().getSize().y) - vMargin;
//toggleY += ((getFontHeight() + (2*titleTextMargin) + (2*vMargin)) - getToggleStrategy().getSize().y)/2;
}
if (toggleLeft){
getToggleStrategy().setLocation(new Point(hMargin,toggleY));
} else {
getToggleStrategy().setLocation(new Point(getGroup().getSize().x - hMargin - getToggleStrategy().getSize().x,toggleY));
}
}
getGroup().getBody().setBounds(0,getTitleHeight(),getGroup().getSize().x,getGroup().getSize().y - getTitleHeight());
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#computeSize(int, int, boolean)
*/
public Point computeSize(int wHint, int hHint, boolean changed) {
Point size = new Point(0,0);
int decorationsWidth = (2*hMargin);
if (getToggleStrategy() != null)
decorationsWidth += getToggleStrategy().getSize().x + betweenSpacing;
if (getGroup().getImage() != null)
decorationsWidth += getGroup().getImage().getBounds().width + betweenSpacing;
//figure out the size of the body for use later
Point bodySize;
int body_wHint = SWT.DEFAULT,body_hHint = SWT.DEFAULT;
if (wHint != SWT.DEFAULT)
body_wHint = wHint;
if (hHint != SWT.DEFAULT)
body_hHint = hHint - getTitleHeight();
bodySize = getGroup().getBody().computeSize(body_wHint,body_hHint,changed);
int textWidth = getTextWidth();
if (wHint == SWT.DEFAULT){
size.x = Math.max(decorationsWidth + textWidth,bodySize.x);
} else {
size.x = Math.max(decorationsWidth + 50,bodySize.x);
size.x = Math.max(size.x,wHint);
}
if (getGroup().isExpanded()){
size.y = getTitleHeight() + bodySize.y;
} else {
size.y = getTitleHeight();
}
return size;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#computeTitleHeight()
*/
public int computeTitleHeight() {
int h = 0;
int imageHeight = 0;
if (getGroup().getImage() != null)
imageHeight = getGroup().getImage().getBounds().height;
h = Math.max(getFontHeight() + (2*titleTextMargin) + (2*vMargin),imageHeight + (2*vMargin));
if (getToggleStrategy() != null){
int toggleHeight = getToggleStrategy().getSize().y + (2*vMargin);
h = Math.max(toggleHeight + (2*vMargin), h);
}
heightWithoutLine = h;
if (lineBottom){
h += separatorHeight;
h += (2*lineMargin);
}
return h;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#computeActiveBounds()
*/
public Rectangle computeActiveBounds() {
return new Rectangle(0,0,getGroup().getSize().x,getTitleHeight());
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#dispose()
*/
public void dispose() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -