📄 myneweditor.java
字号:
package com.editor;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.rtf.*;
import javax.swing.undo.*;
public class MyNewEditor extends JFrame {
protected JTextPane m_monitor;
protected StyleContext m_context;
protected DefaultStyledDocument m_doc;
protected RTFEditorKit m_kit;
protected JFileChooser m_chooser;
protected SimpleFilter m_rtfFilter;
protected JToolBar m_toolBar;
protected JComboBox m_cbFonts;
protected JComboBox m_cbSizes;
protected SmallToggleButton m_bBold;
protected SmallToggleButton m_bItalic;
protected String m_fontName = "";
protected int m_fontSize = 0;
protected boolean m_skipUpdate;
protected int m_xStart = -1;
protected int m_xFinish = -1;
protected ColorMenu m_foreground;
protected ColorMenu m_background;
protected JComboBox m_cbStyles;
protected Hashtable m_styles;
protected UndoManager m_undo = new UndoManager();
protected Action m_undoAction;
protected Action m_redoAction;
protected String[] m_fontNames;
protected String[] m_fontSizes;
protected FindDialog m_findDialog;
public MyNewEditor() {
super("JEDitor");
setSize(600, 400);
m_monitor = new JTextPane();
m_kit = new RTFEditorKit();
m_monitor.setEditorKit(m_kit);
m_context = new StyleContext();
m_doc = new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
JScrollPane ps = new JScrollPane(m_monitor);
getContentPane().add(ps, BorderLayout.CENTER);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
m_chooser = new JFileChooser();
m_chooser.setCurrentDirectory(new File("."));
m_rtfFilter = new SimpleFilter("rtf", "RTF Documents");
m_chooser.setFileFilter(m_rtfFilter);
CaretListener lst = new CaretListener() {
public void caretUpdate(CaretEvent e) {
// showAttributes(e.getDot());
}
};
m_monitor.addCaretListener(lst);
FocusListener flst = new FocusListener() {
public void focusGained(FocusEvent e) {
if (m_xStart>=0 && m_xFinish>=0)
if (m_monitor.getCaretPosition()==m_xStart) {
m_monitor.setCaretPosition(m_xFinish);
m_monitor.moveCaretPosition(m_xStart);
}
else
m_monitor.select(m_xStart, m_xFinish);
}
public void focusLost(FocusEvent e) {
m_xStart = m_monitor.getSelectionStart();
m_xFinish = m_monitor.getSelectionEnd();
}
};
m_monitor.addFocusListener(flst);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
// showAttributes(0);
showStyles();
m_doc.addUndoableEditListener(new Undoer());
setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File");
mFile.setMnemonic('f');
ImageIcon iconNew = new ImageIcon("file_new.gif");
Action actionNew = new AbstractAction("New", iconNew) {
public void actionPerformed(ActionEvent e) {
m_doc = new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
// showAttributes(0);
showStyles();
m_doc.addUndoableEditListener(new Undoer());
}
};
JMenuItem item = mFile.add(actionNew);
item.setMnemonic('n');
ImageIcon iconOpen = new ImageIcon("file_open.gif");
Action actionOpen = new AbstractAction("Open...", iconOpen) {
public void actionPerformed(ActionEvent e) {
MyNewEditor.this.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Thread runner = new Thread() {
public void run() {
if (m_chooser.showOpenDialog(MyNewEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
MyNewEditor.this.repaint();
File fChoosen = m_chooser.getSelectedFile();
try {
InputStream in = new FileInputStream(fChoosen);
m_doc = new DefaultStyledDocument(m_context);
m_kit.read(in, m_doc, 0);
m_monitor.setDocument(m_doc);
in.close();
// showAttributes(0);
showStyles();
m_doc.addUndoableEditListener(new Undoer());
}
catch (Exception ex) {
//ex.printStackTrace();
System.out.println("Problems encountered: Note that RTF"
+ " support is still under development.");
}
MyNewEditor.this.setCursor(Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
}
};
runner.start();
}
};
item = mFile.add(actionOpen);
item.setMnemonic('o');
ImageIcon iconSave = new ImageIcon("file_save.gif");
Action actionSave = new AbstractAction("Save...", iconSave) {
public void actionPerformed(ActionEvent e) {
MyNewEditor.this.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Thread runner = new Thread() {
public void run() {
if (m_chooser.showSaveDialog(MyNewEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
MyNewEditor.this.repaint();
File fChoosen = m_chooser.getSelectedFile();
// Recall that text component read/write operations are
// thread safe. Its ok to do this in a separate thread.
try {
OutputStream out = new FileOutputStream(fChoosen);
m_kit.write(out, m_doc, 0, m_doc.getLength());
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
// Make sure chooser is updated to reflect new file
m_chooser.rescanCurrentDirectory();
MyNewEditor.this.setCursor(Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
}
};
runner.start();
}
};
item = mFile.add(actionSave);
item.setMnemonic('s');
mFile.addSeparator();
Action actionExit = new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
item = mFile.add(actionExit);
item.setMnemonic('x');
menuBar.add(mFile);
JMenu mEdit = new JMenu("Edit");
mEdit.setMnemonic('e');
Action action = new AbstractAction("Copy",
new ImageIcon("edit_copy.gif"))
{
public void actionPerformed(ActionEvent e) {
m_monitor.copy();
}
};
item = mEdit.add(action);
item.setMnemonic('c');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
KeyEvent.CTRL_MASK));
action = new AbstractAction("Cut",
new ImageIcon("edit_cut.gif"))
{
public void actionPerformed(ActionEvent e) {
m_monitor.cut();
}
};
item = mEdit.add(action);
item.setMnemonic('t');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
KeyEvent.CTRL_MASK));
action = new AbstractAction("Paste",
new ImageIcon("edit_paste.gif"))
{
public void actionPerformed(ActionEvent e) {
m_monitor.paste();
}
};
item = mEdit.add(action);
item.setMnemonic('p');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
KeyEvent.CTRL_MASK));
mEdit.addSeparator();
m_undoAction = new AbstractAction("Undo",
new ImageIcon("edit_undo.gif"))
{
public void actionPerformed(ActionEvent e) {
try {
m_undo.undo();
}
catch (CannotUndoException ex) {
System.err.println("Unable to undo: " + ex);
}
updateUndo();
}
};
item = mEdit.add(m_undoAction);
item.setMnemonic('u');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
KeyEvent.CTRL_MASK));
m_redoAction = new AbstractAction("Redo",
new ImageIcon("edit_redo.gif"))
{
public void actionPerformed(ActionEvent e) {
try {
m_undo.redo();
}
catch (CannotRedoException ex) {
System.err.println("Unable to redo: " + ex);
}
updateUndo();
}
};
item = mEdit.add(m_redoAction);
item.setMnemonic('r');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,
KeyEvent.CTRL_MASK));
mEdit.addSeparator();
Action findAction = new AbstractAction("Find...",
new ImageIcon("edit_find.gif"))
{
public void actionPerformed(ActionEvent e) {
MyNewEditor.this.repaint();
if (m_findDialog==null)
m_findDialog = new FindDialog(MyNewEditor.this, 0);
else
m_findDialog.setSelectedIndex(0);
Dimension d1 = m_findDialog.getSize();
Dimension d2 = MyNewEditor.this.getSize();
int x = Math.max((d2.width-d1.width)/2, 0);
int y = Math.max((d2.height-d1.height)/2, 0);
m_findDialog.setBounds(x + MyNewEditor.this.getX(),
y + MyNewEditor.this.getY(), d1.width, d1.height);
m_findDialog.show();
}
};
item = mEdit.add(findAction);
item.setMnemonic('f');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
KeyEvent.CTRL_MASK));
Action replaceAction = new AbstractAction("Replace...") {
public void actionPerformed(ActionEvent e) {
MyNewEditor.this.repaint();
if (m_findDialog==null)
m_findDialog = new FindDialog(MyNewEditor.this, 1);
else
m_findDialog.setSelectedIndex(1);
Dimension d1 = m_findDialog.getSize();
Dimension d2 = MyNewEditor.this.getSize();
int x = Math.max((d2.width-d1.width)/2, 0);
int y = Math.max((d2.height-d1.height)/2, 0);
m_findDialog.setBounds(x + MyNewEditor.this.getX(),
y + MyNewEditor.this.getY(), d1.width, d1.height);
m_findDialog.show();
}
};
item = mEdit.add(replaceAction);
item.setMnemonic('r');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
KeyEvent.CTRL_MASK));
menuBar.add(mEdit);
JMenu mFormat = new JMenu("Format");
m_foreground = new ColorMenu("Foreground");
m_foreground.setColor(m_monitor.getForeground());
m_foreground.setMnemonic('f');
ActionListener lst;
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, m_foreground.getColor());
setAttributeSet(attr);
}
};
m_foreground.addActionListener(lst);
mFormat.add(m_foreground);
MenuListener ml = new MenuListener() {
public void menuSelected(MenuEvent e) {
int p = m_monitor.getCaretPosition();
AttributeSet a = m_doc.getCharacterElement(p).
getAttributes();
Color c = StyleConstants.getForeground(a);
m_foreground.setColor(c);
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}
};
m_foreground.addMenuListener(ml);
m_background = new ColorMenu("Background");
m_background.setColor(m_monitor.getBackground());
m_background.setMnemonic('b');
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, m_background.getColor());
setAttributeSet(attr);
}
};
m_background.addActionListener(lst);
mFormat.add(m_background);
ml = new MenuListener() {
public void menuSelected(MenuEvent e) {
int p = m_monitor.getCaretPosition();
AttributeSet a = m_doc.getCharacterElement(p).
getAttributes();
Color c = StyleConstants.getBackground(a);
m_background.setColor(c);
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}
};
m_background.addMenuListener(ml);
menuBar.add(mFormat);
m_cbStyles = new JComboBox();
m_cbStyles.setMaximumSize(m_cbStyles.getPreferredSize());
m_cbStyles.setEditable(true);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (m_skipUpdate || m_cbStyles.getItemCount()==0)
return;
String name = (String)m_cbStyles.getSelectedItem();
int index = m_cbStyles.getSelectedIndex();
int p = m_monitor.getCaretPosition();
// New name entered
if (index == -1) {
m_cbStyles.addItem(name);
Style style = m_doc.addStyle(name, null);
AttributeSet a = m_doc.getCharacterElement(p).
getAttributes();
style.addAttributes(a);
return;
}
// Apply the selected style
Style currStyle = m_doc.getLogicalStyle(p);
if (!currStyle.getName().equals(name)) {
Style style = m_doc.getStyle(name);
setAttributeSet(style);
}
}
};
m_cbStyles.addActionListener(lst);
JMenu mTools = new JMenu("Tools");
mTools.setMnemonic('t');
menuBar.add(mTools);
return menuBar;
}
protected void setAttributeSet(AttributeSet attr) {
setAttributeSet(attr, false);
}
protected void setAttributeSet(AttributeSet attr,
boolean setParagraphAttributes)
{
if (m_skipUpdate)
return;
int xStart = m_monitor.getSelectionStart();
int xFinish = m_monitor.getSelectionEnd();
if (!m_monitor.hasFocus()) {
xStart = m_xStart;
xFinish = m_xFinish;
}
if (setParagraphAttributes)
m_doc.setParagraphAttributes(xStart,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -