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

Commit

Permalink
Added new way projects are loaded/saved to support saving without
Browse files Browse the repository at this point in the history
directories for JSON/ES6 files and to support custom file naming. #28
#39
  • Loading branch information
Jacob van Mourik committed Jun 20, 2018
1 parent a6e2e10 commit 35ea5da
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 125 deletions.
27 changes: 26 additions & 1 deletion src/main/java/com/jvms/i18neditor/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class Resource {
private final ResourceType type;
private final List<ResourceListener> listeners = Lists.newLinkedList();
private SortedMap<String,String> translations = Maps.newTreeMap();

private String checksum;

/**
* See {@link #Resource(ResourceType, Path, Locale)}.
*/
Expand Down Expand Up @@ -95,6 +96,11 @@ public SortedMap<String,String> getTranslations() {
return ImmutableSortedMap.copyOf(translations);
}

/**
* Sets the translations of the resource.
*
* @param translations the translations
*/
public void setTranslations(SortedMap<String,String> translations) {
this.translations = translations;
}
Expand Down Expand Up @@ -199,6 +205,25 @@ public void removeListener(ResourceListener listener) {
listeners.remove(listener);
}

/**
* Gets the checksum of the resource's file.
* This method only returns the checksum set via {@link #setChecksum(checksum)}.
*
* @return the checksum.
*/
public String getChecksum() {
return checksum;
}

/**
* Sets the checksum of the resource's file.
*
* @param checksum the checksum to set.
*/
public void setChecksum(String checksum) {
this.checksum = checksum;
}

private void duplicateTranslation(String key, String newKey, boolean keepOld) {
Map<String,String> newTranslations = Maps.newTreeMap();
translations.keySet().forEach(k -> {
Expand Down
19 changes: 4 additions & 15 deletions src/main/java/com/jvms/i18neditor/ResourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
* @author Jacob van Mourik
*/
public enum ResourceType {
JSON(".json", false),
ES6(".js", false),
Properties(".properties", true);
JSON(".json"),
ES6(".js"),
Properties(".properties");

private final String extension;
private final boolean embedLocale;

/**
* Gets the file extension of the resource type.
Expand All @@ -24,17 +23,7 @@ public String getExtension() {
return extension;
}

/**
* Whether the locale should be embedded in the filename for this resource type.
*
* @return whether the locale should be embedded in the filename.
*/
public boolean isEmbedLocale() {
return embedLocale;
}

private ResourceType(String extension, boolean embedLocale) {
private ResourceType(String extension) {
this.extension = extension;
this.embedLocale = embedLocale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @author Jacob van Mourik
*/
public abstract class AbstractSettingsPane extends JPanel {
private final static long serialVersionUID = -8953194193840198893L;
private GridBagConstraints vGridBagConstraints;

protected AbstractSettingsPane() {
Expand All @@ -24,11 +25,16 @@ protected AbstractSettingsPane() {
vGridBagConstraints.weightx = 1;
}

protected GridBagConstraints createVerticalGridBagConstraints() {
protected GridBagConstraints createVerticalGridBagConstraints(int weigthy) {
vGridBagConstraints.gridy = (vGridBagConstraints.gridy + 1) % Integer.MAX_VALUE;
vGridBagConstraints.weighty = weigthy;
return vGridBagConstraints;
}

protected GridBagConstraints createVerticalGridBagConstraints() {
return createVerticalGridBagConstraints(1);
}

protected JPanel createFieldset(String title) {
JPanel fieldset = new JPanel(new GridBagLayout());
fieldset.setBorder(BorderFactory.createCompoundBorder(
Expand Down
58 changes: 38 additions & 20 deletions src/main/java/com/jvms/i18neditor/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.tree.TreePath;

import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jvms.i18neditor.Resource;
Expand All @@ -70,6 +70,7 @@
import com.jvms.i18neditor.util.GithubRepoUtil;
import com.jvms.i18neditor.util.GithubRepoUtil.GithubRepoReleaseData;
import com.jvms.i18neditor.util.Images;
import com.jvms.i18neditor.util.Locales;
import com.jvms.i18neditor.util.MessageBundle;
import com.jvms.i18neditor.util.ResourceKeys;
import com.jvms.i18neditor.util.Resources;
Expand All @@ -86,10 +87,10 @@ public class Editor extends JFrame {
public final static String TITLE = "i18n-editor";
public final static String VERSION = "1.0.0";
public final static String GITHUB_REPO = "jcbvm/i18n-editor";
public final static String DEFAULT_RESOURCE_NAME = "translations";
public final static String PROJECT_FILE = ".i18n-editor-metadata";
public final static String SETTINGS_FILE = ".i18n-editor";
public final static String SETTINGS_DIR = System.getProperty("user.home");
public final static String DEFAULT_RESOURCE_DEFINITION = "translations{_LOCALE}";

private EditorProject project;
private EditorSettings settings = new EditorSettings();
Expand Down Expand Up @@ -121,7 +122,8 @@ public void createProject(Path dir, ResourceType type) {
return;
}

List<Resource> resourceList = Resources.get(dir, settings.getResourceName(), Optional.empty());
List<Resource> resourceList = Resources.get(dir,
settings.getResourceFileDefinition(), settings.isUseResourceDirectories(), Optional.of(type));
if (!resourceList.isEmpty()) {
boolean importProject = Dialogs.showConfirmDialog(this,
MessageBundle.get("dialogs.project.new.conflict.title"),
Expand All @@ -138,8 +140,9 @@ public void createProject(Path dir, ResourceType type) {
restoreProjectState(project);
project.setResourceType(type);

if (type == ResourceType.Properties) {
Resource resource = Resources.create(dir, type, Optional.empty(), project.getResourceName());
if (!project.isUseResourceDirectories()) {
Resource resource = Resources.create(type, dir,
project.getResourceFileDefinition(), false, Optional.empty());
setupResource(resource);
project.addResource(resource);
} else {
Expand Down Expand Up @@ -169,7 +172,8 @@ public void importProject(Path dir, boolean showEmptyProjectError) {
restoreProjectState(project);

Optional<ResourceType> type = Optional.ofNullable(project.getResourceType());
List<Resource> resourceList = Resources.get(dir, project.getResourceName(), type);
List<Resource> resourceList = Resources.get(dir,
project.getResourceFileDefinition(), project.isUseResourceDirectories(), type);
Map<String,String> keys = Maps.newTreeMap();

if (resourceList.isEmpty()) {
Expand Down Expand Up @@ -371,13 +375,13 @@ public void showAddLocaleDialog() {
MessageBundle.get("dialogs.locale.add.text"),
JOptionPane.QUESTION_MESSAGE);
if (localeString != null) {
localeString = localeString.trim();
if (localeString.isEmpty()) {
Locale locale = Locales.parseLocale(localeString.trim());
if (locale == null) {
showError(MessageBundle.get("dialogs.locale.add.error.invalid"));
} else {
try {
Locale locale = LocaleUtils.toLocale(localeString);
Resource resource = Resources.create(path, type, Optional.of(locale), project.getResourceName());
Resource resource = Resources.create(type, path,
project.getResourceFileDefinition(), project.isUseResourceDirectories(), Optional.of(locale));
addResource(resource);
} catch (IOException e) {
log.error("Error creating new locale", e);
Expand Down Expand Up @@ -829,6 +833,7 @@ private void showError(String message) {
}

private void updateTreeNodeStatuses() {
if (project == null) return;
Set<String> keys = project.getResources().stream()
.flatMap(r -> r.getTranslations().keySet().stream())
.filter(key -> project.getResources().stream().anyMatch(r -> !r.hasTranslation(key)))
Expand All @@ -837,6 +842,7 @@ private void updateTreeNodeStatuses() {
}

private void updateTreeNodeStatus(String key) {
if (project == null) return;
boolean hasError = project.getResources().stream().anyMatch(r -> !r.hasTranslation(key));
translationTree.updateNode(key, hasError);
}
Expand All @@ -845,8 +851,9 @@ private void storeProjectState() {
ExtendedProperties props = new ExtendedProperties();
props.setProperty("minify_resources", project.isMinifyResources());
props.setProperty("plain_json", project.isPlainJSON());
props.setProperty("resource_name", project.getResourceName());
props.setProperty("resource_type", project.getResourceType().toString());
props.setProperty("resource_definition", project.getResourceFileDefinition());
props.setProperty("resource_directories", project.isUseResourceDirectories());
props.store(Paths.get(project.getPath().toString(), PROJECT_FILE));
}

Expand All @@ -857,11 +864,20 @@ private void restoreProjectState(EditorProject project) {
props.load(Paths.get(project.getPath().toString(), PROJECT_FILE));
project.setMinifyResources(props.getBooleanProperty("minify_resources", settings.isMinifyResources()));
project.setPlainJSON(props.getBooleanProperty("plain_json", settings.isPlainJSON()));
project.setResourceName(props.getProperty("resource_name", settings.getResourceName()));
project.setResourceType(props.getEnumProperty("resource_type", ResourceType.class));
String resourceName = props.getProperty("resource_name"); // for backwards compatibility
if (Strings.isNullOrEmpty(resourceName)) {
project.setResourceFileDefinition(props.getProperty("resource_definition", settings.getResourceFileDefinition()));
project.setUseResourceDirectories(props.getBooleanProperty("resource_directories", settings.isUseResourceDirectories()));
} else {
project.setResourceFileDefinition(resourceName);
project.setUseResourceDirectories(true);
}
} else {
project.setResourceName(settings.getResourceName());
project.setMinifyResources(settings.isMinifyResources());
project.setPlainJSON(settings.isPlainJSON());
project.setResourceFileDefinition(settings.getResourceFileDefinition());
project.setUseResourceDirectories(settings.isUseResourceDirectories());
}
}

Expand All @@ -874,7 +890,8 @@ private void storeEditorState() {
props.setProperty("window_div_pos", contentPane.getDividerLocation());
props.setProperty("minify_resources", settings.isMinifyResources());
props.setProperty("plain_json", settings.isPlainJSON());
props.setProperty("resource_name", settings.getResourceName());
props.setProperty("resource_definition", settings.getResourceFileDefinition());
props.setProperty("resource_directories", settings.isUseResourceDirectories());
props.setProperty("check_version", settings.isCheckVersionOnStartup());
props.setProperty("default_input_height", settings.getDefaultInputHeight());
props.setProperty("key_field_enabled", settings.isKeyFieldEnabled());
Expand Down Expand Up @@ -903,16 +920,17 @@ private void restoreEditorState() {
settings.setWindowPositionX(props.getIntegerProperty("window_pos_x", 0));
settings.setWindowPositionY(props.getIntegerProperty("window_pos_y", 0));
settings.setWindowDeviderPosition(props.getIntegerProperty("window_div_pos", 250));
settings.setHistory(props.getListProperty("history"));
settings.setLastExpandedNodes(props.getListProperty("last_expanded"));
settings.setLastSelectedNode(props.getProperty("last_selected"));
settings.setMinifyResources(props.getBooleanProperty("minify_resources", false));
settings.setPlainJSON(props.getBooleanProperty("plain_json", false));
settings.setResourceName(props.getProperty("resource_name", DEFAULT_RESOURCE_NAME));
settings.setCheckVersionOnStartup(props.getBooleanProperty("check_version", true));
settings.setDefaultInputHeight(props.getIntegerProperty("default_input_height", 5));
settings.setKeyFieldEnabled(props.getBooleanProperty("key_field_enabled", true));
settings.setDoubleClickTreeToggling(props.getBooleanProperty("double_click_tree_toggling", false));
settings.setMinifyResources(props.getBooleanProperty("minify_resources", false));
settings.setPlainJSON(props.getBooleanProperty("plain_json", false));
settings.setHistory(props.getListProperty("history"));
settings.setLastExpandedNodes(props.getListProperty("last_expanded"));
settings.setLastSelectedNode(props.getProperty("last_selected"));
settings.setResourceFileDefinition(props.getProperty("resource_definition", DEFAULT_RESOURCE_DEFINITION));
settings.setUseResourceDirectories(props.getBooleanProperty("resource_directories", false));
}

private class TranslationTreeMouseListener extends MouseAdapter {
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/jvms/i18neditor/editor/EditorProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*/
public class EditorProject {
private Path path;
private String resourceName;
private String resourceFileDefinition;
private ResourceType resourceType;
private List<Resource> resources = Lists.newLinkedList();
private List<Resource> resources;
private boolean minifyResources;
private boolean plainJSON;
private boolean resourceDirectories;

public EditorProject(Path path) {
this.path = path;
this.resources = Lists.newLinkedList();
}

public Path getPath() {
Expand Down Expand Up @@ -56,13 +58,21 @@ public void addResource(Resource resource) {
public boolean hasResources() {
return !resources.isEmpty();
}

public boolean isUseResourceDirectories() {
return resourceDirectories;
}

public String getResourceName() {
return resourceName;
public void setUseResourceDirectories(boolean resourceDirectories) {
this.resourceDirectories = resourceDirectories;
}

public String getResourceFileDefinition() {
return resourceFileDefinition;
}

public void setResourceName(String resourceFilename) {
this.resourceName = resourceFilename;
public void setResourceFileDefinition(String resourceFileDefinition) {
this.resourceFileDefinition = resourceFileDefinition;
}

public boolean isMinifyResources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.swing.JPanel;

import com.jvms.i18neditor.ResourceType;
import com.jvms.i18neditor.swing.JHelpLabel;
import com.jvms.i18neditor.swing.JTextField;
import com.jvms.i18neditor.util.MessageBundle;

Expand All @@ -29,7 +30,6 @@ public EditorProjectSettingsPane(Editor editor) {
}

private void setupUI() {
EditorSettings settings = editor.getSettings();
EditorProject project = editor.getProject();

// General settings
Expand All @@ -41,25 +41,35 @@ private void setupUI() {
minifyBox.addChangeListener(e -> project.setMinifyResources(minifyBox.isSelected()));
fieldset1.add(minifyBox, createVerticalGridBagConstraints());
}

if (project.getResourceType().equals(ResourceType.JSON)) {
JCheckBox plainJSONBox = new JCheckBox(MessageBundle.get("settings.plainJSON.title"));
plainJSONBox.setSelected(project.isPlainJSON());
plainJSONBox.addChangeListener(e -> project.setPlainJSON(plainJSONBox.isSelected()));
fieldset1.add(plainJSONBox, createVerticalGridBagConstraints());
}
JPanel resourcePanel = new JPanel(new GridLayout(0, 1));
JLabel resourceNameLabel = new JLabel(MessageBundle.get("settings.resourcename.title"));
JTextField resourceNameField = new JTextField(project.getResourceName());
resourceNameField.addKeyListener(new KeyAdapter() {

JCheckBox useResourceDirsBox = new JCheckBox(MessageBundle.get("settings.resourcedirs.title"));
useResourceDirsBox.setSelected(project.isUseResourceDirectories());
useResourceDirsBox.addChangeListener(e -> project.setUseResourceDirectories(useResourceDirsBox.isSelected()));
fieldset1.add(useResourceDirsBox, createVerticalGridBagConstraints());

JPanel resourceDefinitionPanel = new JPanel(new GridLayout(0, 1));
JLabel resourceDefinitionLabel = new JLabel(MessageBundle.get("settings.resourcedef.title"));
JTextField resourceDefinitionField = new JTextField(project.getResourceFileDefinition());
resourceDefinitionField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
String value = resourceNameField.getText().trim();
project.setResourceName(value.isEmpty() ? settings.getResourceName() : value);
String value = resourceDefinitionField.getText().trim();
project.setResourceFileDefinition(value.isEmpty() ? Editor.DEFAULT_RESOURCE_DEFINITION : value);
}
});
resourcePanel.add(resourceNameLabel);
resourcePanel.add(resourceNameField);
fieldset1.add(resourcePanel, createVerticalGridBagConstraints());
resourceDefinitionPanel.add(resourceDefinitionLabel);
resourceDefinitionPanel.add(resourceDefinitionField);
fieldset1.add(resourceDefinitionPanel, createVerticalGridBagConstraints());

JHelpLabel resourceDefinitionHelpLabel = new JHelpLabel(MessageBundle.get("settings.resourcedef.help"));
fieldset1.add(resourceDefinitionHelpLabel, createVerticalGridBagConstraints(3));

setLayout(new GridBagLayout());
add(fieldset1, createVerticalGridBagConstraints());
Expand Down
Loading

0 comments on commit 35ea5da

Please sign in to comment.