/*���ܼ�飄1�7
*������棬����ʵ���˱�����ʷ��¼���ܡᅣ1�7
*/
package realcix20.guis.views;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.guis.views.help.tools.ContentsTreeCellRenderer;
import realcix20.guis.views.help.tools.HelpManager;
import realcix20.utils.DAO;
import realcix20.utils.Resources;
public class HelpView extends JFrame implements TreeSelectionListener, HyperlinkListener, ActionListener {
private JButton previousButton;
private JButton forwardButton;
private JButton printButton;
private JTree contentsTree;
private JTextField searchField;
private JList resultsList;
private JEditorPane textPane;
private Vector tracks;
private Node currentPointNode;
private int currentPoint;
private boolean isPAndFButtonClicked;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Cancel")) {
dispose();
}
else {
updatePoint(e.getActionCommand());
}
}
public void valueChanged(TreeSelectionEvent e) {
if (e.getSource().equals(contentsTree)) {
updateTextPane();
if (!isPAndFButtonClicked) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)contentsTree.getLastSelectedPathComponent();
Node node = (Node)treeNode.getUserObject();
removeNodesBehindIndex(currentPoint);
tracks.add(node);
currentPointNode = node;
currentPoint++;
}
updatePreviousAndForwardButtons();
}
}
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
// System.err.println(ev.getURL());
String hlink = HelpManager.getHTML(ev.getURL().getFile());
updateContentsTree(hlink);
textPane.setPage(ev.getURL());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public HelpView() {
super();
String n1 = "ROOT";
isPAndFButtonClicked = false;
createShortcutKeySets();
initComponents();
tracks = new Vector();
Node start = new Node(n1);
currentPoint = -1;
if (!n1.equals("ROOT")) {
Node root = new Node("ROOT");
tracks.add(root);
currentPoint++;
}
currentPointNode = start;
setPage(HelpManager.getPageURL(start.hlink));
updateContentsTree(start.hlink);
}
public HelpView(int clsId) {
super();
isPAndFButtonClicked = false;
createShortcutKeySets();
initComponents();
tracks = new Vector();
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_FROM_ZN_BY_CLSID_SQL);
dao.setInt(1, clsId);
String n1 = null;
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
n1 = rs.getString("N");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
if ( (n1 == null) || (clsId == 0) ) {
n1 = "ROOT";
}
Node start = new Node(n1);
if (!n1.equals("ROOT")) {
Node root = new Node("ROOT");
tracks.add(root);
currentPoint++;
}
currentPointNode = start;
setPage(HelpManager.getPageURL(start.hlink));
updateContentsTree(start.hlink);
}
private void createShortcutKeySets() {
Map commandKeyStrokeSets = new HashMap();
commandKeyStrokeSets.put("ESCAPE", "Cancel");
JMenuBar menuBar = MnemonicGenerator.initShortcutKeySets(this, commandKeyStrokeSets);
setJMenuBar(menuBar);
}
private void doSearch(String searchWord) {
for (int i = 1; i < 10000; i++) {
//
}
}
private void removeNodesBehindIndex(int index) {
int needDeleteNodes = tracks.size() - index - 1;
for (int i = 1; i <= needDeleteNodes; i++) {
tracks.removeElementAt(index + 1);
}
}
private void updatePoint(String action) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
if (action.equals("Previous")) {
currentPoint--;
} else if (action.equals("Forward")) {
currentPoint++;
}
currentPointNode = (Node)tracks.get(currentPoint);
isPAndFButtonClicked = true;
updateContentsTree(currentPointNode.hlink);
isPAndFButtonClicked = false;
}
private void updatePreviousAndForwardButtons() {
if (currentPoint == 0)
previousButton.setEnabled(false);
else
previousButton.setEnabled(true);
if (currentPoint == tracks.size() - 1)
forwardButton.setEnabled(false);
else if (tracks.size() != 1)
forwardButton.setEnabled(true);
}
private void updateContentsTree(String hlink) {
DefaultTreeModel model = (DefaultTreeModel)contentsTree.getModel();
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)model.getRoot();
TreePath path = findPathByHLink(contentsTree.getPathForRow(0), hlink);
contentsTree.setSelectionPath(path);
}
private TreePath findPathByHLink(TreePath treePath, String hlink) {
TreePath specifyTreePath = null;
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)treePath.getLastPathComponent();
Node node = (Node)treeNode.getUserObject();
if (node.hlink.equals(hlink))
specifyTreePath = treePath;
if (specifyTreePath == null) {
boolean expanded = contentsTree.isExpanded(treePath);
if (!expanded)
contentsTree.expandPath(treePath);
int i = contentsTree.getRowForPath(treePath);
int j = 1;
int jj = 1;
while (j <= treeNode.getChildCount()) {
TreePath childTreePath = contentsTree.getPathForRow(i + jj);
DefaultMutableTreeNode childTreeNode = (DefaultMutableTreeNode)childTreePath.getLastPathComponent();
node = (Node)childTreeNode.getUserObject();
if (node.hlink.equals(hlink))
specifyTreePath = childTreePath;
if (specifyTreePath != null)
break;
else {
if (childTreeNode.getChildCount() > 0) {
specifyTreePath = findPathByHLink(childTreePath, hlink);
}
}
if (contentsTree.isExpanded(childTreePath))
jj = jj + childTreeNode.getChildCount() + 1;
else
jj++;
j++;
}
if ((!expanded) && (specifyTreePath == null))
contentsTree.collapsePath(treePath);
}
return specifyTreePath;
}
private void updateTextPane() {
if (contentsTree.getLastSelectedPathComponent() != null) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)contentsTree.getLastSelectedPathComponent();
Node node = (Node)treeNode.getUserObject();
try {
setPage(HelpManager.getPageURL(node.hlink));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void setPage(String URL) {
try {
textPane.setPage(URL);
} catch (Exception e) {
e.printStackTrace();
}
}
private Vector getAllChilds(Node parent) {
Vector childs = new Vector();
DAO dao = DAO.getInstance();
dao.query(Resources.GET_ALL_CHILDS_SQL);
String n1 = parent.n;
dao.setString(1, n1);
ResultSet rs = dao.executeQuery();
try {
while (rs.next()) {
String n2 = rs.getString("N2");
Node child = new Node(n2);
childs.add(child);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return childs;
}
private void addAllChildNodes(DefaultMutableTreeNode parentNode, Vector childs) {
Iterator childsIter = childs.iterator();
while (childsIter.hasNext()) {
Node child = (Node)childsIter.next();
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
if (getAllChilds(child).size() > 0) {
addAllChildNodes(childNode, getAllChilds(child));
}
parentNode.add(childNode);
}
}
private void initContentsTree() {
Node root = new Node("ROOT");
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(root);
Vector rootChilds = getAllChilds(root);
addAllChildNodes(rootNode, rootChilds);
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
contentsTree.setModel(treeModel);
contentsTree.setShowsRootHandles(true);
contentsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
contentsTree.setCellRenderer(new ContentsTreeCellRenderer());
contentsTree.addTreeSelectionListener(this);
}
private void initComponents() {
//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};
JToolBar toolBar = new JToolBar();
//======== toolBar ========
{
toolBar.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//---- previousButton ----
previousButton = new JButton();
previousButton.setIcon(ImageManager.getImage(ImageManager.PREVIOUS_IMAGE));
previousButton.setActionCommand("Previous");
previousButton.addActionListener(this);
toolBar.add(previousButton);
//---- forwardButton ----
forwardButton = new JButton();
forwardButton.setIcon(ImageManager.getImage(ImageManager.FORWARD_IMAGE));
forwardButton.setActionCommand("Forward");
forwardButton.addActionListener(this);
toolBar.add(forwardButton);
toolBar.addSeparator();
}
contentPane.add(toolBar, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
JSplitPane splitPane = new JSplitPane();
//======== splitPane ========
{
splitPane.setDividerLocation(200);
JTabbedPane tabbedPane = new JTabbedPane();
//======== tabbedPane ========
{
tabbedPane.setPreferredSize(new Dimension(150, 500));
JPanel contentsPanel = new JPanel();
//======== contentsPanel ========
{
contentsPanel.setLayout(new GridBagLayout());
((GridBagLayout)contentsPanel.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)contentsPanel.getLayout()).rowHeights = new int[] {0, 0};
((GridBagLayout)contentsPanel.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)contentsPanel.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};
JScrollPane scrollPane = new JScrollPane();
//======== scrollPane ========
{
contentsTree = new JTree();
initContentsTree();
scrollPane.setViewportView(contentsTree);
}
contentsPanel.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
tabbedPane.addTab(TxtManager.getTxt("VIEW.HELPFRAME.CONTENTS"), contentsPanel);
JPanel searchPanel = new JPanel();
//======== searchPanel ========
{
searchPanel.setLayout(new GridBagLayout());
((GridBagLayout)searchPanel.getLayout()).columnWidths = new int[] {0, 0, 0};
((GridBagLayout)searchPanel.getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)searchPanel.getLayout()).columnWeights = new double[] {0.0, 1.0, 1.0E-4};
((GridBagLayout)searchPanel.getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};
//---- searchLabel ----
JLabel searchLabel = new JLabel();
searchLabel.setText(TxtManager.getTxt("VIEW.HELPFRAME.SEARCH.SEARCHLABEL"));
searchPanel.add(searchLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 5, 5, 5), 0, 0));
//---- searchField ----
searchField = new JTextField();
searchPanel.add(searchField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 0, 5, 0), 0, 0));
JScrollPane scrollPane = new JScrollPane();
//======== scrollPane ========
{
resultsList = new JList();
scrollPane.setViewportView(resultsList);
}
searchPanel.add(scrollPane, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
//tabbedPane.addTab(TxtManager.getTxt("VIEW.HELPFRAME.SEARCH"), searchPanel);
}
splitPane.setLeftComponent(tabbedPane);
JScrollPane scrollPane = new JScrollPane();
//======== scrollPane ========
{
scrollPane.setPreferredSize(new Dimension(400, 500));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textPane = new JEditorPane();
textPane.setEditable(false);
scrollPane.setViewportView(textPane);
textPane.addHyperlinkListener(this);
}
splitPane.setRightComponent(scrollPane);
}
contentPane.add(splitPane, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 10, 10, 10), 0, 0));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setIconImage(ImageManager.getImage(ImageManager.HELP_DOCUMENT_IMAGE).getImage());
setTitle(TxtManager.getTxt("VIEW.HELPFRAME.TITLE"));
setVisible(true);
setSize(getMaximumSize());
setLocationRelativeTo(null);
}
class Node {
public String n;
public String txt;
public String hlink;
public int clsId;
public Node(String n) {
this.n = n;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_FROM_ZNL_BY_N_SQL);
dao.setString(1, n);
dao.setString(2, Resources.LANGUAGE);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
txt = rs.getString("TXT");
hlink = rs.getString("HLINK");
clsId =rs.getInt("CLSID");
} else {
txt = n;
hlink = "blank.html";
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String toString() {
return txt;
}
}
}