欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SWT JFace 小制作 文本閱讀器

 更新時間:2009年06月25日 12:07:19   作者:  
SWT JFace 小制作 文本閱讀器
代碼如下:
復(fù)制代碼 代碼如下:

package swt_jface.demo11;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class FileViewer extends ApplicationWindow {
    Text text;
    String content;
    String lineDelimiter;

    IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor)
            throws InvocationTargetException, InterruptedException {
            System.out.println("Running from thread: " + Thread.currentThread().getName());
            getShell().getDisplay().syncExec(new Runnable() {
                public void run() {
                    content = text.getText();
                    lineDelimiter = text.getLineDelimiter();
                }
            });
            monitor.beginTask("Counting total number of lines", content.length());
            int lines = 1;
            for(int i=0; i<content.length(); i++) {
                if(monitor.isCanceled()) {
                    monitor.done();
                    System.out.println("Action cancelled");
                    return;
                }
                if(i + lineDelimiter.length() < content.length()) {
                    if(lineDelimiter.equals(content.substring(i, i+lineDelimiter.length()))) {
                        lines ++;
                    }
                }
                monitor.worked(1);
                Thread.sleep(1);
            }
            monitor.done();
            System.out.println("Total number of lines: " + lines);
        }
    };

    Action actionCount = new Action("Count", ImageDescriptor.createFromFile(null, "C:/icons/run.gif")) {
        public void run() {
            try {
                FileViewer.this.run(true, true, runnableWithProgress);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    public FileViewer(Shell parentShell) {
        super(parentShell);
        addMenuBar();
        addStatusLine();
        addToolBar(SWT.FLAT);
    }
    protected Control createContents(Composite parent) {
        getShell().setText("FileViewer v2.0");
        setStatus("Ready");

        text = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        text.setSize(300, 200);
        return text;
    }

    Action actionOpenFile = new Action("Open", ImageDescriptor.createFromFile(null, "C:/icons/open.gif")) {
        public void run() {
            FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
            final String file = dialog.open();
            if(file != null) {
                try {
                    String content = readFileAsAString(new File(file));
                    text.setText(content);
                    setStatus("File loaded successfully: " + file);
                } catch (IOException e) {
                    e.printStackTrace();
                    setStatus("Failed to load file: " + file);
                }
            }
        }
    };
    protected MenuManager createMenuManager() {
        MenuManager menuManager = new MenuManager("");

        MenuManager fileMenuManager = new MenuManager("&File");
        fileMenuManager.add(actionOpenFile);

        menuManager.add(fileMenuManager);

        MenuManager toolsMenuManager = new MenuManager("&Tools");
        toolsMenuManager.add(actionCount);
        menuManager.add(toolsMenuManager);

        return menuManager;
    }
    protected StatusLineManager createStatusLineManager() {
        return super.createStatusLineManager();
    }
    protected ToolBarManager createToolBarManager(int style) {
        ToolBarManager toolBarManager = new ToolBarManager(style);
        toolBarManager.add(actionOpenFile);
        toolBarManager.add(actionCount);
        return toolBarManager;
    }
    public static void main(String[] args) {
        ApplicationWindow viewer = new FileViewer(null);
        viewer.setBlockOnOpen(true);
        viewer.open();
    }
    public static String readFileAsAString(File file) throws IOException {
        return new String(getBytesFromFile(file));
    }
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("File is too large! (larger or equal to 2G)");
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
            && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException(
                "Could not completely read file " + file.getName());
        }
        is.close();
        return bytes;
    }
}

相關(guān)文章

  • Java ThreadPoolExecutor的參數(shù)深入理解

    Java ThreadPoolExecutor的參數(shù)深入理解

    這篇文章主要介紹了Java ThreadPoolExecutor的參數(shù)深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 如何使用Jackson和JSON Pointer查詢解析任何JSON節(jié)點

    如何使用Jackson和JSON Pointer查詢解析任何JSON節(jié)點

    本文介紹了JSON Pointer是字符串表達式,可以非常方便解析復(fù)雜JSON節(jié)點值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot中讀取application.properties配置文件的方法

    SpringBoot中讀取application.properties配置文件的方法

    這篇文章主要介紹了SpringBoot中讀取application.properties配置文件的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • 在Action中以Struts2的方式輸出JSON數(shù)據(jù)的實例

    在Action中以Struts2的方式輸出JSON數(shù)據(jù)的實例

    下面小編就為大家?guī)硪黄贏ction中以Struts2的方式輸出JSON數(shù)據(jù)的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • idea配置檢查XML中SQL語法及書寫sql語句智能提示的方法

    idea配置檢查XML中SQL語法及書寫sql語句智能提示的方法

    idea連接了數(shù)據(jù)庫,也可以執(zhí)行SQL查到數(shù)據(jù),但是無法識別sql語句中的表導(dǎo)致沒有提示,下面這篇文章主要給大家介紹了關(guān)于idea配置檢查XML中SQL語法及書寫sql語句智能提示的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • 一次java異步任務(wù)的實戰(zhàn)記錄

    一次java異步任務(wù)的實戰(zhàn)記錄

    最近做項目的時候遇到了一個小問題,從前臺提交到服務(wù)端A,A調(diào)用服務(wù)端B處理超時,下面這篇文章主要給大家介紹了一次java異步任務(wù)的實戰(zhàn)記錄,需要的朋友可以參考下
    2022-05-05
  • Java實現(xiàn)俄羅斯方塊游戲的示例代碼

    Java實現(xiàn)俄羅斯方塊游戲的示例代碼

    俄羅斯方塊是一個最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計和編程的益智類視頻游戲。本文將利用Java實現(xiàn)這一經(jīng)典的小游戲,感興趣的可以動手試一試
    2022-03-03
  • Java 常量池的實例詳解

    Java 常量池的實例詳解

    這篇文章主要介紹了Java 常量池的實例詳解的相關(guān)資料,Java的常量池中包含了類、接口、方法、字符串等一系列常量值,需要的朋友可以參考下
    2017-09-09
  • 在spring boot中使用java線程池ExecutorService的講解

    在spring boot中使用java線程池ExecutorService的講解

    今天小編就為大家分享一篇關(guān)于在spring boot中使用java線程池ExecutorService的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • JVM常見垃圾收集器學(xué)習(xí)指南

    JVM常見垃圾收集器學(xué)習(xí)指南

    這篇文章主要為大家介紹了JVM常見垃圾收集器學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論