⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectanglegroupstrategy.java

📁 利用它可以做出非常漂亮的swt界面,包含的组件有PShelf Plist
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
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;

/**  
 * RectangleGroupStrategy is a very flexible painting strategy that displays a (rounded) 
 * rectangle around the PGroup's body.
 * <p>
 * <b>Styles:</b>
 * <ul>
 * <li>TOGGLE_LEFT</li>
 * <li>IMAGE_RIGHT</li>
 * <li>ROUNDED</li>
 * <li>PAINTIMAGEOUTOFBOUNDS</li> 
 * </ul>
 * 
 * @since 1.0
 *
 * @author chris
 */
public class RectangleGroupStrategy 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;	
	/**
	 * Rounds the corners of the bounding rectangle.
	 */
	public static final int ROUNDED = 1 << 7;	
	/**
	 * Causes the bounding rectangle to ignore the height of the image
	 * when determining its height.  Therefore the image may paint higher or 
	 * taller than the bounding rectangle.
	 */
	public static final int PAINTIMAGEOUTOFBOUNDS = 1 << 3;	
	
	private int vMargin = 2;
	private int hMargin = 5;
	
	private boolean toggleLeft = false;
	private boolean imageLeft = true;
	private boolean rounded = false;
	private boolean paintImageInBounds = true;
	
	private int titleTextMargin = 2;
	private int betweenSpacing = 4;
	private int margin = 1;
	
	private Color gradientColors[] = null;
	private int gradientPercents[] = null;
	private boolean gradientVertical = false;
	
	private Color borderColor = null;
	private Color initialBackground;
	private Color g1;
	private Color g2;

	/**
	 * Constructs a RectangleGroupStrategy with the given style without a toggle.
	 * 
	 * @param style
	 */
	public RectangleGroupStrategy(int style) {
		super(style);
		if ((style & ROUNDED) == ROUNDED)
            rounded = true;
        if ((style & PAINTIMAGEOUTOFBOUNDS) == PAINTIMAGEOUTOFBOUNDS)
            paintImageInBounds = false;
        if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
            toggleLeft = true;
        if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
            imageLeft = false;
	}
	
	/**
	 * Constructs a RectangleGroupStrategy with the given toggle and style.
	 * 
	 * @param toggleStrategy
	 * @param style
	 */
	public RectangleGroupStrategy(IToggleStrategy toggleStrategy, int style){
		super(toggleStrategy, style);
		if ((style & ROUNDED) == ROUNDED)
            rounded = true;
        if ((style & PAINTIMAGEOUTOFBOUNDS) == PAINTIMAGEOUTOFBOUNDS)
            paintImageInBounds = false;
        if ((style & TOGGLE_LEFT) == TOGGLE_LEFT)
            toggleLeft = true;
        if ((style & IMAGE_RIGHT) == IMAGE_RIGHT)
            imageLeft = false;
	}

	
	/* (non-Javadoc)
	 * @see com.swtplus.widgets.AbstractGroupStrategy#initialize(com.swtplus.widgets.PGroup)
	 */
	public void initialize(PGroup sg) {
		super.initialize(sg);
		initializeColors();
	}
	
	/* (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);
		Image image = getGroup().getImage();
		
		// Compute Title Area Height for later use
		int titleAreaHeight = 0;
		
		if (image != null && !paintImageInBounds){
			titleAreaHeight = getFontHeight() + (2*titleTextMargin) + (2*vMargin);
			if (getToggleStrategy() != null){
				titleAreaHeight = Math.max(titleAreaHeight, getToggleStrategy().getSize().y + (2*vMargin));
			}
		}
		
		// Paint rectangle
		int toggleHeight = 0;
		if (getToggleStrategy() != null){
			toggleHeight = getToggleStrategy().getSize().y + (2*vMargin) ;
		}
		
		if (paintImageInBounds || image == null){
			
			
			if (gradientColors != null){
				GraphicUtils.fillGradientRectangle(gc,0,0,getGroup().getSize().x,Math.max(getTitleHeight(),toggleHeight),gradientColors,gradientPercents,gradientVertical);
			} else {
				gc.fillRectangle(0,0,getGroup().getSize().x,Math.max(getTitleHeight(),toggleHeight));
				//GraphicUtils.fillRoundRectangle(gc,0,0,getGroup().getSize().x,Math.max(getTitleHeight(),toggleHeight),getGroup().getParent().getBackground(),true,!getGroup().isExpanded());
			}
			if (rounded){
				GraphicUtils.drawRoundRectangle(gc,0,0,getGroup().getSize().x,Math.max(getTitleHeight(),toggleHeight),getGroup().getParent().getBackground(),null,true,!getGroup().isExpanded());
			}
		}else {
			
			gc.setBackground(getGroup().getParent().getBackground());
			gc.fillRectangle(0,0,getGroup().getSize().x,getTitleHeight() - titleAreaHeight);
			
			
			if (gradientColors != null){
				GraphicUtils.fillGradientRectangle(gc,0,getTitleHeight() - titleAreaHeight,getGroup().getSize().x,Math.max(titleAreaHeight,toggleHeight),gradientColors,gradientPercents,gradientVertical);
			} else {
				gc.setBackground(getGroup().getBackground());
				gc.fillRectangle(0,getTitleHeight() - titleAreaHeight,getGroup().getSize().x,Math.max(titleAreaHeight,toggleHeight));
			}
			
			if (rounded){
				GraphicUtils.drawRoundRectangle(gc,0,getTitleHeight() - titleAreaHeight,getGroup().getSize().x -1,Math.max(titleAreaHeight,toggleHeight) ,getGroup().getParent().getBackground(),null,true,!getGroup().isExpanded());
			}
		}
		
		// Paint Image
		
		if (image != null){
			if (imageLeft){
				if (getToggleStrategy() != null){
					if (toggleLeft){
						imagePoint.x = hMargin + getToggleStrategy().getSize().x + betweenSpacing;
					}else {
						imagePoint.x = hMargin;
					}
				}else {
					imagePoint.x = hMargin;
				}
			}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 );
				}
			}
			if (paintImageInBounds && image.getImageData().height > getTitleHeight()){
				imagePoint.y = (getTitleHeight() - image.getImageData().height) /2;
			} else {
				imagePoint.y = (getTitleHeight() - image.getImageData().height)/2;
			}
			gc.drawImage(image,imagePoint.x,imagePoint.y);
		}
		
		// Paint Text
		
		// First, compute the starting text point 

		// Part 1, Image
		
		textPoint.x = hMargin;
		if (image != null){
			if (imageLeft){
				textPoint.x += image.getBounds().width + betweenSpacing;
			}
			if (paintImageInBounds){
				textPoint.y = (getTitleHeight() - getFontHeight()) /2;
			} else {
				textPoint.y = (getTitleHeight() - titleAreaHeight) + (titleAreaHeight - gc.getFontMetrics().getHeight()) /2;
			}
		} else {
			textPoint.y = (getTitleHeight() - getFontHeight()) /2;
		}
		
		// Part 2, Toggle
		
		if (getToggleStrategy() != null){
			if (toggleLeft){
				textPoint.x += getToggleStrategy().getSize().x + betweenSpacing;
			}
		}
		
		// Now calculate the width of the text
		
		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.setForeground(getGroup().getForeground());
		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);
		} else {
			Color _borderColor;
			if (borderColor == null){
				_borderColor = getGroup().getBody().getBackground();
			} else {
				_borderColor = borderColor;
			}
			
			if (rounded){
				gc.setBackground(getGroup().getBody().getBackground());
				gc.fillRectangle(0,getGroup().getSize().y - 5,getGroup().getSize().x -1,5);
				gc.setForeground(_borderColor);
				GraphicUtils.drawRoundRectangle(gc,0,getTitleHeight(),getGroup().getSize().x -1,getGroup().getSize().y - getTitleHeight(),getGroup().getParent().getBackground(),false,true);
			} else {
				gc.setBackground(_borderColor);
				gc.fillRectangle(0,getTitleHeight(),getGroup().getBounds().width,getGroup().getBounds().height);
			}
		}

		gc.setBackground(getGroup().getBackground());	
		gc.setForeground(getGroup().getForeground());
		
	}
	
	/* (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;
			if (paintImageInBounds){
				toggleY = (getTitleHeight() - p.y)/2;
			} else {
				int titleAreaHeight = getFontHeight() + (2*titleTextMargin) + (2*vMargin);
				if (getToggleStrategy() != null){
					titleAreaHeight = Math.max(titleAreaHeight, getToggleStrategy().getSize().y + (2*vMargin));
				}
				toggleY = (getTitleHeight() - titleAreaHeight) + (titleAreaHeight - p.y)/2;
			}
			if (toggleLeft){
				getToggleStrategy().setLocation(new Point(hMargin,toggleY));
			}else{
				getToggleStrategy().setLocation(new Point(getGroup().getSize().x - hMargin - p.x,toggleY));
			}
		}
		if (rounded){
			getGroup().getBody().setBounds(margin,getTitleHeight(),getGroup().getSize().x - (2*margin),getGroup().getSize().y - getTitleHeight() - 5);
		} else {
			getGroup().getBody().setBounds(margin,getTitleHeight(),getGroup().getSize().x - (2*margin),getGroup().getSize().y - getTitleHeight() - margin);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -