Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package com.tlcsdm.eclipse.rbe.ui;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -44,6 +49,32 @@
*/
public final class UIUtils {

/**
* Comparator for {@link Locale} instances, using {@link Collator} with
* {@link Collator#PRIMARY} strength to respect the locale rules of the
* current Eclipse instance. A {@code null} locale (the default locale) is
* always sorted first.
*/
public static final class LocaleComparator implements Comparator<Locale> {
private final Collator collator;

public LocaleComparator() {
this.collator = Collator.getInstance();
this.collator.setStrength(Collator.PRIMARY);
}

@Override
public int compare(Locale o1, Locale o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 != null && o2 == null)
return 1;
if (o1 == null && o2 != null)
return -1;
return collator.compare(o1.getDisplayName(), o2.getDisplayName());
}
}

/** Name of resource bundle image. */
public static final String IMAGE_RESOURCE_BUNDLE = "resourcebundle.gif";
/** Name of properties file image. */
Expand Down Expand Up @@ -219,6 +250,20 @@ public static String getDisplayName(Locale locale) {
return locale.getDisplayName();
}

/**
* Sorts the given collection of locales using {@link Locale#getDisplayName()}
* and {@link Collator} rules of the current locale. A {@code null} locale (the
* default locale) is always placed first.
*
* @param locales locales to sort
* @return new sorted list of locales
*/
public static List<Locale> sortLocales(Collection<Locale> locales) {
ArrayList<Locale> result = new ArrayList<>(locales);
result.sort(new LocaleComparator());
return result;
}

/**
* Gets an image.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.tlcsdm.eclipse.rbe.model.tree.KeyTree;
import com.tlcsdm.eclipse.rbe.model.tree.KeyTreeItem;
import com.tlcsdm.eclipse.rbe.model.workbench.RBEPreferences;
import com.tlcsdm.eclipse.rbe.ui.UIUtils;
import com.tlcsdm.eclipse.rbe.ui.editor.i18n.tree.KeyTreeComposite;
import com.tlcsdm.eclipse.rbe.ui.editor.resources.ResourceManager;

Expand Down Expand Up @@ -190,9 +191,7 @@ private void createEditingPart(ScrolledComposite parent) {
// }
_rightComposite.setLayout(new GridLayout(1, false));
entryComposites.clear();
for (Iterator<Locale> iter = resourceMediator.getLocales().iterator();
iter.hasNext();) {
Locale locale = (Locale) iter.next();
for (Locale locale : UIUtils.sortLocales(resourceMediator.getLocales())) {
BundleEntryComposite entryComposite = new BundleEntryComposite(
_rightComposite, resourceMediator, locale, this);
entryComposite.addFocusListener(localBehaviour);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -74,14 +73,7 @@ public abstract class ResourceFactory implements IResourceFactory {
/**
* A sorted map of {@link SourceEditor}s. Sorted by key (Locale).
*/
private Map<Locale, SourceEditor> sourceEditors = new TreeMap<>(new Comparator<Locale>() {
@Override
public int compare(Locale locale1, Locale locale2) {
String displayName1 = UIUtils.getDisplayName(locale1);
String displayName2 = UIUtils.getDisplayName(locale2);
return displayName1.compareToIgnoreCase(displayName2);
}
});
private Map<Locale, SourceEditor> sourceEditors = new TreeMap<>(new UIUtils.LocaleComparator());

/**
* The {@link PropertiesFileCreator} used to create new files.
Expand Down
Loading