其他分享
首页 > 其他分享> > SWT JFace 小制作 文本阅读器

SWT JFace 小制作 文本阅读器

作者:互联网

  1 package swt_jface.demo11;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.lang.reflect.InvocationTargetException;
  7 import org.eclipse.core.runtime.IProgressMonitor;
  8 import org.eclipse.jface.action.Action;
  9 import org.eclipse.jface.action.MenuManager;
 10 import org.eclipse.jface.action.StatusLineManager;
 11 import org.eclipse.jface.action.ToolBarManager;
 12 import org.eclipse.jface.operation.IRunnableWithProgress;
 13 import org.eclipse.jface.resource.ImageDescriptor;
 14 import org.eclipse.jface.window.ApplicationWindow;
 15 import org.eclipse.swt.SWT;
 16 import org.eclipse.swt.widgets.Composite;
 17 import org.eclipse.swt.widgets.Control;
 18 import org.eclipse.swt.widgets.FileDialog;
 19 import org.eclipse.swt.widgets.Shell;
 20 import org.eclipse.swt.widgets.Text;
 21 public class FileViewer extends ApplicationWindow {
 22     Text text;
 23     String content;
 24     String lineDelimiter;
 25 
 26     IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
 27         public void run(IProgressMonitor monitor)
 28             throws InvocationTargetException, InterruptedException {
 29             System.out.println("Running from thread: " + Thread.currentThread().getName());
 30             getShell().getDisplay().syncExec(new Runnable() {
 31                 public void run() {
 32                     content = text.getText();
 33                     lineDelimiter = text.getLineDelimiter();
 34                 }
 35             });
 36             monitor.beginTask("Counting total number of lines", content.length());
 37             int lines = 1;
 38             for(int i=0; i<content.length(); i++) {
 39                 if(monitor.isCanceled()) {
 40                     monitor.done();
 41                     System.out.println("Action cancelled");
 42                     return;
 43                 }
 44                 if(i + lineDelimiter.length() < content.length()) {
 45                     if(lineDelimiter.equals(content.substring(i, i+lineDelimiter.length()))) {
 46                         lines ++;
 47                     }
 48                 }
 49                 monitor.worked(1);
 50                 Thread.sleep(1);
 51             }
 52             monitor.done();
 53             System.out.println("Total number of lines: " + lines);
 54         }
 55     };
 56 
 57     Action actionCount = new Action("Count", ImageDescriptor.createFromFile(null, "C:/icons/run.gif")) {
 58         public void run() {
 59             try {
 60                 FileViewer.this.run(true, true, runnableWithProgress);
 61             } catch (InvocationTargetException e) {
 62                 e.printStackTrace();
 63             } catch (InterruptedException e) {
 64                 e.printStackTrace();
 65             }
 66         }
 67     };
 68     public FileViewer(Shell parentShell) {
 69         super(parentShell);
 70         addMenuBar();
 71         addStatusLine();
 72         addToolBar(SWT.FLAT);
 73     }
 74     protected Control createContents(Composite parent) {
 75         getShell().setText("FileViewer v2.0");
 76         setStatus("Ready");
 77 
 78         text = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
 79         text.setSize(300, 200);
 80         return text;
 81     }
 82 
 83     Action actionOpenFile = new Action("Open", ImageDescriptor.createFromFile(null, "C:/icons/open.gif")) {
 84         public void run() {
 85             FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
 86             final String file = dialog.open();
 87             if(file != null) {
 88                 try {
 89                     String content = readFileAsAString(new File(file));
 90                     text.setText(content);
 91                     setStatus("File loaded successfully: " + file);
 92                 } catch (IOException e) {
 93                     e.printStackTrace();
 94                     setStatus("Failed to load file: " + file);
 95                 }
 96             }
 97         }
 98     };
 99     protected MenuManager createMenuManager() {
100         MenuManager menuManager = new MenuManager("");
101 
102         MenuManager fileMenuManager = new MenuManager("&File");
103         fileMenuManager.add(actionOpenFile);
104 
105         menuManager.add(fileMenuManager);
106 
107         MenuManager toolsMenuManager = new MenuManager("&Tools");
108         toolsMenuManager.add(actionCount);
109         menuManager.add(toolsMenuManager);
110 
111         return menuManager;
112     }
113     protected StatusLineManager createStatusLineManager() {
114         return super.createStatusLineManager();
115     }
116     protected ToolBarManager createToolBarManager(int style) {
117         ToolBarManager toolBarManager = new ToolBarManager(style);
118         toolBarManager.add(actionOpenFile);
119         toolBarManager.add(actionCount);
120         return toolBarManager;
121     }
122     public static void main(String[] args) {
123         ApplicationWindow viewer = new FileViewer(null);
124         viewer.setBlockOnOpen(true);
125         viewer.open();
126     }
127     public static String readFileAsAString(File file) throws IOException {
128         return new String(getBytesFromFile(file));
129     }
130     public static byte[] getBytesFromFile(File file) throws IOException {
131         InputStream is = new FileInputStream(file);
132         long length = file.length();
133         if (length > Integer.MAX_VALUE) {
134             throw new IllegalArgumentException("File is too large! (larger or equal to 2G)");
135         }
136         byte[] bytes = new byte[(int) length];
137         int offset = 0;
138         int numRead = 0;
139         while (offset < bytes.length
140             && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
141             offset += numRead;
142         }
143         if (offset < bytes.length) {
144             throw new IOException(
145                 "Could not completely read file " + file.getName());
146         }
147         is.close();
148         return bytes;
149     }
150 }

 

标签:JFace,bytes,阅读器,eclipse,swt,jface,org,import,SWT
来源: https://www.cnblogs.com/ypha/p/13742372.html