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

📄 slat.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* Shutter - Friend List Component
*
* 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.qstyle;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
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.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import edu.tsinghua.lumaqq.widgets.BaseComposite;


/**
 * <pre>
 * Blind上的横条
 * 
 * <b>Style:</b>
 *      SWT.LEFT, SWT.RIGHT, SWT.CENTER
 * <b>Note:</b>
 * 		SWT.LEFT, SWT.RIGHT, 指定了SWT.CENTER属性
 *      相当于指定控件为Tab
 * </pre>
 *
 * @author luma
 */
public class Slat extends BaseComposite implements DisposeListener, PaintListener, ControlListener {	
    /**
     * 显示动画的线程
     * 
     * @author luma
     */
    public class Animate implements Runnable {
        private int frame;
        private Image[] frames;
        private volatile boolean stop;
        
        public void setFrames(Image[] images) {
            frame = 0;
            frames = images;
            stop = false;
        }        

        public void run() {
            try {                      
            	if(!stop) {
	                setImage(frames[frame]);
	                frame++;
	                if(frame >= frames.length)
	                    frame = 0;            		
            	}
                if(!stop)
                    getDisplay().timerExec(300, this);
            } catch (SWTException e) {         
                // 这个操作可能会抛出SWTException,如果组件已经dispose的话,
                // 所以我们需要捕捉这个异常,不然程序可能崩溃
            }
        }

		public void setStop(boolean stop) {
			this.stop = stop;
		}
    }

    /**
     * 闪烁图标的线程
     * 
     * @author luma
     */
    private class Blink implements Runnable {
        private Image blinkImage;
        private boolean flag;
        private volatile boolean stop;

        public void run() {
            try {                      
            	if(!stop) {
		            if(flag)
		                setImage(blinkImage);
		            else
		                setImage(null);
		            flag = !flag;            		
            	}
	            if(!stop)
	                getDisplay().timerExec(500, this);
            } catch (SWTException e) {         
                // 这个操作可能会抛出SWTException,如果组件已经dispose的话,
                // 所以我们需要捕捉这个异常,不然程序可能崩溃
            }
        }
        
        public void setStop(boolean stop) {
        	this.stop = stop;
        }
        
		public void setBlinkImage(Image blinkImage) {
			this.blinkImage = blinkImage;
            this.flag = true;
            this.stop = false;
		}
    }

    /**
     * 跳动图标的线程
     * 
     * @author luma
     */
    private class Bounce implements Runnable {
    	private volatile boolean stop;
        private int frame;
        private int[][] offset = new int[][] {
                {-1, 1}, {0, 0}, {1, 1}, {0, 0}
        };
        
        public void setBounceImage(Image bounceImage) {
            this.frame = 0;
            this.stop = false;
            setImage(bounceImage);
        }

        public void run() {
        	try {
	        	if(!stop) {
		            // 设置偏移
		            bX = offset[frame][0];
		            bY = offset[frame][1];
		            frame++;
		            if(frame >= offset.length)
		                frame = 0;
		            // 重画
		            redraw();        		
	        	}
	            if(!stop)
	                getDisplay().timerExec(300, this);        		
            } catch (SWTException e) {         
                // 这个操作可能会抛出SWTException,如果组件已经dispose的话,
                // 所以我们需要捕捉这个异常,不然程序可能崩溃
            }
        }

		public void setStop(boolean stop) {
			this.stop = stop;
		}
    }
    
    /**
     * 缺省的鼠标进入进出动作
     * 
     * @author luma
     */
    private class SlatMouseTrackListener extends MouseTrackAdapter {        
        public void mouseEnter(MouseEvent e) {
            enter = true;
            refreshForeground();
            redraw();
        }

        public void mouseExit(MouseEvent e) {
            enter = false;
            refreshForeground();
            redraw();
        }
    }
    
    /**
     * 缺省的鼠标动作
     * 
     * @author luma
     */
    private class SlatMouseListener extends MouseAdapter {
        public void mouseDown(MouseEvent e) {
            up = false;
            redraw();        		
        }
        
