From 5f16d1452452c4532b1ad730b9a4698517c56452 Mon Sep 17 00:00:00 2001 From: marhali Date: Sun, 30 Jan 2022 12:04:57 +0100 Subject: [PATCH] add new configuration options for enhanced io system --- .../easyi18n/dialog/SettingsDialog.java | 65 +++++++++++++++---- .../de/marhali/easyi18n/io/ArrayMapper.java | 2 +- .../easyi18n/model/FolderStrategy.java | 29 +++++++++ .../marhali/easyi18n/model/SettingsState.java | 21 ++++++ .../easyi18n/model/bus/ParserStrategy.java | 29 +++++++++ src/main/resources/messages.properties | 10 ++- 6 files changed, 138 insertions(+), 18 deletions(-) create mode 100644 src/main/java/de/marhali/easyi18n/model/FolderStrategy.java create mode 100644 src/main/java/de/marhali/easyi18n/model/bus/ParserStrategy.java diff --git a/src/main/java/de/marhali/easyi18n/dialog/SettingsDialog.java b/src/main/java/de/marhali/easyi18n/dialog/SettingsDialog.java index 1c6b3a1..a13f023 100644 --- a/src/main/java/de/marhali/easyi18n/dialog/SettingsDialog.java +++ b/src/main/java/de/marhali/easyi18n/dialog/SettingsDialog.java @@ -2,15 +2,20 @@ package de.marhali.easyi18n.dialog; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.ui.DialogBuilder; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.ui.components.JBCheckBox; import com.intellij.ui.components.JBLabel; +import com.intellij.ui.components.JBPanel; import com.intellij.ui.components.JBTextField; import de.marhali.easyi18n.InstanceManager; +import de.marhali.easyi18n.io.ArrayMapper; +import de.marhali.easyi18n.model.FolderStrategy; import de.marhali.easyi18n.model.SettingsState; +import de.marhali.easyi18n.model.bus.ParserStrategy; import de.marhali.easyi18n.service.SettingsService; import javax.swing.*; @@ -26,6 +31,8 @@ public class SettingsDialog { private final Project project; private TextFieldWithBrowseButton pathText; + private ComboBox folderStrategyComboBox; + private ComboBox parserStrategyComboBox; private JBTextField filePatternText; private JBTextField previewLocaleText; private JBTextField pathPrefixText; @@ -42,6 +49,8 @@ public class SettingsDialog { if(prepare(state).show() == DialogWrapper.OK_EXIT_CODE) { // Save changes state.setLocalesPath(pathText.getText()); + state.setFolderStrategy(FolderStrategy.fromIndex(folderStrategyComboBox.getSelectedIndex())); + state.setParserStrategy(ParserStrategy.fromIndex(parserStrategyComboBox.getSelectedIndex())); state.setFilePattern(filePatternText.getText()); state.setPreviewLocale(previewLocaleText.getText()); state.setPathPrefix(pathPrefixText.getText()); @@ -57,29 +66,57 @@ public class SettingsDialog { } private DialogBuilder prepare(SettingsState state) { + ResourceBundle bundle = ResourceBundle.getBundle("messages"); JPanel rootPanel = new JPanel(new GridLayout(0, 1, 2, 2)); /* path */ - JBLabel pathLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.text")); + JBLabel pathLabel = new JBLabel(bundle.getString("settings.path.text")); pathText = new TextFieldWithBrowseButton(new JTextField(state.getLocalesPath())); pathLabel.setLabelFor(pathText); - pathText.addBrowseFolderListener(ResourceBundle.getBundle("messages").getString("settings.path.title"), null, project, new FileChooserDescriptor( + pathText.addBrowseFolderListener(bundle.getString("settings.path.title"), null, project, new FileChooserDescriptor( false, true, false, false, false, false)); rootPanel.add(pathLabel); rootPanel.add(pathText); - /* file pattern */ - JBLabel filePatternLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.file-pattern")); - filePatternText = new JBTextField(state.getFilePattern()); - filePatternText.setToolTipText(ResourceBundle.getBundle("messages").getString("settings.path.file-pattern-tooltip")); + JBLabel strategyLabel = new JBLabel(bundle.getString("settings.strategy.title")); + rootPanel.add(strategyLabel); - rootPanel.add(filePatternLabel); - rootPanel.add(filePatternText); + JPanel strategyPanel = new JBPanel<>(new GridBagLayout()); + rootPanel.add(strategyPanel); + GridBagConstraints constraints = new GridBagConstraints(); + + /* folder strategy */ + folderStrategyComboBox = new ComboBox<>(bundle.getString("settings.strategy.folder").split(ArrayMapper.SPLITERATOR_REGEX)); + folderStrategyComboBox.setSelectedIndex(state.getFolderStrategy().toIndex()); + folderStrategyComboBox.setToolTipText(bundle.getString("settings.strategy.folder.tooltip")); + folderStrategyComboBox.setMinimumAndPreferredWidth(256); + constraints.fill = GridBagConstraints.HORIZONTAL; + constraints.gridx = 0; + constraints.gridy = 0; + strategyPanel.add(folderStrategyComboBox, constraints); + + /* parser strategy */ + parserStrategyComboBox = new ComboBox<>(bundle.getString("settings.strategy.parser").split(ArrayMapper.SPLITERATOR_REGEX)); + parserStrategyComboBox.setSelectedIndex(state.getParserStrategy().toIndex()); + parserStrategyComboBox.setToolTipText(bundle.getString("settings.strategy.parser.tooltip")); + constraints.fill = GridBagConstraints.HORIZONTAL; + constraints.gridx = 1; + constraints.gridy = 0; + strategyPanel.add(parserStrategyComboBox, constraints); + + /* file pattern strategy */ + filePatternText = new JBTextField(state.getFilePattern()); + filePatternText.setToolTipText(bundle.getString("settings.strategy.file-pattern.tooltip")); + constraints.fill = GridBagConstraints.HORIZONTAL; + constraints.gridx = 2; + constraints.gridy = 0; + constraints.weightx = 1; + strategyPanel.add(filePatternText, constraints); /* preview locale */ - JBLabel previewLocaleLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.preview")); + JBLabel previewLocaleLabel = new JBLabel(bundle.getString("settings.preview")); previewLocaleText = new JBTextField(state.getPreviewLocale()); previewLocaleLabel.setLabelFor(previewLocaleText); @@ -87,32 +124,32 @@ public class SettingsDialog { rootPanel.add(previewLocaleText); /* path prefix */ - JBLabel pathPrefixLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.prefix")); + JBLabel pathPrefixLabel = new JBLabel(bundle.getString("settings.path.prefix")); pathPrefixText = new JBTextField(state.getPathPrefix()); rootPanel.add(pathPrefixLabel); rootPanel.add(pathPrefixText); /* sort keys */ - sortKeysCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.keys.sort")); + sortKeysCheckbox = new JBCheckBox(bundle.getString("settings.keys.sort")); sortKeysCheckbox.setSelected(state.isSortKeys()); rootPanel.add(sortKeysCheckbox); /* nested keys */ - nestedKeysCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.keys.nested")); + nestedKeysCheckbox = new JBCheckBox(bundle.getString("settings.keys.nested")); nestedKeysCheckbox.setSelected(state.isNestedKeys()); rootPanel.add(nestedKeysCheckbox); /* code assistance */ - codeAssistanceCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.editor.assistance")); + codeAssistanceCheckbox = new JBCheckBox(bundle.getString("settings.editor.assistance")); codeAssistanceCheckbox.setSelected(state.isCodeAssistance()); rootPanel.add(codeAssistanceCheckbox); DialogBuilder builder = new DialogBuilder(); - builder.setTitle(ResourceBundle.getBundle("messages").getString("action.settings")); + builder.setTitle(bundle.getString("action.settings")); builder.removeAllActions(); builder.addCancelAction(); builder.addOkAction(); diff --git a/src/main/java/de/marhali/easyi18n/io/ArrayMapper.java b/src/main/java/de/marhali/easyi18n/io/ArrayMapper.java index bebe612..fa0ad2a 100644 --- a/src/main/java/de/marhali/easyi18n/io/ArrayMapper.java +++ b/src/main/java/de/marhali/easyi18n/io/ArrayMapper.java @@ -22,7 +22,7 @@ public abstract class ArrayMapper { static final String SUFFIX = "]"; static final char DELIMITER = ';'; - static final String SPLITERATOR_REGEX = + public static final String SPLITERATOR_REGEX = MessageFormat.format("(? String read(Iterator elements, Function stringFactory) { diff --git a/src/main/java/de/marhali/easyi18n/model/FolderStrategy.java b/src/main/java/de/marhali/easyi18n/model/FolderStrategy.java new file mode 100644 index 0000000..4e8b73f --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/model/FolderStrategy.java @@ -0,0 +1,29 @@ +package de.marhali.easyi18n.model; + +/** + * Represents all supported folder strategies. + * @author marhali + */ +public enum FolderStrategy { + SINGLE, + MODULARIZED_LOCALE, + MODULARIZED_NAMESPACE; + + public int toIndex() { + int index = 0; + + for(FolderStrategy strategy : values()) { + if(strategy == this) { + return index; + } + + index++; + } + + throw new NullPointerException(); + } + + public static FolderStrategy fromIndex(int index) { + return values()[index]; + } +} diff --git a/src/main/java/de/marhali/easyi18n/model/SettingsState.java b/src/main/java/de/marhali/easyi18n/model/SettingsState.java index 8289fad..c5dcb6f 100644 --- a/src/main/java/de/marhali/easyi18n/model/SettingsState.java +++ b/src/main/java/de/marhali/easyi18n/model/SettingsState.java @@ -1,5 +1,6 @@ package de.marhali.easyi18n.model; +import de.marhali.easyi18n.model.bus.ParserStrategy; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,6 +11,8 @@ import org.jetbrains.annotations.Nullable; public class SettingsState { public static final String DEFAULT_PREVIEW_LOCALE = "en"; + public static final FolderStrategy DEFAULT_FOLDER_STRATEGY = FolderStrategy.SINGLE; + public static final ParserStrategy DEFAULT_PARSER_STRATEGY = ParserStrategy.JSON; public static final String DEFAULT_FILE_PATTERN = "*.*"; public static final String DEFAULT_PATH_PREFIX = ""; public static final boolean DEFAULT_SORT_KEYS = true; @@ -17,6 +20,8 @@ public class SettingsState { public static final boolean DEFAULT_CODE_ASSISTANCE = true; private String localesPath; + private FolderStrategy folderStrategy; + private ParserStrategy parserStrategy; private String filePattern; private String previewLocale; private String pathPrefix; @@ -34,6 +39,22 @@ public class SettingsState { this.localesPath = localesPath; } + public @NotNull FolderStrategy getFolderStrategy() { + return folderStrategy != null ? folderStrategy : DEFAULT_FOLDER_STRATEGY; + } + + public void setFolderStrategy(FolderStrategy folderStrategy) { + this.folderStrategy = folderStrategy; + } + + public @NotNull ParserStrategy getParserStrategy() { + return parserStrategy != null ? parserStrategy : DEFAULT_PARSER_STRATEGY; + } + + public void setParserStrategy(ParserStrategy parserStrategy) { + this.parserStrategy = parserStrategy; + } + public @NotNull String getFilePattern() { return filePattern != null ? filePattern : DEFAULT_FILE_PATTERN; } diff --git a/src/main/java/de/marhali/easyi18n/model/bus/ParserStrategy.java b/src/main/java/de/marhali/easyi18n/model/bus/ParserStrategy.java new file mode 100644 index 0000000..7abb802 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/model/bus/ParserStrategy.java @@ -0,0 +1,29 @@ +package de.marhali.easyi18n.model.bus; + +/** + * Represents all supported file parser strategies. + * @author marhali + */ +public enum ParserStrategy { + JSON, + YAML, + PROPERTIES; + + public int toIndex() { + int index = 0; + + for(ParserStrategy strategy : values()) { + if(strategy == this) { + return index; + } + + index++; + } + + throw new NullPointerException(); + } + + public static ParserStrategy fromIndex(int index) { + return values()[index]; + } +} diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index ead960a..27e10d9 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -14,10 +14,14 @@ translation.key=Key translation.locales=Locales settings.path.title=Locales Directory settings.path.text=Locales directory -settings.path.file-pattern=Translation file wildcard matcher -settings.path.file-pattern-tooltip=Defines a wildcard matcher to filter relevant translation files. For example *.json, *.???. -settings.path.prefix=Path prefix +settings.strategy.title=Specify your translation file structure +settings.strategy.folder=Single Directory;Modularized: Locale / Namespace;Modularized: Namespace / Locale +settings.strategy.folder.tooltip=What is the folder structure of your translation files? +settings.strategy.parser=JSON;YAML;Properties +settings.strategy.parser.tooltip=Which file parser should be used to process your translation files? +settings.strategy.file-pattern.tooltip=Defines a wildcard matcher to filter relevant translation files. For example *.json, *.???. settings.preview=Preview locale +settings.path.prefix=Path prefix settings.keys.sort=Sort translation keys alphabetically settings.keys.nested=Escape delimiter character within a section layer. settings.editor.assistance=I18n key completion, annotation and reference inside editor