*** empty log message ***
This commit is contained in:
parent
592d1d6870
commit
a15161fc07
5 changed files with 923 additions and 0 deletions
411
source/org/thdl/tib/input/GlobalResourceHolder.java
Normal file
411
source/org/thdl/tib/input/GlobalResourceHolder.java
Normal file
|
@ -0,0 +1,411 @@
|
|||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
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.SettingsChangeListener ;
|
||||
import org.thdl.tib.input.SettingsServiceProvider ;
|
||||
import org.thdl.tib.input.DictionaryLoadState ;
|
||||
|
||||
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 TibetanScanner tibetanScanner = null ;
|
||||
private static SettingsServiceProvider settingsServiceProvider ;
|
||||
|
||||
private static boolean dictionaryValid = false ;
|
||||
private static boolean dictionaryEnabled = false ;
|
||||
private static boolean dictionaryLocal = false ;
|
||||
private static String dictionaryPath = "" ;
|
||||
|
||||
private static int dictionaryLoadState = DictionaryLoadState.UNDEFINED ;
|
||||
private static Object dictionaryLoadLock = null ;
|
||||
|
||||
//static class Listener implements SettingsChangeListener
|
||||
static class Listener implements Observer
|
||||
{
|
||||
/**
|
||||
* update
|
||||
*
|
||||
*/
|
||||
public void update ( Observable o, Object arg )
|
||||
{
|
||||
onSettingsChange ( ((Integer)arg).intValue () ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* onSettingsChange
|
||||
*
|
||||
*/
|
||||
public void onSettingsChange ( int setting )
|
||||
{
|
||||
if ( null == GlobalResourceHolder.settingsServiceProvider )
|
||||
return ;
|
||||
|
||||
if ( SettingsChangeListener.DICTIONARY_SETTINGS == setting )
|
||||
{
|
||||
//
|
||||
// compare with the current settings
|
||||
//
|
||||
boolean newDictionaryEnabled = GlobalResourceHolder.
|
||||
settingsServiceProvider.getDictionarySettingsEnabled () ;
|
||||
boolean newDictionaryLocal = GlobalResourceHolder.
|
||||
settingsServiceProvider.getDictionarySettingsLocal () ;
|
||||
String newDictionaryPath = GlobalResourceHolder.
|
||||
settingsServiceProvider.getDictionarySettingsPath () ;
|
||||
|
||||
|
||||
if ( newDictionaryEnabled != GlobalResourceHolder.dictionaryEnabled ||
|
||||
newDictionaryLocal != GlobalResourceHolder.dictionaryLocal ||
|
||||
! newDictionaryPath.equals ( GlobalResourceHolder.dictionaryPath ) )
|
||||
{
|
||||
GlobalResourceHolder.dictionaryEnabled = newDictionaryEnabled ;
|
||||
GlobalResourceHolder.dictionaryLocal = newDictionaryLocal ;
|
||||
GlobalResourceHolder.dictionaryPath = newDictionaryPath ;
|
||||
|
||||
|
||||
GlobalResourceHolder.tibetanScanner = GlobalResourceHolder.loadDictionaryScanner () ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Listener listenerObj = null ;
|
||||
|
||||
static
|
||||
{
|
||||
listenerObj = new Listener () ;
|
||||
|
||||
//
|
||||
// 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
|
||||
//
|
||||
tibetanScanner = null ;
|
||||
|
||||
dictionaryValid = false ;
|
||||
dictionaryEnabled = false ;
|
||||
dictionaryLocal = false ;
|
||||
dictionaryPath = "" ;
|
||||
|
||||
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 )
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* loadDictionaryScanner
|
||||
*
|
||||
*/
|
||||
protected static TibetanScanner loadDictionaryScanner ()
|
||||
{
|
||||
//return new LocalTibetanScanner ( "F:/tmp/free/free" ) ;
|
||||
TibetanScanner ts = null ;
|
||||
|
||||
if ( ! dictionaryEnabled )
|
||||
return null ;
|
||||
|
||||
dictionaryValid = false ;
|
||||
|
||||
synchronized ( dictionaryLoadLock )
|
||||
{
|
||||
dictionaryLoadState = DictionaryLoadState.UNDEFINED ;
|
||||
|
||||
try
|
||||
{
|
||||
if ( dictionaryLocal )
|
||||
{
|
||||
ts = new LocalTibetanScanner ( dictionaryPath ) ;
|
||||
dictionaryValid = true ;
|
||||
}
|
||||
else
|
||||
{
|
||||
ts = new RemoteTibetanScanner ( dictionaryPath ) ;
|
||||
dictionaryValid = true ;
|
||||
}
|
||||
|
||||
dictionaryLoadState = DictionaryLoadState.READY ;
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
System.err.println ( "TibetanScanner.loadDictionaryScanner () --> " + e.toString () ) ;
|
||||
dictionaryLoadState = DictionaryLoadState.ERROR ;
|
||||
}
|
||||
}
|
||||
|
||||
return ts ;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 ;
|
||||
}
|
||||
|
||||
/*
|
||||
* setFontSize
|
||||
*
|
||||
* notifies the GlobalResource about the most probably used font size
|
||||
*
|
||||
*/
|
||||
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 ;
|
||||
}
|
||||
|
||||
/*
|
||||
* getTibetanScanner ()
|
||||
*
|
||||
* returns the dictionary scanner object or null
|
||||
*/
|
||||
public static TibetanScanner getTibetanScanner ()
|
||||
{
|
||||
return tibetanScanner ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void setSettingsServiceProvider ( SettingsServiceProvider theSettingsServiceProvider )
|
||||
{
|
||||
settingsServiceProvider = theSettingsServiceProvider ;
|
||||
theSettingsServiceProvider.getObservable ().addObserver ( listenerObj ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void removeSettingsServiceProvider
|
||||
( SettingsServiceProvider theSettingsServiceProvider )
|
||||
{
|
||||
settingsServiceProvider = null ;
|
||||
theSettingsServiceProvider.getObservable ().deleteObserver ( listenerObj ) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isDictionaryValid ()
|
||||
{
|
||||
return dictionaryValid ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isDictionaryEnabled ()
|
||||
{
|
||||
return dictionaryEnabled ;
|
||||
}
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue