logger.log(logger.EXTREME, "Inserting link");
String text = browser.selectedText();
if (text.trim().equalsIgnoreCase(""))
return;
InsertLinkDialog dialog = new InsertLinkDialog(insertHyperlink);
if (currentHyperlink != null && currentHyperlink != "") {
dialog.setUrl(currentHyperlink);
}
dialog.exec();
if (!dialog.okPressed()) {
logger.log(logger.EXTREME, "Insert link canceled");
return;
}
// Take care of inserting new links
if (insertHyperlink) {
String selectedText = browser.selectedText();
if (dialog.getUrl().trim().equals(""))
return;
logger.log(logger.EXTREME, "Inserting link on text "+selectedText);
logger.log(logger.EXTREME, "URL Link " +dialog.getUrl().trim());
String dUrl = StringUtils.replace(dialog.getUrl().trim(), "'", "\\'");
String url = "<a href=\"" +dUrl
+"\" title=" +dUrl
+" >"+selectedText +"</a>";
String script = "document.execCommand('insertHtml', false, '"+url+"');";
browser.page().mainFrame().evaluateJavaScript(script);
return;
}
// Edit existing links
String js = new String( "function getCursorPos() {"
+"var cursorPos;"
+"if (window.getSelection) {"
+" var selObj = window.getSelection();"
+" var selRange = selObj.getRangeAt(0);"
+" var workingNode = window.getSelection().anchorNode.parentNode;"
+" while(workingNode != null) { "
+" if (workingNode.nodeName.toLowerCase()=='a') workingNode.setAttribute('href','" +dialog.getUrl() +"');"
+" workingNode = workingNode.parentNode;"
+" }"
+"}"
+"} getCursorPos();");
browser.page().mainFrame().evaluateJavaScript(js);
if (!dialog.getUrl().trim().equals("")) {
contentChanged();
return;
}
// Remove URL