ApplicationWindow

本文介绍了一个使用ApplicationWindow 和Action 实现的一个文本编辑器。界面美观,基本功能齐全。代码齐全。

首先看 MainWindow.java。

[java] view plain copy
  1. //MainWindow.java  
  2.   
  3. import org.eclipse.jface.action.MenuManager;  
  4. import org.eclipse.jface.action.Separator;  
  5. import org.eclipse.jface.action.StatusLineManager;  
  6. import org.eclipse.jface.window.ApplicationWindow;  
  7. import org.eclipse.swt.SWT;  
  8. import org.eclipse.swt.events.ModifyEvent;  
  9. import org.eclipse.swt.events.ModifyListener;  
  10. import org.eclipse.swt.widgets.Composite;  
  11. import org.eclipse.swt.widgets.Control;  
  12. import org.eclipse.swt.widgets.Display;  
  13. import org.eclipse.swt.widgets.Shell;  
  14. import org.eclipse.swt.widgets.Text;  
  15.   
  16. public class MainWindow extends ApplicationWindow{  
  17.     private NewAction newAction;  
  18.     private OpenAction openAction;  
  19.     private SaveAction saveAction;  
  20.     private SaveAsAction saveAsAction;  
  21.     private ExitAction exitAction;  
  22.     private CopyAction copyAction;  
  23.     private CutAction cutAction;  
  24.     private PasteAction pasteAction;  
  25.     private HelpAction helpAction;  
  26.     private FileManager manager;  
  27.     private Text content;  
  28.     private static MainWindow app;  
  29.       
  30.     private MainWindow(){  
  31.         super(null);  
  32.         app = this;  
  33.         manager = new FileManager();  
  34.         newAction = new NewAction();  
  35.         openAction = new OpenAction();  
  36.         saveAction = new SaveAction();  
  37.         saveAsAction = new SaveAsAction();  
  38.         exitAction = new ExitAction();  
  39.         copyAction = new CopyAction();  
  40.         cutAction = new CutAction();  
  41.         pasteAction = new PasteAction();  
  42.         helpAction = new HelpAction();  
  43.           
  44.         this.addMenuBar();  
  45.         this.addToolBar(SWT.FLAT);  
  46.         this.addStatusLine();  
  47.     }  
  48.       
  49.     public static MainWindow getApp(){  
  50.         return app;  
  51.     }  
  52.       
  53.     protected void configureShell(Shell shell){  
  54.         super.configureShell(shell);  
  55.         shell.setText("简单写字板");  
  56.         shell.setMaximized(true);  
  57.     }  
  58.       
  59.     protected Control createContents(Composite parent){  
  60.         content = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);  
  61.         content.addModifyListener(new ModifyListener(){  
  62.             public void modifyText(ModifyEvent e){  
  63.                 manager.setDirty(true);  
  64.             }  
  65.         });  
  66.         return parent;  
  67.     }  
  68.       
  69.     protected MenuManager createMenuManager(){  
  70.         MenuManager menuBar = new MenuManager();  
  71.           
  72.         MenuManager fileMenu = new MenuManager("文件(&F)");  
  73.         MenuManager editMenu = new MenuManager("编辑(&E)");  
  74.         MenuManager formatMenu = new MenuManager("格式(&F)");  
  75.         MenuManager helpMenu = new MenuManager("帮助(&H)");  
  76.           
  77.         menuBar.add(fileMenu);  
  78.         menuBar.add(editMenu);  
  79.         menuBar.add(formatMenu);  
  80.         menuBar.add(helpMenu);  
  81.           
  82.         fileMenu.add(newAction);  
  83.         fileMenu.add(openAction);  
  84.         fileMenu.add(new Separator());  
  85.         fileMenu.add(saveAction);  
  86.         fileMenu.add(saveAsAction);  
  87.         fileMenu.add(new Separator());  
  88.         fileMenu.add(exitAction);  
  89.           
  90.         editMenu.add(copyAction);  
  91.         editMenu.add(cutAction);  
  92.         editMenu.add(pasteAction);  
  93.           
  94.         formatMenu.add(new FormatAction(FormatAction.TYPE_FONT));  
  95.         formatMenu.add(new FormatAction(FormatAction.TYPE_BGCOLOR));  
  96.         formatMenu.add(new FormatAction(FormatAction.TYPE_FORECOLOR));  
  97.           
  98.         helpMenu.add(helpAction);  
  99.         return menuBar;  
  100.     }  
  101.       
  102.     public static void main(String[] args){  
  103.         MainWindow main = new MainWindow();  
  104.         main.setBlockOnOpen(true);  
  105.         main.open();  
  106.         Display.getCurrent().dispose();  
  107.     }  
  108.       
  109.     public Text getContent(){  
  110.         return content;  
  111.     }  
  112.     public FileManager getManager(){  
  113.         return manager;  
  114.     }  
  115.     public void setManager(FileManager manager){  
  116.         this.manager = manager;  
  117.     }  
  118.       
  119.     public StatusLineManager getStatusLineManager(){  
  120.         return super.getStatusLineManager();  
  121.     }  
  122. }  

该类继承了 ApplicationWindow。Application 类是应用程序的窗口类,它继承自 Window 类。另外还添加了设置菜单栏、工具栏、状态栏等的方法。createMenuManager 方法用于添加菜单内容。addMenuBar 方法将createMenuManager 创建的菜单添加到窗口。菜单项add 方法添加一个action 对象。Action 对象在run()中实现了具体操作,我们将在后面介绍。除此之外,还需要一个FileManager 来管理打开的文件。

FileManager.java:

[java] view plain copy
  1. //FileManager.java  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStreamWriter;  
  11. import java.io.Reader;  
  12. import java.io.Writer;  
  13.   
  14.   
  15. public class FileManager {  
  16.     private String fileName;  
  17.     private boolean dirty = false;  
  18.     private String content;  
  19.     public FileManager(){  
  20.           
  21.     }  
  22.       
  23.     public void load(String name){  
  24.         final String textString;  
  25.         try{  
  26.             File file = new File(name);  
  27.             FileInputStream stream = new FileInputStream(file.getPath());  
  28.             Reader in = new BufferedReader(new InputStreamReader(stream));  
  29.             char[] readBuffer = new char[2048];  
  30.             StringBuffer buffer = new StringBuffer((int)file.length());  
  31.             int n;  
  32.             while((n=in.read(readBuffer))>0){  
  33.                 buffer.append(readBuffer, 0 ,n);  
  34.             }  
  35.             textString = buffer.toString();  
  36.             stream.close();  
  37.         }catch(FileNotFoundException e){  
  38.             MainWindow.getApp().getStatusLineManager().setMessage("文件未找到:"+fileName);  
  39.             return;  
  40.         }catch(IOException e){  
  41.             MainWindow.getApp().getStatusLineManager().setMessage("读文件出错: "+fileName);  
  42.             return;  
  43.         }  
  44.         content = textString;  
  45.         this.fileName= name;  
  46.     }  
  47.       
  48.     public void save(String name){  
  49.         final String textString = content;  
  50.         try{  
  51.             File file = new File(name);  
  52.             FileOutputStream stream = new FileOutputStream(file.getPath());  
  53.             Writer out = new OutputStreamWriter(stream);  
  54.             out.write(textString);  
  55.             out.flush();  
  56.             stream.close();  
  57.         }catch(FileNotFoundException e){  
  58.             MainWindow.getApp().getStatusLineManager().setMessage("文件未找到:"+fileName);  
  59.             return;  
  60.         }catch(IOException e){  
  61.             MainWindow.getApp().getStatusLineManager().setMessage("读文件出错: "+fileName);  
  62.             return;  
  63.         }  
  64.     }  
  65.       
  66.     public String getContent(){  
  67.         return content;  
  68.     }  
  69.       
  70.     public void setContent(String content){  
  71.         this.content = content;  
  72.     }  
  73.       
  74.     public boolean isDirty(){  
  75.         return dirty;  
  76.     }  
  77.       
  78.     public void setDirty(boolean dirty){  
  79.         this.dirty = dirty;  
  80.     }  
  81.       
  82.     public String getFileName(){  
  83.         return fileName;  
  84.     }  
  85.       
  86.     public void setFileName(String fileName){  
  87.         this.fileName = fileName;  
  88.     }  
  89. }  

FileManager 类的 load 和 save 方法分别用于加载和保存文件。

用到的一些Action 类如下所示:

CopyAction.java

[java] view plain copy
  1. //CopyAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6.   
  7.   
  8. public class CopyAction extends Action{  
  9.     public CopyAction(){  
  10.         super();  
  11.         setText("复制(&C)");  
  12.         setToolTipText("复制");  
  13.         setAccelerator(SWT.CTRL + 'C');  
  14.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\copy.gif"));  
  15.     }  
  16.       
  17.     public void run(){  
  18.         MainWindow.getApp().getContent().copy();  
  19.     }  
  20. }  

CutAction.java

[java] view plain copy
  1. //CutAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6.   
  7.   
  8. public class CutAction extends Action{  
  9.     public CutAction(){  
  10.         super();  
  11.         setText("剪切(&C)");  
  12.         setToolTipText("剪切");  
  13.         setAccelerator(SWT.CTRL + 'X');  
  14.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\cut.gif"));  
  15.     }  
  16.       
  17.     public void run(){  
  18.         MainWindow.getApp().getContent().cut();  
  19.     }  
  20. }  

ExitAction.java

[java] view plain copy
  1. //ExitAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5.   
  6.   
  7. public class ExitAction extends Action{  
  8.     public ExitAction(){  
  9.         super();  
  10.         setText("退出(&E)");  
  11.         setToolTipText("退出系统");  
  12.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\exit.gif"));  
  13.     }  
  14.       
  15.     public void run(){  
  16.         MainWindow.getApp().close();  
  17.     }  
  18. }  

FormatAction.java

[java] view plain copy
  1. //FormatAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.graphics.Color;  
  6. import org.eclipse.swt.graphics.Font;  
  7. import org.eclipse.swt.graphics.FontData;  
  8. import org.eclipse.swt.graphics.RGB;  
  9. import org.eclipse.swt.widgets.ColorDialog;  
  10. import org.eclipse.swt.widgets.FontDialog;  
  11. import org.eclipse.swt.widgets.Text;  
  12.   
  13.   
  14. public class FormatAction extends Action{  
  15.     public static final String TYPE_FORECOLOR = "FORECOLOR";  
  16.     public static final String TYPE_BGCOLOR = "BGCOLOR";  
  17.     public static final String TYPE_FONT = "FONT";  
  18.     private String formatType;  
  19.     public FormatAction(String type){  
  20.         super();  
  21.         this.formatType = type;  
  22.         initAction();  
  23.     }  
  24.       
  25.     private void initAction(){  
  26.         if(formatType.equals(TYPE_FONT)){  
  27.             this.setText("设置字体");  
  28.             this.setToolTipText("设置字体");  
  29.             setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\font.gif"));  
  30.         }else if(formatType.equals(TYPE_FORECOLOR)){  
  31.             this.setText("设置前景色");  
  32.             this.setToolTipText("设置前景色");  
  33.             setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\foreColor.gif"));  
  34.         }else{  
  35.             this.setText("设置背景色");  
  36.             this.setToolTipText("设置背景色");  
  37.             setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\bgColor.gif"));  
  38.         }  
  39.     }  
  40.       
  41.     public void run(){  
  42.         Text content = MainWindow.getApp().getContent();  
  43.         if(formatType.equals(TYPE_FONT)){  
  44.             FontDialog fontDialog = new FontDialog(MainWindow.getApp().getShell());  
  45.             fontDialog.setFontList(content.getFont().getFontData());  
  46.             FontData fontData = fontDialog.open();  
  47.             if(fontData != null){  
  48.                 Font font = new Font(MainWindow.getApp().getShell().getDisplay(), fontData);  
  49.                 content.setFont(font);  
  50.             }  
  51.         }else if(formatType.equals(TYPE_FORECOLOR)){  
  52.             ColorDialog colorDialog = new ColorDialog(MainWindow.getApp().getShell());  
  53.             colorDialog.setRGB(content.getForeground().getRGB());  
  54.             RGB rgb = colorDialog.open();  
  55.             if(rgb != null){  
  56.                 Color foregroundColor = new Color(MainWindow.getApp().getShell().getDisplay(), rgb);  
  57.                 content.setForeground(foregroundColor);  
  58.                 foregroundColor.dispose();  
  59.             }  
  60.         }else{  
  61.             ColorDialog colorDialog = new ColorDialog(MainWindow.getApp().getShell());  
  62.             colorDialog.setRGB(content.getForeground().getRGB());  
  63.             RGB rgb = colorDialog.open();  
  64.             if(rgb != null){  
  65.                 Color bgColor = new Color(MainWindow.getApp().getShell().getDisplay(), rgb);  
  66.                 content.setForeground(bgColor);  
  67.                 bgColor.dispose();  
  68.             }  
  69.         }  
  70.     }  
  71. }  

HelpAction.java

[java] view plain copy
  1. //HelpAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.dialogs.MessageDialog;  
  5. import org.eclipse.jface.resource.ImageDescriptor;  
  6.   
  7. public class HelpAction extends Action{  
  8.     public HelpAction(){  
  9.         super();  
  10.         setText("帮助内容(&H)");  
  11.         setToolTipText("帮助");  
  12.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class,"icons/help.gif"));  
  13.     }  
  14.   
  15.     public void run(){  
  16.         MessageDialog.openInformation(null,"帮助","谢谢使用!");  
  17.     }  
  18. }  

NewAction.java

[java] view plain copy
  1. //NewAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6.   
  7.   
  8. public class NewAction extends Action{  
  9.     public NewAction(){  
  10.         super();  
  11.         setText("新建(&N)");  
  12.         this.setAccelerator(SWT.ALT + SWT.SHIFT + 'N');  
  13.         setToolTipText("新建");  
  14.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\new.gif"));  
  15.     }  
  16.       
  17.     public void run(){  
  18.         MainWindow.getApp().getContent().setText("");  
  19.         MainWindow.getApp().setManager(new FileManager());  
  20.     }  
  21. }  

OpenAction.java

[java] view plain copy
  1. //OpenAction.java  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4.   
  5. import org.eclipse.core.runtime.IProgressMonitor;  
  6. import org.eclipse.jface.action.Action;  
  7. import org.eclipse.jface.operation.IRunnableWithProgress;  
  8. import org.eclipse.jface.operation.ModalContext;  
  9. import org.eclipse.jface.resource.ImageDescriptor;  
  10. import org.eclipse.swt.SWT;  
  11. import org.eclipse.swt.widgets.FileDialog;  
  12.   
  13. public class OpenAction extends Action{  
  14.     public OpenAction(){  
  15.         super();  
  16.         setText("打开(&O)");  
  17.         setToolTipText("打开文件");  
  18.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\open.gif"));  
  19.     }  
  20.       
  21.     public void run(){  
  22.         FileDialog dialog = new FileDialog(MainWindow.getApp().getShell(), SWT.OPEN);  
  23.         dialog.setFilterExtensions(new String[]{"*.java", "*.*"});  
  24.         final String name = dialog.open();  
  25.         if((name == null) || (name.length()==0))  
  26.             return;  
  27.         final FileManager fileManager = MainWindow.getApp().getManager();  
  28.         try{  
  29.             ModalContext.run(new IRunnableWithProgress(){  
  30.                 public void run(IProgressMonitor progressMonitor){  
  31.                     progressMonitor.beginTask("打开文件", IProgressMonitor.UNKNOWN);  
  32.                     fileManager.load(name);  
  33.   
  34.                     progressMonitor.done();  
  35.                 }  
  36.             }, true, MainWindow.getApp().getStatusLineManager().getProgressMonitor(),   
  37.             MainWindow.getApp().getShell().getDisplay());  
  38.         }catch(InvocationTargetException e){  
  39.             e.printStackTrace();  
  40.         }catch(InterruptedException e){  
  41.             e.printStackTrace();  
  42.         }  
  43.           
  44.         MainWindow.getApp().getContent().setText(fileManager.getContent());  
  45.         MainWindow.getApp().getStatusLineManager().setMessage("当前打开的文件是: "+name);  
  46.     }  
  47. }  

PasteAction.java

[java] view plain copy
  1. //PasteAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6.   
  7.   
  8. public class PasteAction extends Action{  
  9.     public PasteAction(){  
  10.         super();  
  11.         setText("粘贴(&P)");  
  12.         setToolTipText("粘贴");  
  13.         setAccelerator(SWT.CTRL + 'V');  
  14.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\copy.gif"));  
  15.     }  
  16.       
  17.     public void run(){  
  18.         MainWindow.getApp().getContent().copy();  
  19.     }  
  20. }  

SaveAction.java

[java] view plain copy
  1. //SaveAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6. import org.eclipse.swt.widgets.FileDialog;  
  7. import org.eclipse.swt.widgets.MessageBox;  
  8.   
  9.   
  10. public class SaveAction extends Action{  
  11.     public SaveAction(){  
  12.         super();  
  13.         setText("保存(&S)");  
  14.         setToolTipText("保存文件");  
  15.           
  16.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\save.gif"));  
  17.     }  
  18.       
  19.     public void run(){  
  20.         final FileManager fileManager = MainWindow.getApp().getManager();  
  21.         if(!fileManager.isDirty())  
  22.             return;  
  23.         if(fileManager.getFileName()==null){  
  24.             FileDialog saveDialog = new FileDialog(MainWindow.getApp().getShell(), SWT.SAVE);  
  25.             saveDialog.setText("请选择所要保存的文件");  
  26.             saveDialog.setFilterPath("F:\");  
  27.             saveDialog.setFilterExtensions(new String[]{"*.java","*.*"});  
  28.             String saveFile = saveDialog.open();  
  29.             if(saveFile != null){  
  30.                 fileManager.setFileName(saveFile);  
  31.                 fileManager.setContent(MainWindow.getApp().getContent().getText());  
  32.                 fileManager.save(fileManager.getFileName());  
  33.             }  
  34.             fileManager.setDirty(false);  
  35.             return;  
  36.         }  
  37.         if(fileManager.getFileName()!=null){  
  38.             MessageBox box = new MessageBox(MainWindow.getApp().getShell(), SWT.ICON_QUESTION | SWT.YES| SWT.NO);  
  39.             box.setMessage("您确定要保存文件吗?");  
  40.             int choice = box.open();  
  41.             if(choice == SWT.NO)  
  42.                 return;  
  43.             fileManager.setContent(MainWindow.getApp().getContent().getText());  
  44.             fileManager.save(fileManager.getFileName());  
  45.             fileManager.setDirty(false);  
  46.             return;  
  47.         }  
  48.     }  
  49. }  

SaveAsAction.java

[java] view plain copy
  1. //SaveAsAction.java  
  2.   
  3. import org.eclipse.jface.action.Action;  
  4. import org.eclipse.jface.resource.ImageDescriptor;  
  5. import org.eclipse.swt.SWT;  
  6. import org.eclipse.swt.widgets.FileDialog;  
  7.   
  8.   
  9. public class SaveAsAction extends Action{  
  10.     public SaveAsAction(){  
  11.         super();  
  12.         setText("另存为(&A)");  
  13.         setToolTipText("另存为");  
  14.         setImageDescriptor(ImageDescriptor.createFromFile(NewAction.class, "icons\saveas.gif"));  
  15.     }  
  16.       
  17.     public void run(){  
  18.         final FileManager fileManager = MainWindow.getApp().getManager();  
  19.         FileDialog saveDialog = new FileDialog(MainWindow.getApp().getShell(), SWT.SAVE);  
  20.         saveDialog.setText("请选择所要保存的文件");  
  21.         saveDialog.setFilterPath("F:\");  
  22.         saveDialog.setFilterExtensions(new String[]{"*.java","*.*"});  
  23.         String saveFile = saveDialog.open();  
  24.         if(saveFile !=null){  
  25.             fileManager.setFileName(saveFile);  
  26.             fileManager.setContent(MainWindow.getApp().getContent().getText());  
  27.             fileManager.save(fileManager.getFileName());  
  28.         }  
  29.         fileManager.setDirty(false);  
  30.         return;  
  31.     }  
  32. }  

以上就是代码部分。

程序运行如下所示:

图标文件存储在项目 bin 目录下的 icons 目录下。所有图标文件都是从org.eclipse.ui_*.jar 中获取的。只是改了一下文件名。

原文地址:https://www.cnblogs.com/justuntil/p/5616509.html