package com.longruan.lrgis.gui;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import com.longruan.lrgis.gui.util.SWTResourceManager;
public class GetHtmlCode {
private Label label_2;
private Label label_1;
private TabFolder tabFolder;
private Label label;
private Button saveBtn;
private Button exitBtn;
private Button resetBtn;
private Text fileNameText;
private Text textCode;
private Combo combo;
private Text httpText;
protected Shell shell;
private Button viewBtn;
private Browser browser;
private ProgressBar progressBar;
// Main 函数,主入口
public static void main(String[] args) {
try {
GetHtmlCode window = new GetHtmlCode();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
// 显示设备
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
// 查看输入网页源代码操作
public void viewCode() {
try {
viewBtn.setEnabled(false); // 点击按钮操作后,设置按钮为不可点击状态
URL url = new URL(httpText.getText());
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), combo.getText()));
textCode.setText("");
String tmpCode = "";
int i = 0;
progressBar.setSelection(i);
while ((tmpCode = in.readLine()) != null) {
textCode.append(tmpCode + '/n');
progressBar.setSelection(i);
i++;
}
progressBar.setSelection(5000);
viewBtn.setEnabled(true);
urlOpenMess();
} catch (Exception e) {
e.printStackTrace();
}
}
// 退出提示
public void exit() {
MessageBox mess = new MessageBox(shell, SWT.OK | SWT.CANCEL
| SWT.ICON_QUESTION);
mess.setText("退出");
mess.setMessage("是否确定退出");
if (mess.open() == SWT.OK) {
System.exit(0);
}
}
// 提示是否有源代码需要保存
public void nullMessBox() {
MessageBox mess = new MessageBox(shell, SWT.OK | SWT.ICON_WARNING);
mess.setText("提示");
mess.setMessage("并没有源代码");
mess.open();
}
public void saveCode() {
// 判断是否有源代码,没有则直接返回,不进行任何操作
if (textCode.getText().equals("")) {
nullMessBox();
return;
}
// 新建一个对话框,提示要保存的路径
FileDialog file = new FileDialog(shell, SWT.SAVE);
String[] filter = { "*.txt", "*.html", "*.htm", "*.*" };
file.setFilterExtensions(filter);
String fileName = file.open();
// 验证是否输入要保存的文件名,如果没有,则直接返回,不进行操作
if (fileName == null)
return;
try {
FileWriter in = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(in);
out.write(textCode.getText());
out.flush();
out.close();
fileNameText.setText(fileName); // 保存完毕,显示保存路径
} catch (IOException e) {
e.printStackTrace();
}
}
// 代码显示完后提示是否进行网页浏览
public void urlOpenMess() {
MessageBox mess = new MessageBox(shell, SWT.OK | SWT.CANCEL
| SWT.ICON_QUESTION);
mess.setText("提示");
mess.setMessage("是否打开网页预览");
if (mess.open() == SWT.OK) {
browser.setUrl(httpText.getText());
}
}
// 窗体绘制
protected void createContents() {
shell = new Shell(SWT.MIN);
shell.setImage(SWTResourceManager
.getImage(GetHtmlCode.class, "ico.ICO"));
shell.setSize(685, 554);
// 设置窗体居中,得到屏幕分辨率后与程序大小进行计算
int x = (shell.getMonitor().getClientArea().width - shell.getSize().x) / 2;
int y = (shell.getMonitor().getClientArea().height - shell.getSize().y) / 2;
shell.setLocation(x, y);
shell
.setText("网页源代码查看器修正版 By hmilyld 04.11------ http://www.hmilyld.cn");
final Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
final MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
menuItem.setText("文件(&F)");
final Menu menu_1 = new Menu(menuItem);
menuItem.setMenu(menu_1);
// 点击退出按钮以后的操作
final MenuItem exitItem = new MenuItem(menu_1, SWT.NONE);
exitItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
exit();
}
});
exitItem.setText("退出(&Q)");
label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
label.setBounds(0, 459, 679, 13);
label_1 = new Label(shell, SWT.RIGHT);
label_1.setText("网页代码查看器修正版 By hmilyld 04.11");
label_1.setBounds(420, 478, 249, 13);
tabFolder = new TabFolder(shell, SWT.NONE);
tabFolder.setBounds(10, 83, 659, 370);
final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("源代码");
textCode = new Text(tabFolder, SWT.V_SCROLL | SWT.BORDER);
tabItem.setControl(textCode);
final TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
tabItem_1.setText("网页预览");
browser = new Browser(tabFolder, SWT.NONE);
browser.setUrl("");
tabItem_1.setControl(browser);
label_2 = new Label(shell, SWT.NONE);
label_2.setText("请输入要查看源代码的网页地址:(包含http://)");
label_2.setBounds(10, 10, 270, 13);
httpText = new Text(shell, SWT.BORDER);
httpText.setToolTipText("输入网页地址,包含http://");
httpText.setText("http://www.hmilyld.cn");
httpText.setBounds(10, 27, 362, 25);
// 点击查看按钮以后的操作
viewBtn = new Button(shell, SWT.NONE);
viewBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
viewCode();
}
});
viewBtn.setText("查看");
viewBtn.setBounds(456, 29, 67, 23);
// 点击清除按钮以后的操作
resetBtn = new Button(shell, SWT.NONE);
resetBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
textCode.setText("");
}
});
resetBtn.setBounds(529, 29, 67, 23);
resetBtn.setText("清除");
combo = new Combo(shell, SWT.NONE);
combo.setToolTipText("网页编码类型,如出现乱码,请选择这里");
combo.setText("gb2312");
combo.add("gb2312");
combo.add("utf-8");
combo.setBounds(383, 31, 67, 21);
// 点击退出按钮以后的操作
exitBtn = new Button(shell, SWT.NONE);
exitBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
exit();
}
});
exitBtn.setBounds(602, 29, 67, 23);
exitBtn.setText("退出");
progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setMaximum(5000);
progressBar.setBounds(10, 62, 270, 17);
progressBar.setToolTipText("代码读取进度条");
fileNameText = new Text(shell, SWT.BORDER);
fileNameText.setToolTipText("源代码保存路径");
fileNameText.setBounds(286, 58, 249, 25);
// 保存源代码按钮的操作
saveBtn = new Button(shell, SWT.NONE);
saveBtn.setToolTipText("保存查看到的网页源代码");
saveBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
saveCode();
}
});
saveBtn.setBounds(539, 58, 130, 23);
saveBtn.setText("代码保存");
shell.setTabList(new Control[] { httpText, combo, viewBtn, resetBtn,
exitBtn, saveBtn });
}
}
网站源码查看
最新推荐文章于 2024-09-12 22:54:45 发布