📄 ipseekershell.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.ui;
import java.util.Iterator;
import java.util.List;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import edu.tsinghua.lumaqq.IPEntry;
import edu.tsinghua.lumaqq.IPSeeker;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
/**
* <pre>
* 用于查询IP的窗口
* </pre>
*
* @author 马若劼
*/
public class IPSeekerShell {
private Shell shell;
private MainShell main;
private Display display;
private IconHolder icons = IconHolder.getInstance();
private Text textInput, textResult;
private Button btnAddr2Ip;
private static String CRLF = System.getProperty("line.separator");
private IPSeeker seeker = IPSeeker.getInstance();
/**
* 构造函数,用于从LumaQQ的菜单中启动
* @param main
*/
public IPSeekerShell(MainShell main) {
this.main = main;
this.display = main.getDisplay();
shell = new Shell(this.display, SWT.SHELL_TRIM);
shell.setText(LumaQQ.getString("ip.seeker.title"));
shell.setImage(icons.getImage(IconHolder.icoSearchIp));
shell.setSize(550, 400);
initLayout();
}
/**
* 构造函数,用于做为一个单独的程序启动
*/
protected IPSeekerShell() {
main = null;
display = new Display();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText(LumaQQ.getString("ip.seeker.title"));
shell.setImage(icons.getImage(IconHolder.icoSearchIp));
shell.setSize(400, 500);
initLayout();
}
/**
* 初始化界面布局
*/
private void initLayout() {
// 设置shell的layout
GridLayout layout = new GridLayout();
layout.numColumns = 4;
shell.setLayout(layout);
// 输入标签
Label lblInput = new Label(shell, SWT.NONE);
lblInput.setText(LumaQQ.getString("ip.seeker.label.input"));
lblInput.setLayoutData(new GridData());
// 输入文本框
textInput = new Text(shell, SWT.SINGLE | SWT.BORDER);
textInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// IP -> 地点按钮
Button btnIp2Addr = new Button(shell, SWT.PUSH);
btnIp2Addr.setText(LumaQQ.getString("ip.seeker.button.ip2addr"));
btnIp2Addr.setToolTipText(LumaQQ.getString("ip.seeker.tooltip.ip2addr"));
btnIp2Addr.setLayoutData(new GridData());
btnIp2Addr.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ip2Address();
}
});
// 地点 -> IP 按钮
btnAddr2Ip = new Button(shell, SWT.PUSH);
btnAddr2Ip.setText(LumaQQ.getString("ip.seeker.button.addr2ip"));
btnAddr2Ip.setToolTipText(LumaQQ.getString("ip.seeker.tooltip.addr2ip"));
btnAddr2Ip.setLayoutData(new GridData());
btnAddr2Ip.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
btnAddr2Ip.setEnabled(false);
address2Ip();
}
});
// 结果显示文本框
textResult = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 4;
textResult.setLayoutData(gd);
}
/**
* 从地点查询IP
*/
protected void address2Ip() {
textResult.setText("");
String s = textInput.getText().trim();
if("".equals(s)) {
textResult.append(LumaQQ.getString("ip.seeker.please.input"));
btnAddr2Ip.setEnabled(true);
return;
}
String beginIp = LumaQQ.getString("ip.seeker.text.begin.ip");
String endIp = LumaQQ.getString("ip.seeker.text.end.ip");
String addr = LumaQQ.getString("ip.seeker.text.address");
StringBuffer sb = new StringBuffer();
List ret = seeker.getIPEntries(s);
Iterator iter = ret.iterator();
while(iter.hasNext()) {
IPEntry entry = (IPEntry)iter.next();
sb.append(beginIp);
sb.append(entry.beginIp);
sb.append(' ');
sb.append(endIp);
sb.append(entry.endIp);
sb.append(' ');
sb.append(addr);
sb.append(entry.country);
sb.append(' ');
sb.append(entry.area);
sb.append(CRLF);
textResult.append(sb.toString());
sb.delete(0, sb.length());
}
btnAddr2Ip.setEnabled(true);
}
/**
* 从IP查询地点
*/
protected void ip2Address() {
textResult.setText("");
String ip = textInput.getText().trim();
StringBuffer sb = new StringBuffer();
sb.append("IP: ");
sb.append(ip);
sb.append(CRLF);
sb.append(LumaQQ.getString("ip.seeker.text.address"));
sb.append(seeker.getCountry(ip));
sb.append(' ');
sb.append(seeker.getArea(ip));
textResult.setText(sb.toString());
}
/**
* 打开窗口
*/
public void open() {
if(main == null)
openAsApp();
else
openAsShell();
}
/**
* 做为一个窗口打开
*/
private void openAsShell() {
// set shell to center of screen
Rectangle dialogRect = shell.getBounds();
Rectangle displayRect = display.getBounds();
shell.setLocation((displayRect.width-dialogRect.width)/2, (displayRect.height-dialogRect.height)/2);
// 打开shell
shell.layout();
// set dialog to center of screen
shell.open();
}
/**
* 做为一个独立的应用程序打开
*/
private void openAsApp() {
// set shell to center of screen
Rectangle dialogRect = shell.getBounds();
Rectangle displayRect = display.getBounds();
shell.setLocation((displayRect.width-dialogRect.width)/2, (displayRect.height-dialogRect.height)/2);
// event loop
shell.layout();
shell.open();
while(!shell.isDisposed())
if(!display.readAndDispatch())
display.sleep();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -