mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
Initial commit with new directory structure.
This commit is contained in:
99
components/net/sf/briar/i18n/FontManagerImpl.java
Normal file
99
components/net/sf/briar/i18n/FontManagerImpl.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package net.sf.briar.i18n;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.swing.UIManager;
|
||||
|
||||
import net.sf.briar.api.i18n.FontManager;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
public class FontManagerImpl implements FontManager {
|
||||
|
||||
private static final BundledFont[] BUNDLED_FONTS = {
|
||||
new BundledFont("TibetanMachineUni.ttf", 14f, new String[] { "bo" }),
|
||||
new BundledFont("Padauk.ttf", 14f, new String[] { "my" }),
|
||||
};
|
||||
|
||||
private final Map<String, Font> fonts = new TreeMap<String, Font>();
|
||||
|
||||
private volatile Font defaultFont = null, uiFont = null;
|
||||
|
||||
public void initialize(Locale locale) throws IOException {
|
||||
try {
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
for(BundledFont bf : BUNDLED_FONTS) {
|
||||
InputStream in = loader.getResourceAsStream(bf.filename);
|
||||
if(in == null) {
|
||||
File root = FileUtils.getBriarDirectory();
|
||||
File file = new File(root, "Data/" + bf.filename);
|
||||
in = new FileInputStream(file);
|
||||
}
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
|
||||
font = font.deriveFont(bf.size);
|
||||
for(String language : bf.languages) fonts.put(language, font);
|
||||
}
|
||||
} catch(FontFormatException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
defaultFont = getFont("Sans", 12f);
|
||||
assert defaultFont != null; // FIXME: This is failing on Windows
|
||||
setUiFontForLanguage(locale.getLanguage());
|
||||
}
|
||||
|
||||
private Font getFont(String name, float size) {
|
||||
Map<TextAttribute, Object> attr = new HashMap<TextAttribute, Object>();
|
||||
attr.put(TextAttribute.FAMILY, name);
|
||||
attr.put(TextAttribute.SIZE, Float.valueOf(size));
|
||||
return Font.getFont(attr);
|
||||
}
|
||||
|
||||
public String[] getBundledFontFilenames() {
|
||||
String[] names = new String[BUNDLED_FONTS.length];
|
||||
for(int i = 0; i < BUNDLED_FONTS.length; i++)
|
||||
names[i] = BUNDLED_FONTS[i].filename;
|
||||
return names;
|
||||
}
|
||||
|
||||
public Font getFontForLanguage(String language) {
|
||||
assert defaultFont != null;
|
||||
Font font = fonts.get(language);
|
||||
return font == null ? defaultFont : font;
|
||||
}
|
||||
|
||||
public Font getUiFont() {
|
||||
return uiFont;
|
||||
}
|
||||
|
||||
public void setUiFontForLanguage(String language) {
|
||||
uiFont = getFontForLanguage(language);
|
||||
Enumeration<Object> keys = UIManager.getDefaults().keys();
|
||||
while(keys.hasMoreElements()) {
|
||||
Object key = keys.nextElement();
|
||||
if(UIManager.getFont(key) != null) UIManager.put(key, uiFont);
|
||||
}
|
||||
}
|
||||
|
||||
private static class BundledFont {
|
||||
|
||||
private final String filename;
|
||||
private final float size;
|
||||
private final String[] languages;
|
||||
|
||||
BundledFont(String filename, float size, String[] languages) {
|
||||
this.filename = filename;
|
||||
this.size = size;
|
||||
this.languages = languages;
|
||||
}
|
||||
}
|
||||
}
|
||||
152
components/net/sf/briar/i18n/I18nImpl.java
Normal file
152
components/net/sf/briar/i18n/I18nImpl.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package net.sf.briar.i18n;
|
||||
|
||||
import java.awt.ComponentOrientation;
|
||||
import java.awt.Font;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.swing.UIManager;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import net.sf.briar.api.i18n.FontManager;
|
||||
import net.sf.briar.api.i18n.I18n;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
public class I18nImpl implements I18n {
|
||||
|
||||
private static final String[] uiManagerKeys = {
|
||||
"FileChooser.acceptAllFileFilterText",
|
||||
"FileChooser.cancelButtonText",
|
||||
"FileChooser.cancelButtonToolTipText",
|
||||
"FileChooser.detailsViewButtonAccessibleName",
|
||||
"FileChooser.detailsViewButtonToolTipText",
|
||||
"FileChooser.directoryOpenButtonText",
|
||||
"FileChooser.directoryOpenButtonToolTipText",
|
||||
"FileChooser.fileAttrHeaderText",
|
||||
"FileChooser.fileDateHeaderText",
|
||||
"FileChooser.fileNameHeaderText",
|
||||
"FileChooser.fileNameLabelText",
|
||||
"FileChooser.fileSizeHeaderText",
|
||||
"FileChooser.filesOfTypeLabelText",
|
||||
"FileChooser.fileTypeHeaderText",
|
||||
"FileChooser.helpButtonText",
|
||||
"FileChooser.helpButtonToolTipText",
|
||||
"FileChooser.homeFolderAccessibleName",
|
||||
"FileChooser.homeFolderToolTipText",
|
||||
"FileChooser.listViewButtonAccessibleName",
|
||||
"FileChooser.listViewButtonToolTipText",
|
||||
"FileChooser.lookInLabelText",
|
||||
"FileChooser.newFolderErrorText",
|
||||
"FileChooser.newFolderToolTipText",
|
||||
"FileChooser.openButtonText",
|
||||
"FileChooser.openButtonToolTipText",
|
||||
"FileChooser.openDialogTitleText",
|
||||
"FileChooser.saveButtonText",
|
||||
"FileChooser.saveButtonToolTipText",
|
||||
"FileChooser.saveDialogTitleText",
|
||||
"FileChooser.saveInLabelText",
|
||||
"FileChooser.updateButtonText",
|
||||
"FileChooser.updateButtonToolTipText",
|
||||
"FileChooser.upFolderAccessibleName",
|
||||
"FileChooser.upFolderToolTipText",
|
||||
"OptionPane.cancelButtonText",
|
||||
"OptionPane.noButtonText",
|
||||
"OptionPane.yesButtonText",
|
||||
"ProgressMonitor.progressText"
|
||||
};
|
||||
|
||||
private final Object bundleLock = new Object();
|
||||
private final ClassLoader loader = I18n.class.getClassLoader();
|
||||
private final Set<Listener> listeners = new HashSet<Listener>();
|
||||
private final FontManager fontManager;
|
||||
|
||||
private volatile Locale locale = Locale.getDefault();
|
||||
private volatile ResourceBundle bundle = null;
|
||||
|
||||
@Inject
|
||||
public I18nImpl(FontManager fontManager) {
|
||||
this.fontManager = fontManager;
|
||||
}
|
||||
|
||||
public String tr(String name) {
|
||||
loadResourceBundle();
|
||||
return bundle.getString(name);
|
||||
}
|
||||
|
||||
private void loadResourceBundle() {
|
||||
if(bundle == null) {
|
||||
synchronized(bundleLock) {
|
||||
if(bundle == null) {
|
||||
bundle = ResourceBundle.getBundle("i18n", locale, loader);
|
||||
for(String key : uiManagerKeys) {
|
||||
try {
|
||||
UIManager.put(key, bundle.getString(key));
|
||||
} catch(MissingResourceException ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
public void setLocale(Locale locale) {
|
||||
fontManager.setUiFontForLanguage(locale.getLanguage());
|
||||
Font uiFont = fontManager.getUiFont();
|
||||
synchronized(bundleLock) {
|
||||
this.locale = locale;
|
||||
bundle = null;
|
||||
synchronized(listeners) {
|
||||
for(Listener l : listeners) l.localeChanged(uiFont);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void loadLocale() throws IOException {
|
||||
File root = FileUtils.getBriarDirectory();
|
||||
Scanner s = new Scanner(new File(root, "Data/locale.cfg"));
|
||||
if(s.hasNextLine()) locale = new Locale(s.nextLine());
|
||||
s.close();
|
||||
}
|
||||
|
||||
public void saveLocale() throws IOException {
|
||||
saveLocale(FileUtils.getBriarDirectory());
|
||||
}
|
||||
|
||||
public void saveLocale(File dir) throws IOException {
|
||||
File localeCfg = new File(dir, "locale.cfg");
|
||||
FileOutputStream out = new FileOutputStream(localeCfg);
|
||||
PrintStream print = new PrintStream(out);
|
||||
print.println(locale);
|
||||
print.flush();
|
||||
print.close();
|
||||
}
|
||||
|
||||
public ComponentOrientation getComponentOrientation() {
|
||||
return ComponentOrientation.getOrientation(locale);
|
||||
}
|
||||
|
||||
public void addListener(Listener l) {
|
||||
l.localeChanged(fontManager.getUiFont());
|
||||
synchronized(listeners) {
|
||||
listeners.add(l);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeListener(Listener l) {
|
||||
synchronized(listeners) {
|
||||
listeners.remove(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
components/net/sf/briar/i18n/I18nModule.java
Normal file
16
components/net/sf/briar/i18n/I18nModule.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package net.sf.briar.i18n;
|
||||
|
||||
import net.sf.briar.api.i18n.FontManager;
|
||||
import net.sf.briar.api.i18n.I18n;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
public class I18nModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(FontManager.class).to(FontManagerImpl.class).in(Singleton.class);
|
||||
bind(I18n.class).to(I18nImpl.class).in(Singleton.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user