網站源碼查看

來源:互聯網
上載者:User

 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 });
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.