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

📄 listbarliststrategy.java

📁 利用它可以做出非常漂亮的swt界面,包含的组件有PShelf Plist
💻 JAVA
字号:
package com.swtplus.widgets.list;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

import com.swtplus.internal.FontUtils;
import com.swtplus.internal.PGC;
import com.swtplus.internal.GraphicUtils;
import com.swtplus.widgets.PListItem;
import com.swtplus.widgets.PList;

/**
 * ListBarListStrategy draws ListItems with the image above the text, all
 * of which are centered.  When selected the item is drawn with a gradient
 * background (based on the platform's selection color). 
 */
public class ListBarListStrategy implements IListStrategy {
    
	/**
	 * Shows a faded background when items are hovered over.
	 */
	public final static int HOVER = 1 << 1;
	/**
	 * Rounds the corners of the selection rectangle.
	 */
	public final static int ROUNDED = 1 << 2;
	
    private PList parent;
    private int height = 0;
    private int hMargin1 = 12;
    private int hMargin2 = 5;
    private int hMargin3 = 11;
    
    private Color hoverBackgroundColor;
    
    private Color hoverBorderColor;
    
    private Color selectedBackgroundColor;
    private Color selectedBackgroundShadowColor;
    
    private Font boldFont;
    
    private boolean showHover = false;
    private boolean rounded = false;
	private boolean inited;

    /**
     * Creates a ListBarListStrategy with the given style.
     * 
     * @param style style
     */
    public ListBarListStrategy(int style){
		if ((style & HOVER) == HOVER){
            showHover = true;
		}
		if ((style & ROUNDED) == ROUNDED){
            rounded = true;
		}

    }

    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#computeSize(com.swtplus.widgets.list.ListItem, com.swtplus.internal.GCPlus, int, int)
     */
    public Point computeSize(PListItem item,PGC gc, int wHint, int hHint) {
        if (item.getImage() != null){
            height = hMargin1 + item.getImage().getBounds().height + hMargin2 + gc.getFontMetrics().getHeight() + hMargin3;
        } else {
            height = hMargin2 + gc.getFontMetrics().getHeight() + hMargin3;
        }

        return new Point(0,height);
    }


    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#paintItem(com.swtplus.widgets.list.ListItem, com.swtplus.internal.GCPlus, boolean, boolean, boolean, org.eclipse.swt.graphics.Point)
     */
    public void paintItem(PListItem item, PGC gc, boolean selected,
            boolean focused, boolean hovering,Point size) {

        int x = 0,y = 0;
        
        int itemHeight = 0;
        if (item.getImage() != null){
            itemHeight = hMargin1 + item.getImage().getBounds().height + hMargin2 + gc.getFontMetrics().getHeight() + hMargin3;
        } else {
            itemHeight = hMargin2 + gc.getFontMetrics().getHeight() + hMargin3;
        }
        
        
        Color back = gc.getBackground();
        Color fore = gc.getForeground();
        
        if (selected){
            
            gc.fillRectangle(0,0,size.x,itemHeight);
            
            gc.setBackground(selectedBackgroundColor);
            gc.setForeground(selectedBackgroundShadowColor);
            gc.fillGradientRectangle(0,0,size.x,itemHeight,true);
            
            gc.setForeground(parent.getSelectionColor());
            
            if (!rounded){
				gc.drawRectangle(0,0,size.x -1,itemHeight -1);
            } else {
            	GraphicUtils.drawRoundRectangle(gc.getGc(),gc.getXOffset() ,gc.getYOffset(),size.x -1,itemHeight -0,parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
            }
			
            gc.setBackground(back);
            gc.setForeground(fore);
        } else {
            
            gc.fillRectangle(0,0,size.x,itemHeight);
            
            if (showHover && hovering){
            	gc.setBackground(hoverBackgroundColor);
            	gc.fillRectangle(0,0,size.x,itemHeight);
            	
                gc.setForeground(hoverBorderColor);
                if (!rounded){
                	gc.drawRectangle(0,0,size.x - 1,itemHeight - 1);
                } else {
                	GraphicUtils.drawRoundRectangle(gc.getGc(),gc.getXOffset(),gc.getYOffset(),size.x - 1,itemHeight - 0,parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
                }
                gc.setForeground(fore);
            }   
            
        }            
                
        
 
        if (item.getImage() != null){
            Image img = item.getImage();
            
            x = (size.x - img.getBounds().width)/2;
            y = hMargin1;
            
            gc.drawImage(img,0,0,img.getBounds().width,img.getBounds().height,x,y,img.getBounds().width,img.getBounds().height);
        }
        
        String text = gc.getShortString(item.getText(),parent.getSize().x - 15);
        
        y = hMargin1 + item.getImage().getBounds().height + hMargin2;
                
        if (selected){
        	gc.setForeground(parent.getSelectionTextColor());
        	gc.setFont(boldFont);
        }
        
        x = (size.x - gc.textExtent(text).x)/2;
        
        gc.drawString(text,x,y,true);

    }


    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#checkItem(com.swtplus.widgets.list.ListItem)
     */
    public void checkItem(PListItem item) {
    }



    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#initialize(com.swtplus.widgets.list.PList)
     */
    public void initialize(PList pList) {
        parent = pList;        
        
        inited = false;
        parent.setSelectionColor(pList.getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND));	
        inited = true;
        
        hoverBackgroundColor = GraphicUtils.createNewBlendedColor(pList.getSelectionColor(),pList.getDisplay().getSystemColor(SWT.COLOR_WHITE),15);        
        hoverBorderColor = GraphicUtils.createNewBlendedColor(pList.getSelectionColor(),pList.getDisplay().getSystemColor(SWT.COLOR_WHITE),50);
        
        
        
		Color baseColor = pList.getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND);		
		baseColor = GraphicUtils.createNewBlendedColor(baseColor,pList.getDisplay().getSystemColor(SWT.COLOR_WHITE),60);
		
		
		selectedBackgroundColor = GraphicUtils.createNewSaturatedColor(baseColor,.00f);
		selectedBackgroundShadowColor = GraphicUtils.createNewSaturatedColor(baseColor,-.04f);
		
		baseColor.dispose();
		
		boldFont = FontUtils.createBoldFont(pList.getFont());

		//c.setCursor(c.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
    }

    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#isHoverOnUnselected()
     */
    public boolean isHoverOnUnselected() {
        return showHover;
    }

    /* (non-Javadoc)
     * @see com.swtplus.widgets.list.IListStrategy#dispose()
     */
    public void dispose() {
        if (hoverBackgroundColor != null)
        	hoverBackgroundColor.dispose();

        if (hoverBorderColor != null)
        	hoverBorderColor.dispose();
        
        if (selectedBackgroundColor != null)
    		selectedBackgroundColor.dispose();
        
        if (selectedBackgroundShadowColor != null)
        	selectedBackgroundShadowColor.dispose();

        if (boldFont != null)
        	boldFont.dispose();
        
    }

	/* (non-Javadoc)
	 * @see com.swtplus.widgets.list.IListStrategy#fontChanged()
	 */
	public void fontChanged() {
		boldFont.dispose();
		
		boldFont = FontUtils.createBoldFont(parent.getFont());
		
	}

	/* (non-Javadoc)
	 * @see com.swtplus.widgets.list.IListStrategy#colorChanged()
	 */
	public void colorChanged() {
		
		if (!inited)
			return;
		
		hoverBackgroundColor.dispose();
		hoverBorderColor.dispose();
		selectedBackgroundColor.dispose();
		selectedBackgroundShadowColor.dispose();

		hoverBackgroundColor = GraphicUtils.createNewBlendedColor(parent.getSelectionColor(),parent.getDisplay().getSystemColor(SWT.COLOR_WHITE),15);        
        hoverBorderColor = GraphicUtils.createNewBlendedColor(parent.getSelectionColor(),parent.getDisplay().getSystemColor(SWT.COLOR_WHITE),50);
        
        
		//Color baseColor = parent.getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND);
        Color baseColor = parent.getSelectionColor();
		baseColor = GraphicUtils.createNewBlendedColor(baseColor,parent.getDisplay().getSystemColor(SWT.COLOR_WHITE),60);
		
		selectedBackgroundColor = GraphicUtils.createNewSaturatedColor(baseColor,.04f);
		selectedBackgroundShadowColor = GraphicUtils.createNewSaturatedColor(baseColor,-.04f);
		
		baseColor.dispose();
		
	}

}

⌨️ 快捷键说明

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