📄 verysimpleeditor.java
字号:
/* * VerySimpleEditor * * Created: Wed Apr 11 18:55:30 2001 * $Revision$ * Modified: $Date$ * * Author: Jun Inamori * E-mail: j-office@osa.att.ne.jp * * Copyright (c) 1998-2000 Jun Inamori * 2-24-7 Shinsenri-Kitamachi, Toyonaka , * Osaka 560-0081 , Japan. * All rights reserved. * */package com.oopreserch.example;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.filechooser.*;import com.oopreserch.xml.util.*;/** * @author Jun Inamori */// This class represents the main window of the editor.// So, it should be the subclass of JFrame.// And, because we have already written the GUI in XML,// this class extends XMLFrame_1_0.// According to the user's action, the buttons and the menue// items should be enbabled or disabled.// For example, when the user modified the text, the button// for saving the document should be enabled.// To implement this feature, this class need to accept// DocumentEvent.// So, this class implements DocumentListener.public class VerySimpleEditor extends XMLPanel_1_0 implements DocumentListener{ static final private String TITLE="VerySimpleEditor"; // The following 4 variables will be retrieved // from "gui", which is accessible within the subclass // of XMLFrame_1_0. static private Action new_a=null; static private Action open_a=null; static private Action save_a=null; static private JTextArea text=null; static private File file=null; static private boolean wasModified=false; // This variable will be retrieved from "res", // which is availalble through the subclass // of XMLFrame_1_0. static private String warn=null; static final private VerySimpleAbout about_diag=new VerySimpleAbout(); static final private VerySimpleError err_diag=new VerySimpleError(); final private JFileChooser file_diag=new JFileChooser(System.getProperty("user.home")); static private JApplet applet=null; public VerySimpleEditor(JApplet ap){ // At the time the Window beeing closed, // exitApp() will be invoked. super(); applet=ap; // Based on the Locale, read the property resource file. // It may be VerySimpleEditor.properties, // VerySimpleEditor_ja.properties, // VerySimpleEditor_fr.properties or others. // Each property resource file points the XML file // for this GUI. // Parse the XML file pointed by the "xml_url" property // and place the Components on the JFrame. readXML(); // After the XML file is parsed, all the Components // are accessible from "gui" dictionary. // To add the required ActionListener to each // Component, we should define "iniParts()" method. // And all the actions on JToolBar are also available // in "gui" dictionary. // Same actions can be embed onto JMenu. // The "iniParts()" method is the good place // to prepare the JMenu. iniParts(); } public void iniParts(){ new_a=(Action)gui.getAction("new_doc"); open_a=(Action)gui.getAction("open_doc"); save_a=(Action)gui.getAction("save_doc"); JMenu f_menu=new JMenu(res.getString("MenuTitle")); (f_menu.add(new_a)).setText(res.getString("NewMenu")); (f_menu.add(open_a)).setText(res.getString("OpenMenu")); (f_menu.add(save_a)).setText(res.getString("SaveMenu")); f_menu.addSeparator(); JMenuItem exit_m=new JMenuItem(res.getString("ExitMenu")); exit_m.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ev){ exitApp(); } }); f_menu.add(exit_m); JMenuBar m_bar=new JMenuBar(); m_bar.add(f_menu); applet.setJMenuBar(m_bar); new_a.setEnabled(false); save_a.setEnabled(false); JButton about_b=(JButton)gui.getGUI("about"); about_b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ev){ aboutApp(); } }); text=(JTextArea)gui.getGUI("text"); (text.getDocument()).addDocumentListener(this); file_diag.setFileSelectionMode(JFileChooser.FILES_ONLY); warn=res.getString("Warning"); } public void invokeAction(ToolBarEvent tbe){ int act_num=tbe.getNumber(); switch(act_num){ case 1: newFile(); break; case 2: openFile(); break; case 3: saveFile(); break; default: break; } } // Member of DocumentListener interface. public void changedUpdate(DocumentEvent ev){ modified(); } // Member of DocumentListener interface. public void insertUpdate(DocumentEvent ev){ modified(); } // Member of DocumentListener interface. public void removeUpdate(DocumentEvent ev){ modified(); } private void modified(){ if(!wasModified){ wasModified=true; new_a.setEnabled(false); open_a.setEnabled(false); save_a.setEnabled(true); } } private void newFile(){ text.setText(""); file=null; wasModified=false; new_a.setEnabled(false); open_a.setEnabled(true); save_a.setEnabled(false); } private void openFile(){ file_diag.rescanCurrentDirectory(); file_diag.setSelectedFile(null); if(file_diag.showOpenDialog(null)!=JFileChooser.APPROVE_OPTION) { return; } file=file_diag.getSelectedFile(); // Read the file and display the content on JTextArea... try{ int size=(int)(file.length()); byte[] content=new byte[size]; FileInputStream fis=new FileInputStream(file); fis.read(content); fis.close(); text.setText(new String(content,"UTF-8")); } catch(Exception ex){ err_diag.showMessage("Error",ex.toString()); } wasModified=false; new_a.setEnabled(true); open_a.setEnabled(true); save_a.setEnabled(false); } private void saveFile(){ if(file==null){ file_diag.rescanCurrentDirectory(); file_diag.setSelectedFile(null); if(file_diag.showSaveDialog(null)!=JFileChooser.APPROVE_OPTION) { return; } file=file_diag.getSelectedFile(); } // Write the text on JTextArea into File... try{ BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8")); text.write(bw); bw.flush(); bw.close(); } catch(Exception ex){ err_diag.showMessage("Error",ex.toString()); } wasModified=false; new_a.setEnabled(true); open_a.setEnabled(true); save_a.setEnabled(false); } private void aboutApp(){ about_diag.openDiag(); } public void exitApp(){ err_diag.showMessage("Warning","This is Applet!"); }} //End of : VerySimpleEditor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -