From 11f72664c71dd94c365be302167d045f44c21963 Mon Sep 17 00:00:00 2001 From: marhali Date: Mon, 7 Feb 2022 20:42:21 +0100 Subject: [PATCH] show notification instead of throwing an exception on fresh plugin installs --- CHANGELOG.md | 3 +++ .../java/de/marhali/easyi18n/DataStore.java | 15 +++++++++++++-- .../exception/EmptyLocalesDirException.java | 11 +++++++++++ .../java/de/marhali/easyi18n/io/IOHandler.java | 5 +++-- .../easyi18n/util/NotificationHelper.java | 17 +++++++++++++++++ src/main/resources/META-INF/plugin.xml | 2 ++ src/main/resources/messages.properties | 3 ++- 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/marhali/easyi18n/exception/EmptyLocalesDirException.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 9beac87..d223aae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ # easy-i18n Changelog ## [Unreleased] +### Changed +- Fresh projects will receive a notification instead of an exception to configure the plugin + ### Fixed - Exception on json array value mapping diff --git a/src/main/java/de/marhali/easyi18n/DataStore.java b/src/main/java/de/marhali/easyi18n/DataStore.java index 971ebe8..0d65c42 100644 --- a/src/main/java/de/marhali/easyi18n/DataStore.java +++ b/src/main/java/de/marhali/easyi18n/DataStore.java @@ -5,6 +5,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.*; +import de.marhali.easyi18n.exception.EmptyLocalesDirException; import de.marhali.easyi18n.io.IOHandler; import de.marhali.easyi18n.model.SettingsState; import de.marhali.easyi18n.model.TranslationData; @@ -60,7 +61,12 @@ public class DataStore { } catch (Exception ex) { this.data = new TranslationData(settings.isSortKeys()); successResult.accept(false); - NotificationHelper.createIOError(settings, ex); + + if(ex instanceof EmptyLocalesDirException) { + NotificationHelper.createEmptyLocalesDirNotification(project); + } else { + NotificationHelper.createIOError(settings, ex); + } } }); } @@ -79,7 +85,12 @@ public class DataStore { } catch (Exception ex) { successResult.accept(false); - NotificationHelper.createIOError(settings, ex); + + if(ex instanceof EmptyLocalesDirException) { + NotificationHelper.createEmptyLocalesDirNotification(project); + } else { + NotificationHelper.createIOError(settings, ex); + } } }); } diff --git a/src/main/java/de/marhali/easyi18n/exception/EmptyLocalesDirException.java b/src/main/java/de/marhali/easyi18n/exception/EmptyLocalesDirException.java new file mode 100644 index 0000000..284edf3 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/exception/EmptyLocalesDirException.java @@ -0,0 +1,11 @@ +package de.marhali.easyi18n.exception; + +/** + * Indicates that the translation's directory has not been configured yet + * @author marhali + */ +public class EmptyLocalesDirException extends IllegalArgumentException { + public EmptyLocalesDirException(String message) { + super(message); + } +} diff --git a/src/main/java/de/marhali/easyi18n/io/IOHandler.java b/src/main/java/de/marhali/easyi18n/io/IOHandler.java index 1ee97da..cebe902 100644 --- a/src/main/java/de/marhali/easyi18n/io/IOHandler.java +++ b/src/main/java/de/marhali/easyi18n/io/IOHandler.java @@ -3,6 +3,7 @@ package de.marhali.easyi18n.io; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import de.marhali.easyi18n.exception.EmptyLocalesDirException; import de.marhali.easyi18n.io.folder.FolderStrategy; import de.marhali.easyi18n.io.parser.ParserStrategy; import de.marhali.easyi18n.io.parser.ParserStrategyType; @@ -49,7 +50,7 @@ public class IOHandler { String localesPath = this.settings.getLocalesPath(); if(localesPath == null || localesPath.isEmpty()) { - throw new IllegalArgumentException("Locales path must not be empty"); + throw new EmptyLocalesDirException("Locales path must not be empty"); } VirtualFile localesDirectory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath)); @@ -82,7 +83,7 @@ public class IOHandler { String localesPath = this.settings.getLocalesPath(); if(localesPath == null || localesPath.isEmpty()) { - throw new IllegalArgumentException("Locales path must not be empty"); + throw new EmptyLocalesDirException("Locales path must not be empty"); } List translationFiles = diff --git a/src/main/java/de/marhali/easyi18n/util/NotificationHelper.java b/src/main/java/de/marhali/easyi18n/util/NotificationHelper.java index fd8daea..cfa57bf 100644 --- a/src/main/java/de/marhali/easyi18n/util/NotificationHelper.java +++ b/src/main/java/de/marhali/easyi18n/util/NotificationHelper.java @@ -1,6 +1,11 @@ package de.marhali.easyi18n.util; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationGroupManager; +import com.intellij.notification.NotificationType; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import de.marhali.easyi18n.action.SettingsAction; import de.marhali.easyi18n.io.IOHandler; import de.marhali.easyi18n.model.SettingsState; import org.jetbrains.annotations.NotNull; @@ -21,4 +26,16 @@ public class NotificationHelper { Logger.getInstance(IOHandler.class).error(message, ex); } + + public static void createEmptyLocalesDirNotification(Project project) { + ResourceBundle bundle = ResourceBundle.getBundle("messages"); + + Notification notification = NotificationGroupManager.getInstance() + .getNotificationGroup("Easy I18n Notification Group") + .createNotification("Easy I18n", bundle.getString("warning.missing-config"), + NotificationType.WARNING); + + notification.addAction(new SettingsAction()); + notification.notify(project); + } } \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index ccb65d9..4ff888e 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -37,6 +37,8 @@ + + \ No newline at end of file diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 778a844..e251085 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -29,4 +29,5 @@ error.io=An error occurred while processing translation files. \n\ Config: {0} => {1} ({2}) \n\ Path: {3} \n\ Please check examples at https://github.com/marhali/easy-i18n before reporting an issue! -error.submit=Open Issue \ No newline at end of file +error.submit=Open Issue +warning.missing-config=Configure your local project structure \ No newline at end of file