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

📄 friendselectshell.java

📁 类似于MSN
💻 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.shells;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.events.FriendSelectionEvent;
import edu.tsinghua.lumaqq.events.FriendSelectionListener;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.GroupModel;
import edu.tsinghua.swt.models.ShutterModel;

/**
 * <pre>
 * 把好友显示在树状图中供用户选择的窗口,每当一个好友被选择时,其会
 * 触发一个好友选择事件,事件类是FriendSelectionEvent
 * </pre>
 * 
 * @author 马若劼
 */
public class FriendSelectShell implements ControlListener {
	private Shell shell;
	private Shell parent;
	private Display display;
	private IconHolder icons;
	// 界面控件
	private Tree tree;
	// 事件监听器
	private List listeners;
	
	/**
	 * 构造函数
	 * @param parent
	 */
	public FriendSelectShell(Shell parent) {
		this.parent = parent;
		display = parent.getDisplay();
		shell = new Shell(parent, SWT.BORDER | SWT.RESIZE);
		shell.setLayout(new FormLayout());
		// 添加自己为parent的监听器以能够在parent改变位置的时候也改变位置
		parent.addControlListener(this);
		
		// 初始化变量
		icons = IconHolder.getInstance();
		listeners = new ArrayList();
		
		// 初始化窗口布局
		initLayout();
	}

	/**
	 * 设置窗口的位置,使其附着在父窗口旁
	 */
	private void setLocation() {
		Rectangle bound = parent.getBounds();
		shell.setLocation(bound.x + bound.width, bound.y);
	}
	
	/**
	 * 设置窗口的大小
	 */
	private void setSize() {
		Point parentSize = parent.getSize();
		shell.setSize(200, parentSize.y);
	}
	
	/**
	 * 初始化窗口布局
	 */
	private void initLayout() {
		// 好友树
		tree = new Tree(shell, SWT.CHECK | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		FormData fd = new FormData();
		fd.top = fd.left = new FormAttachment(0, 0);
		fd.right = fd.bottom = new FormAttachment(100, 0);
		tree.setLayoutData(fd);
		tree.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if(e.detail == SWT.CHECK) {
						// 产生List对象
						List models = new ArrayList();
						// 得到被选择的model
						TreeItem ti = (TreeItem)e.item;
						Object modelObj = ti.getData();
						// 检查model的类型
						if(modelObj instanceof FriendModel) {
							models.add(modelObj);
							FriendSelectionEvent event = new FriendSelectionEvent(models);
							if(ti.getChecked())
								fireFriendSelectedEvent(event);
							else
								fireFriendDeselectedEvent(event);
						} else {
							if(ti.getChecked()) {
								// 把它的孩子都设为check状态
								TreeItem[] children = ti.getItems();
								if(children != null) {
									// 没有check的,我们才添加到list中
									for(int i = 0; i < children.length; i++) {
										if(!children[i].getChecked()) {
											children[i].setChecked(true);
											models.add(children[i].getData());
										}
									}									
									// 触发事件
									FriendSelectionEvent event = new FriendSelectionEvent(models);
									fireFriendSelectedEvent(event);
								}
							} else {
								// 把它的孩子都设为uncheck状态
								TreeItem[] children = ti.getItems();
								if(children != null) {
									// 已经check的,我们才添加到list中
									for(int i = 0; i < children.length; i++) {
										if(children[i].getChecked()) {
											children[i].setChecked(false);
											models.add(children[i].getData());
										}
									}
									// 触发事件
									FriendSelectionEvent event = new FriendSelectionEvent(models);
									fireFriendDeselectedEvent(event);
								}
							}
						}							
					}
				}
			}
		);
		shell.layout();
	}
	
	/**
	 * 设置Model,这将会用model中的数据重画树
	 * @param model
	 */
	public void setModel(ShutterModel model) {
		StringBuffer sb = new StringBuffer();
		int tabSize = model.getTabCount();
		for(int i = 0; i < tabSize; i++) {
			GroupModel g = (GroupModel)model.getTab(i);
			if(g.isFriendly() && !g.isCluster()) {
				// 每个组添加为一个根
				TreeItem root = new TreeItem(tree, SWT.NONE);
				root.setImage(icons.getResource(IconHolder.icoFolder));
				root.setText((String)g.getProperty("name"));
				root.setData(g);
				// 添加这个组的好友
				int itemSize = model.getItemCount(i);
				for(int j = 0; j < itemSize; j++) {
					FriendModel f = (FriendModel)model.getItem(i, j);
					// 生成叶子节点
					TreeItem ti = new TreeItem(root, SWT.NONE);
					// 设置图像
					int face = f.getFaceId();
					ti.setImage(icons.getSmallFace(face));
					// 设置文字
					sb.delete(0, sb.length());
					sb.append(f.getProperty("qq"));
					sb.append("  (");
					sb.append(f.getProperty("name"));
					sb.append(')');
					ti.setText(sb.toString());
					// 把model添加到TreeItem中
					ti.setData(f);
				}				
			}
		}
	}
	
	/**
	 * 添加好友选择事件监听器
	 * @param listener
	 */
	public void addFriendSelectionListener(FriendSelectionListener listener) {
		listeners.add(listener);
	}
	
	/**
	 * 除去好友选择事件监听器
	 * @param listener
	 */
	public void removeFriendSelectionListener(FriendSelectionListener listener) {
		listeners.remove(listener);
	}
	
	/**
	 * 触发一个好友被选择的事件,事件的source是个FriendModel
	 * @param e
	 */
	private void fireFriendSelectedEvent(FriendSelectionEvent e) {
		Iterator iter = listeners.iterator();
		while(iter.hasNext())
			((FriendSelectionListener)iter.next()).friendSelected(e);
	}
	
	/**
	 * 触发一个好友被取消选择事件,source是一个FriendModel
	 * @param e
	 */
	private void fireFriendDeselectedEvent(FriendSelectionEvent e) {
		Iterator iter = listeners.iterator();
		while(iter.hasNext())
			((FriendSelectionListener)iter.next()).friendDeselected(e);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlMoved(ControlEvent e) {
		setLocation();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlResized(ControlEvent e) {
		setLocation();
	}
	
	/**
	 * 隐藏或者显示窗口
	 * @param b
	 */
	public void setVisible(boolean b) {
		if(b) {
			setSize();
			setLocation();
		}
		shell.setVisible(b);
	}
	
	/**
	 * @return true表示当前窗口可见
	 */
	public boolean isVisible() {
		return shell.isVisible();
	}

	/**
	 * 把f代表的好友置为选择状态
	 * @param f
	 */
	public void select(FriendModel f) {
		TreeItem[] roots = tree.getItems();
		for(int i = 0; i < roots.length; i++) {
			TreeItem[] tis = roots[i].getItems();
			for(int j = 0; j < tis.length; j++) {
				FriendModel f2 = (FriendModel)tis[j].getData();
				if(f.equals(f2))
					tis[j].setChecked(true);
			}
		}
	}
}

⌨️ 快捷键说明

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