diff options
author | Tom Schindl | 2014-10-30 14:31:46 +0000 |
---|---|---|
committer | Tom Schindl | 2014-10-30 14:31:46 +0000 |
commit | 98ce75b5aaa99a1ff25b9bc797e63b7eb87cdd96 (patch) | |
tree | c602cf23e1a3eefa0292748d72f1dac02b94c0f6 /bundles/runtime/org.eclipse.fx.core | |
parent | 1b4a3690c0d99d3e07b5122a17ac3ec5b4192d60 (diff) | |
download | org.eclipse.efxclipse-98ce75b5aaa99a1ff25b9bc797e63b7eb87cdd96.tar.gz org.eclipse.efxclipse-98ce75b5aaa99a1ff25b9bc797e63b7eb87cdd96.tar.xz org.eclipse.efxclipse-98ce75b5aaa99a1ff25b9bc797e63b7eb87cdd96.zip |
Bug 449258 - Add new NLS support API (Formatters, LocaleProvider, ...)
Diffstat (limited to 'bundles/runtime/org.eclipse.fx.core')
5 files changed, 250 insertions, 1 deletions
diff --git a/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF b/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF index d23755675..b61ac94ef 100755 --- a/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF +++ b/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF @@ -4,7 +4,8 @@ Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.fx.core
Bundle-Version: 1.1.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
-Import-Package: javax.inject;version="1.0.0",
+Import-Package: javax.annotation;version="1.2.0",
+ javax.inject;version="1.0.0",
org.apache.commons.lang.text;version="2.6.0"
Export-Package: org.eclipse.fx.core,
org.eclipse.fx.core.adapter,
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/AbstractTextRegistry.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/AbstractTextRegistry.java new file mode 100644 index 000000000..07b54f8e3 --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/AbstractTextRegistry.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2014 BestSolution.at and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.core.text; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import javax.annotation.PreDestroy; + +import org.eclipse.fx.core.Subscription; + +/** + * Allows to register translated elements + * + * @param <M> + * the message class type + * @since 1.1 + */ +public class AbstractTextRegistry<M> { + private M messages; + + Map<Consumer<String>, Supplier<String>> bindings = new HashMap<>(); + + /** + * @return the current message class + */ + public final M getMessages() { + return this.messages; + } + + /** + * Register a consumer and a function acting as the supplier of the + * translation value: + * <pre> + * @Inject + * private MessageHandler handler; // Subclass of AbstractMessageRegistry + * + * TitledPane tb = new TitledPane(); + * handler.register(tb::setText, (m) -> m.PreferenceView_Title); + * </pre> + * + * @param consumer + * the consumer + * @param valueFunction + * the supplier function + * @return the subscription + */ + public Subscription register(Consumer<String> consumer, Function<M, String> valueFunction) { + return register(consumer, () -> valueFunction.apply(getMessages())); + } + + /** + * Register a consumer and supplier + * <pre> + * @Inject + * private MessageHandler handler; // Subclass of AbstractMessageRegistry + * + * TitledPane tb = new TitledPane(); + * handler.register(tb::setText, handler::PreferenceView_Title); + * </pre> + * + * @param consumer the consumer the value is set on + * @param supplier the supplier + * @return the subscription + */ + public Subscription register(Consumer<String> consumer, Supplier<String> supplier) { + this.bindings.put(consumer, supplier); + consumer.accept(supplier.get()); + return new Subscription() { + + @Override + public void dispose() { + AbstractTextRegistry.this.bindings.remove(consumer); + } + }; + } + + /** + * Update the message object + * + * @param messages the messages + */ + public void updateMessages(M messages) { + this.messages = messages; + Iterator<Entry<Consumer<String>, Supplier<String>>> it = this.bindings.entrySet().iterator(); + while (it.hasNext()) { + Entry<Consumer<String>, Supplier<String>> next = it.next(); + next.getKey().accept(next.getValue().get()); + } + } + + @PreDestroy + void unregister() { + this.bindings.clear(); + } +} diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/DateFormatter.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/DateFormatter.java new file mode 100644 index 000000000..4df73493c --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/DateFormatter.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2014 BestSolution.at and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.core.text; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import javax.inject.Inject; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +/** + * Formatter based on {@link SimpleDateFormat} + * + * @since 1.1.0 + */ +public class DateFormatter implements Formatter<Date> { + @NonNull + private final LocaleProvider localeProvider; + + /** + * Create a new date formatter + * + * @param localeProvider + * the locale provider + */ + @Inject + public DateFormatter(@NonNull LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + + @Override + public @NonNull String format(@Nullable Date object, @NonNull String format) { + String rv = new SimpleDateFormat(format, this.localeProvider.getLocale()).format(object); + return rv == null ? "" : rv; //$NON-NLS-1$ + } +} diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/NumberFormatter.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/NumberFormatter.java new file mode 100644 index 000000000..3a15b8b7e --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/NumberFormatter.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2014 BestSolution.at and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.core.text; + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; + +import javax.inject.Inject; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +/** + * Formatter based on {@link DecimalFormat} + * + * @since 1.1.0 + */ +public class NumberFormatter implements Formatter<Number> { + @NonNull + private final LocaleProvider localeProvider; + + /** + * Create number formatter + * + * @param localeProvider + * the locale provider + */ + @Inject + public NumberFormatter(@NonNull LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + + @Override + public @NonNull String format(@Nullable Number object, @NonNull String format) { + String rv = new DecimalFormat(format, DecimalFormatSymbols.getInstance(this.localeProvider.getLocale())).format(object); + return rv == null ? "" : rv; //$NON-NLS-1$ + } +} diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/TemporalAccessorFormatter.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/TemporalAccessorFormatter.java new file mode 100644 index 000000000..4b0f7873c --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/text/TemporalAccessorFormatter.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2014 BestSolution.at and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.core.text; + +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; + +import javax.inject.Inject; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +/** + * Formatter based on {@link DateTimeFormatter} + * + * @since 1.1.0 + */ +public class TemporalAccessorFormatter implements Formatter<TemporalAccessor> { + @NonNull + private final LocaleProvider localeProvider; + + /** + * Create a temporal access formatter + * + * @param localeProvider + * the locale provider + */ + @Inject + public TemporalAccessorFormatter(@NonNull LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + + @Override + public @NonNull String format(@Nullable TemporalAccessor object, @NonNull String format) { + String rv = DateTimeFormatter.ofPattern(format, this.localeProvider.getLocale()).format(object); + return rv == null ? "" : rv; //$NON-NLS-1$ + } + +} |