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

📄 membereditshell.java

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

import static edu.tsinghua.lumaqq.resource.Messages.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.tsinghua.lumaqq.events.FriendSelectionEvent;
import edu.tsinghua.lumaqq.events.IFriendSelectionListener;
import edu.tsinghua.lumaqq.models.Cluster;
import edu.tsinghua.lumaqq.models.ClusterType;
import edu.tsinghua.lumaqq.models.Model;
import edu.tsinghua.lumaqq.models.Organization;
import edu.tsinghua.lumaqq.models.User;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.beans.Member;
import edu.tsinghua.lumaqq.qq.beans.QQOrganization;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.packets.in.ClusterCommandReplyPacket;
import edu.tsinghua.lumaqq.resource.Colors;
import edu.tsinghua.lumaqq.resource.Resources;
import edu.tsinghua.lumaqq.ui.helper.UITool;
import edu.tsinghua.lumaqq.ui.listener.AroundBorderPaintListener;
import edu.tsinghua.lumaqq.ui.provider.ListContentProvider;
import edu.tsinghua.lumaqq.ui.sorter.UserQQSorter;
import edu.tsinghua.lumaqq.widgets.mac.Ring;
import edu.tsinghua.lumaqq.widgets.qstyle.Slat;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

/**
 * 临时群和组织的信息修改窗口
 * 
 * @author luma
 */
