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

📄 sendmessageshell.java

📁 类似于MSN
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }
    
    /**
     * 移除一个取消或者完成的文件传输会话
     * @param sessionId
     */
    public FileWatcher removeFileSession(char sessionId) {
        return removeFileSession(new Integer(sessionId));
    }
    
    /**
     * 设置当前显示的文件传输会话
     * @param sessionId
     */
    protected void setCurrentFileSession(Integer sessionId) {
        if(sessionId == null) {
            // null表示没有更多的文件传输存在,隐藏相关控件
            tiNo.setEnabled(false);
            tiYes.setEnabled(false);
            bar.setVisible(false);
            lblStatus.setVisible(false);
            lblPercent.setVisible(false);
        } else {
            FileWatcher watcher = (FileWatcher)watchers.get(sessionId);
            currentFileSessionId = sessionId;
            if(watcher != null) {
                // 显示相关控件
                boolean send = watcher instanceof FileSender;
                tiNo.setEnabled(true);
                tiYes.setEnabled(!send && watcher.getFileTransferStatus() == FileWatcher.FT_NEGOTIATING);
                tiNo.setToolTipText(LumaQQ.getResourceString(send ? "tooltip.button.cancel.sendfile" : "tooltip.button.reject.sendfile"));
                tiYes.setToolTipText(send ? "" : LumaQQ.getResourceString("tooltip.button.accept.sendfile"));
                bar.setVisible(true);
                lblStatus.setVisible(true);
                lblPercent.setVisible(true);
                lblPercent.setText("");
                // 设定控件的值,如果滑窗为null说明还在连接中
                SlideWindow window = watcher.getSlideWindow();
                if(window != null) {
	                bar.setMaximum(watcher.getFragments());
	                bar.setSelection(window.getMaxPassed());
	                lblStatus.setText(LumaQQ.getResourceString(send ? "send.message.label.sending" : "send.message.label.receiving"));                    
                } else {
                    bar.setMaximum(100);
                    bar.setSelection(0);
	                lblStatus.setText(LumaQQ.getResourceString(send ? "send.message.label.sending" : "send.message.label.receiving"));                                     
                }
                tb.layout();
                // 设置菜单的选择状态
                setFileItemSelection(currentFileSessionId);
            }
        }
    }

    /**
     * 同意接受文件
     */
    protected void acceptSendFile() {
	    // 得到一个文件守望者实例
		FileReceiver receiver = (FileReceiver)watchers.get(currentFileSessionId);
		if(receiver == null) return;
	    // 打开一个文件对话框
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
		String fileName = receiver.getFileName();
		dialog.setFileName(fileName);
		String s = dialog.open();
		// 如果用户选择了取消则返回
		if(s == null) return;
		// 如果用户没有写扩展名,自动加上扩展名,但是如果接收的文件也没有扩展名就作罢
		if(s.indexOf('.') == -1) {
		    int i = fileName.lastIndexOf('.');
		    if(i != -1)
		        s += fileName.substring(i);
		}
		// 如果用户选择了保存,设置watcher的本地文件名变量
		receiver.setLocalFileName(s);
	    // 尝试打开文件准备写操作,如果失败,返回
	    if(!receiver.openLocalFile()) {
	        setOutputMessage("\n\n");
	        setOutputMessage(LumaQQ.getResourceString("text.error.open.local.file"));
	        receiver.shutdown();
	        return;
	    }
	    receiver.startMajorPort();
	    // 禁止按钮
	    tiYes.setEnabled(false);
	    // 显示提示信息
		outputBox.appendMessage(LumaQQ.getResourceString("text.accept.receive.file", new Object[] { receiver.getFileName() }));
		// 发送接收文件传送包
		main.client.acceptSendFile(qq, receiver.getMyMajorPort(), receiver.getSessionSequence(), true);
    }

    /**
	 * 得到一个表示文件大小的字符串形式
	 * @param len
	 * @return
	 */
	protected String getFileSizeString(long len) {
		String lenStr = null;
		long k = len / 1024;
		if(k <= 0)
			lenStr = String.valueOf(len) + "Bytes";
		else {
			long m = k / 1024;
			if(m <= 0)
				lenStr = String.valueOf(k) + "KB";
			else {
				long g = m / 1024;
				if(g <= 0)
					lenStr = String.valueOf(m) + "MB";
				else
					lenStr = String.valueOf(k) + "GB";
			}
		}
		return lenStr;
	}
	
	/**
	 * 设置消息发送窗口为聊天模式或者消息模式
	 * @param b true表示为聊天模式
	 */
	public void setTalkMode(boolean b) {
	    if(b == talkMode) return;
		if(b) {
			talkMode = true;
			f.addProperty("talkMode", "true");
			outputBox.clear();
			btnMode.setText(LumaQQ.getResourceString("send.message.button.mode.message"));
			FormData fd = (FormData)tb.getLayoutData();
			fd.top.control = viewer;
			fd.top.offset = -136;
			fd.top.alignment = SWT.TOP;
			fd.bottom.control = viewer;
			fd.bottom.offset = -114;
			fd.bottom.alignment = SWT.TOP;
			outputBox.setVisible(true);
			inputBox.setUseEnter(!options.isUseEnterInTalkMode());
			if(!shell.getMaximized()) {
			    Point p = shell.getSize();
			    shell.setSize(p.x, p.y + 100);
			}
			shell.layout();
		} else {
			talkMode = false;
			f.addProperty("talkMode", "false");
			btnMode.setText(LumaQQ.getResourceString("send.message.button.mode.talk"));
			FormData fd = (FormData)tb.getLayoutData();
			fd.top.control = btnFriend;
			fd.top.offset = 5;
			fd.top.alignment = SWT.BOTTOM;
			fd.bottom.control = btnFriend;
			fd.bottom.offset = 27;
			fd.bottom.alignment = SWT.BOTTOM;
			outputBox.setVisible(false);
			inputBox.setUseEnter(!options.isUseEnterInMessageMode());
			if(viewer.isVisible())
			    shell.setSize(400, 500);
			else
			    shell.setSize(400, 300);
			shell.layout();
		}
	}

	/**
	 * 发送消息
	 * @param message 消息内容
	 */
	public void sendMessage(String message) {
	    // 检查消息是否为空
	    if(message.length() == 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.cannot.send.empty.message"));
	        box.open();
	        return;
	    }
	    // 检查用户是否登录
		if(main.client.getUser().isLoggedIn()) {
			// 如果用户设置了字体则得到字体信息
		    String fontName = null;
			RGB rgb = new RGB(0, 0, 0);
			boolean italic = false, bold = false, underline = false;
			int size = 9;
			if(main.font != null) {
				FontData fd = main.font.getFontData()[0];
				fontName = fd.getName();
				size = fd.getHeight();
				if(main.fontColor != null)
					rgb = main.fontColor.getRGB();
				italic = (fd.getStyle() & SWT.ITALIC) != 0;
				bold = (fd.getStyle() & SWT.BOLD) != 0;					
				if(fontName == null || fontName.trim().equals("")) {
					fontName = LumaQQ.getResourceString("default.font");
				}
			}
			
			// 判断消息长度是否大于400,如果是,拆分发送
			int len = message.length();
			int j = 0;
			for(int i = 0; j < len; i += QQ.MAX_SEND_IM_SIZE) {
				j = i + QQ.MAX_SEND_IM_SIZE;
				if(j > len) j = len;
				String segment = message.substring(i, j);
				main.client.sendMessage(segment, qq, fontName, size, bold, italic, underline, rgb.red, rgb.green, rgb.blue, QQ.QQ_IM_NORMAL_REPLY);
				// 如果是聊天模式,则把发送的消息显示到输出框中
				date.setTime(System.currentTimeMillis());
				setOutputMessage((String)main.myModel.getProperty("name"), segment, date);
				// 保存到聊天记录中
				main.mm.saveMessage(qq, qq, main.myModel.getQQ(), segment, (int)(date.getTime() / 1000));
			}
			// 如果不是聊天模式才动画并且最小化
			//     如果是聊天模式,要清空输入框中的内容
			if(!talkMode) {
				// 开始动画
				startAnimate();
				// 最小化窗口
				shell.setMinimized(true);							
			} else {
				inputBox.clear();
			}
		} else {
			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.please.login.first"));
			box.open();
		}
	}

	/**
	 * 打开shell
	 */
	public void open()	{
		// 设置窗口位置
		setShellLocation();
		// 打开shell
		shell.layout();
		shell.open();
		inputBox.setFocus();
		if(main.font != null)
			inputBox.setFont(main.font);
		if(main.fontColor != null)
			inputBox.setForeground(main.fontColor);
		// add me as qq listener
		main.client.addQQListener(this);
	}

	// 设置窗口位置
	private void setShellLocation() {
		Point loc = new Point(50, 50);
		int x = f.getLocationX();
		int y = f.getLocationY();
		if(x != -1) loc.x = x;
		if(y != -1) loc.y = y;
		shell.setLocation(loc);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
	 */
	public void widgetDisposed(DisposeEvent e) {
		buttonColor.dispose();
		toolbarBackground.dispose();
	}	
	
	/**
	 * @return Returns the qq.
	 */
	public int getQQ() {
		return qq;
	}
	
	/**
	 * 设置好友位置信息
	 * @param place
	 */
	public void setPlace(String place) {
		lblPlace.setText(place);
		shell.layout();
	}
	
	/**
	 * 设置好友ip信息
	 * @param ip
	 */
	public void setIp(String ip) {
		lblIp.setText(ip);
		shell.layout();
	}
	
	/**
	 * 得到用户输入的消息的可发送形式
	 * @return
	 */
	public String getMessage() {
		return inputBox.getMessage();
	}
	
	/**
	 * 设置要发送的信息
	 * @param msg
	 */
	public void setInputMessage(String msg) {
		inputBox.setMessage(msg);
	}
	
	/**
	 * 设置已经发送的消息,这个方法只用在聊天模式时
	 * @param name 发送消息的人的昵称
	 * @param msg 消息内容
	 * @param time 消息发送时间
	 */
	public void setOutputMessage(String name, String msg, long time) {
	    date.setTime(time);
		setOutputMessage(name, msg, date);
	}
	
	/**
	 * 设置已经发送的消息,这个方法只用在聊天模式时
	 * @param name 发送消息的人的昵称
	 * @param msg 消息内容
	 * @param date 消息发送时间
	 */
	public void setOutputMessage(String name, String msg, Date date) {
		outputBox.appendMessage("(" + name + ")  " + DateFormat.getTimeInstance(DateFormat.LONG).format(date));
		outputBox.appendMessage("\n" + msg + "\n\n");
	}
	
	/**
	 * 在输出框中设置消息
	 * @param msg
	 */
	public void setOutputMessage(String msg) {	    
	    outputBox.appendMessage(msg);
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent)
	 */
	public void shellClosed(ShellEvent e) {
		main.client.removeQQListener(this);
		main.removeMeFromSMSMap(f);
	}
	
    /* (non-Javadoc)
     * @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent)
     */
    public void shellActivated(ShellEvent e) {
        stopBlinkImage();
        active = true;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent)
     */
    public void shellDeactivated(ShellEvent e) {
        active = false;
    }
    
    /**
	 * 是当前窗口激活
	 */
	public void setActive() {
		shell.setActive();
	}	
	
	/**
	 * 代理方法,设置窗口的最小化状态
	 * @param b
	 */
	public void setMinimized(boolean b) {
		shell.setMinimized(b);
	}
	
	/**
	 * 使chatbox获得焦点
	 */
	public void setFocus() {
		inputBox.setFocus();
	}
	
	/**
	 * 关闭窗口
	 */
	public void close() {
		Iterator iter = watchers.values().iterator();
		while(iter.hasNext()) {
		    FileWatcher watcher = (FileWatcher)iter.next();
		    watcher.removeFileListener(this);
		    watcher.abort();
		}
		shell.close();
	}
	
	/**
	 * 设置窗口的位置
	 * @param p
	 */
	public void setLocation(Point p) {
		shell.setLocation(p);
	}
	
	/**
	 * 发送消息开始时,开始动画
	 */
	public void startAnimate() {
		btnFriend.startAnimate(frames);
	}
	
	/**
	 * 停止动画
	 */
	public void stopAnimate() {
		btnFriend.stopAnimate();
	}
	
	/**
	 * 初始化动画帧
	 */
	private void generateAnimateFrames() {
		int temp = face - face % 3;
		frames = new Image[] { icons.getFace(temp), icons.getFace(temp + 2) };
	}
	
	/**
	 * 返回shell
	 * @return
	 */

⌨️ 快捷键说明

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