ApplicationWindow
本文介绍了一个使用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 ...
随机推荐
- 关于SAP的事务提交和回滚(LUW)
1 Sap的更新的类型 在sap中,可以使用CALL FUNCTION ... IN UPDATE TASK将多个数据更新绑定到一个database LUW中.程序使用COMMIT WORK提交修改请 ...
- VSS 请求程序和 SharePoint 2013
Windows Server 中的 VSS 可用于创建可备份和还原 Microsoft SharePoint Foundation 的应用程序.VSS 提供了一个基础结构,使第三方存储管理程序.业务程 ...
- ICSharpCode.SharpZipLib简单使用
胡乱做了个小例子,记录下来,以便后面复习. using System; using System.Collections.Generic; using System.Linq; using Syste ...
- 获取设备IMEI ,手机名称,系统SDK版本号,系统版本号
1.获取设备IMEI TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Str ...
- Android判断屏幕开关状态
方法一:使用系统服务 PowerManager pm= (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); if(!pm. ...
- iOS 跳转到App Store下载或评论
//跳转到app在AppStore页面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString string ...
- 【代码笔记】iOS-读取一段文字
一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" @interface RootVie ...
- 转:查看sql语句执行时间/测试sql语句性能
原文出处:http://www.cnblogs.com/qanholas/archive/2011/05/06/2038543.html 写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务 ...
- jetty for linux 启用日志
jetty7.8 文档 :https://wiki.eclipse.org/Jetty jetty9 文档: http://www.eclipse.org/jetty/documentation/cu ...
- 【转】JavaScript 异步进化史
前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...