📄 helpdialog.java
字号:
Object source = e.getSource();
String actionCommand = e.getActionCommand();
// If they click on the "Display" button on the index tab, show
// the help they selected.
if (source.equals( indexDisplayButton )) {
loadSelectedHelpPageIndex();
}
// If they click on the "Display" button on the search tab, show the
// help they selected.
else if (source.equals( searchDisplayButton )) {
loadSelectedHelpPageSearch();
}
// If they click on the "List Topics" button on the search tab,
// show matching topics.
else if (actionCommand.equals("ListTopics")) {
searchString = searchField.getText(); // Remember the searched-for text.
populateSearchList();
}
else if (actionCommand.equals("Back")) {
// No, we don't want history updated (we've already seen the
// coming page).
updateHistory = false;
highlightSearchString = false;
// Increment the "position" in history and update the page.
if (historyPos>0) {
historyPos--;
setHelpPageURL(((HelpTreeNode)history.get(historyPos)).url);
}
else {
// Root must be visible but not contain a page. So we'll
// just select the root.
setHelpPageURL(null);
}
// If they've gone backward to the last page, they can't go
// back any further.
if (historyPos==0)
backButton.setEnabled(false);
// They can go forward if there are pages "ahead" of this one.
if (historyPos < history.size()-1)
forwardButton.setEnabled(true);
}
else if (actionCommand.equals("Forward")) {
// No, you don't want history updated (we've already seen the
// coming page).
updateHistory = false;
highlightSearchString = false;
// Increment the "position" in history and update the page.
historyPos++;
setHelpPageURL(((HelpTreeNode)history.get(historyPos)).url);
// If they've gone forward to the final page, they can't go
// forward any longer.
if (historyPos==history.size()-1)
forwardButton.setEnabled(false);
// They can go backward if there are pages "behind" this one.
if (historyPos>0)
backButton.setEnabled(true);
}
}
/*****************************************************************************/
/**
* Creates the tree of help we'll be displaying and stores the root in
* <code>root</code>.
*/
private void createRoot(String helpXMLFile) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
org.w3c.dom.Document doc = null;
try {
db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new FileReader(helpXMLFile));
// InputSource is = new InputSource(new UnicodeReader(
// new FileInputStream(file), "UTF-8"));
// is.setEncoding("UTF-8");
doc = db.parse(is);//db.parse(file);
} catch (Exception ex) {
guiApp.displayException(this, ex);
return;
}
root = initializeFromXMLFile(doc);
}
/*****************************************************************************/
/**
* Returns a list of all nodes under <code>root</code>'s tree
* with URL's containing <code>searchString</code>.
*/
private ArrayList getArrayList(DefaultMutableTreeNode root,
String searchString) {
// Our return value.
ArrayList arrayList = new ArrayList();
// Loop through all children of root.
int count = root.getChildCount();
String searchStringLower = searchString.toLowerCase();
for (int i=0; i<count; i++) {
// Get the current child.
DefaultMutableTreeNode child =
(DefaultMutableTreeNode)root.getChildAt(i);
// Search for searchString in its text.
// Strip the HTML tags out of this line so they can't search for
// like "HTML" and have everything be a match.
HelpTreeNode node = (HelpTreeNode)child.getUserObject();
String contents = getContents(node); // will be non-null.
contents = contents.replaceAll("<[^>]*>", "");
if (contents.toLowerCase().indexOf(searchStringLower) != -1)
arrayList.add(node);
// If this node has children, we must search them too for
// searchString.
if (child.getChildCount()>0) {
ArrayList temp = getArrayList(child, searchString);
if (temp.size() > 0)
arrayList.addAll(temp);
}
} // End of for (int i=0; i<count; i++).
// We have our list of URL's containing searchString, so return it.
return arrayList;
}
/*****************************************************************************/
/**
* Returns the contents of the HelpTreeNode's URL as a String,
* highlighting all occurences of the String searchString if desired.
*
* @param node The node from whose URL you wish to get the contents.
* @return The contents. If the URL was invalid or an
* <code>Exception</code> was thrown, an empty string is
* returned.
*/
private String getContents(HelpTreeNode node) {
// The String that will hole the text in the file.
String contents = "";
// node.url may be null if we're in a branch node (or may not).
if (node!=null && node.url!=null) {
// Read in the file.
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
node.url.openConnection().
getInputStream()));
contents = RUtilities.read(in);
in.close();
} catch (IOException e) {
guiApp.displayException(this, e);
}
// Highlight all occurences of searchString if desired.
if (highlightSearchString) {
// Keep highlighting the searched-for text until the end of
// the string is reached.
int searchStringLength = searchString.length();
String searchStringLower = searchString.toLowerCase();
int pos = contents.toLowerCase().indexOf(searchStringLower);
while (pos != -1) {
// An attempt at seeing if found text is in a tag. If
// so, skip it. We assume that this is proper HTML
// with > and < used where necessary.
int gtPos = contents.indexOf(">", pos);
if (gtPos<contents.indexOf("<", pos)) {
pos = contents.toLowerCase().indexOf(searchStringLower, gtPos+1);
continue;
}
// Otherwise, insert HTML tag to "highlight" found text in yellow.
int tempEnd = pos + searchStringLength;
contents = contents.substring(0, pos) + "<font bgcolor=#FFFF00>"
+ contents.substring(pos, tempEnd) + "</font>"
+ contents.substring(tempEnd, contents.length());
int startPos = pos + searchStringLength +
"<font bgcolor#FFFF00>".length() +
"</font>".length();
pos = contents.toLowerCase().indexOf(searchStringLower, startPos);
}
} // End of if (highlightSearchString).
} // End of if (node!=null && node.url!=null).
// Return whatever of the file (if any) you got.
return contents;
}
/*****************************************************************************/
/**
* Returns the label on the "Contents" tab.
*
* @return The text on the "Contents" tab.
* @see #setContentsTabText
*/
public final String getContentsTabText() {
return tabbedPane.getTitleAt(0);
}
/*****************************************************************************/
/**
* Returns the label on the "Display" button on the Index and Search
* panels.
*
* @return The text on the "Display" buttons.
* @see #setDisplayButtonText
*/
public final String getDisplayButtonText() {
return indexDisplayButton.getText();
}
/*****************************************************************************/
/**
* Returns the resource bundle for the help dialog.
*
* @return The resource bundle.
*/
private final ResourceBundle getHelpBundle() {
return ResourceBundle.getBundle(
"org.fife.help.HelpDialog", getLocale());
}
/*****************************************************************************/
/**
* Returns the label on the "Index" tab.
*
* @return The text on the "Index" tab.
* @see #setIndexTabText
*/
public final String getIndexTabText() {
return tabbedPane.getTitleAt(1);
}
/*****************************************************************************/
/**
* Returns the text label above the "Type in a keyword to find" text boxes.
*
* @return The text label.
* @see #setKeywordFieldLabelText
*/
public final String getKeywordFieldLabelText() {
// Arbitrarily one of this and the "2" brother.
return keywordToFindLabel.getText();
}
/*****************************************************************************/
/**
* Returns the label on the "List Topics" button on the Search panels.
*
* @return The text on the "List Topics" button.
* @see #setListTopicsButtonText
*/
public final String getListTopicsButtonText() {
return listTopicsButton.getText();
}
/*****************************************************************************/
/**
* Returns the label on the "Search" tab.
*
* @return The text on the "Search" tab.
* @see #setSearchTabText
*/
public final String getSearchTabText() {
return tabbedPane.getTitleAt(2);
}
/*****************************************************************************/
/**
* Returns the text label above the "Select topic to display" text box.
*
* @return The text label.
* @see #setTopicToDisplayFieldLabelText
*/
public final String getTopicToDisplayFieldLabelText() {
return topicToDisplayLabel.getText();
}
/*****************************************************************************/
/**
* Takes a stab at the content type (text/html vs. text/plain) for
* the file specified by the given path. This is used to figure out
* whether the help browser should display a page as text or HTML.
*
* @param path The path to the file for which to guess the content type.
* @return The content type.
*/
private String guessContentType(String path) {
if (path!=null) {
String lower = path.toLowerCase();
if (lower.endsWith("html") || lower.endsWith("htm") ||
lower.endsWith("jsp"))
return HTML_TYPE;
}
return TEXT_TYPE; // Assume plain text.
}
/*****************************************************************************/
/**
* Used in parsing an XML document containing a macro. This method
* initializes the help tree and index, as well as sets the "root
* visible" property of the help tree.
*
* @param node The root node of the parsed XML document.
* @return The root node of the help tree (<code>JTree</code>).
*/
private DefaultMutableTreeNode initializeFromXMLFile(Node node) {
/*
* This method expects the XML document to be in the following format:
*
* <?xml version="1.0" encoding="UTF-8" ?>
* ...
*/
if (node==null)
return null;
int type = node.getNodeType();
switch (type) {
// Handle document nodes.
case Node.DOCUMENT_NODE:
return initializeFromXMLFile(((org.w3c.dom.Document)node).
getDocumentElement());
// Handle element nodes.
case Node.ELEMENT_NODE:
String nodeName = node.getNodeName();
// This is the root of the entire XML file. What we do
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -