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

Commit

Permalink
Lots of improvements. #16 #17 #18 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob van Mourik committed Feb 7, 2017
1 parent e72858f commit aff553e
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/jvms/i18neditor/LookAndFeel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
* @author Jacob
*/
public class LookAndFeel {
public final static int TEXT_FIELD_COLS = 25;
public final static Color TEXTFIELD_BORDER_COLOR = Color.LIGHT_GRAY;
public final static Color BORDER_COLOR = Color.LIGHT_GRAY;
public final static Color PRIMARY_COLOR = new Color(83, 125, 157);
}
1 change: 0 additions & 1 deletion src/main/java/com/jvms/i18neditor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void main(String[] args) {
if (!SystemUtils.IS_OS_LINUX) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.put("Tree.drawDashedFocusIndicator", false);
} catch (Exception e) {
//
}
Expand Down
151 changes: 108 additions & 43 deletions src/main/java/com/jvms/i18neditor/editor/Editor.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.jvms.i18neditor.editor;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
Expand Down Expand Up @@ -102,6 +104,7 @@ public Editor() {
super();
setupUI();
setupFileDrop();
setupGlobalKeyEventDispatcher();
}

public void createProject(Path dir, ResourceType type) {
Expand Down Expand Up @@ -372,7 +375,7 @@ public void showRenameTranslationDialog(String key) {
boolean isReplace = newNode.isLeaf() || oldNode.isLeaf();
boolean confirm = Dialogs.showConfirmDialog(this,
MessageBundle.get("dialogs.translation.conflict.title"),
MessageBundle.get("dialogs.translation.conflict.text." + (isReplace ? "replace" : "merge")),
MessageBundle.get("dialogs.translation.conflict.text." + (isReplace?"replace":"merge")),
JOptionPane.WARNING_MESSAGE);
if (confirm) {
renameTranslationKey(key, newKey);
Expand Down Expand Up @@ -403,7 +406,7 @@ public void showDuplicateTranslationDialog(String key) {
boolean isReplace = newNode.isLeaf() || oldNode.isLeaf();
boolean confirm = Dialogs.showConfirmDialog(this,
MessageBundle.get("dialogs.translation.conflict.title"),
MessageBundle.get("dialogs.translation.conflict.text." + (isReplace ? "replace" : "merge")),
MessageBundle.get("dialogs.translation.conflict.text." + (isReplace?"replace":"merge")),
JOptionPane.WARNING_MESSAGE);
if (confirm) {
duplicateTranslationKey(key, newKey);
Expand Down Expand Up @@ -560,6 +563,47 @@ public void launch() {
}
}

public void updateUI() {
TranslationTreeNode selectedNode = translationTree.getSelectedNode();

resourcesPanel.removeAll();
resourceFields = resourceFields.stream().sorted().collect(Collectors.toList());
resourceFields.forEach(field -> {
Locale locale = field.getResource().getLocale();
String label = locale != null ? locale.getDisplayName() : MessageBundle.get("resources.locale.default");
field.setEnabled(selectedNode != null && selectedNode.isEditable());
field.setRows(settings.getInputHeight());
resourcesPanel.add(Box.createVerticalStrut(5));
resourcesPanel.add(new JLabel(label));
resourcesPanel.add(Box.createVerticalStrut(5));
resourcesPanel.add(field);
resourcesPanel.add(Box.createVerticalStrut(10));
});

Container container = getContentPane();
if (project != null) {
container.add(contentPane);
container.remove(introText);
List<Resource> resources = project.getResources();
editorMenu.setEnabled(true);
editorMenu.setEditable(!resources.isEmpty());
translationTree.setEditable(!resources.isEmpty());
translationField.setEditable(!resources.isEmpty());
} else {
container.add(introText);
container.remove(contentPane);
editorMenu.setEnabled(false);
editorMenu.setEditable(false);
translationTree.setEditable(false);
translationField.setEditable(false);
}
translationField.setVisible(settings.isShowKeyField());

updateTitle();
validate();
repaint();
}

private void clearUI() {
translationField.clear();
translationTree.clear();
Expand All @@ -584,14 +628,14 @@ private void setupUI() {
translationField = new TranslationField();
translationField.addKeyListener(new TranslationFieldKeyListener());
translationField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(1,0,0,1,LookAndFeel.TEXTFIELD_BORDER_COLOR),
BorderFactory.createMatteBorder(1,0,0,1,LookAndFeel.BORDER_COLOR),
((CompoundBorder)translationField.getBorder()).getInsideBorder()));

JScrollPane translationsScrollPane = new JScrollPane(translationTree);
translationsScrollPane.getViewport().setOpaque(false);
translationsScrollPane.setOpaque(false);
translationsScrollPane.setBorder(
BorderFactory.createMatteBorder(0,0,0,1,LookAndFeel.TEXTFIELD_BORDER_COLOR));
BorderFactory.createMatteBorder(0,0,0,1,LookAndFeel.BORDER_COLOR));

translationsPanel = new JPanel(new BorderLayout());
translationsPanel.add(translationsScrollPane);
Expand Down Expand Up @@ -654,51 +698,68 @@ public void filesDropped(java.io.File[] files) {
});
}

private void setupGlobalKeyEventDispatcher() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
if (!e.isAltDown() || e.getID() != KeyEvent.KEY_PRESSED) {
return false;
}
TreePath selected = translationTree.getSelectionPath();
if (selected == null) {
return false;
}
boolean result = false;
int row = translationTree.getRowForPath(selected);
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
if (translationTree.isExpanded(row)) {
translationTree.setSelectionRow(row+1);
} else {
translationTree.expandRow(row);
}
result = true;
break;
case KeyEvent.VK_LEFT:
if (translationTree.isCollapsed(row)) {
translationTree.setSelectionPath(selected.getParentPath());
} else {
translationTree.collapseRow(row);
}
result = true;
break;
case KeyEvent.VK_UP:
translationTree.setSelectionRow(Math.max(0, row-1));
result = true;
break;
case KeyEvent.VK_DOWN:
TreePath next = translationTree.getPathForRow(row+1);
if (next != null) {
translationTree.setSelectionPath(next);
}
result = true;
break;
}
if (result && !resourceFields.isEmpty()) {
Component comp = getFocusOwner();
if (comp != null && (comp instanceof ResourceField || comp.equals(this))) {
TranslationTreeNode current = translationTree.getSelectedNode();
if (!current.isLeaf() || current.isRoot()) {
requestFocusInWindow();
} else if (comp.equals(this)) {
resourceFields.get(0).requestFocusInWindow();
}
}
}
return result;
});
}

private void setupResource(Resource resource) {
resource.addListener(e -> setDirty(true));
ResourceField field = new ResourceField(resource);
field.addKeyListener(new ResourceFieldKeyListener());
resourceFields.add(field);
}

private void updateUI() {
TranslationTreeNode selectedNode = translationTree.getSelectedNode();

resourcesPanel.removeAll();
resourceFields.stream().sorted().forEach(field -> {
Locale locale = field.getResource().getLocale();
String label = locale != null ? locale.getDisplayName() : MessageBundle.get("resources.locale.default");
field.setEditable(selectedNode != null && selectedNode.isEditable());
resourcesPanel.add(Box.createVerticalStrut(5));
resourcesPanel.add(new JLabel(label));
resourcesPanel.add(Box.createVerticalStrut(5));
resourcesPanel.add(field);
resourcesPanel.add(Box.createVerticalStrut(10));
});

Container container = getContentPane();
if (project != null) {
container.add(contentPane);
container.remove(introText);
List<Resource> resources = project.getResources();
editorMenu.setEnabled(true);
editorMenu.setEditable(!resources.isEmpty());
translationTree.setEditable(!resources.isEmpty());
translationField.setEditable(!resources.isEmpty());
} else {
container.add(introText);
container.remove(contentPane);
editorMenu.setEnabled(false);
editorMenu.setEditable(false);
translationTree.setEditable(false);
translationField.setEditable(false);
}

updateTitle();
validate();
repaint();
}

