📄 contenteditor.java
字号:
package g2w.app.gchm.gui;
import g2w.app.gchm.GChm;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DropMode;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.jdom.Document;
/**
* ContentEditor is the editor to manage the contents of the CHM project.
* It goes a simple way to help you to manage the contents, but still functional.
* As the famouse pdf reader Foxit Reader dose, content editor support two ways
* to manage your contents.
* <ul>
* <li><b>Add Topic: </b>Add a new topic by a click.</li>
* <li><b>Remove Topic: </b>Remove a topic by a click.
* (If the topic has children, the function will go after approved.)</li>
* <li><b>Drag and Drop</b>ContentEditor support organizing the contents through a Drag-And-Drop way.
* Multi-slection DnD is not supported not yet in this version.
*
* @author GreatGhoul
* @version 018 2009-3-20 20:08:01
*/
public class ContentEditor extends JPanel
implements ActionListener, TreeModelListener{
/** The owner editor : Project Editor */
private ProjectEditor owner;
/** The content tree */
private ContentTree contentTree;
/**
* Construct a content editor with a given owner.
*
* @param owner The owner.
*/
public ContentEditor(ProjectEditor owner) {
super();
this.owner = owner;
initComponents();
}
/**
* Initialize all components.
*/
private void initComponents() {
setLayout(new BorderLayout());
// Add the toolbar.
add(ToolBarProvider.getContentEditorToolBar(), BorderLayout.NORTH);
// Initialize the tree model.
Document document = owner.getProject().getDocument();
ContentTreeNode root = ContentTreeNode.createRoot(document);
ContentTreeModel model = new ContentTreeModel(root);
// Initialize the tree.
contentTree = new ContentTree(model);
contentTree.setRootVisible(true);
contentTree.setShowsRootHandles(false);
contentTree.setDragEnabled(true);
contentTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
contentTree.setDropMode(DropMode.ON_OR_INSERT);
contentTree.setTransferHandler(new TreeTransferHandler());
contentTree.setCellRenderer(new DefaultTreeCellRenderer() {
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
try {
super.getTreeCellRendererComponent(tree, value, sel,
expanded, leaf, row, hasFocus);
// Configures icons for the node.
if(value == null) return this;
ContentTreeNode node = (ContentTreeNode) value;
if(node.isRoot()) {
setIcon(GChm.getImage("/resources/chm/chm.png"));
} else if (node.isLeaf()) {
setIcon(GChm.getImage("/resources/chm/topic_general.png"));
} else {
setClosedIcon(GChm.getImage("/resources/chm/heading_closed.png"));
setOpenIcon(GChm.getImage("/resources/chm/heading_open.png"));
}
} catch(Exception e) {
e.printStackTrace();
}
return this;
}
});
contentTree.updateModel(root);
contentTree.expandRow(0);
model.addTreeModelListener(this);
add(new JScrollPane(contentTree), BorderLayout.CENTER);
validate();
repaint();
}
/**
* Append the new node after selection.
* If the selection is empty, append it to the root.
*
* @param node The node to append.
*/
public void appendAfterSelection(ContentTreeNode node) {
ContentTreeModel model = (ContentTreeModel) contentTree.getModel();
// Get and fix the selected node.
ContentTreeNode selNode = contentTree.getSelectionNode();
if (selNode == null) {
selNode = (ContentTreeNode) model.getRoot();
}
// Configure the parent node.
ContentTreeNode parentNode;
if (selNode.isRoot())
parentNode = selNode;
else
parentNode = (ContentTreeNode) selNode.getParent();
// Configrue the index to insert to.
int index;
if (selNode.isRoot()) {
index = selNode.getChildCount();
} else {
index = model.getIndexOfChild(selNode.getParent(), selNode) + 1;
}
model.insertNodeInto(node, parentNode, index);
}
/**
* Select the indicated node.
*
* @param node The indicated node.
*/
public void setSelection(ContentTreeNode node) {
contentTree.setSelection(node);
}
/**
* Select the root node.
*/
public void selectRoot() {
ContentTreeModel model = (ContentTreeModel) contentTree.getModel();
ContentTreeNode root = (ContentTreeNode) model.getRoot();
setSelection(root);
}
/**
* Remove the selected node.
*
* @return The neighbor node of the selection.
*/
public ContentTreeNode removeSelection() {
ContentTreeModel model = (ContentTreeModel) contentTree.getModel();
ContentTreeNode node = contentTree.getSelectionNode();
ContentTreeNode neighbor = model.getNeighbor(node);
model.removeNodeFromParent(node);
return neighbor;
}
/**
* Update the value at the given path.
*
* @param treePath The given path.
* @param topic The new value.
*/
public void updateValueForPath(TreePath treePath, ContentElement topic) {
ContentTreeModel model = (ContentTreeModel) contentTree.getModel();
model.valueForPathChanged(treePath, topic);
}
/**
* Add the selection listener of the content tree.
*
* @param listener The given listener to add
*/
public void addContentTreeSelectionListener(TreeSelectionListener listener) {
contentTree.addTreeSelectionListener(listener);
}
/**
* Remove the selection listener from the content tree.
*
* @param listener The given listener to remove.
*/
public void removeContentTreeSelectionListener(TreeSelectionListener listener) {
contentTree.removeTreeSelectionListener(listener);
}
/**
* This method will be invoked when the submit button in the
* topic editor is clicked.
*/
@Override
public void actionPerformed(ActionEvent evt) {
// Get the topic editor.
TopicEditor topicEditor = owner.getTopicEditor();
// Get the page editor.
PageEditor pageEditor = topicEditor.getPageEditor();
// Get the submit button.
// Modify the topic's attributes.
try {
topicEditor.submit();
owner.getConsole().hey("文件" + pageEditor.getFilePath() + "保存成功!");
} catch (IOException e) {
owner.getConsole().hey("文件" + pageEditor.getFilePath() + "保存失败!");
owner.showConsole();
}
// Update the value for the given path.
TreePath path = topicEditor.getTreePath();
ContentElement topic = topicEditor.getTopic();
ContentTreeModel model = (ContentTreeModel) contentTree.getModel();
model.valueForPathChanged(path, topic);
}
@Override
public void treeNodesChanged(TreeModelEvent evt) {
owner.getProject().setChanged(true);
GChm.getAction("SaveProject").setEnabled(true);
}
@Override
public void treeNodesInserted(TreeModelEvent evt) {
owner.getProject().setChanged(true);
GChm.getAction("SaveProject").setEnabled(true);
}
@Override
public void treeNodesRemoved(TreeModelEvent evt) {
owner.getProject().setChanged(true);
GChm.getAction("SaveProject").setEnabled(true);
}
@Override
public void treeStructureChanged(TreeModelEvent evt) {
owner.getProject().setChanged(true);
GChm.getAction("SaveProject").setEnabled(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -