2006-08-12 22:31:51 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.thdl.tib.input ;
|
|
|
|
|
|
|
|
import org.thdl.tib.scanner.TibetanScanner ;
|
|
|
|
import org.thdl.tib.scanner.LocalTibetanScanner ;
|
|
|
|
import org.thdl.tib.scanner.RemoteTibetanScanner ;
|
|
|
|
import org.thdl.tib.scanner.Word ;
|
|
|
|
|
|
|
|
import org.thdl.tib.input.SettingsServiceProvider ;
|
|
|
|
import org.thdl.tib.input.DictionaryLoadState ;
|
2006-08-19 04:43:23 +00:00
|
|
|
import org.thdl.tib.input.DictionarySettings ;
|
2006-08-26 01:39:37 +00:00
|
|
|
import org.thdl.tib.dictionary.DictionaryInterface ;
|
|
|
|
import org.thdl.tib.dictionary.ScannerBasedDictionary ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
|
|
|
import java.awt.GraphicsEnvironment ;
|
|
|
|
import java.awt.Font ;
|
|
|
|
import java.net.URL ;
|
|
|
|
import java.net.URLConnection ;
|
|
|
|
import java.io.InputStream ;
|
|
|
|
import java.util.HashMap ;
|
|
|
|
import java.util.Observer ;
|
|
|
|
import java.util.Observable ;
|
|
|
|
|
|
|
|
public class GlobalResourceHolder
|
|
|
|
{
|
|
|
|
protected static final int NUM_FONTS = 10 ;
|
|
|
|
protected static final String FONT_NAME_PREFIX = "TibetanMachineWeb" ;
|
|
|
|
|
|
|
|
private static boolean flagIsUsingLocalFonts = false ;
|
|
|
|
private static Font baseTMWFont [] = null ;
|
|
|
|
private static Font derivedTMWFont [] = null ;
|
|
|
|
private static HashMap nameToNumber = null ;
|
|
|
|
private static SettingsServiceProvider settingsServiceProvider ;
|
|
|
|
|
2006-08-19 04:43:23 +00:00
|
|
|
private static DictionarySettings dictionarySettings = null ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
|
|
|
private static int dictionaryLoadState = DictionaryLoadState.UNDEFINED ;
|
|
|
|
private static Object dictionaryLoadLock = null ;
|
|
|
|
|
2006-08-26 01:39:37 +00:00
|
|
|
private static DictionaryInterface dictionary = null ;
|
|
|
|
|
2006-08-12 22:31:51 +00:00
|
|
|
static class Listener implements Observer
|
|
|
|
{
|
2006-08-19 21:47:03 +00:00
|
|
|
public static final int DICTIONARY_SETTINGS = 1 ;
|
|
|
|
|
2006-08-19 04:43:23 +00:00
|
|
|
/**
|
2006-08-12 22:31:51 +00:00
|
|
|
* update
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void update ( Observable o, Object arg )
|
|
|
|
{
|
2006-08-29 04:31:20 +00:00
|
|
|
onSettingsChange ( arg ) ;
|
2006-08-12 22:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onSettingsChange
|
|
|
|
*
|
|
|
|
*/
|
2006-08-29 04:31:20 +00:00
|
|
|
public void onSettingsChange ( Object arg )
|
2006-08-12 22:31:51 +00:00
|
|
|
{
|
|
|
|
if ( null == GlobalResourceHolder.settingsServiceProvider )
|
|
|
|
return ;
|
2006-08-29 04:31:20 +00:00
|
|
|
if ( arg instanceof DictionarySettings )
|
2006-08-12 22:31:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// compare with the current settings
|
|
|
|
//
|
2006-08-29 04:31:20 +00:00
|
|
|
DictionarySettings newSettings =
|
|
|
|
DictionarySettings.cloneDs ( GlobalResourceHolder.
|
|
|
|
settingsServiceProvider.getDictionarySettings () ) ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
2006-08-19 04:43:23 +00:00
|
|
|
if ( ! newSettings.equal ( GlobalResourceHolder.dictionarySettings ) )
|
2006-08-12 22:31:51 +00:00
|
|
|
{
|
2006-08-19 04:43:23 +00:00
|
|
|
GlobalResourceHolder.dictionarySettings.set ( newSettings ) ;
|
2006-08-26 01:39:37 +00:00
|
|
|
GlobalResourceHolder.dictionary = GlobalResourceHolder.loadDictionary () ;
|
2006-08-12 22:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Listener listenerObj = null ;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
2006-08-19 04:43:23 +00:00
|
|
|
listenerObj = new Listener () ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// we have no service provider until the client class is born
|
|
|
|
//
|
|
|
|
settingsServiceProvider = null ;
|
|
|
|
|
|
|
|
//
|
|
|
|
// init the font array
|
|
|
|
//
|
|
|
|
//
|
|
|
|
baseTMWFont = new Font [ NUM_FONTS ] ;
|
|
|
|
derivedTMWFont = new Font [ NUM_FONTS ] ;
|
|
|
|
|
|
|
|
//
|
|
|
|
// if TibetanMachineWeb fonts are not installed
|
|
|
|
// load the fonts from the .jar file and set
|
|
|
|
// the flag
|
|
|
|
//
|
|
|
|
if ( ! isTMWInstalled () )
|
|
|
|
setLocalFonts () ;
|
|
|
|
|
|
|
|
//
|
|
|
|
// initialize the dictionary stuff
|
|
|
|
//
|
2006-08-19 04:43:23 +00:00
|
|
|
dictionarySettings = new DictionarySettings () ;
|
2006-08-12 22:31:51 +00:00
|
|
|
dictionaryLoadLock = new Object () ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public static Listener getListener ()
|
|
|
|
{
|
|
|
|
return listenerObj ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static int getDictionaryLoadState ()
|
|
|
|
{
|
|
|
|
int state ;
|
|
|
|
synchronized ( dictionaryLoadLock )
|
|
|
|
{
|
|
|
|
state = dictionaryLoadState ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state ;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void waitForDictionary ()
|
|
|
|
{
|
|
|
|
synchronized ( dictionaryLoadLock )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-26 01:39:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static DictionaryInterface getDictionary ()
|
|
|
|
{
|
|
|
|
return dictionary ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* loadDictionary
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected static DictionaryInterface loadDictionary ()
|
|
|
|
{
|
|
|
|
DictionaryInterface d = null ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
2006-08-19 04:43:23 +00:00
|
|
|
if ( ! dictionarySettings.isEnabled () )
|
2006-08-12 22:31:51 +00:00
|
|
|
return null ;
|
|
|
|
|
2006-08-19 04:43:23 +00:00
|
|
|
dictionarySettings.setValid ( false ) ;
|
2006-08-12 22:31:51 +00:00
|
|
|
|
|
|
|
synchronized ( dictionaryLoadLock )
|
|
|
|
{
|
|
|
|
dictionaryLoadState = DictionaryLoadState.UNDEFINED ;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2006-08-19 04:43:23 +00:00
|
|
|
if ( dictionarySettings.isLocal () )
|
2006-08-12 22:31:51 +00:00
|
|
|
{
|
2006-08-26 01:39:37 +00:00
|
|
|
d = new ScannerBasedDictionary ( new LocalTibetanScanner ( dictionarySettings.getPathOrUrl () ) ) ;
|
2006-08-19 04:43:23 +00:00
|
|
|
dictionarySettings.setValid ( true ) ;
|
2006-08-12 22:31:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-26 01:39:37 +00:00
|
|
|
d = new ScannerBasedDictionary ( new RemoteTibetanScanner ( dictionarySettings.getPathOrUrl () ) ) ;
|
2006-08-19 04:43:23 +00:00
|
|
|
dictionarySettings.setValid ( true ) ;
|
2006-08-12 22:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dictionaryLoadState = DictionaryLoadState.READY ;
|
|
|
|
}
|
|
|
|
catch ( Exception e )
|
|
|
|
{
|
|
|
|
System.err.println ( "TibetanScanner.loadDictionaryScanner () --> " + e.toString () ) ;
|
|
|
|
dictionaryLoadState = DictionaryLoadState.ERROR ;
|
|
|
|
}
|
2006-08-19 21:47:03 +00:00
|
|
|
}
|
2006-08-12 22:31:51 +00:00
|
|
|
|
2006-08-26 01:39:37 +00:00
|
|
|
return d ;
|
|
|
|
}
|
2006-08-12 22:31:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* isTMWInstalled ()
|
|
|
|
*
|
|
|
|
* returns true if TibetanMachineWeb fonts are
|
|
|
|
* installed on the system.
|
|
|
|
*/
|
|
|
|
protected static boolean isTMWInstalled ()
|
|
|
|
{
|
|
|
|
String [] familyNames =
|
|
|
|
GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames () ;
|
|
|
|
|
|
|
|
int mask = 0 ;
|
|
|
|
|
|
|
|
for ( int i = 0; i < familyNames.length; i++ )
|
|
|
|
{
|
|
|
|
String family = familyNames [i] ;
|
|
|
|
|
|
|
|
if ( family.startsWith ( FONT_NAME_PREFIX ) )
|
|
|
|
{
|
|
|
|
int fontNum = 0 ;
|
|
|
|
|
|
|
|
if ( family == FONT_NAME_PREFIX )
|
|
|
|
{
|
|
|
|
fontNum = 1 ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fontNum = Character.digit ( family.charAt ( family.length () - 1 ), 10 ) ;
|
|
|
|
|
|
|
|
if ( fontNum < 0 || fontNum > NUM_FONTS )
|
|
|
|
{
|
|
|
|
fontNum = 0 ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( fontNum > 0 )
|
|
|
|
{
|
|
|
|
mask |= ( 1 << (fontNum-1) ) ;
|
|
|
|
if ( ( ( 1 << NUM_FONTS ) - 1 ) == mask )
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setLocalFonts ()
|
|
|
|
*
|
|
|
|
* loads the fonts from Fonts directory in
|
|
|
|
* the .jar file, if the dir exists
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected static void setLocalFonts ()
|
|
|
|
{
|
|
|
|
nameToNumber = new HashMap () ;
|
|
|
|
|
|
|
|
for ( int i = 0; i < NUM_FONTS; i++ )
|
|
|
|
{
|
|
|
|
String suffix = ((i>0)?String.valueOf (i):"") ;
|
|
|
|
URL url =
|
|
|
|
GlobalResourceHolder.class.getResource ( "/Fonts/TibetanMachineWeb/timwn" + suffix + ".ttf" ) ;
|
|
|
|
|
|
|
|
if ( null != url )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
URLConnection conn = url.openConnection () ;
|
|
|
|
InputStream is = conn.getInputStream () ;
|
|
|
|
baseTMWFont [i] = Font.createFont ( Font.TRUETYPE_FONT, is ) ;
|
|
|
|
is.close () ;
|
|
|
|
}
|
|
|
|
catch ( Exception e )
|
|
|
|
{
|
|
|
|
System.err.println ( "Failed to load fonts from JAR !" ) ;
|
|
|
|
flagIsUsingLocalFonts = false ;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// we use a hash map to translate font names into their numbers (indices)
|
|
|
|
//
|
|
|
|
nameToNumber.put ( FONT_NAME_PREFIX + ( ( 0 == i ) ? "" : String.valueOf (i) ), new Integer ( i ) ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
flagIsUsingLocalFonts = true ;
|
|
|
|
}
|
|
|
|
|
2006-08-19 21:47:03 +00:00
|
|
|
/*
|
|
|
|
* setFontSize
|
|
|
|
*
|
|
|
|
* notifies the GlobalResource about the most probably used font size
|
|
|
|
*
|
2006-08-12 22:31:51 +00:00
|
|
|
*/
|
|
|
|
protected static void setFontSize ( int fontSize )
|
|
|
|
{
|
|
|
|
if ( !flagIsUsingLocalFonts )
|
|
|
|
return ;
|
|
|
|
|
|
|
|
if ( null == derivedTMWFont [0] || fontSize != derivedTMWFont [0].getSize () )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < NUM_FONTS; i++ )
|
|
|
|
{
|
|
|
|
derivedTMWFont [i] = baseTMWFont [i].deriveFont ( Font.PLAIN, fontSize ) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getFont ( fontName, size )
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static Font getFont ( String fontName, int fontSize )
|
|
|
|
{
|
|
|
|
Integer intObj = (Integer)nameToNumber.get ( fontName ) ;
|
|
|
|
|
|
|
|
if ( intObj != null )
|
|
|
|
{
|
|
|
|
int fontNum = intObj.intValue () ;
|
|
|
|
return getFont ( fontNum, fontSize ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getFont ( fontNum, size )
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static Font getFont ( int fontNum, int fontSize )
|
|
|
|
{
|
|
|
|
Font font = null ;
|
|
|
|
|
|
|
|
if ( flagIsUsingLocalFonts )
|
|
|
|
{
|
|
|
|
if ( null == derivedTMWFont [fontNum] || fontSize != derivedTMWFont [fontNum].getSize () )
|
|
|
|
{
|
|
|
|
font = derivedTMWFont [fontNum] = baseTMWFont [fontNum].deriveFont ( Font.PLAIN, fontSize ) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return font ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* isUsingLocalFonts ()
|
|
|
|
*
|
|
|
|
* returns true if the app should use the fonts from the package .jar file
|
|
|
|
* (==> fonts are not installed on the system)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static boolean isUsingLocalFonts ()
|
|
|
|
{
|
|
|
|
return flagIsUsingLocalFonts ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static void setSettingsServiceProvider ( SettingsServiceProvider theSettingsServiceProvider )
|
|
|
|
{
|
|
|
|
settingsServiceProvider = theSettingsServiceProvider ;
|
|
|
|
theSettingsServiceProvider.getObservable ().addObserver ( listenerObj ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static void removeSettingsServiceProvider
|
|
|
|
( SettingsServiceProvider theSettingsServiceProvider )
|
|
|
|
{
|
|
|
|
settingsServiceProvider = null ;
|
|
|
|
theSettingsServiceProvider.getObservable ().deleteObserver ( listenerObj ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2006-08-19 04:43:23 +00:00
|
|
|
public static DictionarySettings getDictionarySettings ()
|
2006-08-12 22:31:51 +00:00
|
|
|
{
|
2006-08-19 04:43:23 +00:00
|
|
|
return dictionarySettings ;
|
|
|
|
}
|
2006-08-12 22:31:51 +00:00
|
|
|
};
|