SWT JFace 小制作 文本閱讀器
代碼如下:
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;
}
}
上一篇:SWT(JFace) 簡(jiǎn)易瀏覽器 制作實(shí)現(xiàn)代碼第1/2頁(yè)
欄 目:Java編程
下一篇:SWT(JFace)小制作 FileBrowser文件瀏覽
本文標(biāo)題:SWT JFace 小制作 文本閱讀器
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/8507.html
您可能感興趣的文章
- 01-10SWT(JFace)體驗(yàn)之FormLayout布局
- 01-10SWT(JFace)體驗(yàn)之RowLayout布局
- 01-10SWT JFace Bookmark 制作
- 01-10SWT(JFace)小制作 BugTracker
- 01-10SWT(JFace)體驗(yàn)之StyledText類
- 01-10SWT(JFace)體驗(yàn)之GridLayout布局
- 01-10SWT JFace 拖曳效果
- 01-10SWT(JFace)體驗(yàn)之ApplicationWindow
- 01-10SWT(JFace)體驗(yàn)之復(fù)制粘貼
- 01-10SWT(JFace)體驗(yàn)之打開(kāi)多個(gè)Form


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒(méi)有被捕獲的異常
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什