private void updateHistory() {
List<String> recentDirs = settings.getHistory();
if (project != null) {
Expand Down Expand Up @@ -762,6 +823,8 @@ private void storeEditorState() {
props.setProperty("minify_resources", settings.isMinifyResources());
props.setProperty("resource_name", settings.getResourceName());
props.setProperty("check_version", settings.isCheckVersionOnStartup());
props.setProperty("input_height", settings.getInputHeight());
props.setProperty("key_field", settings.isShowKeyField());
if (!settings.getHistory().isEmpty()) {
props.setProperty("history", settings.getHistory());
}
Expand Down Expand Up @@ -792,6 +855,8 @@ private void restoreEditorState() {
settings.setMinifyResources(props.getBooleanProperty("minify_resources", false));
settings.setResourceName(props.getProperty("resource_name", DEFAULT_RESOURCE_NAME));
settings.setCheckVersionOnStartup(props.getBooleanProperty("check_version", true));
settings.setInputHeight(props.getIntegerProperty("input_height", 5));
settings.setShowKeyField(props.getBooleanProperty("key_field", true));
}

private class TranslationTreeMouseListener extends MouseAdapter {
Expand Down Expand Up @@ -836,7 +901,7 @@ public void valueChanged(TreeSelectionEvent e) {
translationField.setValue(key);
resourceFields.forEach(f -> {
f.setValue(key);
f.setEditable(node.isEditable());
f.setEnabled(node.isEditable());
});

// Restore scroll position
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/jvms/i18neditor/editor/EditorSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class EditorSettings {
private List<String> lastExpandedNodes;
private String lastSelectedNode;
private boolean checkVersionOnStartup;
private int inputHeight;
private boolean showKeyField;

public int getWindowPositionX() {
return windowPositionX;
Expand Down Expand Up @@ -107,4 +109,20 @@ public boolean isCheckVersionOnStartup() {
public void setCheckVersionOnStartup(boolean checkVersionOnStartup) {
this.checkVersionOnStartup = checkVersionOnStartup;
}

public int getInputHeight() {
return inputHeight;
}

public void setInputHeight(int rows) {
this.inputHeight = rows;
}

public boolean isShowKeyField() {
return showKeyField;
}

public void setShowKeyField(boolean showKeyField) {
this.showKeyField = showKeyField;
}
}
32 changes: 28 additions & 4 deletions src/main/java/com/jvms/i18neditor/editor/EditorSettingsPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import com.jvms.i18neditor.swing.JTextField;
import com.jvms.i18neditor.util.MessageBundle;
Expand Down Expand Up @@ -41,7 +42,7 @@ private void setupUI() {
// New project settings
JPanel fieldset2 = createFieldset(MessageBundle.get("settings.fieldset.newprojects"));

JPanel resourcePanel = new JPanel(new GridLayout(0, 1));
JPanel resourceNamePanel = new JPanel(new GridLayout(0, 1));
JLabel resourceNameLabel = new JLabel(MessageBundle.get("settings.resourcename.title"));
JTextField resourceNameField = new JTextField(settings.getResourceName());
resourceNameField.addKeyListener(new KeyAdapter() {
Expand All @@ -51,17 +52,40 @@ public void keyReleased(KeyEvent e) {
settings.setResourceName(value.isEmpty() ? Editor.DEFAULT_RESOURCE_NAME : value);
}
});
resourcePanel.add(resourceNameLabel);
resourcePanel.add(resourceNameField);
fieldset2.add(resourcePanel, createVerticalGridBagConstraints());
resourceNamePanel.add(resourceNameLabel);
resourceNamePanel.add(resourceNameField);
fieldset2.add(resourceNamePanel, createVerticalGridBagConstraints());

JCheckBox minifyBox = new JCheckBox(MessageBundle.get("settings.minify.title"));
minifyBox.setSelected(settings.isMinifyResources());
minifyBox.addChangeListener(e -> settings.setMinifyResources(minifyBox.isSelected()));
fieldset2.add(minifyBox, createVerticalGridBagConstraints());

// Editing settings
JPanel fieldset3 = createFieldset("Editing");

JCheckBox keyFieldBox = new JCheckBox("Show translation key field");
keyFieldBox.setSelected(settings.isShowKeyField());
keyFieldBox.addChangeListener(e -> {
settings.setShowKeyField(keyFieldBox.isSelected());
editor.updateUI();
});
fieldset3.add(keyFieldBox, createVerticalGridBagConstraints());

JPanel resourceHeightPanel = new JPanel(new GridLayout(0, 1));
JLabel resourceHeightLabel = new JLabel("Default input height");
JSlider resourceHeightSlider = new JSlider(JSlider.HORIZONTAL, 1, 15, settings.getInputHeight());
resourceHeightSlider.addChangeListener(e -> {
settings.setInputHeight(resourceHeightSlider.getValue());
editor.updateUI();
});
resourceHeightPanel.add(resourceHeightLabel);
resourceHeightPanel.add(resourceHeightSlider);
fieldset3.add(resourceHeightPanel, createVerticalGridBagConstraints());

setLayout(new GridBagLayout());
add(fieldset1, createVerticalGridBagConstraints());
add(fieldset2, createVerticalGridBagConstraints());
add(fieldset3, createVerticalGridBagConstraints());
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/jvms/i18neditor/editor/ResourceField.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public int compareTo(ResourceField o) {
}

private void setupUI() {
setRows(10);

// Add focus traversal support
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
Expand Down
Loading

0 comments on commit aff553e

Please sign in to comment.