From fd2a1aacd3841b84dcad14346aa7ce1715607ad0 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 7 Nov 2012 09:56:51 +0100 Subject: + added locale config parameter --- .../main/java/at/asit/pdfover/gui/Messages.java | 49 +++++++++++++++++-- .../asit/pdfover/gui/utils/LocaleSerializer.java | 57 ++++++++++++++++++++++ .../pdfover/gui/workflow/ConfigManipulator.java | 12 +++++ .../asit/pdfover/gui/workflow/ConfigProvider.java | 7 +++ .../pdfover/gui/workflow/ConfigProviderImpl.java | 40 ++++++++++++++- 5 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/LocaleSerializer.java diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Messages.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Messages.java index 1e8f899a..0c6101b9 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Messages.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Messages.java @@ -16,21 +16,63 @@ package at.asit.pdfover.gui; // Imports +import java.util.HashMap; +import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Localizes string messages for PDFOver GUI */ public class Messages { + + /** + * SFL4J Logger instance + **/ + static final Logger log = LoggerFactory.getLogger(Messages.class); + private static final String BUNDLE_NAME = "at.asit.pdfover.gui.messages"; //$NON-NLS-1$ - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle - .getBundle(BUNDLE_NAME); + private static HashMap bundles = new HashMap(); + + //private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + // .getBundle(BUNDLE_NAME); + private static Locale currentLocale = Locale.getDefault(); + private Messages() { } + /** + * Sets the currently used locals + * @param locale + */ + public static void setLocale(Locale locale) { + currentLocale = locale; + } + + private static ResourceBundle getBundle() { + if(!bundles.containsKey(currentLocale)) { + ResourceBundle tmp = null; + try { + tmp = ResourceBundle.getBundle(BUNDLE_NAME, currentLocale); + } catch(Exception e) { + log.error("NO RESOURCE BUNDLE FOR " + currentLocale.toString(), e); //$NON-NLS-1$ + tmp = ResourceBundle.getBundle(BUNDLE_NAME); + } + if(tmp == null) { + log.error("NO RESOURCE BUNDLE FOR " + currentLocale.toString()); //$NON-NLS-1$ + tmp = ResourceBundle.getBundle(BUNDLE_NAME); + } + bundles.put(currentLocale, tmp); + return tmp; + } + return bundles.get(currentLocale); + } + /** * Gets the localized message * @param key @@ -38,7 +80,8 @@ public class Messages { */ public static String getString(String key) { try { - return RESOURCE_BUNDLE.getString(key); + return getBundle().getString(key); + //return RESOURCE_BUNDLE.getString(key); } catch (MissingResourceException e) { return '!' + key + '!'; } diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/LocaleSerializer.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/LocaleSerializer.java new file mode 100644 index 00000000..fccbedc8 --- /dev/null +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/LocaleSerializer.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.gui.utils; + +// Imports +import java.util.Locale; + +/** + * + */ +public class LocaleSerializer { + + /** + * Parse a locale from a string + * @param localeString the string + * @return the locale + */ + public static Locale parseFromString(String localeString) { + + if(localeString == null || localeString.isEmpty()) { + return null; + } + + Locale targetLocal = null; + Locale[] locale = Locale.getAvailableLocales(); + for(int i = 0; i < locale.length; i++) { + if(locale[i].toString().equals(localeString)) { + targetLocal = locale[i]; + break; + } + } + return targetLocal; + } + + /** + * creates a parseable string for a locale + * @param locale the locale + * @return the parseable string + */ + public static String getParseableString(Locale locale) { + return locale.toString(); + } + +} diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigManipulator.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigManipulator.java index d7b23e58..c5f0b930 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigManipulator.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigManipulator.java @@ -16,6 +16,7 @@ package at.asit.pdfover.gui.workflow; import java.io.IOException; +import java.util.Locale; import at.asit.pdfover.signator.BKUs; import at.asit.pdfover.signator.SignaturePosition; @@ -73,6 +74,11 @@ public interface ConfigManipulator { */ public static final String EMBLEM_CONFIG = "EMBLEM"; //$NON-NLS-1$ + /** + * The locale config parameter + */ + public static final String LOCALE_CONFIG = "LOCALE"; //$NON-NLS-1$ + /** * The proxy host config parameter */ @@ -163,4 +169,10 @@ public interface ConfigManipulator { * @throws IOException */ public void saveCurrentConfiguration() throws IOException; + + /** + * Sets the locale to be used! + * @param locale the locale + */ + public void setLocale(Locale locale); } diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProvider.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProvider.java index b545f400..f0b53cbf 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProvider.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProvider.java @@ -17,6 +17,7 @@ package at.asit.pdfover.gui.workflow; import java.io.IOException; import java.io.InputStream; +import java.util.Locale; import at.asit.pdfover.signator.BKUs; import at.asit.pdfover.signator.SignaturePosition; @@ -109,6 +110,12 @@ public interface ConfigProvider { */ public String getSigantureNote(); + /** + * Gets the configured locale + * @return the configured locale + */ + public Locale getConfigLocale(); + /** * Loads the current configuration to the current configuration file * @param configSource diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProviderImpl.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProviderImpl.java index 8da0a069..801f626c 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProviderImpl.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/ConfigProviderImpl.java @@ -28,6 +28,8 @@ import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.asit.pdfover.gui.Messages; +import at.asit.pdfover.gui.utils.LocaleSerializer; import at.asit.pdfover.signator.BKUs; import at.asit.pdfover.signator.SignaturePosition; @@ -64,6 +66,8 @@ public class ConfigProviderImpl implements ConfigProvider, ConfigManipulator { private String defaultPassword = STRING_EMPTY; + private Locale locale = Locale.getDefault(); + private String emblem = STRING_EMPTY; private String proxyHost = STRING_EMPTY; @@ -362,6 +366,11 @@ public class ConfigProviderImpl implements ConfigProvider, ConfigManipulator { props.setProperty(SIGNATURE_PLACEHOLDER_TRANSPARENCY_CONFIG, Integer.toString(this.getPlaceholderTransparency())); + Locale configLocale = this.getConfigLocale(); + if(configLocale != null) { + props.setProperty(LOCALE_CONFIG, LocaleSerializer.getParseableString(configLocale)); + } + SignaturePosition pos = this.getDefaultSignaturePosition(); if (pos == null) { @@ -416,9 +425,16 @@ public class ConfigProviderImpl implements ConfigProvider, ConfigManipulator { this.setDefaultOutputFolder(config .getProperty(ConfigManipulator.OUTPUT_FOLDER_CONFIG)); + String localString = config.getProperty(ConfigManipulator.LOCALE_CONFIG); + + Locale targetLocal = LocaleSerializer.parseFromString(localString); + if(targetLocal != null) { + this.setLocale(targetLocal); + } + String bku = config .getProperty(ConfigManipulator.MOBILE_BKU_URL_CONFIG); - + if (bku != null && !bku.equals("")) { //$NON-NLS-1$ this.mobileBKU = bku; } @@ -550,4 +566,26 @@ public class ConfigProviderImpl implements ConfigProvider, ConfigManipulator { } } + /* (non-Javadoc) + * @see at.asit.pdfover.gui.workflow.ConfigProvider#getConfigLocale() + */ + @Override + public Locale getConfigLocale() { + return this.locale; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.gui.workflow.ConfigManipulator#setLocale(java.util.Locale) + */ + @Override + public void setLocale(Locale locale) { + if(locale == null) { + this.locale = Locale.getDefault(); + } else { + this.locale = locale; + Locale.setDefault(locale); + Messages.setLocale(locale); + } + } + } -- cgit v1.2.3 From ca6544acdadbc42dbbc9be68718782eeb815b0ad Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 7 Nov 2012 10:02:36 +0100 Subject: + localized error "This may not be a PDF File" --- .../at/asit/pdfover/gui/composites/PositioningComposite.java | 9 +++++++-- .../at/asit/pdfover/gui/workflow/states/PositioningState.java | 2 +- .../src/main/resources/at/asit/pdfover/gui/messages.properties | 1 + .../main/resources/at/asit/pdfover/gui/messages_de.properties | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java index d612f3c9..c7e1b7b7 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java @@ -173,8 +173,13 @@ public class PositioningComposite extends StateComposite { .map(FileChannel.MapMode.READ_ONLY, 0, chan.size()); chan.close(); rafile.close(); - - this.pdf = new PDFFile(buf); + try + { + this.pdf = new PDFFile(buf); + } + catch (IOException e) { + throw new IOException(Messages.getString("error.MayNotBeAPDF"), e); //$NON-NLS-1$ + } if (this.viewer == null) { this.viewer = new SignaturePanel(this.pdf); this.frame.add(this.viewer); diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java index 44513806..d877cdcf 100644 --- a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java @@ -101,7 +101,7 @@ public class PositioningState extends State { log.error("Failed to display PDF document", e); //$NON-NLS-1$ ErrorDialog dialog = new ErrorDialog( this.stateMachine.getGUIProvider().getMainShell(), - e.getMessage(), ERROR_BUTTONS.RETRY_CANCEL); + e.getLocalizedMessage(), ERROR_BUTTONS.RETRY_CANCEL); if(dialog.open() == SWT.RETRY) { this.stateMachine.update(); } else { diff --git a/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties b/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties index a60175fa..413e9db5 100644 --- a/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties +++ b/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties @@ -77,6 +77,7 @@ error.SignaturePanel.NoRender="Could not render page" error.TanTooLong=Entered TAN too long error.Unexpected=Unexpected Error error.title=Error +error.MayNotBeAPDF=This may not be a PDF File exception.InvalidEmblemFile=%s is an invalid signature logo file! exception.InvalidPort=%s is invalid: has to be a number between %d and %d exception.PasswordTooLong=Given password is too long! diff --git a/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties b/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties index 789880f9..c3a1d302 100644 --- a/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties +++ b/trunk/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties @@ -77,6 +77,7 @@ error.SignaturePanel.NoRender="Konnte Seite nicht darstellen" error.TanTooLong=Eingegebener TAN zu lange error.Unexpected=Unerwarteter Fehler error.title=Fehler +error.MayNotBeAPDF=Dies ist möglicherweise keine PDF Datei exception.InvalidEmblemFile=%s ist eine ungültige Bildmarken-Datei! exception.InvalidPort=%s ist ungültig: muss eine Nummer zwischen %d und %d sein. exception.PasswordTooLong=Eingegebenes Passwort ist zu lange! -- cgit v1.2.3