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

📄 qbutton.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import edu.tsinghua.lumaqq.Colors;

/**
 * QQ 2004 style button
 * 
 * @author luma
 */
public class QButton extends Composite implements PaintListener, MouseTrackListener, MouseListener {
    private boolean hover;
    private boolean down;
    private String text;
    private int marginWidth;
    private int marginHeight;
    private int style;
    
    private static final String ELLIPSIS = "...";
    
    /**
     * Create a QButton
     * 
     * @param parent
     * @param style
     */
    public QButton(Composite parent, int style) {
        super(parent, SWT.NONE);
        this.style = style;
        checkStyle();
        if(isArrow()) {
            text = getArrowString();
            marginWidth = 0;
        } else {
            text = "";
            marginWidth = 5;
        }
        down = hover = false;
        marginHeight = 2;
        addPaintListener(this);
        addMouseListener(this);
        addMouseTrackListener(this);
    }
    
    /**
     * @return
     * 		箭头字符
     */
    private String getArrowString() {
        if(style == SWT.ARROW_DOWN)
            return "↓";
        else
            return "↑";
    }
    
    /**
     * 检查样式标志
     * 
     * @param style
     */
    private void checkStyle() {
        style &= SWT.NONE | SWT.ARROW_DOWN | SWT.ARROW_UP;
    }
    
    /**
     * @return
     * 		true表示按钮是箭头按钮
     */
    private boolean isArrow() {
        return style != SWT.NONE;
    }
    
    /**
     * @param parent
     */
    public QButton(Composite parent) {
        this(parent, SWT.NONE);
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
     */
    public void paintControl(PaintEvent e) {
        Point size = getSize();
        if(size.x <= 0 || size.y <= 0)
            return;
        e.gc.setBackground(getParent().getBackground());
        e.gc.fillRectangle(0, 0, size.x, size.y);
        
        paintGradientBackgroud(e.gc, size, hover ? Colors.QBUTTON_HOVER_BACKGROUD : Colors.QBUTTON_BACKGROUND);
        paintBorder(e.gc, size);
        paintText(e.gc, size);
    }
    
    /**
     * 画边框
     * 
     * @param gc
     */
    private void paintBorder(GC gc, Point size) {
        gc.setForeground(Colors.QBUTTON_BORDER);
        gc.drawRoundRectangle(0, 0, size.x - 1, size.y - 1, 6, 6);
    }
    
    /**
     * 画渐变背景
     * 
     * @param gc
     */
    private void paintGradientBackgroud(GC gc, Point size, Color c) {
        gc.setForeground(Colors.WHITE);
        gc.setBackground(c);
        gc.fillGradientRectangle(1, 1, size.x - 2, size.y - 2, true);
    }
    
    /**
     * 画文本
     * 
     * @param gc
     * @param size
     */
    private void paintText(GC gc, Point size) {
        String s = text;
        Point extent = gc.textExtent(s);
        if(extent.x > getAvailableWidth()) {
	        s = shortenText(gc, text, getAvailableWidth());
	        extent = gc.textExtent(s);            
        }
        
        size.x = (size.x - extent.x) / 2;
        size.y = (size.y - extent.y) / 2;
        
        if(!down) {
            size.x--;
            size.y--;
        }
        
        gc.setForeground(isEnabled() ? Colors.BLACK : Colors.GRAY);
        gc.drawString(s, size.x, size.y, true);
    }
    
    /* (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
	 */
	public void setEnabled(boolean b) {
		super.setEnabled(b);
		redraw();
	}
    
    /**
     * @return
     * 		可用的画文本的最大宽度
     */
    private int getAvailableWidth() {
        Point size = getSize();
        // 减去边框
        size.x -= 4;
        // 减去边缘
        size.x -= marginWidth << 1;
        // 如果小于0
        if(size.x < 0)
            size.x = 0;
        return size.x;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Control#computeSize(int, int)
     */
    public Point computeSize(int wHint, int hHint) {
        Point size = getTotalSize();
        if(wHint != SWT.DEFAULT)
            size.x = wHint;
        if(hHint != SWT.DEFAULT)
            size.y = hHint;
        return size;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Composite#computeSize(int, int, boolean)
     */
    public Point computeSize(int wHint, int hHint, boolean changed) {        
        return computeSize(wHint, hHint);
    }
    
    /**
     * @return
     * 		控件最优大小
     */
    private Point getTotalSize() {
    	Point size = new Point(0, 0);
    	GC gc = new GC(this);
    	if (text != null && text.length() > 0) {
    		Point e = gc.textExtent(text);
    		size.x += e.x;
    		size.y = Math.max(size.y, e.y);
    	} else {
    		size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
    	}
    	
    	// 加上margin
		size.x += (marginWidth << 1) + 4;
		size.y += (marginHeight << 1) + 4;
		
		if(size.y < 20)
		    size.y = 20;
		
		gc.dispose();
    	return size;
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseTrackListener#mouseEnter(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseEnter(MouseEvent e) {
        hover = true;
        redraw();
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseTrackListener#mouseExit(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseExit(MouseEvent e) {
        hover = false;
        redraw();
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseHover(MouseEvent e) {
    }
    
    /**
     * @return Returns the text.
     */
    public String getText() {
        return text;
    }
    
    /**
     * @param text The text to set.
     */
    public void setText(String text) {
        if(!isArrow()) {
	        this.text = text;
	        redraw();            
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseDoubleClick(MouseEvent e) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseDown(MouseEvent e) {
        down = true;
        redraw();
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
     */
    public void mouseUp(MouseEvent e) {
        down = false;
        redraw();
    }
    
    /**
     * Shorten the given text <code>t</code> so that its length doesn't exceed
     * the given width. The default implementation replaces characters in the
     * center of the original string with an ellipsis ("...").
     * Override if you need a different strategy.
     */
    protected String shortenText(GC gc, String t, int width) {
    	if (t == null) return null;
    	int w = gc.textExtent(ELLIPSIS).x;
    	int l = t.length();
    	int pivot = l/2;
    	int s = pivot;
    	int e = pivot+1;
    	while (s >= 0 && e < l) {
    		String s1 = t.substring(0, s);
    		String s2 = t.substring(e, l);
    		int l1 = gc.textExtent(s1).x;
    		int l2 = gc.textExtent(s2).x;
    		if (l1+w+l2 < width) {
    			t = s1 + ELLIPSIS + s2;
    			break;
    		}
    		s--;
    		e++;
    	}
    	return t;
    }
    
    /**
     * @return Returns the marginHeight.
     */
    public int getMarginHeight() {
        return marginHeight;
    }
    /**
     * @param marginHeight The marginHeight to set.
     */
    public void setMarginHeight(int marginHeight) {
        this.marginHeight = marginHeight;
    }
    /**
     * @return Returns the marginWidth.
     */
    public int getMarginWidth() {
        return marginWidth;
    }
    /**
     * @param marginWidth The marginWidth to set.
     */
    public void setMarginWidth(int marginWidth) {
        this.marginWidth = marginWidth;
    }
}

⌨️ 快捷键说明

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