📄 imagetogglestrategy.java
字号:
package com.swtplus.widgets.toggle;
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.Rectangle;
import org.eclipse.swt.widgets.Widget;
import com.swtplus.internal.PGC;
/**
* The ImageToggleStrategy shows a toggle using different images you
* specify for each of the toggle's states.
*
* @author Jeremy
*/
public class ImageToggleStrategy implements IToggleStrategy {
private Point location;
private Image expandedImg;
private Image collapsedImg;
private Image expandedHoveringImg;
private Image collapsedHoveringImg;
private boolean toggleExpanded = true;
/**
* Creates an ImageToggleStrategy with the expanded and collapsed images
* specified.
*
* @param expanded The image to be displayed when the toggle is expanded
* @param collapsed The image to be displayed when the toggle is collapsed
*/
public ImageToggleStrategy(Image expanded, Image collapsed){
this.expandedImg = expanded;
this.collapsedImg = collapsed;
this.expandedHoveringImg = expanded;
this.collapsedHoveringImg = collapsed;
}
/**
* Creates an ImageToggleStrategy with the expanded, expanded and hovering, collapsed
* and collapsed and hovering images specified.
*
* @param expanded The image when the toggle is expanded
* @param collapsed The image when the toggle is collapsed
* @param expandedHovering The image when the toggle is expanded and the mouse is hovering over the toggle
* @param collapsedHovering The image when the toggle is collapsed and the mouse is hovering over the toggle
*/
public ImageToggleStrategy(Image expanded, Image collapsed, Image expandedHovering, Image collapsedHovering){
this.expandedImg = expanded;
this.collapsedImg = collapsed;
this.expandedHoveringImg = expandedHovering;
this.collapsedHoveringImg = collapsedHovering;
}
/**
* Creates an ImageToggleStrategy with no images specified.
*
* Note that the toggle will not paint until all four images are set.
* Also see setExpandedImage(), setCollapsedImage(),setExpandedHovering(),setCollapsedHovering();
*/
public ImageToggleStrategy (){}
/* (non-Javadoc)
* @see com.swtplus.widgets.toggle.IToggleStrategy#paint(org.eclipse.swt.events.PaintEvent, boolean, boolean, boolean)
*/
public void paint(Widget parent,GC gc, boolean expanded, boolean hasFocus,
boolean hovering) {
this.toggleExpanded = expanded;
if (expandedImg == null
|| collapsedImg == null
|| expandedHoveringImg == null
|| collapsedHoveringImg == null)
return;
PGC gcPlus = new PGC(gc,location.x,location.y);
Color back = gcPlus.getBackground();
Color fore = gcPlus.getForeground();
Rectangle bounds = new Rectangle(0,0,0,0);
if (toggleExpanded){
if (hovering){
bounds = expandedHoveringImg.getBounds();
}else{
bounds = expandedImg.getBounds();
}
}else{
if (hovering){
bounds = collapsedHoveringImg.getBounds();
}else{
bounds = collapsedImg.getBounds();
}
}
if (toggleExpanded){
if (hovering){
gcPlus.drawImage(expandedHoveringImg,bounds.x,bounds.y);
}else{
gcPlus.drawImage(expandedImg,bounds.x,bounds.y);
}
}else{
if (hovering){
gcPlus.drawImage(collapsedHoveringImg,bounds.x,bounds.y);
}else{
gcPlus.drawImage(collapsedImg,bounds.x,bounds.y);
}
}
if (hasFocus){
gcPlus.setBackground(back);
gcPlus.setForeground(fore);
gcPlus.drawFocus(bounds.x - 1,bounds.y - 1,bounds.width + 2,bounds.height + 2);
}
}
/*
* Returns the toggle's size (based upon the image).
* @see com.swtplus.widgets.toggle.IToggleStrategy#getSize()
*/
public Point getSize() {
Point size = new Point(0,0);
if (toggleExpanded){
size.x = expandedImg.getBounds().width;
size.y = expandedImg.getBounds().height;
}else{
size.x = collapsedImg.getBounds().width;
size.y = collapsedImg.getBounds().height;
}
return size;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.toggle.IToggleStrategy#setLocation(org.eclipse.swt.graphics.Point)
*/
public void setLocation(Point p) {
location = p;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.toggle.IToggleStrategy#getLocation()
*/
public Point getLocation() {
return location;
}
public void setExpandedImage(Image image){
this.expandedImg = image;
}
public void setCollapsedImage(Image image){
this.collapsedImg = image;
}
public void setExpandedHoveringImage(Image image){
this.expandedHoveringImg = image;
}
public void setCollapsedHoveringImage(Image image){
this.collapsedHoveringImg = image;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -