本文中代码主要参考了《Eclipse SWT/JFace 核心应用》第15章的程序。
本文介绍了一个使用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 中获取的。只是改了一下文件名。