📄 helpdialog.java
字号:
// here is loop through its children (which should be an
// IndexList element, a PropertiesFile element and a
// RootNode element).
if (nodeName.equals(ROOT_ELEMENT)) {
NodeList childNodes = node.getChildNodes();
if (childNodes!=null) {
int length = childNodes.getLength();
for (int i=0; i<length; i++) {
initializeFromXMLFile(childNodes.item(i));
}
}
treeBundle = null; // To help GC.
return root;
}
// The first element in the XML file should tell where the
// properties file is that maps tree node keys to their
// corresponding (localized) strings.
else if (nodeName.equals(PROPERTIES_FILE)) {
NamedNodeMap attributes = node.getAttributes();
if (attributes==null || attributes.getLength()!=1)
return null;
Node node2 = attributes.item(0);
if (!validateAttributeNode(node2, NAME))
return null;
String name = node2.getNodeValue();
try {
treeBundle = new PropertyResourceBundle(
new FileInputStream(baseDir + name));
} catch (Exception e) {
// Keep guiApp as owner as this dialog isn't
// displayable yet.
guiApp.displayException(e);
return null;
}
return null;
}
// This is the "RootNode" element in the XML file; that is,
// the node that details the tree structure for the help.
else if (nodeName.equals(TREE_ROOT_NODE)) {
NamedNodeMap attributes = node.getAttributes();
if (attributes==null || attributes.getLength()<2)
return null;
String name = null;
String file = null;
int count = attributes.getLength();
for (int i=0; i<count; i++) {
Node node2 = attributes.item(i);
String v = node2.getNodeValue();
if (node2.getNodeName().equals(NAME))
name = treeBundle.getString(v);
else if (node2.getNodeName().equals(VISIBLE))
rootVisible = new Boolean(v).booleanValue();
else if (node2.getNodeName().equals(PAGE_VALUE))
file = baseDir + v;
}
if (name==null)
return null; // "name" attribute is required.
HelpTreeNode helpRoot = file==null ?
new HelpTreeNode(name) :
new HelpTreeNode(name, file);
// Set our global root.
root = new DefaultMutableTreeNode(helpRoot);
NodeList childNodes = node.getChildNodes();
if (childNodes!=null) {
int length = childNodes.getLength();
for (int i=0; i<length; i++) {
DefaultMutableTreeNode dmtn =
initializeFromXMLFile(childNodes.item(i));
// null could mean properties file or error.
if (dmtn!=null)
root.add(dmtn);
}
}
return root;
}
// This represents a "Node" structure; that is, a node in
// the tree help structure that contains child nodes.
else if (nodeName.equals(TREE_NODE)) {
NamedNodeMap attributes = node.getAttributes();
if (attributes==null || attributes.getLength()<1)
return null;
String name = null;
String file = null;
int count = attributes.getLength();
for (int i=0; i<count; i++) {
Node node2 = attributes.item(i);
String v = node2.getNodeValue();
if (node2.getNodeName().equals(NAME))
name = treeBundle.getString(v);
else if (node2.getNodeName().equals(PAGE_VALUE))
file = baseDir + v;
}
HelpTreeNode tempNode = file==null ?
new HelpTreeNode(name) :
new HelpTreeNode(name, file);
DefaultMutableTreeNode dmtn = new
DefaultMutableTreeNode(tempNode);
NodeList childNodes = node.getChildNodes();
if (childNodes!=null) {
int length = childNodes.getLength();
for (int i=0; i<length; i++) {
DefaultMutableTreeNode newNode =
initializeFromXMLFile(childNodes.item(i));
if (newNode!=null)
dmtn.add(newNode);
}
}
return dmtn;
}
// This is a "leaf" in the tree node structure; that is,
// an actual help HTML page.
else if (nodeName.equals(PAGE)) {
NamedNodeMap attributes = node.getAttributes();
if (attributes==null || attributes.getLength()!=2)
return null;
String name = null;
String file = null;
for (int i=0; i<2; i++) {
Node node2 = attributes.item(i);
String v = node2.getNodeValue();
if (node2.getNodeName().equals(NAME))
name = treeBundle.getString(v);
else if (node2.getNodeName().equals(PAGE_VALUE))
file = baseDir + v;
}
if (name==null || file==null)
return null;
HelpTreeNode tempNode = new HelpTreeNode(name, file);
return new DefaultMutableTreeNode(tempNode);
}
// This is the list of words to put in the index.
else if (nodeName.equals(INDEXITEMS)) {
NodeList childNodes = node.getChildNodes();
if (childNodes!=null) {
int length = childNodes.getLength();
// Cache to avoid all of the text (whitespace)
// elements.
ArrayList elements = new ArrayList(length/2);
for (int i=0; i<length; i++) {
Node node2 = childNodes.item(i);
if (node2.getNodeType()==Node.TEXT_NODE)
continue; // Whitespace between elements.
NamedNodeMap attributes = node2.getAttributes();
if (attributes==null || attributes.getLength()!=1)
return null;
node2 = attributes.item(0);
if (!validateAttributeNode(node2, NAME))
continue;
String name = node2.getNodeValue();
elements.add(name);
}
int size = elements.size();
indexElements = new String[size];
for (int i=0; i<size; i++) {
indexElements[i] = (String)elements.get(i);
}
}
return null;
}
throw new InternalError("Should never get here (nodename=='" + nodeName + "')");
case Node.TEXT_NODE:
// Whitespace node - discard.
break;
// An error occured?
default:
System.err.println("... ERROR!!!");
break;
}
// Everything went poorly!
return null;
}
/*****************************************************************************/
/**
* Called whenever a user selects an index item (through double-
* clicking or the "Display" button). This function searches through
* all of the help HTML for the string selected from the index; if
* only one match is found, that page is displayed, but if more than
* one match is found, the user is prompted to select the desired
* page to display.
*/
private void loadSelectedHelpPageIndex() {
String selected = (String)indexList.getSelectedValue();
// Search through all of the help pages to see where this item is.
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tocTree.getModel().getRoot();
ArrayList matchNodes = getArrayList(root, selected);
int size = matchNodes.size();
// If there's only one match, just display it.
if (size==1) {
HelpTreeNode node = (HelpTreeNode)matchNodes.get(0);
if (node.url!=null) {
updateHistory = true;
highlightSearchString = false;
setHelpPageURL(node.url);
}
} // End of if (matchNodes.size() == 1).
// If there is > 1 match found, have the user pick the one they want.
else if (size>1) {
TopicsFoundDialog tfDialog = new TopicsFoundDialog(this, matchNodes);
tfDialog.setVisible(true);
int selectedIndex = tfDialog.getSelectedIndex();
if (selectedIndex != -1) {
HelpTreeNode node = (HelpTreeNode)matchNodes.get(selectedIndex);
if (node.url!=null) {
updateHistory = true;
highlightSearchString = false;
setHelpPageURL(node.url);
}
}
}
// If there are no matches (shouldn't happen if set up right), say so.
else if (size==0) {
editorPane.setText(NO_MATCH_HTML);
}
}
/*****************************************************************************/
// Helper method; called whenever a user selects an search item
// (through double-clicking or the "Display" button).
private void loadSelectedHelpPageSearch() {
// Get the HelpTreeNode they chose.
HelpTreeNode chosenNode = (HelpTreeNode)searchList.getSelectedValue();
// Now, set the html in the right-hand pane to be the page associated with this node.
if (chosenNode.url != null) {
// We do want this page remembered in the history, and we
// want searchString highlighted.
updateHistory = true;
highlightSearchString = true;
setHelpPageURL(chosenNode.url);
} // End of if (chosenNode.url != null).
}
/*****************************************************************************/
// Populates the "Search Results" panel with results matching the user's search.
private void populateSearchList() {
String selected = searchField.getText();
// Search through all of the help pages to see where this item is.
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tocTree.
getModel().getRoot();
ArrayList matchNodes = getArrayList(root, selected);
// Populate the searchList panel with possible places to go.
searchList.setListData(matchNodes.toArray());
// Make sure "Display" button is active or not correctly.
if (matchNodes.size() > 0) {
searchDisplayButton.setEnabled(true);
searchList.setSelectedIndex(0);
}
else
searchDisplayButton.setEnabled(false);
}
/*****************************************************************************/
/**
* Sets the icon to use for the "back" button.
*
* @param icon The icon to use. If <code>null</code>, no icon will be
* used.
* @see #setForwardButtonIcon
*/
public void setBackButtonIcon(Icon icon) {
backButton.setIcon(icon);
}
/*****************************************************************************/
/**
* Sets the "base URL" from which all links will be searched for.
* That is, all HTML pages loaded will have their links treated as
* relative to this URL.
*
* @param url The base URL.
*/
public void setBaseURL(URL url) {
baseURL = url;
}
/*****************************************************************************/
/**
* Sets the label on the "Contents" tab.
*
* @param text The text for the "Contents" tab.
* @see #getContentsTabText
*/
public void setContentsTabText(String text) {
tabbedPane.setTitleAt(0, text);
}
/*****************************************************************************/
/**
* Sets the label on the "Display" button on the Index and Search panels.
*
* @param text The text for the "Display" button.
* @see #getDisplayButtonText
*/
public void setDisplayButtonText(String text) {
indexDisplayButton.setText(text);
searchDisplayButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the icon to use for the "forward" button.
*
* @param icon The icon to use. If <code>null</code>, no icon will be
* used.
* @see #setBackButtonIcon
*/
public void setForwardButtonIcon(Icon icon) {
forwardButton.setIcon(icon);
}
/*****************************************************************************/
/**
* Both updates the tocTree AND updates the help pane to display the
* correct Help page. This method simply searches for the specified URL
* in the help tree. If it is found, it is selected, which triggers the
* HTML pane to update itself accordingly. If it is not found (which
* would mean an error on the caller's part), nothing is selected and
* an error message is given.
*
* @param url The URL to load. This URL should not contain an anchor
* (reference) as this will mess up the search for the URL in
* the help tree.
*/
private void setHelpPageURL(URL url) {
// If we got into this method, the user changed the help screen some
// way other than clicking directly on tocTree. We need to know this
// to correctly update the history.
clickedOnTOCTree = false;
// Select the root or first node if url==null.
if (url==null) {
tocTree.setSelectionRow(0);
return;
}
// In the tocTree, make the new page the selected one.
// This fires a TreeEvent action to occur, and method valueChanged()
// will set the new Help page for us.
// NOTE: getRowCount() returns number of EXPANDED, visible rows, so we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -