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

📄 slat.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    	}
    }
    
    /**
     * 设置鼠标进入控件范围时显示的文字颜色
     * @param color 鼠标进入控件范围时显示的文字颜色
     */
    public void setMouseTrackColor(Color color) {
        this.mouseTrackColor = color;
    }
 
    /* (non-Javadoc)
	 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
	 */
	public void widgetDisposed(DisposeEvent e) {
		disposeEditor();
		image = imageBak = null;
		imageBound = null;
		text = null;
		listeners = null;
		mouseTrackColor = null;
		totalBound = null;
		clientArea = null;
		oldForeground = null;
		blinkRunnable = null;
		bounceRunnable = null;
		animateRunnable = null;
		topColor.dispose();
		bottomColor.dispose();
		borderColor.dispose();
		textColor.dispose();
		outBorderColor.dispose();
		underlayColor.dispose();
		hoverTopColor.dispose();
		hoverBottomColor.dispose();
		downTopColor.dispose();
		downBottomColor.dispose();
	}

    /* (non-Javadoc)
     * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
     */
    public void paintControl(PaintEvent event) {
        // 得到客户区大小
        if(clientArea.width == 0 || clientArea.height == 0)
            return;
        
        GC gc = event.gc;
        
        // 填充背景        
        gc.setBackground(getBackground());
        gc.fillRectangle(clientArea);
        
        // 画衬底
        gc.setBackground(underlayColor);
        gc.fillRectangle(1, 2, clientArea.width - 2, clientArea.height - 3);
        
        // 画主色
        if(!up) {
        	gc.setForeground(downTopColor);
        	gc.setBackground(downBottomColor);
        } else if(enter) {
        	gc.setForeground(hoverTopColor);
        	gc.setBackground(hoverBottomColor);
        } else {
        	gc.setForeground(topColor);
        	gc.setBackground(bottomColor);        	
        }
        gc.fillGradientRectangle(2, 
        		2, 
        		clientArea.width - 4, 
        		clientArea.height - 4, 
        		true);
        
        // 画外边框
        gc.setForeground(outBorderColor);
        gc.drawRoundRectangle(0, 
        		0, 
        		clientArea.width - 1, 
        		clientArea.height - 1, 
        		4, 
        		4);
        
        // 画内边框
        gc.setForeground(borderColor);        
        gc.drawRoundRectangle(0, 
        		0, 
        		clientArea.width - 1, 
        		clientArea.height - 1, 
        		6, 
        		6);
        
        // 画图标,先计算图标位置
        int availableHeight = getAvailableHeight();
        int x = 0, y = 0;
        // 不为null则画图标,这里画完后,x的值将为
        if(image != null) { 
        	switch(textOrientation) {
        		case SWT.RIGHT:
        		case SWT.CENTER:
        			x = 2;
        			y = 2;
        			break;
        		case SWT.LEFT:
        			x = clientArea.width - 2 - imageBound.width;
        			y = 2;
        			break;
        	}
        	int drawWidth = (imageBound.width > availableHeight) ? availableHeight : imageBound.width;
        	if(imageBound.height > availableHeight)
        		gc.drawImage(image, 
        				0, 
        				0, 
        				imageBound.width, 
        				imageBound.height, 
        				x + bX, 
        				y + bY, 
        				drawWidth, 
						availableHeight);
        	else
        		gc.drawImage(image, 
        				0, 
        				0, 
        				imageBound.width, 
        				imageBound.height, 
        				x + bX, 
        				y + bY + ((availableHeight - imageBound.height) >>> 1), 
        				drawWidth, 
        				imageBound.height);
            x = availableHeight;
        }
        
        // 画文字
        if(!showText)        	
        	return;
        
        // 计算可用宽度,如果宽度不够,截短文字
        boolean shorten = false;
        String t = text;
        int availableWidth = clientArea.width - 2;
        Point extent = getTotalSize();
        if(extent.x > availableWidth) 
            shorten = true;
        // 如果需要截短文字,得到截短后的文字
        if(shorten) 
            t = shortenText(gc, t, availableWidth);

        int h = gc.getFontMetrics().getHeight();
        int w = gc.textExtent(t, SWT.DRAW_TRANSPARENT).x;

        // 设置文本前景色,如果设置了鼠标hover时的文本色,则使用
        if(!isEnabled())
        	gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
        else if(enter && mouseTrackColor != null)
        	gc.setForeground(mouseTrackColor);
        else
        	gc.setForeground(textColor);
        
        if(doEffect) 
        	x = imageBound.width;
        switch(textOrientation) {
        	case SWT.LEFT:
        		gc.drawString(t, clientArea.width - 1 - x - 5 - w, (clientArea.height - h) / 2, true);
        		break;
        	case SWT.RIGHT:
        		gc.drawString(t, 1 + x + 5, (clientArea.height - h) / 2, true);
        		break;
        	case SWT.CENTER:
        		gc.drawString(t, (clientArea.width - w) / 2, (clientArea.height - h) / 2, true);
        		break;
        }
    }
    
    /**
     * @return
     * 		可用高度
     */
    private int getAvailableHeight() {
    	if(clientArea == null)
    		clientArea = getClientArea();
    	return clientArea.height - 4;
	}

	/**
     * 计算截短的字符串,从中间开始不断减少,中间填省略号,直到宽度合适未知
     * 
     * @param gc
     * @param t
     * @param width
     * @return
     */
    protected String shortenText(GC gc, String t, int width) {
        if(t == null)
            return null;
        int w = gc.textExtent("...").x;
        int l = t.length();
        int pivot = l / 2;
        int s = pivot;
        for(int e = pivot + 1; s >= 0 && e < l; e++) {
            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 + "..." + s2;
                break;
            }
            s--;
        }

        return t;
    }
    
    /**
     * @return Returns the style.
     */
    public int getStyle() {
        return style;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
     */
	@Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        Point e = getTotalSize();
        if(wHint == SWT.DEFAULT)
            e.x += 2;
        else
            e.x = wHint;
        if(hHint == SWT.DEFAULT)
            e.y += 2;
        else
            e.y = hHint;
        return e;
    }
    
    /**
     * 得到image和text的总大小
     * 
     * @return
     * 		组件大小
     */
    private Point getTotalSize() {
        Point size = new Point(0, 0);
        size.y = getAvailableHeight();
        size.x = (image == null) ? 0 : size.y;
        
        GC gc = new GC(this);
        if(text.length() > 0 && showText) {
            Point e = gc.textExtent(text, SWT.DRAW_TRANSPARENT);
            // 在文字两旁留5个象素的空隙
            size.x += e.x + 10;
        } 
        size.y = Math.max(size.y, gc.getFontMetrics().getHeight() + 4);
        gc.dispose();
        return size;
    }
    
    /**
     * @return Returns the text.
     */
    public String getText() {
        return text;
    }
    
    /**
     * @param text The text to set.
     */
    public void setText(String text) {
        if(text == null)
            text = "";
        else
        	this.text = text;
        if(showText) {
            // 重新设置image和text的总体外围矩形
            refreshTotalBound();
            // 重画
	        redraw();            
        }
    }
    
    /**
     * @return Returns the textOrientation.
     */
    public int getTextOrientation() {
        return textOrientation;
    }
    
    /**
     * @param textOrientation The textOrientation to set.
     */
    public void setTextOrientation(int textOrientation) {
        this.textOrientation = textOrientation;
        refreshTotalBound();
        redraw();
    }
    
    /**
     * @return Returns the image.
     */
    public Image getImage() {
        return image;
    }
    
    /**
     * @param image The image to set.
     */
    public void setImage(Image image) {
        this.image = image;
        if(image != null) 
        	this.imageBound = image.getBounds();
        
        // 重新设置image和text的总体外围矩形
        refreshTotalBound();
        // 重画
        redraw();
    }
    
    /**
     * 闪烁一个图标
     * @param blinkImage 要闪烁的图标
     */
    public void startBlinkImage(Image blinkImage) {
        if(doEffect || blinkImage == null) return;
        doEffect = blinking = true;
        this.getParent().layout();
        imageBak = image;
        blinkRunnable.setBlinkImage(blinkImage);
        getDisplay().timerExec(0, blinkRunnable);
    }
    
    /**
     * 停止闪烁图标
     */
    public void stopBlinkImage() {
        if(!blinking) return;
        blinkRunnable.setStop(true);
        doEffect = blinking = false;        
        setImage(imageBak);
        this.getParent().layout();
    }
    
    /**
     * 跳动一个图标
     * @param bounceImage 要跳动的图标
     */
    public void startBounceImage(Image bounceImage) {        
        if(doEffect || bounceImage == null) return;
        doEffect = bouncing = true;
        this.getParent().layout();
        imageBak = image;
        bounceRunnable.setBounceImage(bounceImage);
        getDisplay().timerExec(0, bounceRunnable);
    }
    
    /**
     * 停止跳动图标
     */
    public void stopBounceImage() {
        if(!bouncing) return;
        bounceRunnable.setStop(true);
        doEffect = bouncing = false;
        bX = bY = 0;
        setImage(imageBak);
        this.getParent().layout();
    }
    
    /**
     * 开始动画
     * @param images 动画帧
     */
    public void startAnimate(Image[] images) {
        if(doEffect || images == null) return;
        doEffect = animating = true;
        this.getParent().layout();
        imageBak = image;
        animateRunnable.setFrames(images);
        getDisplay().timerExec(0, animateRunnable);
    }
    
    /**
     * 停止动画
     */
    public void stopAnimate() {
        if(!animating) return;
        animateRunnable.setStop(true);
        doEffect = animating = false;
        setImage(imageBak);
        this.getParent().layout();
    }
    
    // 重新设置image和text的总体外围矩形
    private void refreshTotalBound() {
        Point extent = getTotalSize();
        totalBound.width = extent.x;
        totalBound.height = extent.y;
		clientArea = getClientArea();
		if(textOrientation == SWT.LEFT)
			totalBound.x = clientArea.width - totalBound.width;
		else
			totalBound.x = 2;
    }
    
    // 根据鼠标是否在图像和文字之上重新设置前景色
    private void refreshForeground() {
		if(mouseTrackColor != null) {
			if(oldForeground == null) oldForeground = getForeground();
			if(enter) 
				setForeground(mouseTrackColor);						
			else {
				setForeground(oldForeground);
				oldForeground = null;
			}
		}
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
     */
	@Override
    public void setEnabled(boolean enabled) {
    	super.setEnabled(enabled);
    	redraw();
    }
    
    /**
     * 设置选择状态
     * @param selection
     */
    public void setSelection(boolean selection) {
    	if(this.up == !selection) return;
    	this.up = !selection;
    	redraw();
    }
    
    /**
     * 返回当前选择状态
     * @return true如果被按下
     */
    public boolean getSelection() {
    	return !up;
    }
    
    /* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlMoved(ControlEvent e) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlResized(ControlEvent e) {
		refreshTotalBound();
	}
	
	/**
	 * @return Returns the animating.
	 */
	public boolean isAnimating() {
		return animating;
	}
	
	/**
	 * @return Returns the blinking.
	 */
	public boolean isBlinking() {
		return blinking;
	}
	
	/**
	 * @return Returns the bouncing.
	 */
	public boolean isBouncing() {
		return bouncing;
	}
	
	/**
	 * @return Returns the showText.
	 */
	public boolean isShowText() {
		return showText;
	}
}

⌨️ 快捷键说明

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