        public void mouseUp(MouseEvent e) {
            up = true;
            redraw(); 
        	forceFocus();
        }
    }
    
	// small and large image
	private Image image, imageBak;
	// image bound
	private Rectangle imageBound;
	// style
	private int style;
	// 是否按下
	private boolean up;
	// 鼠标是否进入控件区域
	private boolean enter;
	// 文字
	private String text;
	// 是否显示文字
	private boolean showText;
	// 文字的朝向
	private int textOrientation;
	// 文本编辑器
	private ControlEditor editor;
	// 事件监听器
	private List<ISlatListener> listeners;
	// 鼠标进入控件范围时显示的文字颜色
	private Color mouseTrackColor;
	// 表示是否正在闪烁或者跳动图标
	private boolean doEffect, blinking, bouncing, animating;
	// 跳动一个图标时,图标的偏移量
	private int bX, bY;
	// 图像和文字的外围矩形
	private Rectangle totalBound;
	// 客户区大小
	private Rectangle clientArea;
	// 老的前景色
	private Color oldForeground;
	// 特效任务
	private Blink blinkRunnable;
	private Bounce bounceRunnable;
	private Animate animateRunnable;
	
	private Color topColor;
	private Color bottomColor;
	private Color textColor;
	private Color borderColor;
	private Color underlayColor;
	private Color outBorderColor;
	private Color hoverTopColor;
	private Color hoverBottomColor;
	private Color downTopColor;
	private Color downBottomColor;

