extend plugin settings to include key sorting and nesting

This commit is contained in:
Marcel Haßlinger 2021-11-01 14:38:48 +01:00
parent b3cb14c4c8
commit de88d0d96c
3 changed files with 52 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.service.SettingsService;
import de.marhali.easyi18n.service.DataStore;
@ -28,6 +29,8 @@ public class SettingsDialog {
private JBTextField filePatternText;
private JBTextField previewLocaleText;
private JBTextField pathPrefixText;
private JBCheckBox sortKeysCheckbox;
private JBCheckBox nestedKeysCheckbox;
private JBCheckBox codeAssistanceCheckbox;
public SettingsDialog(Project project) {
@ -35,30 +38,28 @@ public class SettingsDialog {
}
public void showAndHandle() {
String localesPath = SettingsService.getInstance(project).getState().getLocalesPath();
String filePattern = SettingsService.getInstance(project).getState().getFilePattern();
String previewLocale = SettingsService.getInstance(project).getState().getPreviewLocale();
String pathPrefix = SettingsService.getInstance(project).getState().getPathPrefix();
boolean codeAssistance = SettingsService.getInstance(project).getState().isCodeAssistance();
SettingsState state = SettingsService.getInstance(project).getState();
if(prepare(localesPath, filePattern, previewLocale, pathPrefix, codeAssistance).show() == DialogWrapper.OK_EXIT_CODE) { // Save changes
SettingsService.getInstance(project).getState().setLocalesPath(pathText.getText());
SettingsService.getInstance(project).getState().setFilePattern(filePatternText.getText());
SettingsService.getInstance(project).getState().setPreviewLocale(previewLocaleText.getText());
SettingsService.getInstance(project).getState().setCodeAssistance(codeAssistanceCheckbox.isSelected());
SettingsService.getInstance(project).getState().setPathPrefix(pathPrefixText.getText());
if(prepare(state).show() == DialogWrapper.OK_EXIT_CODE) { // Save changes
state.setLocalesPath(pathText.getText());
state.setFilePattern(filePatternText.getText());
state.setPreviewLocale(previewLocaleText.getText());
state.setPathPrefix(pathPrefixText.getText());
state.setSortKeys(sortKeysCheckbox.isSelected());
state.setNestedKeys(nestedKeysCheckbox.isSelected());
state.setCodeAssistance(codeAssistanceCheckbox.isSelected());
// Reload instance
DataStore.getInstance(project).reloadFromDisk();
}
}
private DialogBuilder prepare(String localesPath, String filePattern, String previewLocale, String pathPrefix, boolean codeAssistance) {
private DialogBuilder prepare(SettingsState state) {
JPanel rootPanel = new JPanel(new GridLayout(0, 1, 2, 2));
/* path */
JBLabel pathLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.text"));
pathText = new TextFieldWithBrowseButton(new JTextField(localesPath));
pathText = new TextFieldWithBrowseButton(new JTextField(state.getLocalesPath()));
pathLabel.setLabelFor(pathText);
pathText.addBrowseFolderListener(ResourceBundle.getBundle("messages").getString("settings.path.title"), null, project, new FileChooserDescriptor(
@ -69,14 +70,14 @@ public class SettingsDialog {
/* file pattern */
JBLabel filePatternLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.file-pattern"));
filePatternText = new JBTextField(filePattern);
filePatternText = new JBTextField(state.getFilePattern());
rootPanel.add(filePatternLabel);
rootPanel.add(filePatternText);
/* preview locale */
JBLabel previewLocaleLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.preview"));
previewLocaleText = new JBTextField(previewLocale);
previewLocaleText = new JBTextField(state.getPreviewLocale());
previewLocaleLabel.setLabelFor(previewLocaleText);
rootPanel.add(previewLocaleLabel);
@ -84,14 +85,26 @@ public class SettingsDialog {
/* path prefix */
JBLabel pathPrefixLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.prefix"));
pathPrefixText = new JBTextField(pathPrefix);
pathPrefixText = new JBTextField(state.getPathPrefix());
rootPanel.add(pathPrefixLabel);
rootPanel.add(pathPrefixText);
/* sort keys */
sortKeysCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.keys.sort"));
sortKeysCheckbox.setSelected(state.isSortKeys());
rootPanel.add(sortKeysCheckbox);
/* nested keys */
nestedKeysCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.keys.nested"));
nestedKeysCheckbox.setSelected(state.isNestedKeys());
rootPanel.add(nestedKeysCheckbox);
/* code assistance */
codeAssistanceCheckbox = new JBCheckBox(ResourceBundle.getBundle("messages").getString("settings.editor.assistance"));
codeAssistanceCheckbox.setSelected(codeAssistance);
codeAssistanceCheckbox.setSelected(state.isCodeAssistance());
rootPanel.add(codeAssistanceCheckbox);

View File

@ -12,12 +12,16 @@ public class SettingsState {
public static final String DEFAULT_PREVIEW_LOCALE = "en";
public static final String DEFAULT_FILE_PATTERN = ".*";
public static final String DEFAULT_PATH_PREFIX = "";
public static final boolean DEFAULT_SORT_KEYS = true;
public static final boolean DEFAULT_NESTED_KEYS = true;
public static final boolean DEFAULT_CODE_ASSISTANCE = true;
private String localesPath;
private String filePattern;
private String previewLocale;
private String pathPrefix;
private Boolean sortKeys;
private Boolean nestedKeys;
private Boolean codeAssistance;
public SettingsState() {}
@ -54,6 +58,22 @@ public class SettingsState {
this.pathPrefix = pathPrefix;
}
public boolean isSortKeys() {
return sortKeys == null ? DEFAULT_SORT_KEYS : sortKeys;
}
public void setSortKeys(boolean sortKeys) {
this.sortKeys = sortKeys;
}
public boolean isNestedKeys() {
return nestedKeys == null ? DEFAULT_NESTED_KEYS : nestedKeys;
}
public void setNestedKeys(boolean nestedKeys) {
this.nestedKeys = nestedKeys;
}
public boolean isCodeAssistance() {
return codeAssistance == null ? DEFAULT_CODE_ASSISTANCE : codeAssistance;
}

View File

@ -16,4 +16,6 @@ settings.path.text=Locales directory
settings.path.file-pattern=Translation file pattern
settings.path.prefix=Path prefix
settings.preview=Preview locale
settings.keys.sort=Sort translation keys alphabetically
settings.keys.nested=Nest translation keys if possible
settings.editor.assistance=I18n key completion, annotation and reference inside editor