]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ThreadNLS.java
new proposal to address the generic "open file" issue using a cleaner strategy.
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rcp / src / main / java / org / argeo / eclipse / ui / specific / ThreadNLS.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23
24 import org.argeo.util.LocaleUtils;
25 import org.eclipse.osgi.util.NLS;
26
27 /** NLS attached to a given thread */
28 public class ThreadNLS<T extends NLS> extends InheritableThreadLocal<T> {
29 public final static String DEFAULT_BUNDLE_LOCATION = "/properties/plugin";
30
31 private final String bundleLocation;
32
33 private Class<T> type;
34 private Boolean utf8 = false;
35
36 public ThreadNLS(String bundleLocation, Class<T> type, Boolean utf8) {
37 this.bundleLocation = bundleLocation;
38 this.type = type;
39 this.utf8 = utf8;
40 }
41
42 public ThreadNLS(Class<T> type) {
43 this(DEFAULT_BUNDLE_LOCATION, type, false);
44 }
45
46 @Override
47 protected T initialValue() {
48 ResourceBundle bundle = ResourceBundle.getBundle(bundleLocation,
49 LocaleUtils.threadLocale.get(), type.getClassLoader());
50 T result;
51 try {
52 NLS.initializeMessages(bundleLocation, type);
53 Constructor<T> constructor = type.getConstructor();
54 constructor.setAccessible(true);
55 result = constructor.newInstance();
56 final Field[] fieldArray = type.getDeclaredFields();
57 for (int i = 0; i < fieldArray.length; i++) {
58 int modifiers = fieldArray[i].getModifiers();
59 if (String.class.isAssignableFrom(fieldArray[i].getType())
60 && Modifier.isPublic(modifiers)
61 && !Modifier.isStatic(modifiers)) {
62 try {
63 String value = bundle
64 .getString(fieldArray[i].getName());
65 byte[] bytes = value.getBytes();
66
67 String forcedValue;
68 if (utf8)
69 forcedValue = new String(bytes, "UTF8");
70 else
71 forcedValue = value;
72 if (value != null) {
73 fieldArray[i].setAccessible(true);
74 fieldArray[i].set(result, forcedValue);
75 }
76 } catch (final MissingResourceException mre) {
77 fieldArray[i].setAccessible(true);
78 fieldArray[i].set(result, "");
79 mre.printStackTrace();
80 }
81 }
82 }
83 return result;
84 } catch (final Exception ex) {
85 throw new IllegalStateException(ex.getMessage());
86 }
87 }
88
89 }