	public Slat(Composite parent) {
		this(parent, SWT.CENTER | SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
	}
	
    /**
     * @param parent parent
     * @param style style
     */
    public Slat(Composite parent, int style) {
		super(parent, SWT.NONE | SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
        init(style, null, null);
    }
    
	/**
	 * @param parent composite
	 * @param style style
	 * @param text text string
	 */
	public Slat(Composite parent, int style, String text) {
		super(parent, SWT.NONE | SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
	    init(style, text, null);
	}
	
	/**
	 * @param parent parent
	 * @param style style
	 * @param text text string
	 * @param smallImage small image
	 * @param largeImage large image
	 */
	public Slat(Composite parent, int style, String text, Image image) {
		super(parent, SWT.NONE | SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
		init(style, text, image);
	}

    /**
     * @param _style
     * @param _text
     * @param smallImage2
     * @param largeImage2
     */
    private void init(int _style, String _text, Image _image) {
		// 初始化image
        this.image = this.imageBak = _image;
        if(_image != null)
            imageBound = _image.getBounds();
        else 
        	imageBound = new Rectangle(0, 0, 0, 0);
		// 检查style
		checkStyle(_style);
		// 初始化变量
		up = true;
		enter = false;
		doEffect = blinking = bouncing = animating = false;
		showText = true;
		if(_text == null)
		    _text = "";
		this.text = _text;
		listeners = new ArrayList<ISlatListener>();
		bX = bY = 0;
		totalBound = new Rectangle(1, 1, 0, 0);
		refreshTotalBound();
		blinkRunnable = new Blink();
		bounceRunnable = new Bounce();
		animateRunnable = new Animate();
		topColor = new Color(getDisplay(), 0xD8, 0xF1, 0xFF);
		bottomColor = new Color(getDisplay(), 0xA8, 0xD8, 0xFF);
		textColor = new Color(getDisplay(), 0x1E, 0x3E, 0x93);
		borderColor = new Color(getDisplay(), 0x5E, 0x85, 0xB1);
		underlayColor = new Color(getDisplay(), 0xBB, 0xF4, 0xFF);
		outBorderColor = new Color(getDisplay(), 0xC6, 0xD4, 0xE3);
		hoverTopColor = new Color(getDisplay(), 0xF6, 0xFC, 0xFF);
		hoverBottomColor = new Color(getDisplay(), 0xC0, 0xE4, 0xFF);
		downTopColor = new Color(getDisplay(), 0x9B, 0xC8, 0xEE);
		downBottomColor = new Color(getDisplay(), 0xC4, 0xE3, 0xF4);
        // set layout
    	GridLayout layout = new GridLayout();
    	layout.horizontalSpacing = layout.verticalSpacing = 0;
    	layout.marginHeight = layout.marginWidth = 0;
        this.setLayout(layout);
        setBackground(getParent().getBackground());
		// 添加监听器
		addDisposeListener(this);
		addPaintListener(this);
		addControlListener(this);
		super.addMouseListener(new SlatMouseListener());
		super.addMouseTrackListener(new SlatMouseTrackListener());
    }
    
    /**
     * 处理编辑时焦点丢失事件
     * 
     * @param e
     */
    protected void onFocusLost(FocusEvent e) {
		// new a event object
		SlatEvent event = new SlatEvent(this);
		event.oldText = getText();
		if(event.oldText == null)
		    event.oldText = "";
		
		// set new text
		Text t = (Text)e.getSource();
		setText(t.getText());
		disposeEditor();
		
		// if text changed, fire event
		if(!event.oldText.equals(getText())) {
			event.newText = getText();
			for(ISlatListener listener : listeners)
				listener.textChanged(event);
		}
	}

	/**
     * 处理编辑时按键按下事件
     * 
     * @param e
     */
    protected void onKeyPressed(KeyEvent e) {
		if(e.keyCode == SWT.CR) {
			// new a event object
			SlatEvent event = new SlatEvent(this);
			event.oldText = getText();
			if(event.oldText == null)
			    event.oldText = "";
			
			// set new text
			Text t = (Text)e.getSource();
			setText(t.getText());
			disposeEditor();
			
			// if text changed, fire event
			if(!event.oldText.equals(getText())) {
				event.newText = getText();
				for(ISlatListener listener : listeners)
					listener.textChanged(event);
			}
		} else if(e.keyCode == SWT.ESC) {
			disposeEditor();
		}
	}
    
    /**
     * 释放编辑器资源
     */
    private void disposeEditor() {
    	if(editor != null) {    		
    		if(editor.getEditor() != null) 
    			editor.getEditor().dispose();
    		editor.dispose();
    		editor = null;
    	}
    }

	/**
     * 检查style
     * 
     * @param _style
     */
    private void checkStyle(int _style) {
		// 检查style,只允许下列值
		this.style = _style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
		// 检查style看看文本位置设置了没有,没有则用缺省的right
	    textOrientation = this.style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
	    if(textOrientation == 0) 
	    	textOrientation = SWT.CENTER;
    }
    
    /**
     * 添加一个listener
     * @param listener
     */
    public void addSlatListener(ISlatListener listener) {
        listeners.add(listener);
    }
    
    /**
     * 移除一个listener
     * @param listener
     */
    public void removeShutterLabelListener(ISlatListener listener) {
        listeners.remove(listener);
    }
    
    /**
     * 使编辑框显示出来,以便于编辑文本
     */
    public void editText() {
    	if(editor == null) {
    		editor = new ControlEditor(this);
    		editor.grabHorizontal = editor.grabVertical = true;
    	}
    	Text t = new Text(this, SWT.NONE);
    	t.setText(text);
    	t.selectAll();
    	t.addKeyListener(new KeyAdapter() {
    		public void keyPressed(KeyEvent e) {
    			onKeyPressed(e);
    		}
    	});
    	t.addFocusListener(new FocusAdapter() {
    		public void focusLost(FocusEvent e) {
    			onFocusLost(e);
    		}
    	});
    	editor.setEditor(t);
    	t.setFocus();
    }
    
    /**
     * 设置是否显示文字
     * @param show true表示显示文字
     */
    public void setShowText(boolean show) {
    	if(showText != show) {
    		showText = show;
    		redraw();

⌨️ 快捷键说明

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