📄 console.java
字号:
package xml;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class Console extends JFrame implements ActionListener
{
private JTextField textField;
private JButton b_open;
private JButton b_edit;
private JButton b_insert;
private JButton b_delete;
private JButton b_save;
private String xmlFilePath = null;
private Document doc = null;
private JTree tree = null;
private DomDefaultMutableTreeNode selectedNode = null;
public Console()
{
super("元数据编辑系统");
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-600)/2, (screenSize.height-500)/2, 600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final FlowLayout flowLayout_1 = new FlowLayout();
flowLayout_1.setHgap(20);
panel.setLayout(flowLayout_1);
getContentPane().add(panel, BorderLayout.NORTH);
textField = new JTextField();
textField.setPreferredSize(new Dimension(400, 25));
panel.add(textField);
b_open = new JButton();
b_open.setPreferredSize(new Dimension(100, 25));
b_open.setText("打开 (O)");
b_open.setMnemonic(KeyEvent.VK_O);
b_open.addActionListener(this);
panel.add(b_open);
final JPanel panel_1 = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setHgap(20);
panel_1.setLayout(flowLayout);
panel_1.setPreferredSize(new Dimension(0, 40));
getContentPane().add(panel_1, BorderLayout.SOUTH);
b_edit = new JButton();
b_edit.setPreferredSize(new Dimension(100, 25));
b_edit.setText("编辑 (E)");
b_edit.setMnemonic(KeyEvent.VK_E);
b_edit.addActionListener(this);
panel_1.add(b_edit);
b_insert = new JButton();
b_insert.setPreferredSize(new Dimension(100, 25));
b_insert.setText("插入 (I)");
b_insert.setMnemonic(KeyEvent.VK_I);
b_insert.addActionListener(this);
panel_1.add(b_insert);
b_delete = new JButton();
b_delete.setPreferredSize(new Dimension(100, 25));
b_delete.setText("删除 (D)");
b_delete.setMnemonic(KeyEvent.VK_D);
b_delete.addActionListener(this);
panel_1.add(b_delete);
b_save = new JButton();
b_save.setPreferredSize(new Dimension(100, 25));
b_save.setText("保存 (S)");
b_save.setMnemonic(KeyEvent.VK_S);
b_save.addActionListener(this);
panel_1.add(b_save);
tree = new JTree(new DefaultTreeModel(null));
tree.addMouseListener(ma);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
//
}
private void init()
{
doc = getDocument(xmlFilePath);
tree.setModel(new DomTreeModelFactory(doc).getDomTreeModel());
}
private Document getDocument(String xmlFilePath)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
try {
doc = db.parse(new File(xmlFilePath));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b_open)
{
FileDialog fileDialog = new FileDialog(this, "打开XML文件...", FileDialog.LOAD);
fileDialog.setVisible(true);
xmlFilePath = fileDialog.getDirectory() + fileDialog.getFile();
textField.setText(xmlFilePath);
init();
}
else if(e.getSource() == b_edit)
{
if(selectedNode == null)
JOptionPane.showMessageDialog(null, "您未选择节点!", "警告", JOptionPane.WARNING_MESSAGE);
else if(((Node)selectedNode.getUserObject()).getNodeType() == 3)
new EditNodeValue(this, selectedNode, tree).setVisible(true);
}
else if(e.getSource() == b_insert)
{
if(selectedNode == null)
JOptionPane.showMessageDialog(null, "您未选择节点!", "警告", JOptionPane.WARNING_MESSAGE);
else if(((Node)selectedNode.getUserObject()).getNodeType() != 3)
new InsertTextNode(this, selectedNode, tree, doc).setVisible(true);
}
else if(e.getSource() == b_delete)
{
if(selectedNode == null)
JOptionPane.showMessageDialog(null, "您未选择节点!", "警告", JOptionPane.WARNING_MESSAGE);
else
{
Node node = (Node)selectedNode.getUserObject();
if(node.getNodeType() == 3)
{
((DomDefaultMutableTreeNode)(selectedNode.getParent())).remove(selectedNode);
node.getParentNode().removeChild(node);
tree.updateUI(); //刷新树
}
else
JOptionPane.showMessageDialog(null, "您无法删除该节点!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getSource() == b_save)
{
try {
((XmlDocument)doc).write(new FileOutputStream(new File(xmlFilePath)));
JOptionPane.showMessageDialog(null, "保存成功!", "提示", JOptionPane.WARNING_MESSAGE);
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
private MouseAdapter ma = new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JTree tree = (JTree) e.getSource();
int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
if (rowLocation > 0)
{
TreePath treepath = tree.getPathForRow(rowLocation);
selectedNode = (DomDefaultMutableTreeNode) treepath.getLastPathComponent();
}
}
};
public static void main(String args[])
{
try {
Console frame = new Console();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -