📄 topiceditor.java
字号:
package g2w.app.gchm.gui;
import g2w.app.gchm.GChm;
import g2w.app.gchm.lib.GLightBox;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreePath;
/**
* TopicEditor is a mini editor to edit the topic
* with a simple html source editor embeded in.
* <i>The html source editor not yet supports all general encodings, <br/>
* but we will try to do more.</i>
*
* @author Yangle
* @version 034 2009-3-22 8:47:52
*/
public class TopicEditor extends JPanel implements DocumentListener {
/** The owner. */
private ProjectEditor owner;
/** A light box to show the page editor. */
private GLightBox pageEditorView;
/** The page editor. */
private PageEditor pageEditor;
/** The current treepath */
private TreePath treePath;
/** The current topic. */
private ContentElement topic;
/** Used to display the topic's name. */
private JTextField topicName;
/** Used to indicated whether to link a document to the topic or not. */
private JToggleButton linkingMode;
/** A file chooser to chose the html document file. */
private JFileChooser chooser;
/** A button used to submit the changes. */
private JButton submit;
/** A flag to show if the content has changed. */
private boolean contentChanged;
public TopicEditor(ProjectEditor owner) {
super();
this.owner = owner;
initComponents();
}
/**
* Initialize all compoments.
*/
private void initComponents() {
setLayout(new BorderLayout());
// Configure the file chooser.
chooser = new JFileChooser();
chooser.setDialogTitle("选择与标题关联的文档");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("HTML文档(*.htm, *.html)", "htm", "html"));
chooser.setAcceptAllFileFilterUsed(false);
chooser.setMultiSelectionEnabled(false);
add(createTopicWrap(), "North");
add(createPageWrap(), "Center");
topicName.getDocument().addDocumentListener(this);
pageEditor.addDocumentListener(this);
}
private Component createPageWrap() {
pageEditorView = new GLightBox("编辑关联文档", false);
pageEditor = new PageEditor();
pageEditor.load(null);
pageEditorView.setView(pageEditor);
return pageEditorView;
}
private JToolBar createTopicWrap() {
JToolBar topicWrap = new JToolBar();
topicWrap.setFloatable(false);
topicWrap.add(new JLabel("标题:"));
topicWrap.add(Box.createHorizontalStrut(2));
topicName = new JTextField(20);
topicWrap.add(topicName);
topicWrap.add(Box.createHorizontalStrut(2));
linkingMode = new JToggleButton(new AbstractAction("关联文档") {
@Override
public void actionPerformed(ActionEvent evt) {
toggleLinkedDocument();
}
});
topicWrap.add(linkingMode);
topicWrap.add(Box.createHorizontalGlue());
submit = new JButton("确定");
submit.setIcon(GChm.getImage("/resources/submit.png"));
submit.setToolTipText("保存文档并更新标题");
submit.setEnabled(false);
topicWrap.add(submit);
return topicWrap;
}
/**
* Toogle whether to link a document or not.
*/
private void toggleLinkedDocument() {
if (linkingMode.isSelected()) {
int state = chooser.showOpenDialog(this);
File file = chooser.getSelectedFile();
if (file != null && state == JFileChooser.APPROVE_OPTION) {
if (owner.isFileInProject(file)) {
pageEditor.load(file);
contentChanged = true;
} else {
JOptionPane.showMessageDialog(this, "请先将文件添加到工程目录下!");
linkingMode.setSelected(false);
}
} else {
linkingMode.setSelected(false);
}
} else {
System.out.println("Break linking.");
if (JOptionPane.showConfirmDialog(this, "确定取消文档关联?")
== JOptionPane.OK_OPTION) {
pageEditor.load(null);
contentChanged = true;
} else {
linkingMode.setSelected(true);
}
}
linkingMode.setText(linkingMode.isSelected()
? "取消关联 " : "关联文档");
linkingMode.setToolTipText(linkingMode.isSelected()
? "取消文档关联" : "设置标题关联的文档");
updateSubmitStatus();
}
/**
* Set a topic to edit.
*
* @param topic The topic to edit.
*/
public void setTopic(ContentTreeNode node) {
if (contentChanged) {
if (JOptionPane.showConfirmDialog(this, "是否保存更改?")
== JOptionPane.OK_OPTION) {
try {
submit();
owner.getConsole().hey("文件" + pageEditor.getFilePath() + "保存成功!");
} catch (IOException e) {
owner.getConsole().hey("文件" + pageEditor.getFilePath() + "保存失败!");
owner.showConsole();
}
}
}
topic = node.getUserObject();
treePath = new TreePath(node.getPath());
final File htmlFile = new File(topic.getLocal());
topicName.setText(topic.getName());
boolean fileExist = htmlFile.exists();
linkingMode.setSelected(fileExist);
linkingMode.setText(fileExist ? "取消关联 " : "关联文档");
linkingMode.setToolTipText(fileExist ? "取消文档关联" : "设置标题关联的文档");
pageEditor.load(htmlFile.exists() ? htmlFile : null);
contentChanged = false;
updateSubmitStatus();
}
/**
* The following methods are used to listen changes
* of the topic name field and page content field.
*/
@Override
public void changedUpdate(DocumentEvent evt) {
contentChanged = true;
updateSubmitStatus();
}
@Override
public void insertUpdate(DocumentEvent evt) {
contentChanged = true;
updateSubmitStatus();
}
@Override
public void removeUpdate(DocumentEvent evt) {
contentChanged = true;
updateSubmitStatus();
}
public void updateSubmitStatus() {
submit.setEnabled(contentChanged);
}
/**
* Submit changes of the topic.
* @throws IOException
*/
public void submit() throws IOException {
// Update the topic's attributes.
topic.setName(topicName.getText());
pageEditor.save();
topic.setLocal(pageEditor.getFilePath());
contentChanged = false;
updateSubmitStatus();
}
/**
* Add a action listener for the submit button.
*
* @param listener The listener to add.
*/
public void addSubmitListener(ActionListener listener) {
submit.addActionListener(listener);
}
/**
* Remove the given action listener from the submit button.
*
* @param listener The listener to Remove.
*/
public void removeSubmitListener(ActionListener listener) {
submit.removeActionListener(listener);
}
public TreePath getTreePath() {
return treePath;
}
public ContentElement getTopic() {
return topic;
}
/**
* Get the page editor.
*
* @return The page editor.
*/
public PageEditor getPageEditor() {
return pageEditor;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -