diff --git a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/LtDictionary.java b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/LtDictionary.java index aa5966c42c41..f5980454f77d 100644 --- a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/LtDictionary.java +++ b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/LtDictionary.java @@ -60,24 +60,35 @@ public class LtDictionary { private static Set dictionaryList = new HashSet<>(); private static String listIgnoredWords = null; + private static boolean isDisposed = false; + /** + * Add a non permanent dictionary to LO/OO that contains additional words defined in LT + */ + public static void setDisposed() { + isDisposed = true; + } + /** * Add a non permanent dictionary to LO/OO that contains additional words defined in LT */ public static boolean setLtDictionary(XComponentContext xContext, Locale locale, LinguisticServices linguServices) { boolean debugMode = OfficeTools.DEBUG_MODE_LD; + if (isDisposed) { + return false; + } XSearchableDictionaryList searchableDictionaryList = OfficeTools.getSearchableDictionaryList(xContext); if (searchableDictionaryList == null) { MessageHandler.printToLogFile("LtDictionary: setLtDictionary: searchableDictionaryList == null"); return false; } - if (listIgnoredWords == null) { + if (listIgnoredWords == null && !isDisposed) { setListIgnoredWords(searchableDictionaryList.getDictionaries()); } String shortCode = OfficeTools.localeToString(locale); String dictionaryNamePrefix = INTERNAL_DICT_PREFIX + shortCode + "_internal"; String dictionaryName = dictionaryNamePrefix + "1" + DICT_FILE_POSTFIX; - if (!dictionaryList.contains(dictionaryName) && searchableDictionaryList.getDictionaryByName(dictionaryName) == null) { + if (!isDisposed && !dictionaryList.contains(dictionaryName) && searchableDictionaryList.getDictionaryByName(dictionaryName) == null) { dictionaryList.add(dictionaryName); String ltDictionaryPath = getLTDictionaryFile(locale, linguServices); if (ltDictionaryPath != null) { @@ -91,6 +102,9 @@ public static boolean setLtDictionary(XComponentContext xContext, Locale locale, int count = 0; int dictNum = 1; int dictCount = 0; + if (isDisposed) { + return false; + } XDictionary manualDictionary = searchableDictionaryList.createDictionary(dictionaryName, locale, DictionaryType.POSITIVE, ""); if (debugMode) { MessageHandler.printToLogFile("Add " + words.size() + " words to " + dictionaryName); @@ -104,11 +118,17 @@ public static boolean setLtDictionary(XComponentContext xContext, Locale locale, dictCount += manualDictionary.getCount(); dictNum++; dictionaryName = dictionaryNamePrefix + dictNum + DICT_FILE_POSTFIX; + if (isDisposed) { + return false; + } manualDictionary = searchableDictionaryList.createDictionary(dictionaryName, locale, DictionaryType.POSITIVE, ""); count = 0; } } manualDictionary.setActive(true); + if (isDisposed) { + return false; + } searchableDictionaryList.addDictionary(manualDictionary); dictCount += manualDictionary.getCount(); long endTime = System.currentTimeMillis(); diff --git a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/MultiDocumentsHandler.java b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/MultiDocumentsHandler.java index b913ac53eb5f..a615183d0fe9 100644 --- a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/MultiDocumentsHandler.java +++ b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/MultiDocumentsHandler.java @@ -129,6 +129,8 @@ public class MultiDocumentsHandler { private int heapCheckInterval = HEAP_CHECK_INTERVAL; private boolean testMode = false; private boolean javaLookAndFeelIsSet = false; + private boolean isHelperDisposed = false; + MultiDocumentsHandler(XComponentContext xContext, XProofreader xProofreader, XEventListener xEventListener) { @@ -403,6 +405,14 @@ private void setContextOfClosedDoc(XComponent xComponent) { found = true; document.dispose(true); isDisposed = true; + if (documents.size() < 2) { + LtDictionary.setDisposed(); + if (textLevelQueue != null) { + textLevelQueue.setStop(); + textLevelQueue = null; + } + isHelperDisposed = true; + } /* save only if document is saved if (config.saveLoCache()) { document.writeCaches(); @@ -1973,12 +1983,16 @@ public boolean isRunning() { /** class to start a separate thread to check for Impress documents */ private class LtHelper extends Thread { + @Override public void run() { try { SingleDocument currentDocument = null; - while (currentDocument == null) { - Thread.sleep(1000); + while (!isHelperDisposed && currentDocument == null) { + Thread.sleep(250); + if (isHelperDisposed) { + return; + } currentDocument = getCurrentDocument(); if (currentDocument != null && currentDocument.getDocumentType() == DocumentType.IMPRESS) { checkImpressDocument = true; @@ -1995,6 +2009,7 @@ public void run() { MessageHandler.showError(e); } } + } } diff --git a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/TextLevelCheckQueue.java b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/TextLevelCheckQueue.java index 01ef34bad165..7fbb786be3f0 100644 --- a/languagetool-office-extension/src/main/java/org/languagetool/openoffice/TextLevelCheckQueue.java +++ b/languagetool-office-extension/src/main/java/org/languagetool/openoffice/TextLevelCheckQueue.java @@ -181,7 +181,7 @@ public void setStop() { if (debugMode) { MessageHandler.printToLogFile("TextLevelCheckQueue: setStop: stop queue"); } - textRuleQueue.add(queueEntry); + textRuleQueue.add(0, queueEntry); } wakeupQueue(); }