public class MemberEditShell implements IFriendSelectionListener, IQQListener {
	private class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			switch(columnIndex) {
				case 0:
					User u = (User)element;
					return res.getSmallHead(u.headId);
				default:
					return null;
			}
		}

		public String getColumnText(Object element, int columnIndex) {
			User u = (User)element;
			switch(columnIndex) {
				case 0:
					return String.valueOf(u.qq);
				case 1:
					return u.nick;
				case 2:
					if(u.info != null)
						return u.info.gender;
					else
						return "";
				case 3:
					if(u.info != null)
						return String.valueOf(u.info.age);
					else
						return "";
				default:
					return "";
			}
		}		
	}
	
	private Shell shell;
    private Display display;
	private MainShell main;
	private TableViewer viewer;
	private Slat btnOK;
	private Resources res;
	private Text textName;
	private FriendSelectionShell fss;
	private Ring ring;
	
	private List<User> members;
	private String name;
	
	// 父群和父组织,如果父群不为空,那么创建一个顶层组织
	// 如果父群是空,那么创建一个多人对话
	private Cluster parentCluster;
	private Organization parentOrganization;
	
	// 新建组织时,创建的新组织bean
	private QQOrganization newOrg;
	
	// 实际创建的对象或者修改的对象,可能为Cluster或者Organization
	private Model model;
	
	// model的类型
	private int type;
	
	// 期待的返回包序号
	private char expected;
	
	// 用在修改临时群信息时,因为修改一个群信息最多牵涉到3个包
	private char addMemberSequence, removeMemberSequence, modifyInfoSequence;
	
	// 用在修改组织信息时
	private char commitMemberSequence, commitOrganizationSequence;
	
	public static final int TEMP_CLUSTER = 0;
	public static final int ORGANIZATION = 1;	
	
	public MemberEditShell(MainShell m, int t) {
		main = m;
		type = t;
        display = main.getDisplay();
        res = Resources.getInstance();
        shell = new Shell(display, SWT.MIN | SWT.TITLE | SWT.CLOSE);
        shell.setText(member_edit_title);
        shell.setImage(res.getImage(Resources.icoLumaQQ));
        shell.setBackground(Colors.DIALOG_BACKGROUND);
        shell.setSize(420, 330);
        shell.addShellListener(new ShellAdapter() {
        	@Override
        	public void shellActivated(ShellEvent e) {
        		onShellActivated(e);
        	}
        	@Override
        	public void shellClosed(ShellEvent e) {
        		onShellClosed(e);
        	}
        });
        
        addMemberSequence = removeMemberSequence = modifyInfoSequence = 0;
        commitMemberSequence = commitOrganizationSequence = 0;
		fss = new FriendSelectionShell(shell, false);
		fss.addFriendSelectionListener(this);
        expected = 0;
        name = "";
        members = new ArrayList<User>();
        if(type == TEMP_CLUSTER)
        	members.add(main.getMyModel());
        
        initLayout();
        validate();
	}

	/**
	 * @param e
	 */
	protected void onShellClosed(ShellEvent e) {
		main.getClient().removeQQListener(this);
		if(fss != null && !fss.isDisposed())
			fss.setVisible(false);
		if(model != null)
			main.getShellRegistry().deregisterMemberEditShell(model);
	}

	/**
	 * @param e
	 */
	protected void onShellActivated(ShellEvent e) {
		if(fss == null || fss.isDisposed()) {
			fss = new FriendSelectionShell(shell, false);
			fss.addFriendSelectionListener(this);
		}
		fss.setVisible(true);
	}	
    
    /**
     * 设置焦点
     */
    public void setFocus() {
        shell.setFocus();
    }
    
    /**
     * 设置窗口激活
     */
    public void setActive() {
        shell.setActive();
    }

	/**
	 * 初始化窗口布局
	 */
	private void initLayout() {
		GridLayout layout = new GridLayout(2, false);
		shell.setLayout(layout);
		shell.addPaintListener(new AroundBorderPaintListener(new Class[] { Text.class, Table.class }, Colors.PAGE_CONTROL_BORDER));
		
		UITool.setDefaultBackground(shell.getBackground());		

		// 名称
		UITool.createLabel(shell, member_edit_name);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 18;
		textName = UITool.createSingleText(shell, gd);
		textName.setBackground(Colors.WHITE);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				Text t = (Text)e.getSource();
				name = t.getText().trim();
				validate();
			}
		});
		
		// 成员列表
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		gd.verticalIndent = 20;
		UITool.createLabel(shell, member_edit_list, gd);
		
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		viewer = new TableViewer(shell, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE);
		viewer.setContentProvider(new ListContentProvider<User>(members));
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new UserQQSorter());
		Table t = viewer.getTable();
		TableColumn tc = new TableColumn(t, SWT.LEFT);
		tc.setText(member_edit_qq);
		tc.setMoveable(true);
		tc.setWidth(120);
		tc = new TableColumn(t, SWT.CENTER);
		tc.setText(member_edit_nick);
		tc.setMoveable(true);
		tc.setWidth(120);
		tc = new TableColumn(t, SWT.CENTER);
		tc.setText(member_edit_gender);
		tc.setMoveable(true);
		tc.setWidth(80);
		tc = new TableColumn(t, SWT.CENTER);
		tc.setText(member_edit_age);
		tc.setMoveable(true);
		tc.setWidth(80);
		t.setHeaderVisible(true);
		t.setLinesVisible(false);
		t.setLayoutData(gd);
		viewer.setInput(this);
		
		// 按钮区
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		Composite comp = UITool.createContainer(shell, gd, new GridLayout(3, false));
		// busy ring
		ring = UITool.createRing(comp);
		// 确定按钮
		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_END);
		gd.grabExcessHorizontalSpace = true;
		gd.widthHint = 80;
		btnOK = UITool.createSlat(comp, button_ok, gd);
		btnOK.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				onOK();
			}
		});
		// 取消按钮
		gd = new GridData();
		gd.widthHint = 80;
		UITool.createSlat(comp, button_cancel, gd).addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.close();
			}
		});
	}
	
    /**
     * 确认按钮事件处理
     */
    protected void onOK() {    	
		btnOK.setEnabled(false);
		ring.rotate();
		if(type == TEMP_CLUSTER) {					
			if(model == null)
				createTempCluster();
			else
				modifyTempCluster();
		} else {
			if(model == null)
				createOrganization();
			else
				modifyOrganization();
		}
	}

	/**
	 * 修改组织
	 */
	private void modifyOrganization() {
		// 修改名称
		Organization org = (Organization)model;
		if(!name.equals(org.name)) {
			List<QQOrganization> temp = new ArrayList<QQOrganization>();
			for(Organization o : parentCluster.organizations.values()) {
				QQOrganization qqOrg = new QQOrganization();
				qqOrg.id = o.id;
				qqOrg.path = o.path;
				qqOrg.name = o.name;
				if(o == org) 
					qqOrg.name = name;
				temp.add(qqOrg);
			}
			commitOrganizationSequence = main.getClient().commitOrganization(parentCluster.clusterId, temp);
		}
		
		// 得到旧成员
		Map<Integer, User> oldMember = new HashMap<Integer, User>();
		for(User u : parentCluster.members.values()) {
			if(u.organizationId == org.id)
				oldMember.put(u.qq, u);
		}
		// 添加新成员
		List<Member> toBeCommited = new ArrayList<Member>();
		for(User u : members) {
			if(oldMember.remove(u.qq) == null) {
				Member m = new Member();
				m.qq = u.qq;
				m.organization = org.id;
				toBeCommited.add(m);
			}
		}
		// 添加删除的成员
		for(User u : oldMember.values()) {
			Member m = new Member();
			m.qq = u.qq;
			m.organization = 0;
			toBeCommited.add(m);
		}
		// 修改
		if(!toBeCommited.isEmpty()) {
			commitMemberSequence = main.getClient().commitMemberOrganization(parentCluster.clusterId, toBeCommited);
		}
	}

	/**
	 * 创建组织
	 */
	private void createOrganization() {
		// 创建组织
		List<QQOrganization> temp = new ArrayList<QQOrganization>();
		for(Organization o : parentCluster.organizations.values()) {
			QQOrganization org = new QQOrganization();
			org.id = o.id;
			org.path = o.path;
			org.name = o.name;
			temp.add(org);
		}
		newOrg = buildNewOrganization();
		temp.add(newOrg);
		commitOrganizationSequence = main.getClient().commitOrganization(parentCluster.clusterId, temp);
		
		// 修改成员结构		
		if(!members.isEmpty()) {
			List<Member> mList = new ArrayList<Member>();
			for(User u : members) {
				Member m = new Member();
				m.qq = u.qq;
				m.organization = newOrg.id;
				mList.add(m);
			}
			commitMemberSequence = main.getClient().commitMemberOrganization(parentCluster.clusterId, mList);
		}
	}
	
	/**
	 * 生成正确的QQOrganization对象
	 * 
	 * @return
	 */
	private QQOrganization buildNewOrganization() {
		if(parentOrganization == null) {		
			QQOrganization org = new QQOrganization();
			org.id = parentCluster.organizations.size() + 1;
			org.path = org.id << 24;
			org.name = name;
			return org;
		} else {
			int index = 1;
			for(Organization o : parentCluster.organizations.values()) {
				if(o.isChildOf(parentOrganization))
					index++;
			}
			int shift = 18 - parentOrganization.getLevel() * 6;
			QQOrganization org = new QQOrganization();
			org.id = parentCluster.organizations.size() + 1;
			org.path = parentOrganization.path | (index << shift);
			org.name = name;
			return org;
		}
	}

	/**
	 * 修改临时群
	 */
    private void modifyTempCluster() {
    	Cluster c = (Cluster)model;

⌨️ 快捷键说明

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