Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Added undo / redo functionality #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 94 additions & 65 deletions src/main/java/com/jvms/i18neditor/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
import com.jvms.i18neditor.util.ResourceKeys;

/**
* A resource is a container for storing i18n data and is defined by the following properties:
* A resource is a container for storing i18n data and is defined by the
* following properties:
*
* <ul>
* <li>{@code type} the type of the resource, either {@code JSON} or {@code ES6}.</li>
* <li>{@code path} the path to the resource file on disk.</li>
* <li>{@code type} the type of the resource, either {@code JSON} or
* {@code ES6}.</li>
* <li>{@code path} the path to the resource file on disk.</li>
* <li>{@code locale} the locale of the resource.</li>
* <li>{@code translations} a sorted map containing the translations of the resource by key value pair.</li>
* <li>{@code translations} a sorted map containing the translations of the
* resource by key value pair.</li>
* </ul>
*
* <p>Objects can listen to a resource by adding a {@link ResourceListener} which
* will be called when any change is made to the {@code translations}.</p>
* <p>
* Objects can listen to a resource by adding a {@link ResourceListener} which
* will be called when any change is made to the {@code translations}.
* </p>
*
* @author Jacob van Mourik
*/
Expand All @@ -33,104 +38,119 @@ public class Resource {
private final Locale locale;
private final ResourceType type;
private final List<ResourceListener> listeners = Lists.newLinkedList();
private SortedMap<String,String> translations = Maps.newTreeMap();
private SortedMap<String, String> translations = Maps.newTreeMap();

/**
* See {@link #Resource(ResourceType, Path, Locale)}.
*/
public Resource(ResourceType type, Path path) {
this(type, path, null);
}

/**
* Creates a new instance of a resource.
*
* @param type the type of the resource.
* @param path the path to the file on disk.
* @param locale the locale of the translations.
* @param type
* the type of the resource.
* @param path
* the path to the file on disk.
* @param locale
* the locale of the translations.
*/
public Resource(ResourceType type, Path path, Locale locale) {
this.path = path;
this.locale = locale;
this.type = type;
}

/**
* Gets the type of the resource.
*
* @return the type.
* @return the type.
*/
public ResourceType getType() {
return type;
}

/**
* Gets the path to the resource file on disk.
*
* @return the path.
* @return the path.
*/
public Path getPath() {
return path;
}

/**
* Gets the locale of the translations of the resource.
*
* @return the locale of the resource, may be {@code null}.
* @return the locale of the resource, may be {@code null}.
*/
public Locale getLocale() {
return locale;
}

/**
* Gets a map of the translations of the resource.
*
* <p>The returned map is an immutable sorted map. Modifications to the translations should be done via
* {@link #storeTranslation(String, String)}, {@link #removeTranslation(String)} or
* {@link #renameTranslation(String, String)}.</p>
* <p>
* The returned map is an immutable sorted map. Modifications to the
* translations should be done via
* {@link #storeTranslation(String, String)},
* {@link #removeTranslation(String)} or
* {@link #renameTranslation(String, String)}.
* </p>
*
* @return the translations of the resource.
* @return the translations of the resource.
*/
public SortedMap<String,String> getTranslations() {
public SortedMap<String, String> getTranslations() {
return ImmutableSortedMap.copyOf(translations);
}
public void setTranslations(SortedMap<String,String> translations) {

public void setTranslations(SortedMap<String, String> translations) {
this.translations = translations;
}

public boolean hasTranslation(String key) {
return !Strings.isNullOrEmpty(translations.get(key));
}

/**
* Gets a translation from the resource's translations.
*
* @param key the key of the translation to get.
* @return value of the translation or {@code null} if there is no translation for the given key.
* @param key
* the key of the translation to get.
* @return value of the translation or {@code null} if there is no
* translation for the given key.
*/
public String getTranslation(String key) {
return translations.get(key);
}

/**
* Stores a translation to the resource's translations.
*
* <ul>
* <li>If the given key does not exists yet and is not empty, a new translation will be added to the map.</li>
* <li>If the given key already exists, the existing value will be overwritten with the given value.</li>
* <li>If the given key is a parent key of any existing keys, the existing child keys will be removed.</li>
* <li>If the given key is a child key of any existing keys, the existing parent keys will be removed.</li>
* <li>If the given key does not exists yet and is not empty, a new
* translation will be added to the map.</li>
* <li>If the given key already exists, the existing value will be
* overwritten with the given value.</li>
* <li>If the given key is a parent key of any existing keys, the existing
* child keys will be removed.</li>
* <li>If the given key is a child key of any existing keys, the existing
* parent keys will be removed.</li>
* </ul>
*
* @param key the key of the translation to add.
* @param value the value of the translation to add corresponding the given key.
* @param key
* the key of the translation to add.
* @param value
* the value of the translation to add corresponding the given
* key.
*/
public void storeTranslation(String key, String value) {
checkKey(key);
String existing = translations.get(key);
if (existing == null && value.isEmpty() ||
existing != null && existing.equals(value)) {
if (existing == null && value.isEmpty() || existing != null && existing.equals(value)) {
return;
}
removeParents(key);
Expand All @@ -142,65 +162,74 @@ public void storeTranslation(String key, String value) {
}
notifyListeners();
}

/**
* Removes a translation from the resource's translations.
* Any child keys of the given key will also be removed.
* Removes a translation from the resource's translations. Any child keys of
* the given key will also be removed.
*
* @param key the key of the translation to remove.
* @param key
* the key of the translation to remove.
*/
public void removeTranslation(String key) {
removeChildren(key);
translations.remove(key);
notifyListeners();
}

/**
* Renames a translation key in the resource's translations.
* Any existing child keys of the translation key will also be renamed.
* Renames a translation key in the resource's translations. Any existing
* child keys of the translation key will also be renamed.
*
* @param key the old key of the translation to rename.
* @param newKey the new key.
* @param key
* the old key of the translation to rename.
* @param newKey
* the new key.
*/
public void renameTranslation(String key, String newKey) {
checkKey(newKey);
duplicateTranslation(key, newKey, false);
notifyListeners();
}

/**
* Duplicates a translation key, and any child keys, in the resource's translations.
* Duplicates a translation key, and any child keys, in the resource's
* translations.
*
* @param key the key of the translation to duplicate.
* @param newKey the new key.
* @param key
* the key of the translation to duplicate.
* @param newKey
* the new key.
*/
public void duplicateTranslation(String key, String newKey) {
checkKey(newKey);
duplicateTranslation(key, newKey, true);
notifyListeners();
}

/**
* Adds a listener to the resource. The listener will be called whenever there is made
* a change to the translations of the resource.
* Adds a listener to the resource. The listener will be called whenever
* there is made a change to the translations of the resource.
*
* @param listener the listener to add.
* @param listener
* the listener to add.
*/
public void addListener(ResourceListener listener) {
listeners.add(listener);
}

/**
* Removes a listener from the resource previously added by {@link #addListener(ResourceListener)}.
* Removes a listener from the resource previously added by
* {@link #addListener(ResourceListener)}.
*
* @param listener the listener to remove.
* @param listener
* the listener to remove.
*/
public void removeListener(ResourceListener listener) {
listeners.remove(listener);
}

private void duplicateTranslation(String key, String newKey, boolean keepOld) {
Map<String,String> newTranslations = Maps.newTreeMap();
Map<String, String> newTranslations = Maps.newTreeMap();
translations.keySet().forEach(k -> {
if (ResourceKeys.isChildKeyOf(k, key)) {
String nk = ResourceKeys.create(newKey, ResourceKeys.childKey(k, key));
Expand All @@ -216,27 +245,27 @@ private void duplicateTranslation(String key, String newKey, boolean keepOld) {
}
newTranslations.forEach(this::storeTranslation);
}

private void removeChildren(String key) {
Lists.newLinkedList(translations.keySet()).forEach(k -> {
if (ResourceKeys.isChildKeyOf(k, key)) {
translations.remove(k);
}
});
}

private void removeParents(String key) {
Lists.newLinkedList(translations.keySet()).forEach(k -> {
if (ResourceKeys.isChildKeyOf(key, k)) {
translations.remove(k);
}
});
}

private void notifyListeners() {
listeners.forEach(l -> l.resourceChanged(new ResourceEvent(this)));
}

private void checkKey(String key) {
Preconditions.checkArgument(ResourceKeys.isValid(key), "Key is not valid.");
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/jvms/i18neditor/editor/ActionTranslation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jvms.i18neditor.editor;

/**
* This class represents the action save in Undo/Redo
*
* @author Maximiliano Diaz
*/
public abstract class ActionTranslation implements ActionTranslationInterface {
protected String key;

public ActionTranslation(String key) {
this.key = key;
}

public String getkey() {
return key;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jvms.i18neditor.editor;

import java.util.Map;

import com.jvms.i18neditor.Resource;

/**
* This interface represents the methods importants that execute the actions
*
* @author Maximiliano Diaz
*/
public interface ActionTranslationInterface {

public void execute(Editor editor);

public void setTranslationMap(Map<Resource, String> map);
}
30 changes: 30 additions & 0 deletions src/main/java/com/jvms/i18neditor/editor/AddActionTranslation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jvms.i18neditor.editor;

import java.util.Map;

import com.jvms.i18neditor.Resource;

/**
* This class represents the action save in Undo/Redo when RemoveTranslationKey
*
* @author Maximiliano Diaz
*/
public class AddActionTranslation extends ActionTranslation {

public AddActionTranslation(String key) {
super(key);
}

@Override
public void execute(Editor editor) {
editor.removeTranslationKey(key);
editor.setNodeInFocus("");

}

@Override
public void setTranslationMap(Map<Resource, String> map) {
// TODO Auto-generated method stub

}
}
Loading