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

📄 titleanddescliststrategy.java

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

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.Assert;
import com.swtplus.internal.FontUtils;
import com.swtplus.internal.PGC;
import com.swtplus.widgets.PListItem;
import com.swtplus.widgets.PList;

/**
 * TitleAndDescListStrategy draw a list item with the image to the left,
 * followed by a bold title and underneath the title is the description.
 * The description will wrap if necessary.
 * <p>
 * To add the description text to a ListItem, you must call the setData()
 * method as:
 * <p>
 * <code>
 * ListItem listItem = new ListItem(pList,SWT.NONE);<BR>
 * listItem.setImage(myImage);<BR>
 * listItem.setText("The Title");<BR>
 * listItem.setData(TitleAndDescListStrategy.DESCRIPTION,"The description here.");<BR>
 * </code>
 */
public class TitleAndDescListStrategy implements IListStrategy {
    
	/**
	 * Key to be used when setting the description data in
	 * the ListItem.
	 */
	public static final String DESCRIPTION = "description";

    private Font titleFont;
    private int margin = 5;
    private int hSpacing = 7;
    private int textVSpacing = 3;

	private Font intialTitleFont;

	private PList parent;
	
    /**
     * Creates a TitleAndDescListStrategy with the given style.
     * 
     * @param style style
     */
    public TitleAndDescListStrategy(int style){
    	
    }
    
    /* (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) {
        
        int height = 0;
        Point p = new Point(margin,margin);
        
        Font old = gc.getFont();
        gc.setFont(titleFont);
        int fontHeight = gc.getFontMetrics().getHeight();
        gc.setFont(old);
        
        p.y += fontHeight + textVSpacing;
        
        if (item.getImage() != null){
            p.x += item.getImage().getBounds().width + hSpacing;
        }
        
        int width = wHint - p.x - margin;
        
        Point p2 = gc.computeTextWrapSize(getItemDescription(item),width);
        
        height = p.y + p2.y + margin;
        
        if (item.getImage() != null){
            height = Math.max(height,margin + item.getImage().getBounds().height + margin);
        }
        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;
        
        Color back = gc.getBackground();
        if (selected){            
            gc.setBackground(parent.getSelectionColor());
        }        
        gc.fillRectangle(0,0,size.x,size.y);
        if (selected)
            gc.setBackground(back);
        
        if (focused)
            gc.drawFocus(0,0,size.x,size.y);
        
        x = margin;
        y = margin;
        
        if (item.getImage() != null){
            Image img = item.getImage();
           
            gc.drawImage(img,margin,margin);    
            
            x += img.getBounds().width + hSpacing;
            
        }
        
        if (selected)
            gc.setForeground(parent.getSelectionTextColor());
        
        Font old = gc.getFont();
        gc.setFont(titleFont);
        
        String text = gc.getShortString(item.getText(),size.x - x);
        gc.drawString(text,x,y,true);
        
        
        y += gc.getFontMetrics().getHeight() + textVSpacing;
        
        gc.setFont(old);
        
        gc.drawWrapText(getItemDescription(item),x,y,size.x - margin - x);
        
    }


    /* (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;
    	
    	intialTitleFont = FontUtils.createBoldFont(pList.getFont());
        setTitleFont(intialTitleFont);
    }


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

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

    
    private String getItemDescription(PListItem item){
        Object text = item.getData(DESCRIPTION);
        if (text == null)
            return "";
        
        Assert.isTrue(text instanceof String, "Description data not of type String!  Description data must be String.");

        return (String)text;
    }

	/**
	 * Returns the title font.
	 * @return the title font
	 */
	public Font getTitleFont() {
		return titleFont;
	}

	/**
	 * Sets the title font.
	 * @param titleFont the title font
	 */
	public void setTitleFont(Font titleFont) {
		this.titleFont = titleFont;
	}


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


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

⌨️ 快捷键说明

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