SWT JFace Small making text reader _java programming

Source: Internet
Author: User
The code is as follows:
Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.