📄 abstractgroupstrategy.java
字号:
package com.swtplus.widgets.group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.toggle.IToggleStrategy;
/**
* AbstractGroupStrategy is a convenient starting point for all IGroupStrategy's.
* <P>
* The AbstractGroupStrategy handles most behavior for you. All that is required
* of extending classes, is to implement painting and sizing.
*
* @author chris
*/
public abstract class AbstractGroupStrategy implements IGroupStrategy{
private PGroup group;
private IToggleStrategy toggleStrategy;
private boolean expanded = true;
private boolean hoveringOnToggle = false;
private int fontHeight = 0;
private int textWidth = 0;
private int titleHeight = 0;
//used for hand cursor
private Rectangle activeBounds = new Rectangle(0,0,0,0);
/**
* Default constructor
*
* @param style
*/
public AbstractGroupStrategy(int style) {
}
/**
* Constructs the strategy with the given toggle strategy and style.
*
* @param t
* @param style
*/
public AbstractGroupStrategy(IToggleStrategy t, int style){
this.toggleStrategy = t;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#initialize(com.swtplus.widgets.PGroup)
*/
public void initialize(PGroup g){
group = g;
group.addControlListener(new ControlListener(){
public void controlMoved(ControlEvent arg0) {
}
public void controlResized(ControlEvent arg0) {
activeBounds = computeActiveBounds();
}});
if (toggleStrategy != null) {
group.addMouseTrackListener(new MouseTrackListener(){
public void mouseEnter(MouseEvent e) {
}
public void mouseExit(MouseEvent e) {
AbstractGroupStrategy.this.group.setCursor(null);
if (hoveringOnToggle = true){
hoveringOnToggle = false;
group.redraw();
}
hoveringOnToggle = false;
}
public void mouseHover(MouseEvent e) {
}});
group.addMouseMoveListener(new MouseMoveListener(){
public void mouseMove(MouseEvent e) {
boolean newHoveringOnToggle = false;
boolean showHand = false;
if (activeBounds.intersects(e.x,e.y,1,1)){
showHand = true;
}
if (toggleStrategy != null){
Rectangle r = new Rectangle(toggleStrategy.getLocation().x,toggleStrategy.getLocation().y,toggleStrategy.getSize().x,toggleStrategy.getSize().y);
if (r.intersects(e.x,e.y,1,1)){
newHoveringOnToggle = true;
showHand = true;
}
}
if (newHoveringOnToggle != hoveringOnToggle){
hoveringOnToggle = newHoveringOnToggle;
group.redraw();
}
if (showHand){
AbstractGroupStrategy.this.group.setCursor(AbstractGroupStrategy.this.group.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
} else {
AbstractGroupStrategy.this.group.setCursor(null);
}
}
});
group.addMouseListener(new MouseListener(){
public void mouseDoubleClick(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
Rectangle r = new Rectangle(toggleStrategy.getLocation().x,toggleStrategy.getLocation().y,toggleStrategy.getSize().x,toggleStrategy.getSize().y);
if (activeBounds.intersects(e.x,e.y,1,1) || r.intersects(e.x,e.y,1,1)){
AbstractGroupStrategy.this.group.setExpanded(!AbstractGroupStrategy.this.group.isExpanded());
AbstractGroupStrategy.this.getGroup().getBody().setFocus();
}
}
public void mouseUp(MouseEvent e) {
}});
group.addListener (SWT.Traverse, new Listener () {
public void handleEvent (Event e) {
switch (e.detail) {
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
group.addListener (SWT.FocusIn, new Listener () {
public void handleEvent (Event e) {
group.redraw();
}
});
group.addListener (SWT.FocusOut, new Listener () {
public void handleEvent (Event e) {
group.redraw();
}
});
group.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if (e.character == ' ' || e.character == '\r'){
AbstractGroupStrategy.this.group.setExpanded(!AbstractGroupStrategy.this.group.isExpanded());
}
}
public void keyReleased(KeyEvent e) {
}});
}
fontChanged();
titleHeight = computeTitleHeight();
activeBounds = computeActiveBounds();
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#paint(org.eclipse.swt.graphics.GC)
*/
public void paint(GC gc){
Color back = gc.getBackground();
Color fore = gc.getForeground();
paintGroup(gc);
gc.setBackground(back);
gc.setForeground(fore);
paintToggle(gc);
}
/**
* Paints the actual group widget. This method is to be implemented by
* extending classes.
*
* @param gc
*/
public abstract void paintGroup(GC gc);
/**
* Paints the toggle on top of the group. Not intended to be implemented
* in extending classes.
*
* @param gc
*/
public void paintToggle(GC gc){
if (getToggleStrategy() != null){
getToggleStrategy().paint(getGroup(),gc,isExpanded(),getGroup().isFocusControl(),hoveringOnToggle);
}
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#resize(org.eclipse.swt.events.ControlEvent)
*/
public abstract void resize(ControlEvent e);
/**
* Returns the height of the title area of the PGroup. This is the area
* where the PGroup's decoration is expected to be drawn. This method is
* not technically necessary but is provided for convenience. The height
* of this title area is typically used throughout many methods including
* painting and resizing. Therefore calculating this height ahead of time
* is beneficial. This method is called whenever any property that might
* affect the title height is affected (i.e. font, image, text).
*
* @return
*/
public abstract int computeTitleHeight();
/**
* Returns the active area. The active area is the area where the mouse will
* change to a hand cursor and the user will be able to expand/collapse the
* PGroup. This area and behavior are in addition to the toggle.
*
* @return
*/
public abstract Rectangle computeActiveBounds();
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#isExpanded()
*/
public boolean isExpanded() {
return expanded;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#setExpanded(boolean)
*/
public void setExpanded(boolean expanded) {
this.expanded = expanded;
getGroup().getBody().setVisible(expanded);
group.redraw();
group.getParent().layout();
group.redraw();
}
/**
* @return Returns the PGroup.
*/
public PGroup getGroup() {
return group;
}
/**
* Returns the height (in pixels) of the PGroup's font.
*
* @return the height (in pixels) of the PGroup's font
*/
protected int getFontHeight() {
return fontHeight;
}
/**
* Returns the width (in pixels) of the PGroup's text. This is the
* full string, not a truncated string relative to the PGroup's size.
*
* @return
*/
public int getTextWidth() {
return textWidth;
}
/**
* @return Returns the toggleStrategy.
*/
public IToggleStrategy getToggleStrategy() {
return toggleStrategy;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#fontChanged()
*/
public void fontChanged() {
GC gc = new GC(group);
fontHeight = gc.getFontMetrics().getHeight();
gc.dispose();
titleHeight = computeTitleHeight();
activeBounds = computeActiveBounds();
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#imageChanged()
*/
public void imageChanged() {
// TODO Auto-generated method stub
titleHeight = computeTitleHeight();
activeBounds = computeActiveBounds();
}
/* (non-Javadoc)
* @see com.swtplus.widgets.IGroupStrategy#textChanged()
*/
public void textChanged() {
GC gc = new GC(group);
textWidth = gc.textExtent(group.getText()).x;
gc.dispose();
titleHeight = computeTitleHeight();
activeBounds = computeActiveBounds();
}
/**
* @return Returns the titleHeight.
*/
public int getTitleHeight() {
return titleHeight;
}
/**
* @param titleHeight The titleHeight to set.
*/
public void setTitleHeight(int titleHeight) {
this.titleHeight = titleHeight;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -