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

📄 findusersdialog.java

📁 一个使用Java实现的类似与QQ的聊天程序。使用了Hibernate组建。可用于学习Java网络编程和Hiberante数据库组件的学习
💻 JAVA
字号:
package com.jim.client;

import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.jim.database.JIMUser;

public class FindUsersDialog extends Dialog {

	private List fdList;
	private Button findB;
	private Button addB;
	protected ArrayList<JIMUser> result;
	private HashMap<Integer,JIMUser> users;
	protected Shell shell;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public FindUsersDialog(Shell parent, int style) {
		super(parent, style);
		result = new ArrayList<JIMUser>(0);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public FindUsersDialog(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public ArrayList<JIMUser> open() {
		createContents();
		Rectangle smallRect = shell.getBounds();
		Rectangle displayRect = getParent().getDisplay().getBounds();
		shell.setLocation((displayRect.width - smallRect.width) / 2, (displayRect.height - smallRect.height) / 2);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new FormLayout());
		shell.setSize(380, 269);
		shell.setText("查找用户");

		fdList = new List(shell, SWT.BORDER);
		final FormData formData = new FormData();
		formData.bottom = new FormAttachment(0, 175);
		formData.right = new FormAttachment(100, -10);
		formData.top = new FormAttachment(0, 10);
		formData.left = new FormAttachment(0, 10);
		fdList.setLayoutData(formData);

		findB = new Button(shell, SWT.NONE);
		findB.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				findUsers();
			}
		});
		final FormData formData_1 = new FormData();
		formData_1.bottom = new FormAttachment(100, -17);
		formData_1.top = new FormAttachment(fdList, 23, SWT.DEFAULT);
		formData_1.left = new FormAttachment(0, 185);
		findB.setLayoutData(formData_1);
		findB.setText("查找好友");

		addB = new Button(shell, SWT.NONE);
		addB.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				addFriend();
			}
		});
		addB.setEnabled(false);
		formData_1.right = new FormAttachment(addB, -23, SWT.LEFT);
		final FormData formData_2 = new FormData();
		formData_2.bottom = new FormAttachment(100, -17);
		formData_2.left = new FormAttachment(100, -94);
		formData_2.right = new FormAttachment(100, -22);
		formData_2.top = new FormAttachment(findB, 0, SWT.TOP);
		addB.setLayoutData(formData_2);
		addB.setText("添加为好友");

		//
	}

	protected void addFriend() {
		int selected = fdList.getSelectionIndex();
		if(selected < 0)
			return;
		Integer uid = MainWin.retrieveNo(fdList.getItem(selected));
		if(uid.equals(MainWin.client.getUser().getJimno())){
			MessageBox mb = new MessageBox(shell,SWT.ICON_INFORMATION | SWT.OK);
			mb.setMessage("不能添加自己");
			mb.open();
			return;
		}
		JIMUser u = MainWin.client.getUser().getFriends().get(uid);
		if(u != null ){
			MessageBox mb = new MessageBox(shell,SWT.ICON_INFORMATION | SWT.OK);
			mb.setMessage("该用户已在好友列表中");
			mb.open();
			return;
		}			
		MainWin.client.addFriend(uid);
		result.add(users.get(uid));
		MessageBox mb = new MessageBox(shell,SWT.ICON_INFORMATION | SWT.OK);
		mb.setMessage("添加好友成功");
		mb.open();
	}

	protected void findUsers() {
		users = MainWin.client.findUsers();
		for(JIMUser u : users.values()){
			fdList.add(u.toString());
		}
		fdList.redraw();
		findB.setEnabled(false);
		addB.setEnabled(true);
	}

}

⌨️ 快捷键说明

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