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

📄 sendmessageshell.java

📁 类似于MSN
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		fd.top = new FormAttachment(0, 5);
		lblQQ.setLayoutData(fd);
		// 昵称标签
		lblNick = new Label(shell, SWT.NONE);
		fd = new FormData();
		fd.left = new FormAttachment(btnFriend, 10, SWT.RIGHT);
		fd.top = new FormAttachment(lblQQ, 5, SWT.BOTTOM);
		lblNick.setLayoutData(fd);
		// 好友所在地区标签
		lblPlace = new Label(shell, SWT.CENTER);
		fd = new FormData();
		fd.left = new FormAttachment(40, 0);
		fd.right = new FormAttachment(100, 0);
		fd.top = new FormAttachment(0, 5);
		lblPlace.setLayoutData(fd);
		// ip地址标签
		lblIp = new Label(shell, SWT.CENTER);
		fd = new FormData();
		fd.left = new FormAttachment(40, 0);
		fd.right = new FormAttachment(100, 0);
		fd.top = new FormAttachment(lblPlace, 5, SWT.BOTTOM);
		lblIp.setLayoutData(fd);
		// 工具条
		tb = new ToolBar(shell, SWT.FLAT | SWT.HORIZONTAL);
		tb.setBackground(toolbarBackground);
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(btnFriend, 5, SWT.BOTTOM);
		fd.bottom = new FormAttachment(btnFriend, 27, SWT.BOTTOM);
		tb.setLayoutData(fd);
		tb.setLayout(new FormLayout());
		// 文件传输进度条
		bar = new ProgressBar(tb, SWT.HORIZONTAL | SWT.SMOOTH);
		bar.setVisible(false);
		bar.setMinimum(0);
		bar.setBackground(toolbarBackground);
		bar.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
		fd = new FormData();
		fd.right = new FormAttachment(100, -5);
		fd.left = new FormAttachment(100, -105);
		fd.top = new FormAttachment(0, 3);
		fd.bottom = new FormAttachment(100, -3);
		bar.setLayoutData(fd);
		// 文件传输百分比标签
		lblPercent = new Label(tb, SWT.CENTER);
		lblPercent.setBackground(toolbarBackground);
		lblPercent.setVisible(false);
		fd = new FormData();
		fd.right = new FormAttachment(bar, -5, SWT.LEFT);
		fd.left = new FormAttachment(bar, -30, SWT.LEFT);
		fd.top = new FormAttachment(0, 4);
		lblPercent.setLayoutData(fd);
		// 文件传输状态标签
		lblStatus = new Label(tb, SWT.RIGHT);
		lblStatus.setBackground(toolbarBackground);
		lblStatus.setVisible(false);
		fd = new FormData();
		fd.right = new FormAttachment(lblPercent, -5, SWT.LEFT);
		fd.top = new FormAttachment(0, 4);
		lblStatus.setLayoutData(fd);
		// 输出消息框,这个框只在聊天模式时显示
		outputBox = new ChatBox(shell, SWT.NONE);
		outputBox.setEnabled(false);
		outputBox.setVisible(false);
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(btnFriend, 5, SWT.BOTTOM);
		fd.bottom = new FormAttachment(tb, 0, SWT.TOP);
		outputBox.setLayoutData(fd);
		// 选择字体按钮
		ToolItem ti = new ToolItem(tb, SWT.PUSH);
		ti.setImage(icons.getResource(IconHolder.icoFont));
		ti.setToolTipText(LumaQQ.getResourceString("tooltip.button.font"));
		ti.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					FontDialog dialog = new FontDialog(shell);
					if(main.font != null) 
						dialog.setFontList(main.font.getFontData());
					if(main.fontColor != null)
						dialog.setRGB(main.fontColor.getRGB());
					dialog.open();
					FontData[] fontList = dialog.getFontList();
					if(fontList == null) return;
					RGB rgb = dialog.getRGB();
					if(main.font != null) main.font.dispose();
					if(main.fontColor != null) main.fontColor.dispose();
					main.font = new Font(display, fontList);
					if(rgb != null) {
						main.fontColor = new Color(display, rgb);
						inputBox.setForeground(main.fontColor);						
					}
					inputBox.setFont(main.font);
				}
			}
		);
		// 选择表情按钮
		ti = new ToolItem(tb, SWT.PUSH);
		ti.setImage(icons.getResource(IconHolder.icoSmiley));
		ti.setToolTipText(LumaQQ.getResourceString("tooltip.button.smiley"));
		ti.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if(iss == null || iss.isDisposed()) {
						iss = new ImageSelectShell(shell);
						iss.addShellListener(new SmileyPanelListener());
						iss.setImageSize(20, 20);
						iss.setMarginWidth(1);
						iss.setMarginHeight(1);
						iss.setVerticalSpacing(2);
						iss.setHorizontalSpacing(2);
						iss.setDimension(15, 6);
						iss.setContinuous(true);
						iss.addAllSmiley();		
						iss.refresh();
						Rectangle bound = tb.getBounds();
						iss.setLocation(shell.toDisplay(bound.x, bound.y + bound.height));
						iss.open();								
					}
				}
			}
		);
		// 发送文件按钮
		ti = new ToolItem(tb, SWT.DROP_DOWN);
		ti.setImage(icons.getResource(IconHolder.icoSendFile));
		ti.setToolTipText(LumaQQ.getResourceString("tooltip.button.sendfile"));
		ti.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
				    if(e.detail == SWT.ARROW) {
				        Rectangle bound = ((ToolItem)e.widget).getBounds();
				        fileMenu.setLocation(tb.toDisplay(bound.x, bound.y + bound.height));
				        fileMenu.setVisible(true);				        
				    } else
				        requestSendFile();
				}
			}
		);
		// 同意接收文件按钮
		tiYes = new ToolItem(tb, SWT.PUSH);
		tiYes.setEnabled(false);
		tiYes.setImage(icons.getResource(IconHolder.icoYes));
		tiYes.setToolTipText(LumaQQ.getResourceString("tooltip.button.accept.sendfile"));
		tiYes.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
				    acceptSendFile();
				}
			}
		);
		// 拒绝接收文件按钮
		tiNo = new ToolItem(tb, SWT.PUSH);
		tiNo.setEnabled(false);
		tiNo.setImage(icons.getResource(IconHolder.icoNo));
		tiNo.setToolTipText(LumaQQ.getResourceString("tooltip.button.reject.sendfile"));
		tiNo.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
				    cancelFileOperation();
				}
			}
		);
		// 聊天记录面板
		viewer = new RecordViewer(shell, SWT.NONE);
		viewer.setVisible(false);
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(100, 0);
		fd.bottom = new FormAttachment(100, -5);
		viewer.setLayoutData(fd);
		// 输入消息框
		inputBox = new ChatBox(shell, SWT.NONE);
		inputBox.setUseEnter(! (talkMode ? options.isUseEnterInTalkMode() : options.isUseEnterInMessageMode()));
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(tb, 0, SWT.BOTTOM);
		fd.bottom = new FormAttachment(viewer, -36, SWT.TOP);
		inputBox.setLayoutData(fd);
		inputBox.addAcceleratorListener(this);
		// 聊天记录按钮
		ShutterLabel btnRecord = new ShutterLabel(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getResourceString("send.message.button.record"), null);
		btnRecord.setBackground(buttonColor);
		fd = new FormData();
		fd.left = new FormAttachment(0, 8);
		fd.top = new FormAttachment(inputBox, 8, SWT.BOTTOM);
		btnRecord.setLayoutData(fd);
		btnRecord.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
				    showRecord();
				}
			}
		);
		// 聊天模式按钮
		btnMode = new ShutterLabel(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getResourceString("send.message.button.mode.talk"), null);
		btnMode.setBackground(buttonColor);
		fd = new FormData();
		fd.left = new FormAttachment(btnRecord, 3, SWT.RIGHT);
		fd.top = new FormAttachment(inputBox, 8, SWT.BOTTOM);
		btnMode.setLayoutData(fd);
		btnMode.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					if(talkMode) {
					    // 检查是否在传送文件,如果是,不能切换到消息模式
					    if(hasFileOperating()) {
					        MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
					        box.setText(LumaQQ.getResourceString("message.box.common.warning.title"));
					        box.setMessage(LumaQQ.getResourceString("message.box.cannot.switch.to.message.mode"));
					        box.open();
					        return;
					    }
						setTalkMode(false);
					} else {
						setTalkMode(true);
					}
				}
			}
		);
		Button btnUseEnter = new Button(shell, SWT.ARROW | SWT.DOWN);
		fd = new FormData();
		fd.right = new FormAttachment(100, -8);
		fd.left = new FormAttachment(100, -18);
		fd.top = new FormAttachment(inputBox, 8, SWT.BOTTOM);
		fd.bottom = new FormAttachment(btnMode, 0, SWT.BOTTOM);
		btnUseEnter.setLayoutData(fd);
		btnUseEnter.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					// 设置菜单位置
					Rectangle bound = btnSend.getBounds();
					enterMenu.setLocation(shell.toDisplay(bound.x, bound.y + bound.height));
					// 显示菜单
					enterMenu.setVisible(true);
				}
			}
		);
		// 发送按钮
		btnSend = new ShutterLabel(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getResourceString("send.message.button.send"), null);
		btnSend.setBackground(buttonColor);
		fd = new FormData();
		fd.right = new FormAttachment(btnUseEnter, 0, SWT.LEFT);
		fd.left = new FormAttachment(btnUseEnter, -70, SWT.LEFT);
		fd.top = new FormAttachment(inputBox, 8, SWT.BOTTOM);
		btnSend.setLayoutData(fd);
		btnSend.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					sendMessage(inputBox.getMessage());
				}
			}
		);
		// 关闭按钮
		ShutterLabel btnClose = new ShutterLabel(shell, MySWT.HOVER | SWT.CENTER, LumaQQ.getResourceString("send.message.button.close"), null);
		btnClose.setBackground(buttonColor);
		fd = new FormData();
		fd.right = new FormAttachment(btnSend, -3, SWT.LEFT);
		fd.left = new FormAttachment(btnSend, -73, SWT.LEFT);
		fd.top = new FormAttachment(inputBox, 8, SWT.BOTTOM);
		btnClose.setLayoutData(fd);
		btnClose.addMouseListener(
			new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
				    // 检查是否在传输文件,如果是,显示对话框要用户确认关闭
				    if(hasFileOperating()) {
				        MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				        box.setText(LumaQQ.getResourceString("message.box.common.question.title"));
				        box.setMessage(LumaQQ.getResourceString("message.box.file.transfering"));
				        if(box.open() == SWT.YES)
				            abortAllFileTransfer();
				        else
				            return;
				    }
					close();
				}
			}
		);
		// 设置消息管理器、字符串格式化器,导出器
		viewer.setMessageManager(main.mm);
		// 添加表头
		viewer.setTableColumn(new String[] { LumaQQ.getResourceString("send.message.table.header.nick"), LumaQQ.getResourceString("send.message.table.header.time"), LumaQQ.getResourceString("send.message.table.header.message") });
		viewer.setTableColumnWidth(new String[] { "80", "120", "*" });
		// tableitem双击事件监听器
		viewer.addSelectionListener(
			new SelectionAdapter() {
				public void widgetDefaultSelected(SelectionEvent e) {
					TableItem ti = viewer.getSelectedItem();
					inputBox.setMessage(ti.getText(2));
				}
			}
		);
	}

    /**
     * 显示聊天记录
     */
    protected void showRecord() {
		FormData fd = (FormData)viewer.getLayoutData();
		viewer.setVisible(!viewer.getVisible());
		if(viewer.getVisible()) {
			// 得到记录,显示在table中
			viewer.refreshRecord(false);
			// 设置shell布局和大小
			fd.top.offset = -200;
			if(!shell.getMaximized()) {
			    Point p = shell.getSize();
			    shell.setSize(p.x, p.y + 200);
			}
			// 显示最后一条记录
			viewer.showLast();
		} else {
			fd.top.offset = 0;
			if(!shell.getMaximized()) {
			    Point p = shell.getSize();
			    shell.setSize(p.x, p.y - 200);
			}
		}
		// 专用伺候linux的代码
		shell.layout();		
    }

    /**
     * 取消所有正在进行的文件传输
     */
    protected void abortAllFileTransfer() {
        Iterator iter = watchers.values().iterator();
        while(iter.hasNext()) {
            FileWatcher watcher = (FileWatcher)iter.next();
            watcher.abort();
        }
    }

    // 请求发送文件
    public void requestSendFile() {
        // 检查是否是自己,不能给自己发
        if(qq == main.myModel.getQQ()) {
            MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
            box.setText(LumaQQ.getResourceString("message.box.common.warning.title"));
            box.setMessage(LumaQQ.getResourceString("message.box.cannot.send.file.to.self"));
            box.open();
            return;
        }
        // 要用户选择要发送的文件
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
		String fileName = dialog.open();
		if(fileName != null) {						
			File file = new File(fileName);
			fileName = file.getName();
			// 计算文件大小
			int fileSize = (int)file.length();
			// 如果文件长度为0,返回
			if(fileSize == 0) {
			    MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
			    box.setText(LumaQQ.getResourceString("message.box.common.warning.title"));
			    box.setMessage(LumaQQ.getResourceString("message.box.file.length.zero"));
			    box.open();
			    return;
			}
			// 把数字形式的文件长度转化成humen readable格式
			String lenStr = getFileSizeString(fileSize);
			// 如果当前不是聊天模式,设置为聊天模式
			if(!talkMode)
				setTalkMode(true);
			// 设置提示文本
			setOutputMessage("\n\n");
			setOutputMessage(LumaQQ.getResourceString("text.send.file", new Object[] { f.getProperty("name"), fileName, lenStr }));
			// 设置守望者参数
			FileSender sender = new FileSender(main.client);
			sender.addFileListener(this);
			sender.setHisQQ(qq);
			sender.setFileName(fileName);
			sender.setLocalFileName(file.getAbsolutePath());
			sender.setFileSize(fileSize);
			sender.setMaxFragmentSize(1000);
			sender.setFragments(fileSize / 1000);
			sender.setFileTransferStatus(FileWatcher.FT_NEGOTIATING);
			// 打开要发送的文件,如果打开失败,取消发送
			if(!sender.openLocalFile()) {
			    MessageBox box = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
			    box.setText(LumaQQ.getResourceString("message.box.common.fail.title"));
			    box.setMessage(LumaQQ.getResourceString("message.box.file.open.error"));
			    box.open();
			    return;
			}
			// 启动sender
			sender.start();
			// 发送请求发送文件消息包
			sender.setSessionSequence(main.client.requestSendFile(qq, fileName, fileSize, sender.getMyMajorPort(), true));
			// 把这个sender加入到哈希表,并设置为当前file session
			currentFileSessionId = new Integer(sender.getSessionSequence());
			watchers.put(currentFileSessionId, sender);
			addFileItem(true, sender.getFileName(), lenStr, currentFileSessionId);
		    setCurrentFileSession(currentFileSessionId);
			// 设置工具条按钮的状态
			tiNo.setEnabled(true);
		}
    }
    
    /**
     * 取消文件传输,可能是在协商阶段取消,也可能是在传送过程中取消
     */
    protected void cancelFileOperation() {
	    FileWatcher watcher = removeFileSession(currentFileSessionId);
	    watcher.abort();
	    if(watcher instanceof FileSender)
	        outputBox.appendMessage(LumaQQ.getResourceString("text.cancel.transfer.file", new Object[] { watcher.getFileName() }));
	    else
	        outputBox.appendMessage(LumaQQ.getResourceString("text.reject.receive.file"));
    }
    
    /**
     * 移除一个取消或者完成的文件传输会话
     * @param sessionId
     */
    public FileWatcher removeFileSession(Integer sessionId) {
	    tiNo.setEnabled(false);
	    tiYes.setEnabled(false);
        FileWatcher watcher = (FileWatcher)watchers.remove(sessionId);
	    if(watchers.size() > 0)
	        setCurrentFileSession((Integer)watchers.keys().nextElement());
	    else
	        setCurrentFileSession(null);
	    return watcher;

⌨️ 快捷键说明

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