SWT JFace 小制作 文本阅读器

所属分类: 软件编程 / Java编程 阅读数: 126
收藏 0 赞 0 分享
代码如下:
复制代码 代码如下:

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;
    }
}
更多精彩内容其他人还在看

Java实现文件分割和文件合并实例

本篇文章主要介绍了Java实现文件分割和文件合并实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

LeetCode -- Path Sum III分析及实现方法

这篇文章主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

使用MUI框架构建App请求http接口实例代码

下面小编就为大家分享一篇使用MUI框架构建App请求http接口实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java发送邮箱验证码、session校验功能

本篇主要描述“发送邮箱验证码、session校验”相关前(html\js)后(java)台代码,业务逻辑示例,需要的朋友可以参考下
收藏 0 赞 0 分享

windows下 jdk1.7安装教程图解

java编程的初学者在开始编码前都会遇到一个难题,那就是jdk1.7环境变量配置怎么操作,怎么安装,针对这个难题,小编特地为大家整理相关教程,不了解的朋友可以前往查看使用
收藏 0 赞 0 分享

JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别

最近有由于工作需要,开始接触阿里云的云市场的印刷文字识别API-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单参考
收藏 0 赞 0 分享

springboot 使用yml配置文件给静态变量赋值教程

这篇文章主要介绍了springboot 使用yml配置文件给静态变量赋值教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

关于springboot 配置文件中属性变量引用方式@@解析

这篇文章主要介绍了关于springboot 配置文件中属性变量引用方式@@解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springboot yml定义属性,下文中${} 引用说明

这篇文章主要介绍了springboot yml定义属性,下文中${} 引用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

IDEA解决Java:程序包xxxx不存在的问题

这篇文章主要介绍了IDEA解决Java:程序包xxxx不存在的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多