📄 formgroupstrategy.java
字号:
package com.swtplus.widgets.group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import com.swtplus.internal.GraphicUtils;
import com.swtplus.internal.TextUtils;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.toggle.IToggleStrategy;
/**
* FormGroupStrategy makes a PGroup mimic the look and feel of an Eclipse Form
* Section.
* <p>
* <b>Styles:</b>
* <ul>
* <li>TOGGLE_LEFT</li>
* <li>IMAGE_RIGHT</li>
* </ul>
*
* @since 1.0
*
* @author chris
*/
public class FormGroupStrategy extends AbstractGroupStrategy {
/**
* Places the toggle on the left.
*/
public static final int TOGGLE_LEFT = 1 << 1;
/**
* Places the image on the right
*/
public static final int IMAGE_RIGHT = 1 << 4;
private boolean imageLeft = true;
private boolean toggleLeft = false;
private Color initialBackColor;
private Color initialBorderColor;
private int titleTextMargin = 2;
private int betweenSpacing = 6;
private int margin = 0;
private int vMargin = 2;
private int hMargin = 6;
private Color borderColor;
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#initialize(com.swtplus.widgets.PGroup)
*/
public void initialize(PGroup sg) {
super.initialize(sg);
initializeColors();
}
/**
* Creates a FormGroupStrategy with the given style without any toggle.
*
* @param style
*/
public FormGroupStrategy(int style) {
super(style);
if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
toggleLeft = true;
if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
imageLeft = false;
}
/**
* Creates a FormGroupStrategy with the given toggle and style.
*
* @param toggle
* @param style
*/
public FormGroupStrategy(IToggleStrategy toggle, int style) {
super(toggle, style);
if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
toggleLeft = true;
if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
imageLeft = false;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#paintGroup(org.eclipse.swt.graphics.GC)
*/
public void paintGroup(GC gc) {
Point textPoint = new Point (0,0);
//int textWidth = 0;
Point imagePoint = new Point (0,0);
Color back = gc.getBackground();
Color fore = gc.getForeground();
gc.setForeground(gc.getBackground());
gc.setBackground(getGroup().getBody().getBackground());
gc.fillGradientRectangle(0,0,getGroup().getSize().x,getTitleHeight(),true);
gc.setForeground(borderColor);
GraphicUtils.drawRoundRectangle(gc,0,0,getGroup().getSize().x -1,getTitleHeight(),getGroup().getParent().getBackground(),true,false);
gc.setForeground(getGroup().getBody().getBackground());
if (getGroup().isExpanded())
gc.drawLine(1,getTitleHeight() -1,getGroup().getSize().x - 2,getTitleHeight() -1);
gc.setBackground(back);
gc.setForeground(fore);
Image image = getGroup().getImage();
if (image != null){
imagePoint.x = hMargin;
if (imageLeft){
if (getToggleStrategy() != null){
if (toggleLeft){
imagePoint.x += getToggleStrategy().getSize().x + betweenSpacing;
}
}
}else {
if (getToggleStrategy() != null){
if (toggleLeft){
imagePoint.x = getGroup().getSize().x - (hMargin + image.getBounds().width );
}else{
imagePoint.x = getGroup().getSize().x - (hMargin + image.getBounds().width + getToggleStrategy().getSize().x+ betweenSpacing);
}
}else {
imagePoint.x = getGroup().getSize().x - (hMargin + image.getBounds().width );
}
}
imagePoint.y = ((getTitleHeight() - image.getBounds().height)/2);
gc.drawImage(image,imagePoint.x,imagePoint.y);
}
// Paint Text
// First, compute the starting text point
// Part 1, Image
textPoint.x = hMargin;
textPoint.y = (getTitleHeight() - getFontHeight()) /2;
if (image != null){
if (imageLeft){
textPoint.x += image.getBounds().width + betweenSpacing;
}
}
// Part 2, Toggle
if (getToggleStrategy() != null){
if (toggleLeft){
textPoint.x += getToggleStrategy().getSize().x + betweenSpacing;
}
}
// Now calculate the width of the text
int textWidth = getGroup().getSize().x - (hMargin *2);
// Part 1, Image
if (image != null){
textWidth -= (image.getBounds().width + betweenSpacing);
}
// Part 2, Toggle
if (getToggleStrategy() != null){
textWidth -= getToggleStrategy().getSize().x + betweenSpacing;
}
gc.drawText(TextUtils.getShortString(gc,getGroup().getText(),textWidth),textPoint.x,textPoint.y, true);
if (!getGroup().isExpanded()){
gc.setBackground(getGroup().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) {
Point p = getToggleStrategy().getSize();
int toggleY = 0;
toggleY = (getTitleHeight() - p.y)/2;
int toggleX = 0;
if (toggleLeft){
toggleX = hMargin;
} else {
toggleX = getGroup().getSize().x - hMargin - p.x ;
}
getToggleStrategy().setLocation(new Point(toggleX,toggleY));
}
getGroup().getBody().setBounds(margin,getTitleHeight(),getGroup().getSize().x - (2*margin),getGroup().getSize().y - getTitleHeight() - margin);
}
/* (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);
Point textPoint = new Point (0,0);
//figure out min width
int decorationsWidth = 0;
if (getGroup().getImage() != null){
decorationsWidth += hMargin + getGroup().getImage().getBounds().width + betweenSpacing;
} else {
decorationsWidth += hMargin;
}
decorationsWidth += hMargin;
if (getToggleStrategy() != null)
decorationsWidth += getToggleStrategy().getSize().x + hMargin + 3;
//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 - (2*margin);
if (hHint != SWT.DEFAULT){
body_hHint = hHint - getTitleHeight();
}
bodySize = getGroup().getBody().computeSize(body_wHint,body_hHint,changed);
textPoint.x = hMargin;
if (getGroup().getImage() != null && imageLeft){
textPoint.x += getGroup().getImage().getBounds().width + betweenSpacing;
}
if (getToggleStrategy() != null && toggleLeft){
textPoint.x += getToggleStrategy().getSize().x + betweenSpacing;
}
int textWidth = getTextWidth();
if (wHint == SWT.DEFAULT){
int titleWidth = decorationsWidth + textWidth;
size.x = Math.max(titleWidth,bodySize.x + (2*margin));
} else {
int minWidth = decorationsWidth + 50;
size.x = Math.max(minWidth,bodySize.x + (2*margin));
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 height = 0;
if (getGroup().getImage() != null)
height = getGroup().getImage().getBounds().height + (2*vMargin);
if (getToggleStrategy() != null){
int toggleHeight = getToggleStrategy().getSize().y;
height = Math.max(toggleHeight + (2*vMargin), height);
}
return Math.max(getFontHeight() + (2*titleTextMargin) + (2*vMargin),height);
}
/* (non-Javadoc)
* @see com.swtplus.widgets.AbstractGroupStrategy#computeActiveBounds()
*/
public Rectangle computeActiveBounds() {
Point textPoint = new Point (0,0);
textPoint.x = hMargin;
if (getGroup().getImage() != null && imageLeft){
textPoint.x += getGroup().getImage().getBounds().width + betweenSpacing;
}
if (getToggleStrategy() != null && toggleLeft){
textPoint.x += getToggleStrategy().getSize().x + betweenSpacing;
}
textPoint.y = (getTitleHeight() - getFontHeight()) /2;
int textWidth = getGroup().getSize().x - textPoint.x - hMargin;
if (getToggleStrategy() != null && !toggleLeft)
textWidth -= getToggleStrategy().getSize().x + betweenSpacing;
if (getGroup().getImage() != null && !imageLeft)
textWidth -= getGroup().getImage().getBounds().width + betweenSpacing;
return new Rectangle(textPoint.x,textPoint.y,textWidth,getFontHeight());
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#dispose()
*/
public void dispose() {
if (initialBackColor != null)
initialBackColor.dispose();
if (initialBorderColor != null)
initialBorderColor.dispose();
}
/**
* Initializes the Form Group's colors.
*/
private void initializeColors() {
getGroup().getBody().setBackground(getGroup().getBody().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
RGB borderRGB = GraphicUtils.blend(getGroup().getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT).getRGB(),getGroup().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),100);
initialBorderColor = new Color(getGroup().getDisplay(),borderRGB);
borderColor = initialBorderColor;
RGB backRGB = GraphicUtils.blend(getGroup().getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT).getRGB(),getGroup().getBody().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),40);
initialBackColor = new Color(getGroup().getDisplay(),backRGB);
getGroup().setBackground(initialBackColor);
}
/**
* @return Returns the borderColor.
*/
public Color getBorderColor() {
return borderColor;
}
/**
* @param borderColor The borderColor to set.
*/
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -