Skip to content

Commit

Permalink
[LO extension] solves issue #7298
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKruse committed Nov 19, 2022
1 parent f1b2e5a commit bdf0dd4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ loContextMenuOptions = LanguageTool Options
loContextMenuRenewMarkups = Renew grammar markups
loContextMenuDeactivateRule = Deactivate Rule
loContextMenuActivateRule = Activate Rule
loContextMenuAddToDictionary = Add to Dictionary
loDialogLanguageHelp = The language of the paragraph shown in the editor element is displayed.\nIf you want to change the language of the underlined match or the whole paragraph, choose the new language first.\nAfter a new language is selected the "Change Language" button will be activated.
loDialogChangeLanguageHelp = The list is activated, after a new language was selected.\nClick on the list to choose:\n - change the language of the match (underlined in the editor element)\n - the whole paragraph
loDialogMatchDescriptionHelp = Gives a description of the possible error found by the spell or grammar checker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ loContextMenuRenewMarkups = Renew grammar markups
loContextMenuOptions = LanguageTool Options
loContextMenuDeactivateRule = Deactivate Rule
loContextMenuActivateRule = Activate Rule
loContextMenuAddToDictionary = Add to Dictionary
loDialogLanguageHelp = The language of the paragraph shown in the editor element is displayed.\nIf you want to change the language of the underlined match or the whole paragraph, choose the new language first.\nAfter a new language is selected the "Change Language" button will be activated.
loDialogChangeLanguageHelp = The list is activated, after a new language was selected.\nClick on the list to choose:\n - change the language of the match (underlined in the editor element)\n - the whole paragraph
loDialogMatchDescriptionHelp = Gives a description of the possible error found by the spell or grammar checker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class LanguageToolMenus {
private static boolean isRunning = false;

private XComponentContext xContext;
private XComponent xComponent;
private SingleDocument document;
private Configuration config;
// private boolean switchOff;
Expand All @@ -81,6 +82,7 @@ public class LanguageToolMenus {
debugModeTm = OfficeTools.DEBUG_MODE_TM;
this.document = document;
this.xContext = xContext;
this.xComponent = xComponent;
setConfigValues(config);
ltHeadMenu = new LTHeadMenu(xComponent);
ltContextMenu = new ContextMenuInterceptor(xContext);
Expand Down Expand Up @@ -392,6 +394,7 @@ class ContextMenuInterceptor implements XContextMenuInterceptor {
private final static String LT_ACTIVATE_RULE = "service:org.languagetool.openoffice.Main?activateRule_";
private final static String LT_REMOTE_HINT = "service:org.languagetool.openoffice.Main?remoteHint";
private final static String LT_RENEW_MARKUPS = "service:org.languagetool.openoffice.Main?renewMarkups";
private final static String LT_ADD_TO_DICTIONARY = "service:org.languagetool.openoffice.Main?addToDictionary_";

public ContextMenuInterceptor() {}

Expand Down Expand Up @@ -470,6 +473,34 @@ public ContextMenuInterceptorAction notifyContextMenuExecute(ContextMenuExecuteE
str = tmpProps.getPropertyValue("CommandURL").toString();
}
if (ADD_TO_DICTIONARY_2.equals(str) || ADD_TO_DICTIONARY_3.equals(str)) {
ViewCursorTools viewCursor = new ViewCursorTools(xComponent);
String wrongWord = viewCursor.getViewCursorSelectedArea();
if (wrongWord != null && !wrongWord.isEmpty()) {
if (wrongWord.charAt(wrongWord.length() - 1) == '.') {
wrongWord= wrongWord.substring(0, wrongWord.length() - 1);
}
if (!wrongWord.isEmpty()) {
MessageHandler.printToLogFile("wrong Word: " + wrongWord);
XMultiServiceFactory xMenuElementFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, xContextMenu);
XIndexContainer xSubMenuContainer = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class,
xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerContainer"));
int j = 0;
for (String dict : document.getMultiDocumentsHandler().getLtDictionary().getUserDictionaries(xContext)) {
XPropertySet xNewSubMenuEntry = UnoRuntime.queryInterface(XPropertySet.class,
xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger"));
xNewSubMenuEntry.setPropertyValue("Text", dict);
xNewSubMenuEntry.setPropertyValue("CommandURL", LT_ADD_TO_DICTIONARY + dict + ":" + wrongWord);
xSubMenuContainer.insertByIndex(j, xNewSubMenuEntry);
j++;
}
XPropertySet xNewMenuEntry = UnoRuntime.queryInterface(XPropertySet.class,
xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger"));
xNewMenuEntry.setPropertyValue("Text", MESSAGES.getString("loContextMenuAddToDictionary"));
xNewMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer );
xContextMenu.removeByIndex(n);
xContextMenu.insertByIndex(n, xNewMenuEntry);
}
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public String[] getUserDictionaries(XComponentContext xContext) {
for (XDictionary dictionary : dictionaryList) {
if (dictionary.isActive()) {
String name = dictionary.getName();
if (!name.startsWith("INTERNAL_DICT_PREFIX") && !name.equals(listIgnoredWords.getName())) {
if (!name.startsWith(INTERNAL_DICT_PREFIX) && !name.equals(listIgnoredWords.getName())) {
userDictionaries.add(new String(name));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,9 @@ public void trigger(String sEvent) {
} else if (sEvent.startsWith("activateRule_")) {
String ruleId = sEvent.substring(13);
activateRule(ruleId);
} else if (sEvent.startsWith("addToDictionary_")) {
String[] sArray = sEvent.substring(16).split(":");
getLtDictionary().addWordToDictionary(sArray[0], sArray[1], xContext);;
} else if ("renewMarkups".equals(sEvent)) {
renewMarkups();
} else if ("checkDialog".equals(sEvent) || "checkAgainDialog".equals(sEvent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,50 @@ int getViewCursorCharacter() {
}
}

/**
* Returns the selected String inside a paragraph
* Returns null if fails
*/
String getViewCursorSelectedArea() {
isBusy++;
try {
XTextViewCursor vCursor = getViewCursor();
if (vCursor == null) {
return null;
}
XText xViewCursorText = vCursor.getText();
if (xViewCursorText == null) {
return null;
}
// Test if cursor position is in table
XTextDocument curDoc = getTextDocument();
if (curDoc != null) {
XTextTablesSupplier xTableSupplier = UnoRuntime.queryInterface(XTextTablesSupplier.class, curDoc);
XIndexAccess xTables = UnoRuntime.queryInterface(XIndexAccess.class, xTableSupplier.getTextTables());
if (xTables != null) {
for (int i = 0; i < xTables.getCount(); i++) {
XTextTable xTable = UnoRuntime.queryInterface(XTextTable.class, xTables.getByIndex(i));
if (xTable != null) {
for (String cellName : xTable.getCellNames()) {
XText xTableText = UnoRuntime.queryInterface(XText.class, xTable.getCellByName(cellName) );
if (xTableText != null && xViewCursorText.equals(xTableText)) {
XTextRange range = vCursor;
return xTableText.createTextCursorByRange(range).getString();
}
}
}
}
}
}
return vCursor.getString();
} catch (Throwable t) {
MessageHandler.printException(t); // all Exceptions thrown by UnoRuntime.queryInterface are caught
return null; // Return negative value as method failed
} finally {
isBusy--;
}
}

/**
* Set the view cursor if the paragraph is inside of text region
*/
Expand Down

0 comments on commit bdf0dd4

Please sign in to comment.