📄 htmldocumenteditor.java
字号:
"Center", StyleConstants.ALIGN_CENTER));
JButton rightAlignButton = new JButton(
new StyledEditorKit.AlignmentAction("Right Align",
StyleConstants.ALIGN_RIGHT));
JButton colorButton = new JButton(new StyledEditorKit.AlignmentAction(
"Right Align", StyleConstants.ALIGN_RIGHT));
leftAlignButton.setIcon(new ImageIcon("left.gif"));
centerButton.setIcon(new ImageIcon("center.gif"));
rightAlignButton.setIcon(new ImageIcon("right.gif"));
colorButton.setIcon(new ImageIcon("color.gif"));
leftAlignButton.setText("Left Align");
centerButton.setText("Center");
rightAlignButton.setText("Right Align");
JPanel alignPanel = new JPanel();
alignPanel.setLayout(new FlowLayout());
alignPanel.add(leftAlignButton);
alignPanel.add(centerButton);
alignPanel.add(rightAlignButton);
document.addUndoableEditListener(undoHandler);
resetUndoManager();
textPane = new JTextPane(document);
textPane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5 * screenSize.width / 8,
5 * screenSize.height / 8);
scrollPane.setPreferredSize(scrollPaneSize);
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER);
toolPanel.add(alignPanel, BorderLayout.SOUTH);
getContentPane().add(menuBar, BorderLayout.NORTH);
//getContentPane().add(toolPanel, BorderLayout.CENTER);
getContentPane().add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
startNewDocument();
show();
}
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (debug) {
int modifier = ae.getModifiers();
long when = ae.getWhen();
String parameter = ae.paramString();
System.out.println("actionCommand: " + actionCommand);
System.out.println("modifier: " + modifier);
System.out.println("when: " + when);
System.out.println("parameter: " + parameter);
}
if (actionCommand.compareTo("New") == 0) {
startNewDocument();
} else if (actionCommand.compareTo("Open") == 0) {
openDocument();
} else if (actionCommand.compareTo("Save") == 0) {
saveDocument();
} else if (actionCommand.compareTo("Save As") == 0) {
saveDocumentAs();
} else if (actionCommand.compareTo("Exit") == 0) {
exit();
} else if (actionCommand.compareTo("Clear") == 0) {
clear();
} else if (actionCommand.compareTo("Select All") == 0) {
selectAll();
} else if (actionCommand.compareTo("Help") == 0) {
help();
} else if (actionCommand.compareTo("Keyboard Shortcuts") == 0) {
showShortcuts();
} else if (actionCommand.compareTo("About QuantumHyperSpace") == 0) {
aboutQuantumHyperSpace();
}
}
protected void resetUndoManager() {
undo.discardAllEdits();
undoAction.update();
redoAction.update();
}
public void startNewDocument() {
Document oldDoc = textPane.getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();
textPane.setDocument(document);
currentFile = null;
setTitle("HTMLDocumentEditor");
textPane.getDocument().addUndoableEditListener(undoHandler);
resetUndoManager();
}
public void openDocument() {
try {
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION) {
currentFile = chooser.getSelectedFile();
setTitle(currentFile.getName());
FileReader fr = new FileReader(currentFile);
Document oldDoc = textPane.getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();
editorKit.read(fr, document, 0);
document.addUndoableEditListener(undoHandler);
textPane.setDocument(document);
resetUndoManager();
}
} catch (BadLocationException ble) {
System.err.println("BadLocationException: " + ble.getMessage());
} catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: " + fnfe.getMessage());
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
public void saveDocument() {
if (currentFile != null) {
try {
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
} catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: "
+ fnfe.getMessage());
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
} else {
saveDocumentAs();
}
}
public void saveDocumentAs() {
try {
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION) {
File newFile = chooser.getSelectedFile();
if (newFile.exists()) {
String message = newFile.getAbsolutePath()
+ " already exists. \n"
+ "Do you want to replace it?";
if (JOptionPane.showConfirmDialog(this, message) == JOptionPane.YES_OPTION) {
currentFile = newFile;
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug)
System.out.println("Saved "
+ currentFile.getAbsolutePath());
}
} else {
currentFile = new File(newFile.getAbsolutePath());
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug)
System.out.println("Saved "
+ currentFile.getAbsolutePath());
}
}
} catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: " + fnfe.getMessage());
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
public void exit() {
String exitMessage = "Are you sure you want to exit?";
if (JOptionPane.showConfirmDialog(this, exitMessage) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
public void clear() {
startNewDocument();
}
public void selectAll() {
textPane.selectAll();
}
public void help() {
JOptionPane.showMessageDialog(this, "DocumentEditor.java\n"
+ "Author: Charles Bell\n" + "Version: May 25, 2002\n"
+ "http://www.quantumhyperspace.com\n"
+ "QuantumHyperSpace Programming Services");
}
public void showShortcuts() {
String shortcuts = "Navigate in | Tab\n" + "Navigate out | Ctrl+Tab\n"
+ "Navigate out backwards | Shift+Ctrl+Tab\n"
+ "Move up/down a line | Up/Down Arrown\n"
+ "Move left/right a component or char | Left/Right Arrow\n"
+ "Move up/down one vertical block | PgUp/PgDn\n"
+ "Move to start/end of line | Home/End\n"
+ "Move to previous/next word | Ctrl+Left/Right Arrow\n"
+ "Move to start/end of data | Ctrl+Home/End\n"
+ "Move left/right one block | Ctrl+PgUp/PgDn\n"
+ "Select All | Ctrl+A\n"
+ "Extend selection up one line | Shift+Up Arrow\n"
+ "Extend selection down one line | Shift+Down Arrow\n"
+ "Extend selection to beginning of line | Shift+Home\n"
+ "Extend selection to end of line | Shift+End\n"
+ "Extend selection to beginning of data | Ctrl+Shift+Home\n"
+ "Extend selection to end of data | Ctrl+Shift+End\n"
+ "Extend selection left | Shift+Right Arrow\n"
+ "Extend selection right | Shift+Right Arrow\n"
+ "Extend selection up one vertical block | Shift+PgUp\n"
+ "Extend selection down one vertical block | Shift+PgDn\n"
+ "Extend selection left one block | Ctrl+Shift+PgUp\n"
+ "Extend selection right one block | Ctrl+Shift+PgDn\n"
+ "Extend selection left one word | Ctrl+Shift+Left Arrow\n"
+ "Extend selection right one word | Ctrl+Shift+Right Arrow\n";
JOptionPane.showMessageDialog(this, shortcuts);
}
public void aboutQuantumHyperSpace() {
JOptionPane.showMessageDialog(this,
"QuantumHyperSpace Programming Services\n"
+ "http://www.quantumhyperspace.com\n"
+ "email: support@quantumhyperspace.com\n" + " or \n"
+ "email: charles@quantumhyperspace.com\n",
"QuantumHyperSpace", JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("quantumhyperspace.gif"));
}
class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent we) {
exit();
}
}
class SubscriptAction extends StyledEditorKit.StyledTextAction {
public SubscriptAction() {
super(StyleConstants.Subscript.toString());
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean subscript = (StyleConstants.isSubscript(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSubscript(sas, subscript);
setCharacterAttributes(editor, sas, false);
}
}
}
class SuperscriptAction extends StyledEditorKit.StyledTextAction {
public SuperscriptAction() {
super(StyleConstants.Superscript.toString());
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean superscript = (StyleConstants.isSuperscript(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSuperscript(sas, superscript);
setCharacterAttributes(editor, sas, false);
}
}
}
class StrikeThroughAction extends StyledEditorKit.StyledTextAction {
public StrikeThroughAction() {
super(StyleConstants.StrikeThrough.toString());
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sas, strikeThrough);
setCharacterAttributes(editor, sas, false);
}
}
}
class HTMLFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File f) {
return ((f.isDirectory()) || (f.getName().toLowerCase().indexOf(
".htm") > 0));
}
public String getDescription() {
return "html";
}
}
class UndoHandler implements UndoableEditListener {
/**
* Messaged when the Document has created an edit, the edit is added to
* <code>undo</code>, an application of UndoManager.
*/
public void undoableEditHappened(UndoableEditEvent e) {
undo.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.undo();
} catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update() {
if (undo.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction {
public RedoAction() {
super("Redo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.redo();
} catch (CannotRedoException ex) {
System.err.println("Unable to redo: " + ex);
ex.printStackTrace();
}
update();
undoAction.update();
}
protected void update() {
if (undo.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -