if (!preferredDabs.isEmpty()) {
// Preferred disambiguations
for (String preferredDab : preferredDabs) {
fixedBegin += addItem(
submenu, null, preferredDab, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, preferredDab),
element, textPane));
}
} else {
// Last replacement
String title = LinkReplacement.getLastReplacement(disambigPage.getTitle());
if (title != null) {
fixedBegin += addItem(
submenu, null, title, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, title),
element, textPane));
}
}
// Wiktionary links
if ((wiktionary != null) && (wiktionary.size() > 0)) {
if ((!separators) && (fixedBegin > 0)) {
fixedBegin += addSeparator(submenu);
separators = true;
}
for (String wikt : wiktionary) {
String name = "wikt:" + wikt;
addItem(
submenu, null, name, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, name),
element, textPane));
}
}
// Possible links
if ((links != null) && (links.size() > 0)) {
if ((!separators) && (fixedBegin > 0)) {
fixedBegin += addSeparator(submenu);
separators = true;
}
for (Page p : links) {
if (p.isRedirect()) {
JMenu submenu1 = new JMenu(p.getTitle());
Map<Page, List<String>> anchorsRedirectMap = new HashMap<Page, List<String>>();
p.getLinksWithRedirect(anchorsRedirectMap);
Iterator<Page> iter = p.getRedirectIteratorWithPage();
while (iter.hasNext()) {
Page pageTmp = iter.next();
List<String> anchors = anchorsRedirectMap.get(pageTmp);
addItem(
submenu1, pageTmp, null, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, pageTmp.getTitle()),
element, textPane));
if ((anchors != null) && (anchors.size() > 0)) {
for (String anchor : anchors) {
addItem(
submenu1, pageTmp, anchor, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, anchor),
element, textPane));
}
}
}
submenu.add(submenu1);
} else {
addItem(
submenu, p, null, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, p.getTitle()),
element, textPane));
// Anchors
List<String> anchors = anchorsMap.get(p);
if (anchors != null) {
for (String anchor : anchors) {
addItem(
submenu, p, anchor, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, anchor),
element, textPane));
}
}
}
}
}
if (separators) {
if (!preferredDabs.isEmpty()) {
// Preferred disambiguations
fixedEnd += addSeparator(submenu);
for (String preferredDab : preferredDabs) {
fixedEnd += addItem(
submenu, null, preferredDab, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, preferredDab),
element, textPane));
}
} else {
// Last replacement
String title = LinkReplacement.getLastReplacement(disambigPage.getTitle());
if (title != null) {
fixedEnd += addSeparator(submenu);
fixedEnd += addItem(
submenu, null, title, true,
new ReplaceTextAction(
matcher.getReplacement(page, template, indexReplacement, title),
element, textPane));
}
}
}