📄 helpdialog.java
字号:
// will try to expand each node as we go along to be sure we hit all
// possible nodes.
for (int i=0; i<tocTree.getRowCount(); i++) {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode)tocTree.
getPathForRow(i).getLastPathComponent();
tocTree.expandRow(i);
HelpTreeNode htn = (HelpTreeNode)temp.getUserObject();
if (htn.url!=null && htn.url.equals(url)) {
tocTree.setSelectionRow(i);
tocTree.scrollRowToVisible(i);
return;
}
}
// If the page wasn't found, tell the user so.
ResourceBundle msg = getHelpBundle();
JOptionPane.showMessageDialog(this,
msg.getString("PageNotFound"),
msg.getString("Error"),
JOptionPane.ERROR_MESSAGE);
msg = null; // May help GC.
}
/*****************************************************************************/
/**
* Sets the label on the "Index" tab.
*
* @param text The text for the "Index" tab.
* @see #getIndexTabText
*/
public void setIndexTabText(String text) {
tabbedPane.setTitleAt(1, text);
}
/*****************************************************************************/
/**
* Sets the text label above the "Type in a keyword to find" text boxes.
*
* @param text The text label.
* @see #getKeywordFieldLabelText
*/
public void setKeywordFieldLabelText(String text) {
keywordToFindLabel.setText(text);
keywordToFindLabel2.setText(text);
}
/*****************************************************************************/
/**
* Sets the label on the "List Topics" button on the Search panels.
*
* @param text The text for the "List Topics" button.
* @see #getListTopicsButtonText
*/
public void setListTopicsButtonText(String text) {
listTopicsButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the label on the "Search" tab.
*
* @param text The text for the "Search" tab.
* @see #getSearchTabText
*/
public void setSearchTabText(String text) {
tabbedPane.setTitleAt(2, text);
}
/*****************************************************************************/
/**
* Sets the text label above the "Select topic to display" text box.
*
* @param text The text label.
* @see #getTopicToDisplayFieldLabelText
*/
public void setTopicToDisplayFieldLabelText(String text) {
topicToDisplayLabel.setText(text);
}
/*****************************************************************************/
/**
* Helper function to validate that a given node is indeed an Attribute
* node with the specified name.
*
* @param node The node to check.
* @param name The name the node should have.
* @return Whether the specified node is an Attribute node with the
* specified name.
*/
private final boolean validateAttributeNode(Node node, String name) {
return (node!=null && node.getNodeType()==Node.ATTRIBUTE_NODE &&
node.getNodeName().equals(name));
}
/*****************************************************************************/
/****************** INNER CLASSES ********************************************/
/*****************************************************************************/
/**
* Listens for events in the help dialog.
*/
protected class HelpListener extends MouseAdapter
implements ChangeListener,
HyperlinkListener, DocumentListener,
KeyListener, TreeSelectionListener {
public void changedUpdate(DocumentEvent e) {
}
/**
* Called whenever the user clicks on a hyperlink in editorPane.
*/
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType eventType = e.getEventType();
if (eventType.equals(HyperlinkEvent.EventType.ACTIVATED)) {
// Yes, you want this to update the memory of pages visited.
updateHistory = true;
highlightSearchString = false;
// This is a hack so that, for their href, they can specify a "relative path"
// to any HTML help, which especially comes in handy in jar files.
URL url = e.getURL();
if (url==null) {
try {
url = new URL("file://" + baseURL.getPath() + e.getDescription());
} catch (MalformedURLException mue) {
guiApp.displayException(HelpDialog.this, mue);
}
}
String anchor = url.getRef(); // null if no reference.
if (anchor!=null) {
try {
String protocol = url.getProtocol();
if (protocol==null || protocol.equals(""))
protocol = "file://";
String urlString = "file://" + url.getPath();
url = new URL(urlString); // No anchor.
} catch (MalformedURLException mue) {
mue.printStackTrace();
}
}
setHelpPageURL(url);
if (anchor!=null)
editorPane.scrollToReference(anchor);
}
else if (eventType.equals(HyperlinkEvent.EventType.ENTERED)) {
editorPane.setToolTipText(e.getDescription());
}
else if (eventType.equals(HyperlinkEvent.EventType.EXITED)) {
editorPane.setToolTipText(null);
}
}
/**
* Called whenever indexField or searchField's contents are
* inserted into.
*/
public void insertUpdate(DocumentEvent e) {
Document doc = e.getDocument();
// If it was the index text field that changed...
if ( doc.equals(indexField.getDocument()) ) {
// Find closest match to entered text in index list and highlight it.
int closestMatch = indexList.getNextMatch(
indexField.getText(), 0, Position.Bias.Forward);
if (closestMatch != -1) {
indexList.setSelectedIndex(closestMatch);
indexList.ensureIndexIsVisible(indexList.getSelectedIndex());
}
}
// If it was the search text field that changed...
else if ( doc.equals(searchField.getDocument()) ) {
// Ensure that the "List Topics" button is enabled.
listTopicsButton.setEnabled(true);
}
}
/**
* Called whenever the user presses a key in indexField or searchField.
*/
public void keyPressed(KeyEvent e) {
// The only keypress we're interested in is the "Enter" key.
if ( e.getKeyCode() == KeyEvent.VK_ENTER ) {
Object source = e.getSource();
// If they pressed Enter while in the index tab, load the
// help page currently selected.
if ( source.equals(indexField) || source.equals(indexList) ) {
if (indexList.getSelectedIndex()!=-1)
loadSelectedHelpPageIndex();
}
// If they press Enter while in searchField, display a list
// of Help topics.
else if (source.equals( searchField )) {
searchString = searchField.getText(); // Remember the searched-for text.
if (!searchString.equals(""))
populateSearchList();
}
// If they press Enter while in searchList, display the
// help topic they choose.
else if (source.equals( searchList )) {
if (indexList.getSelectedIndex()!=-1)
loadSelectedHelpPageSearch();
}
// If they press enter while on an expandable node in the
// tocTree, toggle it's expandedness.
else if (source.equals( tocTree )) {
int row = tocTree.getMaxSelectionRow();
// These are okay since tree is single-selection.
if (tocTree.isExpanded(row)==true)
tocTree.collapseRow(row);
else
tocTree.expandRow(row);
}
} // End of if ( e.getKeyCode() == KeyEvent.VK_ENTER ).
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
/**
* We're listening for when the user clicks in one of our components.
*/
public void mouseClicked(MouseEvent e) {
// They must double-click for anything to happen.
if (e.getClickCount()==2) {
// If they double-click in the index panel's JList,
// load the clicked-on help page.
if (e.getComponent().equals(indexList))
loadSelectedHelpPageIndex();
// If they double-click in the search panel's JList,
// load the clicked-on help page.
else if (e.getComponent().equals(searchList))
loadSelectedHelpPageSearch();
} // End of if (e.getClickCount()==2).
}
/**
* Called whenever indexField or searchField's contents are shortened.
*/
public void removeUpdate(DocumentEvent e) {
Document doc = e.getDocument();
// If it was the index text field that changed...
if ( doc.equals(indexField.getDocument()) ) {
// Find closest match to entered text in index list and
// highlight it.
int closestMatch = indexList.getNextMatch(
indexField.getText(), 0, Position.Bias.Forward);
if (closestMatch != -1) {
indexList.setSelectedIndex(closestMatch);
indexList.ensureIndexIsVisible(indexList.getSelectedIndex());
}
}
// If it was the search text field that changed...
else if ( doc.equals(searchField.getDocument()) ) {
// If there is no more text in the search field,
// disable the "List Topics" button.
if (doc.getLength() == 0)
listTopicsButton.setEnabled(false);
}
}
/**
* Called whenever the user clicks on the Contents, Index, or
* Search tabs.
*/
public void stateChanged(ChangeEvent e) {
int selection = ((JTabbedPane)e.getSource()).getSelectedIndex();
JTextField field = null;
// If they selected the Index or Search tab, select the top
// text field.
if (selection==1)
field = indexField;
else if (selection==2)
field = searchField;
if (field!=null) {
final JTextField field2 = field; // Must be final...
SwingUtilities.invokeLater(new Runnable() {
public void run() {
field2.requestFocusInWindow();
field2.selectAll();
}
});
}
}
/**
* Called whenever an item in the TOC tree is selected/deselected.
* This will display the help HTML corresponding to the TOC tree node
* selected in the right-hand pane.
*/
public void valueChanged(TreeSelectionEvent tse) {
// Get the node they clicked on.
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
tocTree.getLastSelectedPathComponent();
// Selected node could be null if they selected a leaf, then
// collapsed its parent tree node, leaving "no node" selected.
if (selectedNode==null)
return;
HelpTreeNode htn = (HelpTreeNode)selectedNode.getUserObject();
// Now, set the html in the right-hand pane to be the page
// associated with this node. NOTE: Must check for null URL's
// as often, branch nodes don't have HTML associated with them.
if (htn.url != null) {
// As we support both plain text and HTML as help pages,
// first check whether our text should be displayed as HTML.
// We must re-set the IgnoreCharsetDirective property as
// setContentType() gives us a new document.
String contentType = guessContentType(htn.url.getPath());
if (!contentType.equals(editorPane.getContentType())) {
editorPane.setContentType(contentType);
//editorPane.setDocument(editorPane.getEditorKit().createDefaultDocument());
// The Document class does not yet handle charsets properly -
// without the line below, you'll get a CharSetChangedException...
editorPane.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE);
}
String allText = getContents(htn);
// If the document is HTML, ensure that the it uses the
// correct base URL so links work.
Document document = editorPane.getDocument();
if (document instanceof HTMLDocument) {
HTMLDocument htmldoc = (HTMLDocument)document;
htmldoc.setBase(baseURL);
}
editorPane.setText(allText);
editorPane.setCaretPosition(0);
// If they want to remember this page in the history...
if (updateHistory || clickedOnTOCTree) {
// This will be the "last" page. Remove all pages
// ahead of the current page and replace them with
//this one.
while (history.size()-1 > historyPos)
history.remove(history.size()-1);
history.add(htn);
historyPos++;
// The user can definitely go backward and not forward.
backButton.setEnabled(true);
forwardButton.setEnabled(false);
}
// Next time through, we'll assume we clicked on tocTree
// unless told otherwise
clickedOnTOCTree = true;
} // End of if (htn.url != null).
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -