add filter functionality for missing translation values

Resolves #74
This commit is contained in:
marhali 2022-01-13 16:00:08 +01:00
parent 0b9d6c0369
commit 7762b0cacd
10 changed files with 116 additions and 5 deletions

View File

@ -8,6 +8,7 @@
- I18n key nesting will now escape every delimiter within a section layer (can be inverted via option)
### Added
- Filter functionality for translations with missing values
- Full keyboard shortcut support inside tool-window
- Support for dots within key nodes in YAML files

View File

@ -43,7 +43,7 @@ public class DataBus {
}
@Override
public void onFocusKey(@Nullable KeyPath key) {
public void onFocusKey(@NotNull KeyPath key) {
listener.forEach(li -> li.onFocusKey(key));
}
@ -51,6 +51,11 @@ public class DataBus {
public void onSearchQuery(@Nullable String query) {
listener.forEach(li -> li.onSearchQuery(query));
}
@Override
public void onFilterMissingTranslations(boolean filter) {
listener.forEach(li -> li.onFilterMissingTranslations(filter));
}
};
}
}

View File

@ -0,0 +1,30 @@
package de.marhali.easyi18n.action;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import de.marhali.easyi18n.InstanceManager;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.ResourceBundle;
/**
* Action which toggles translation filter on missing values.
* @author marhali
*/
public class FilterMissingTranslationsAction extends AnAction {
public FilterMissingTranslationsAction() {
super(ResourceBundle.getBundle("messages").getString("action.toggle-missing"),
null, AllIcons.General.ShowWarning);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = Objects.requireNonNull(e.getProject());
boolean enable = e.getPresentation().getIcon() == AllIcons.General.ShowWarning;
e.getPresentation().setIcon(enable ? AllIcons.General.Warning : AllIcons.General.ShowWarning);
InstanceManager.get(project).bus().propagate().onFilterMissingTranslations(enable);
}
}

View File

@ -5,4 +5,5 @@ package de.marhali.easyi18n.model.bus;
* Every listener needs to be registered manually via {@link de.marhali.easyi18n.DataBus}.
* @author marhali
*/
public interface BusListener extends UpdateDataListener, FocusKeyListener, SearchQueryListener {}
public interface BusListener extends UpdateDataListener, FocusKeyListener,
SearchQueryListener, FilterMissingTranslationsListener {}

View File

@ -0,0 +1,13 @@
package de.marhali.easyi18n.model.bus;
/**
* Single event listener.
* @author marhali
*/
public interface FilterMissingTranslationsListener {
/**
* Toggles filter of missing translations
* @param filter True if only translations with missing values should bw shown
*/
void onFilterMissingTranslations(boolean filter);
}

View File

@ -46,6 +46,7 @@ public class TranslatorToolWindowFactory implements ToolWindowFactory, DumbAware
// ToolWindow Actions (Can be used for every view)
List<AnAction> actions = new ArrayList<>();
actions.add(new AddAction());
actions.add(new FilterMissingTranslationsAction());
actions.add(new ReloadAction());
actions.add(new SettingsAction());
actions.add(new SearchAction((query) -> InstanceManager.get(project).bus().propagate().onSearchQuery(query)));

View File

@ -107,6 +107,14 @@ public class TableView implements BusListener {
}
}
@Override
public void onFilterMissingTranslations(boolean filter) {
if(this.currentMapper != null) {
this.currentMapper.onFilterMissingTranslations(filter);
this.table.updateUI();
}
}
public JPanel getRootPanel() {
return rootPanel;
}

View File

@ -106,6 +106,15 @@ public class TreeView implements BusListener {
}
}
@Override
public void onFilterMissingTranslations(boolean filter) {
if (this.currentMapper != null) {
this.currentMapper.onFilterMissingTranslations(filter);
this.expandAll();
this.tree.updateUI();
}
}
private void showEditPopup(@Nullable TreePath path) {
if (path == null) {
return;

View File

@ -1,6 +1,7 @@
package de.marhali.easyi18n.tabs.mapper;
import de.marhali.easyi18n.model.*;
import de.marhali.easyi18n.model.bus.FilterMissingTranslationsListener;
import de.marhali.easyi18n.model.bus.SearchQueryListener;
import org.jetbrains.annotations.Nls;
@ -17,7 +18,7 @@ import java.util.function.Consumer;
* Mapping {@link TranslationData} to {@link TableModel}.
* @author marhali
*/
public class TableModelMapper implements TableModel, SearchQueryListener {
public class TableModelMapper implements TableModel, SearchQueryListener, FilterMissingTranslationsListener {
private final @NotNull TranslationData data;
private final @NotNull KeyPathConverter converter;
@ -63,6 +64,24 @@ public class TableModelMapper implements TableModel, SearchQueryListener {
this.fullKeys = matches;
}
@Override
public void onFilterMissingTranslations(boolean filter) {
if(!filter) { // Reset
this.fullKeys = new ArrayList<>(this.data.getFullKeys());
return;
}
List<KeyPath> matches = new ArrayList<>();
for(KeyPath key : this.data.getFullKeys()) {
if(this.data.getTranslation(key).values().size() != this.locales.size()) {
matches.add(key);
}
}
this.fullKeys = matches;
}
@Override
public int getRowCount() {
return this.fullKeys.size();

View File

@ -4,6 +4,7 @@ import com.intellij.ide.projectView.PresentationData;
import com.intellij.ui.JBColor;
import de.marhali.easyi18n.model.*;
import de.marhali.easyi18n.model.bus.FilterMissingTranslationsListener;
import de.marhali.easyi18n.model.bus.SearchQueryListener;
import de.marhali.easyi18n.util.UiUtil;
@ -19,7 +20,7 @@ import java.util.Map;
* Mapping {@link TranslationData} to {@link TreeModel}.
* @author marhali
*/
public class TreeModelMapper extends DefaultTreeModel implements SearchQueryListener {
public class TreeModelMapper extends DefaultTreeModel implements SearchQueryListener, FilterMissingTranslationsListener {
private final TranslationData data;
private final KeyPathConverter converter;
@ -42,7 +43,7 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
TranslationData shadow = new TranslationData(this.state.isSortKeys());
if(query == null) {
if(query == null) { // Reset
this.generateNodes(rootNode, this.data.getRootNode());
super.setRoot(rootNode);
return;
@ -71,6 +72,29 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
super.setRoot(rootNode);
}
@Override
public void onFilterMissingTranslations(boolean filter) {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
TranslationData shadow = new TranslationData(this.state.isSortKeys());
if(!filter) { // Reset
this.generateNodes(rootNode, this.data.getRootNode());
super.setRoot(rootNode);
return;
}
for(KeyPath currentKey : this.data.getFullKeys()) {
Translation translation = this.data.getTranslation(currentKey);
if(translation.values().size() != this.data.getLocales().size()) {
shadow.setTranslation(currentKey, translation);
}
}
this.generateNodes(rootNode, shadow.getRootNode());
super.setRoot(rootNode);
}
/**
*
* @param parent Parent tree node