本文介绍了一个使用ApplicationWindow 和Action 实现的一个文本编辑器。界面美观,基本功能齐全。代码齐全。
首先看 MainWindow.java。
- //MainWindow.java
- import org.eclipse.jface.action.MenuManager;
- import org.eclipse.jface.action.Separator;
- import org.eclipse.jface.action.StatusLineManager;
- import org.eclipse.jface.window.ApplicationWindow;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.ModifyEvent;
- import org.eclipse.swt.events.ModifyListener;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Control;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- public class MainWindow extends ApplicationWindow{
- private NewAction newAction;
- private OpenAction openAction;
- private SaveAction saveAction;
- private SaveAsAction saveAsAction;
- private ExitAction exitAction;
- private CopyAction copyAction;
- private CutAction cutAction;
- private PasteAction pasteAction;
- private HelpAction helpAction;
- private FileManager manager;
- private Text content;
- private static MainWindow app;
- private MainWindow(){
- super(null);
- app = this;
- manager = new FileManager();
- newAction = new NewAction();
- openAction = new OpenAction();
- saveAction = new SaveAction();
- saveAsAction = new SaveAsAction();
- exitAction = new ExitAction();
- copyAction = new CopyAction();
- cutAction = new CutAction();
- pasteAction = new PasteAction();
- helpAction = new HelpAction();
- this.addMenuBar();
- this.addToolBar(SWT.FLAT);
- this.addStatusLine();
- }
- public static MainWindow getApp(){
- return app;
- }
- protected void configureShell(Shell shell){
- super.configureShell(shell);
- shell.setText("简单写字板");
- shell.setMaximized(true);
- }
- protected Control createContents(Composite parent){
- content = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
- content.addModifyListener(new ModifyListener(){
- public void modifyText(ModifyEvent e){
- manager.setDirty(true);
- }
- });
- return parent;
- }
- protected MenuManager createMenuManager(){
- MenuManager menuBar = new MenuManager();
- MenuManager fileMenu = new MenuManager("文件(&F)");
- MenuManager editMenu = new MenuManager("编辑(&E)");
- MenuManager formatMenu = new MenuManager("格式(&F)");
- MenuManager helpMenu = new MenuManager("帮助(&H)");
- menuBar.add(fileMenu);
- menuBar.add(editMenu);
- menuBar.add(formatMenu);
- menuBar.add(helpMenu);
- fileMenu.add(newAction);
- fileMenu.add(openAction);
- fileMenu.add(new Separator());
- fileMenu.add(saveAction);
- fileMenu.add(saveAsAction);
- fileMenu.add(new Separator());
- fileMenu.add(exitAction);
- editMenu.add(copyAction);
- editMenu.add(cutAction);
- editMenu.add(pasteAction);
- formatMenu.add(new FormatAction(FormatAction.TYPE_FONT));
- formatMenu.add(new FormatAction(FormatAction.TYPE_BGCOLOR));
- formatMenu.add(new FormatAction(FormatAction.TYPE_FORECOLOR));
- helpMenu.add(helpAction);
- return menuBar;
- }
- public static void main(String[] args){
- MainWindow main = new MainWindow();
- main.setBlockOnOpen(true);
- main.open();
- Display.getCurrent().dispose();
- }
- public Text getContent(){
- return content;
- }
- public FileManager getManager(){
- return manager;
- }
- public void setManager(FileManager manager){
- this.manager = manager;
- }
- public StatusLineManager getStatusLineManager(){
- return super.getStatusLineManager();
- }
- }
该类继承了 ApplicationWindow。Application 类是应用程序的窗口类,它继承自 Window 类。另外还添加了设置菜单栏、工具栏、状态栏等的方法。createMenuManager 方法用于添加菜单内容。addMenuBar 方法将createMenuManager 创建的菜单添加到窗口。菜单项add 方法添加一个action 对象。Action 对象在run()中实现了具体操作,我们将在后面介绍。除此之外,还需要一个FileManager 来管理打开的文件。
FileManager.java:
- //FileManager.java
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.Reader;
- import java.io.Writer;
- public class FileManager {
- private String fileName;
- private boolean dirty = false;
- private String content;
- public FileManager(){
- }
- public void load(String name){
- final String textString;
- try{
- File file = new File(name);
- FileInputStream stream = new FileInputStream(file.getPath());
- Reader in = new BufferedReader(new InputStreamReader(stream));
- char[] readBuffer = new char[2048];
- StringBuffer buffer = new StringBuffer((int)file.length());
- int n;
- while((n=in.read(readBuffer))>0){
- buffer.append(readBuffer, 0 ,n);
- }
- textString = buffer.toString();
- stream.close();
- }catch(FileNotFoundException e){
- MainWindow.getApp().getStatusLineManager().setMessage("文件未找到:"+fileName);
- return;
- }catch(IOException e){
- MainWindow.getApp().getStatusLineManager().setMessage("读文件出错: "+fileName);
- return;
- }
- content = textString;
- this.fileName= name;
- }
- public void save(String name){
- final String textString = content;
- try{
- File file = new File(name);
- FileOutputStream stream = new FileOutputStream(file.getPath());
- Writer out = new OutputStreamWriter(stream);
- out.write(textString);
- out.flush();
- stream.close();
- }catch(FileNotFoundException e){
- MainWindow.getApp().getStatusLineManager().setMessage("文件未找到:"+fileName);
- return;
- }catch(IOException e){
- MainWindow.getApp().getStatusLineManager().setMessage("读文件出错: "+fileName);
- return;
- }
- }
- public String getContent(){
- return content;
- }
- public void setContent(String content){
- this.content = content;
- }
- public boolean isDirty(){
- return dirty;
- }
- public void setDirty(boolean dirty){
- this.dirty = dirty;
- }
- public String getFileName(){
- return fileName;
- }
- public void setFileName(String fileName){
- this.fileName = fileName;
- }
- }
FileManager 类的 load 和 save 方法分别用于加载和保存文件。
用到的一些Action 类如下所示:
CopyAction.java
- //CopyAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- public class CopyAction extends Action{
- public CopyAction(){
- super();
- setText("复制(&C)");
- setToolTipText("复制");
- setAccelerator(SWT.CTRL + 'C');
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\copy.gif"));
- }
- public void run(){
- MainWindow.getApp().getContent().copy();
- }
- }
CutAction.java
- //CutAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- public class CutAction extends Action{
- public CutAction(){
- super();
- setText("剪切(&C)");
- setToolTipText("剪切");
- setAccelerator(SWT.CTRL + 'X');
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\cut.gif"));
- }
- public void run(){
- MainWindow.getApp().getContent().cut();
- }
- }
ExitAction.java
- //ExitAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- public class ExitAction extends Action{
- public ExitAction(){
- super();
- setText("退出(&E)");
- setToolTipText("退出系统");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\exit.gif"));
- }
- public void run(){
- MainWindow.getApp().close();
- }
- }
FormatAction.java
- //FormatAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.graphics.Color;
- import org.eclipse.swt.graphics.Font;
- import org.eclipse.swt.graphics.FontData;
- import org.eclipse.swt.graphics.RGB;
- import org.eclipse.swt.widgets.ColorDialog;
- import org.eclipse.swt.widgets.FontDialog;
- import org.eclipse.swt.widgets.Text;
- public class FormatAction extends Action{
- public static final String TYPE_FORECOLOR = "FORECOLOR";
- public static final String TYPE_BGCOLOR = "BGCOLOR";
- public static final String TYPE_FONT = "FONT";
- private String formatType;
- public FormatAction(String type){
- super();
- this.formatType = type;
- initAction();
- }
- private void initAction(){
- if(formatType.equals(TYPE_FONT)){
- this.setText("设置字体");
- this.setToolTipText("设置字体");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\font.gif"));
- }else if(formatType.equals(TYPE_FORECOLOR)){
- this.setText("设置前景色");
- this.setToolTipText("设置前景色");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\foreColor.gif"));
- }else{
- this.setText("设置背景色");
- this.setToolTipText("设置背景色");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\bgColor.gif"));
- }
- }
- public void run(){
- Text content = MainWindow.getApp().getContent();
- if(formatType.equals(TYPE_FONT)){
- FontDialog fontDialog = new FontDialog(MainWindow.getApp().getShell());
- fontDialog.setFontList(content.getFont().getFontData());
- FontData fontData = fontDialog.open();
- if(fontData != null){
- Font font = new Font(MainWindow.getApp().getShell().getDisplay(), fontData);
- content.setFont(font);
- }
- }else if(formatType.equals(TYPE_FORECOLOR)){
- ColorDialog colorDialog = new ColorDialog(MainWindow.getApp().getShell());
- colorDialog.setRGB(content.getForeground().getRGB());
- RGB rgb = colorDialog.open();
- if(rgb != null){
- Color foregroundColor = new Color(MainWindow.getApp().getShell().getDisplay(), rgb);
- content.setForeground(foregroundColor);
- foregroundColor.dispose();
- }
- }else{
- ColorDialog colorDialog = new ColorDialog(MainWindow.getApp().getShell());
- colorDialog.setRGB(content.getForeground().getRGB());
- RGB rgb = colorDialog.open();
- if(rgb != null){
- Color bgColor = new Color(MainWindow.getApp().getShell().getDisplay(), rgb);
- content.setForeground(bgColor);
- bgColor.dispose();
- }
- }
- }
- }
HelpAction.java
- //HelpAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.dialogs.MessageDialog;
- import org.eclipse.jface.resource.ImageDescriptor;
- public class HelpAction extends Action{
- public HelpAction(){
- super();
- setText("帮助内容(&H)");
- setToolTipText("帮助");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class,"icons/help.gif"));
- }
- public void run(){
- MessageDialog.openInformation(null,"帮助","谢谢使用!");
- }
- }
NewAction.java
- //NewAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- public class NewAction extends Action{
- public NewAction(){
- super();
- setText("新建(&N)");
- this.setAccelerator(SWT.ALT + SWT.SHIFT + 'N');
- setToolTipText("新建");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\new.gif"));
- }
- public void run(){
- MainWindow.getApp().getContent().setText("");
- MainWindow.getApp().setManager(new FileManager());
- }
- }
OpenAction.java
- //OpenAction.java
- import java.lang.reflect.InvocationTargetException;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.operation.IRunnableWithProgress;
- import org.eclipse.jface.operation.ModalContext;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.FileDialog;
- public class OpenAction extends Action{
- public OpenAction(){
- super();
- setText("打开(&O)");
- setToolTipText("打开文件");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\open.gif"));
- }
- public void run(){
- FileDialog dialog = new FileDialog(MainWindow.getApp().getShell(), SWT.OPEN);
- dialog.setFilterExtensions(new String[]{"*.java", "*.*"});
- final String name = dialog.open();
- if((name == null) || (name.length()==0))
- return;
- final FileManager fileManager = MainWindow.getApp().getManager();
- try{
- ModalContext.run(new IRunnableWithProgress(){
- public void run(IProgressMonitor progressMonitor){
- progressMonitor.beginTask("打开文件", IProgressMonitor.UNKNOWN);
- fileManager.load(name);
- progressMonitor.done();
- }
- }, true, MainWindow.getApp().getStatusLineManager().getProgressMonitor(),
- MainWindow.getApp().getShell().getDisplay());
- }catch(InvocationTargetException e){
- e.printStackTrace();
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- MainWindow.getApp().getContent().setText(fileManager.getContent());
- MainWindow.getApp().getStatusLineManager().setMessage("当前打开的文件是: "+name);
- }
- }
PasteAction.java
- //PasteAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- public class PasteAction extends Action{
- public PasteAction(){
- super();
- setText("粘贴(&P)");
- setToolTipText("粘贴");
- setAccelerator(SWT.CTRL + 'V');
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\copy.gif"));
- }
- public void run(){
- MainWindow.getApp().getContent().copy();
- }
- }
SaveAction.java
- //SaveAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.FileDialog;
- import org.eclipse.swt.widgets.MessageBox;
- public class SaveAction extends Action{
- public SaveAction(){
- super();
- setText("保存(&S)");
- setToolTipText("保存文件");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\save.gif"));
- }
- public void run(){
- final FileManager fileManager = MainWindow.getApp().getManager();
- if(!fileManager.isDirty())
- return;
- if(fileManager.getFileName()==null){
- FileDialog saveDialog = new FileDialog(MainWindow.getApp().getShell(), SWT.SAVE);
- saveDialog.setText("请选择所要保存的文件");
- saveDialog.setFilterPath("F:\\");
- saveDialog.setFilterExtensions(new String[]{"*.java","*.*"});
- String saveFile = saveDialog.open();
- if(saveFile != null){
- fileManager.setFileName(saveFile);
- fileManager.setContent(MainWindow.getApp().getContent().getText());
- fileManager.save(fileManager.getFileName());
- }
- fileManager.setDirty(false);
- return;
- }
- if(fileManager.getFileName()!=null){
- MessageBox box = new MessageBox(MainWindow.getApp().getShell(), SWT.ICON_QUESTION | SWT.YES| SWT.NO);
- box.setMessage("您确定要保存文件吗?");
- int choice = box.open();
- if(choice == SWT.NO)
- return;
- fileManager.setContent(MainWindow.getApp().getContent().getText());
- fileManager.save(fileManager.getFileName());
- fileManager.setDirty(false);
- return;
- }
- }
- }
SaveAsAction.java
- //SaveAsAction.java
- import org.eclipse.jface.action.Action;
- import org.eclipse.jface.resource.ImageDescriptor;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.FileDialog;
- public class SaveAsAction extends Action{
- public SaveAsAction(){
- super();
- setText("另存为(&A)");
- setToolTipText("另存为");
- setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\\saveas.gif"));
- }
- public void run(){
- final FileManager fileManager = MainWindow.getApp().getManager();
- FileDialog saveDialog = new FileDialog(MainWindow.getApp().getShell(), SWT.SAVE);
- saveDialog.setText("请选择所要保存的文件");
- saveDialog.setFilterPath("F:\\");
- saveDialog.setFilterExtensions(new String[]{"*.java","*.*"});
- String saveFile = saveDialog.open();
- if(saveFile !=null){
- fileManager.setFileName(saveFile);
- fileManager.setContent(MainWindow.getApp().getContent().getText());
- fileManager.save(fileManager.getFileName());
- }
- fileManager.setDirty(false);
- return;
- }
- }
以上就是代码部分。
程序运行如下所示:
图标文件存储在项目 bin 目录下的 icons 目录下。所有图标文件都是从org.eclipse.ui_*.jar 中获取的。只是改了一下文件名。
ApplicationWindow的更多相关文章
-
QML的Window与ApplicationWindow
ApplicationWindow需要导入QtQuick.Controls Window需要导入QtQuick.Window . 默认不可见,需要设置visible:true才可见. 主要区别就是Ap ...
-
Qt5.3中qml ApplicationWindow设置窗口无边框问题
这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...
-
JFace下ApplicationWindow关闭窗口时结束进程
/** * Configure the shell. * @param newShell */ @Override protected void configureShell(Shell newShe ...
-
《Qt Quick 4小时入门》学习笔记2
http://edu.csdn.net/course/detail/1042/14805?auto_start=1 Qt Quick 4小时入门 第五章:Qt Quick基本界面元素介绍 1. ...
-
使SWT/JFace支持跨平台
由于SWT的实现机制,在不同平台下,必须引用不同swt*.jar. 由于这个瓶颈,我们要为不同的平台编译不同的版本.但是这是可以避免的.这将是本文要讨论的内容. 我一共google到了3种soluti ...
-
写出形似QML的C++代码
最开始想出的标题是<Declarative C++ GUI库>,但太标题党了.只写了两行代码,连Demo都算不上,怎么能叫库呢……后来想换掉“库”这个字,但始终找不到合适词来替换.最后还是 ...
-
qml基础学习 基础概念
一.概括 学习qt已有2年多的时间,从qt4.7开始使用直到现在正在使用的qt5.6,基本都在windows机器上做开发.最近有意向看了下qt的qml部分,觉着还是挺不错的,毕竟可以做嵌入式移动端产品 ...
-
一个QMLListView的例子--
一般人不知道怎么去过滤ListView里面的数据,下面是一个转载的文章:http://imaginativethinking.ca/use-qt-quicks-delegatemodelgroup/ ...
-
qml去标题栏
只要加入"flags: Qt.Window | Qt.FramelessWindowHint "属性就可实现去标题栏. 注意:在使用这个属性的时候要先导入QtQuick.Windo ...
随机推荐
-
linux 文件类型 文件权限
linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log “d”表示目录 :drwxr-xr- ...
-
JavaScript 中的内存泄漏
JavaScript 中的内存泄漏 JavaScript 是一种垃圾收集式语言,这就是说,内存是根据对象的创建分配给该对象的,并会在没有对该对象的引用时由浏览器收回.JavaScript 的垃圾收集机 ...
-
行内元素有哪些?块级元素有哪些?CSS的盒模型?转载
块级元素:div p h1 h2 h3 h4 form ul行内元素: a b br i span input selectCss盒模型:内容,border ,margin,padding css中的 ...
-
VRTK实现瞬移需要添加的脚本
进入一个新的公司,boss让实现漫游,但是新公司的Unity版本是5.6,我之前的瞬移插件不好用了,无奈之下找到一个我不熟悉的插件VRTK,但是查了很多资料也没有实现瞬移.经过自己查脚本与实验终于得到 ...
-
编年史:OI测试
2019.4.18 t1:给出不定方程ax+by+c=0,求x在x1~x2并且y在y1~y2时的解个数.考场上想的是一个扩欧板子敲下去,然后构造出x>=x1的最小解,同时得出y,然后通过通项来枚 ...
-
1.8.3suspend与resume方法的缺点--不同步
package com.cky.bean; /** * Created by edison on 2017/12/3. */ public class MyObject { private Strin ...
-
jsoncpp在Windows和Linux下的安装
Windows下: 参考这个网站,没什么问题,注意MTd这些选对就行了. http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.h ...
-
Activity的setResult方法
Activity的setResult方法http://blog.csdn.net/dinglin_87/article/details/8970144 调用setResult()方法必须在finish ...
-
[转载]Linux I/O 调度方法
http://scoke.blog.51cto.com/769125/490546 IO调度器的总体目标是希望让磁头能够总是往一个方向移动,移动到底了再往反方向走,这恰恰就是现实生活中的电梯模型,所以 ...
-
End-to-End Speech Recognition in English and Mandarin
w语音识别.噪音.方言,算法迭代. https://arxiv.org/abs/1512.02595 We show that an end-to-end deep learning approach ...