Skip to content

Commit e61c809

Browse files
authored
The "Add..." context menu in Messages editor: two bugs #27 (#30)
Fixed NPE and switched behaviour for the "Add" action so that it only adds a new key to the ROOT locale
1 parent 207c421 commit e61c809

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

org.eclipse.babel.core/src/org/eclipse/babel/core/message/internal/MessagesBundleGroup.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ public void dispose() {
118118
*/
119119
@Override
120120
public IMessagesBundle getMessagesBundle(Locale locale) {
121-
return localeBundles.get(locale);
121+
// The ROOT locale is currently stored as a null key in the HashMap, need to swap it!
122+
Locale localeToUse = locale == Locale.ROOT ? null : locale;
123+
return localeBundles.get(localeToUse);
122124
}
123125

124126
/**

org.eclipse.babel.editor/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dialog.duplicate.body.single = Duplicate "{0}" to:
1111
dialog.duplicate.head.multiple = Duplicate key group
1212
dialog.duplicate.head.single = Duplicate key
1313
dialog.error.exists = This key already exists.
14+
dialog.error.invalidkey = Invalid key.
1415
dialog.identical.body = Below are keys having identical value as key "{0}" within the locale "{1}":
1516
dialog.identical.head = Identical value(s) found.
1617
dialog.rename.body.multiple = Rename key group "{0}" to (all nested keys will be renamed):

org.eclipse.babel.editor/src/org/eclipse/babel/editor/i18n/SideNavTextBoxComposite.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
******************************************************************************/
1111
package org.eclipse.babel.editor.i18n;
1212

13+
import java.util.Locale;
14+
15+
import org.eclipse.babel.core.message.IMessagesBundle;
16+
import org.eclipse.babel.core.message.internal.MessagesBundle;
17+
import org.eclipse.babel.core.message.internal.MessagesBundleGroup;
1318
import org.eclipse.babel.core.message.tree.IKeyTreeNode;
1419
import org.eclipse.babel.core.message.tree.visitor.NodePathRegexVisitor;
1520
import org.eclipse.babel.editor.internal.AbstractMessagesEditor;
@@ -120,8 +125,12 @@ public void selectedKeyChanged(String oldKey, String newKey) {
120125
}
121126

122127
private void addKey(String key) {
123-
editor.getBundleGroup().addMessages(key);
124-
editor.setSelectedKey(key);
128+
MessagesBundleGroup messagesBundleGroup = editor.getBundleGroup();
129+
IMessagesBundle messagesBundle = messagesBundleGroup.getMessagesBundle(Locale.ROOT);
130+
if (messagesBundle instanceof MessagesBundle theBundle) {
131+
theBundle.addMessage(key);
132+
editor.setSelectedKey(key);
133+
}
125134
}
126135

127136
private boolean isNewKey(String key) {

org.eclipse.babel.editor/src/org/eclipse/babel/editor/tree/actions/AddKeyAction.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
******************************************************************************/
1111
package org.eclipse.babel.editor.tree.actions;
1212

13+
import java.util.Locale;
14+
15+
import org.eclipse.babel.core.message.IMessagesBundle;
16+
import org.eclipse.babel.core.message.internal.MessagesBundle;
1317
import org.eclipse.babel.core.message.internal.MessagesBundleGroup;
1418
import org.eclipse.babel.core.message.tree.internal.KeyTreeNode;
1519
import org.eclipse.babel.editor.internal.AbstractMessagesEditor;
@@ -43,15 +47,17 @@ public AddKeyAction(AbstractMessagesEditor editor, TreeViewer treeViewer) {
4347
@Override
4448
public void run() {
4549
KeyTreeNode node = getNodeSelection();
46-
String key = node.getMessageKey();
50+
String selectedKey = node != null ? node.getMessageKey() : "";
4751
String msgHead = MessagesEditorPlugin.getString("dialog.add.head");
4852
String msgBody = MessagesEditorPlugin.getString("dialog.add.body");
49-
InputDialog dialog = new InputDialog(getShell(), msgHead, msgBody, key,
53+
InputDialog dialog = new InputDialog(getShell(), msgHead, msgBody, selectedKey,
5054
new IInputValidator() {
5155
public String isValid(String newText) {
5256
if (getBundleGroup().isMessageKey(newText)) {
5357
return MessagesEditorPlugin
5458
.getString("dialog.error.exists");
59+
} else if (newText.isBlank()) {
60+
return MessagesEditorPlugin.getString("dialog.error.invalidkey");
5561
}
5662
return null;
5763
}
@@ -60,7 +66,10 @@ public String isValid(String newText) {
6066
if (dialog.getReturnCode() == Window.OK) {
6167
String inputKey = dialog.getValue();
6268
MessagesBundleGroup messagesBundleGroup = getBundleGroup();
63-
messagesBundleGroup.addMessages(inputKey);
69+
IMessagesBundle messagesBundle = messagesBundleGroup.getMessagesBundle(Locale.ROOT);
70+
if ( messagesBundle instanceof MessagesBundle theBundle ) {
71+
theBundle.addMessage(inputKey);
72+
}
6473
}
6574
}
6675

0 commit comments

Comments
 (0)