X

Geertjan's Blog

  • July 5, 2007

Visual Editor (Part 7)

Geertjan Wielenga
Product Manager
Today, an interesting development in this series—the integration of a link check feature. To do this, I first changed the 'Display' and 'Design' toggle buttons to 'Outline' and 'Link Check'. Then in the TopicPanel constructor, let the code branch in different directions, depending on the value of the display property, which was discussed yesterday:

if (display == 0) {
utils.createHyperlinkHierarchy(dObj, toc, panel);
} else {
linkCheckProcess();
}

The linkCheckProcess() method ensures that the link check is done in a background process, so that the IDE remains enabled during the link check, which could take some time because both the TOC file and the map file need to be parsed and matched:

private void linkCheckProcess() {
RequestProcessor.getDefault().post(new Runnable() {
public void run() {
progress = ProgressHandleFactory.createHandle(
"Performing link check for you, "
+ System.getProperty("user.name")
+ "...", new Cancellable() {
public boolean cancel() {
synchronized (TocPanel.this) {
cancelled = true;
}
return true;
}
private boolean cancelled;
});
progress.start();
progress.switchToIndeterminate();
try {utils.createLinkCheckHierarchy(dObj, toc, panel);
} finally {
progress.finish();
}
}
});
}

The TocUtilities.createLinkCheckHierarchy method and the TocUtilities.createHyperlinkHierarchy create either link buttons or text fields, which depends on the display property. In the case of the text fields, when the text field is added, if the text 'Broken link!' is returned from the matching process, the text field background is red, otherwise it is green:

public void addFieldToView(TocDataObject dObj, String name, String target, GridBagConstraints gbc, SectionInnerPanel panel) {
JTextField textField = new JTextField();
textField.setText(name);if ("Broken link!".equals(matchTargets(dObj, target))) {
textField.setBackground(Color.red);
} else {
textField.setBackground(Color.green);
}

textField.setEditable(false);
panel.add(textField, gbc);
}

This is the part that can take some time. For this reason, only the text field creation part is in a separate thread with a progress bar, while the link button creation doesn't need it. So, now, whenever the 'Link Check' toggle button is clicked, the link check is performed, with this result:

Finally, the method that does all the work has been discussed before. We parse the TOC file and the map file and compare the 'target' attributes in both. If there are no matches, we have a broken link. If there is a match, the map file's 'url' attribute is returned:

public String matchTargets(TocDataObject dObj, String target) {
List listMap = unmarshallMapFile(dObj).getMapID();
MapID id = null;
for (int i = 0; i < listMap.size(); i++) {
id = (MapID) listMap.get(i);
if (target.equals(id.getTarget())) {
return id.getUrl();
}
}
return "Broken link!";
}

Hurray—we now have a successful JavaHelp link checker.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha