show notification instead of throwing an exception on fresh plugin installs

This commit is contained in:
marhali 2022-02-07 20:42:21 +01:00
parent 512daedf09
commit 11f72664c7
7 changed files with 51 additions and 5 deletions

View File

@ -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

View File

@ -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);
}
}
});
}

View File

@ -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);
}
}

View File

@ -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<TranslationFile> translationFiles =

View File

@ -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);
}
}

View File

@ -37,6 +37,8 @@
<psi.referenceContributor
implementation="de.marhali.easyi18n.editor.generic.GenericKeyReferenceContributor" />
<notificationGroup displayType="BALLOON" id="Easy I18n Notification Group" />
<errorHandler implementation="de.marhali.easyi18n.service.ErrorReportHandler" />
</extensions>
</idea-plugin>

View File

@ -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
error.submit=Open Issue
warning.missing-config=Configure your local project structure