📄 pageeditor.java
字号:
package g2w.app.gchm.gui;
import g2w.app.gchm.util.HTMUtility;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.DocumentListener;
/**
* PageEditor is a component to edit html source code.
*
* @author YangLe
* @version 021 2009-3-21 9:27:32
*/
public class PageEditor extends JPanel {
/** The current file. */
private File file;
/** A text field to show the file path. */
private JTextField filePath;
/** A text area to show the source of the html page. */
private JTextArea textarea;
/** The textarea's document listener. */
private DocumentListener listener;
/**
* Construct a page editor with the default setup.
*/
public PageEditor() {
super();
initComponents();
}
/**
* Initialize all components.
*/
private void initComponents() {
// Configure the component.
setLayout(new BorderLayout());
// Add the toolbar.
JToolBar toolbar = new JToolBar();
toolbar.setFloatable(false);
filePath = new JTextField();
filePath.setEditable(false);
toolbar.add(filePath);
add(toolbar, BorderLayout.NORTH);
textarea = new JTextArea();
textarea.setLineWrap(true);
textarea.setEditable(false);
add(new JScrollPane(textarea), BorderLayout.CENTER);
}
/**
* Load a file to the editor.
*
* @param file The text file to load.
*/
public void load(File file) {
this.file = file;
if (file == null) {
setEditable(false);
filePath.setText("");
textarea.setText("");
} else {
setEditable(true);
filePath.setText(file.getAbsolutePath());
String html = HTMUtility.getHTMLContent(file.getAbsolutePath());
textarea.setText(html);
}
}
/**
* Save the document.
*
* @return If done, return true.
* @throws IOException
*/
public void save() throws IOException {
if (this.file == null) return;
FileWriter writer = new FileWriter(this.file);
writer.write(textarea.getText());
writer.close();
}
/**
* Set the textarea's document listener.
*
* @param listener The document listener.s
*/
public void setDocumentListener(DocumentListener listener) {
textarea.getDocument().removeDocumentListener(this.listener);
this.listener = listener;
textarea.getDocument().addDocumentListener(listener);
}
/**
* Get the file's path.
*
* @return The file's path.
*/
public String getFilePath() {
return (file == null) ? "" : file.getAbsolutePath();
}
/**
* Enable or disable the textarea with a better style.
*
* @param aFlag
*/
public void setEditable(boolean aFlag) {
textarea.setEditable(aFlag);
textarea.setBackground(aFlag ? Color.WHITE : new Color(227, 227, 227));
}
/**
* Add the document listener.
*
* @param listener The given listener.
*/
public void addDocumentListener(DocumentListener listener) {
textarea.getDocument().addDocumentListener(listener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -