1. Dictionary support in Jskad

2. Local fonts from the .jar file are used if TWM fonts not available on the system
3. Bug fix - SWING not rendering font correctly
This commit is contained in:
micha3lp 2006-08-12 22:23:01 +00:00
parent ced3767335
commit 592d1d6870
5 changed files with 823 additions and 21 deletions

View file

@ -21,22 +21,54 @@ package org.thdl.tib.input;
import java.awt.Component;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.beans.PropertyChangeListener ;
import java.beans.PropertyChangeEvent ;
import org.thdl.tib.input.DictionaryLoadState ;
/** Shows a standard dialog window to set the preferences
for the tibetan and roman script used
*/
public class PreferenceWindow
public class PreferenceWindow implements ActionListener, ItemListener
{
private JDialog dialog;
private JComboBox tibetanFontSizes;
private JComboBox romanFontSizes;
private JComboBox romanFontFamilies;
private JCheckBox dictionaryEnabled ;
private ButtonGroup dictionaryType ;
private JRadioButton dictionaryLocal ;
private JRadioButton dictionaryRemote ;
private JLabel dictionaryPathLabel ;
private JTextField dictionaryPath ;
private JButton dictionaryBrowse ;
private JOptionPane pane ;
boolean valDictionaryEnabled ;
boolean valDictionaryLocal ;
String valDictionaryPath ;
private DuffPane dp;
public PreferenceWindow(Component parent, DuffPane dp)
@ -45,6 +77,14 @@ public class PreferenceWindow
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = genv.getAvailableFontFamilyNames();
// TODO
valDictionaryEnabled = dp.getDictionarySettingsEnabled () ;
valDictionaryLocal = dp.getDictionarySettingsLocal () ;
valDictionaryPath = dp.getDictionarySettingsPath () ;
//dp.getDictionarySettings ( valDictionaryEnabled, valDictionaryLocal, valDictionaryPath ) ;
JPanel tibetanPanel;
tibetanPanel = new JPanel();
@ -71,13 +111,101 @@ public class PreferenceWindow
romanPanel.add(romanFontFamilies);
romanPanel.add(romanFontSizes);
JPanel dictionaryPanel ;
dictionaryPanel = new JPanel () ;
dictionaryPanel.setBorder ( BorderFactory.createTitledBorder ( "Dictionary" ) ) ;
dictionaryEnabled = new JCheckBox ( "Enable Dictionary Support" ) ;
dictionaryEnabled.setMnemonic ( KeyEvent.VK_E ) ;
dictionaryEnabled.setActionCommand ( "SetDictionaryEnable" ) ;
dictionaryEnabled.addItemListener ( this ) ;
dictionaryLocal = new JRadioButton ( "Local") ;
dictionaryLocal.setActionCommand ( "SetDictionaryLocal" ) ;
dictionaryLocal.setMnemonic ( KeyEvent.VK_L ) ;
dictionaryLocal.addActionListener ( this ) ;
dictionaryRemote = new JRadioButton ( "Remote") ;
dictionaryRemote.setMnemonic ( KeyEvent.VK_R ) ;
dictionaryRemote.setActionCommand ( "SetDictionaryRemote" ) ;
dictionaryRemote.addActionListener ( this ) ;
dictionaryPathLabel = new JLabel ( "Path" ) ;
dictionaryPath = new JTextField () ;
dictionaryBrowse = new JButton ( "Browse" ) ;
dictionaryBrowse.setMnemonic ( KeyEvent.VK_B ) ;
dictionaryBrowse.setActionCommand ( "BrowseDictionaryPath" ) ;
dictionaryBrowse.addActionListener ( this ) ;
dictionaryPanel.setLayout ( new GridBagLayout () ) ;
GridBagConstraints gc = new GridBagConstraints () ;
gc.anchor = GridBagConstraints.NORTHWEST ;
gc.fill = GridBagConstraints.BOTH ;
gc.weightx = 1.0 ;
gc.weighty = 1.0 ;
gc.gridwidth = GridBagConstraints.REMAINDER ;
gc.insets = new Insets ( 1, 1, 1, 1 ) ;
dictionaryPanel.add ( dictionaryEnabled, gc ) ;
gc.gridwidth = GridBagConstraints.RELATIVE ;
dictionaryPanel.add ( dictionaryLocal, gc ) ;
gc.weightx = 0.0 ;
gc.gridwidth = GridBagConstraints.REMAINDER ;
dictionaryPanel.add ( dictionaryRemote, gc ) ;
gc.weightx = 1.0 ;
gc.gridwidth = GridBagConstraints.REMAINDER ;
dictionaryPanel.add ( dictionaryPathLabel, gc ) ;
gc.gridwidth = GridBagConstraints.RELATIVE ;
gc.weightx = 3.0 ;
dictionaryPanel.add ( dictionaryPath, gc ) ;
gc.weightx = 0.0 ;
gc.gridwidth = GridBagConstraints.REMAINDER ;
dictionaryPanel.add ( dictionaryBrowse, gc ) ;
dictionaryType = new ButtonGroup () ;
dictionaryType.add ( dictionaryLocal ) ;
dictionaryType.add ( dictionaryRemote ) ;
JPanel preferencesPanel = new JPanel();
preferencesPanel.setLayout(new GridLayout(2,1));
preferencesPanel.setLayout(new BoxLayout(preferencesPanel,BoxLayout.Y_AXIS));
preferencesPanel.add(tibetanPanel);
preferencesPanel.add(romanPanel);
preferencesPanel.add(dictionaryPanel);
JOptionPane pane = new JOptionPane(preferencesPanel);
pane = new JOptionPane(preferencesPanel);
updateDictionaryGui () ;
dialog = pane.createDialog(parent, "Preferences");
dialog.setDefaultCloseOperation ( JDialog.DO_NOTHING_ON_CLOSE ) ;
pane.addPropertyChangeListener ( new PropertyChangeListener ()
{
public void propertyChange ( PropertyChangeEvent e )
{
if ( /*dialog.isVisible () &&*/ e.getSource () == pane )
{
if ( pane.getValue () != null &&
e.getPropertyName ().equals ( JOptionPane.VALUE_PROPERTY ) )
{
if ( pane.getValue ().toString ().equals ( JOptionPane.UNINITIALIZED_VALUE.toString () ) )
return ;
int val = ((Integer)pane.getValue ()).intValue () ;
if ( val == JOptionPane.OK_OPTION )
{
if ( validateInput () )
{
dialog.setVisible ( false ) ;
}
else
{
dialog.setVisible ( true ) ;
}
}
else if ( val == JOptionPane.CANCEL_OPTION )
{
dialog.setVisible ( false ) ;
}
}
}
}
} ) ;
}
private static int stringToInt(String s)
@ -108,20 +236,194 @@ public class PreferenceWindow
return romanFontFamilies.getSelectedItem().toString();
}
public void setDictionaryEnabled ( boolean enabled )
{
valDictionaryEnabled = enabled ;
}
public boolean getDictionaryEnabled ()
{
return valDictionaryEnabled ;
}
public void setDictionaryLocal ( boolean local )
{
valDictionaryLocal = local ;
}
public boolean getDictionaryLocal ()
{
return valDictionaryLocal ;
}
public void setDictionaryPath ( String path )
{
valDictionaryPath = path ;
}
public String getDictionaryPath ()
{
return valDictionaryPath ;
}
private void readDictionaryGui ()
{
valDictionaryEnabled = dictionaryEnabled.isSelected () ;
valDictionaryLocal = dictionaryLocal.isSelected () ;
valDictionaryPath = dictionaryPath.getText () ;
}
private void updateDictionaryGui ()
{
if ( ! valDictionaryEnabled )
{
dictionaryLocal.setEnabled ( false ) ;
dictionaryRemote.setEnabled ( false ) ;
dictionaryPathLabel.setEnabled ( false ) ;
dictionaryPath.setEnabled ( false ) ;
dictionaryBrowse.setEnabled ( false ) ;
}
else
{
dictionaryLocal.setEnabled ( true ) ;
dictionaryRemote.setEnabled ( true ) ;
if ( valDictionaryLocal )
{
dictionaryPathLabel.setEnabled ( true ) ;
dictionaryPathLabel.setText ( "Path" ) ;
dictionaryPath.setEnabled ( true ) ;
dictionaryBrowse.setEnabled ( true ) ;
}
else
{
dictionaryPathLabel.setEnabled ( true ) ;
dictionaryPathLabel.setText ( "URL" ) ;
dictionaryPath.setEnabled ( true ) ;
dictionaryBrowse.setEnabled ( false ) ;
}
}
if ( valDictionaryEnabled != dictionaryEnabled.isSelected () )
{
dictionaryEnabled.setSelected ( valDictionaryEnabled ) ;
}
if ( valDictionaryLocal != dictionaryLocal.isSelected () )
{
dictionaryLocal.setSelected ( valDictionaryLocal ) ;
dictionaryRemote.setSelected ( !valDictionaryLocal ) ;
}
dictionaryPath.setText ( valDictionaryPath ) ;
}
private void guiSetDictionaryEnabled ( boolean enabled )
{
setDictionaryEnabled ( enabled ) ;
updateDictionaryGui () ;
}
public void actionPerformed ( ActionEvent e )
{
if ( e.getActionCommand () == "SetDictionaryLocal" )
{
setDictionaryLocal ( true ) ;
updateDictionaryGui () ;
}
else if ( e.getActionCommand() == "SetDictionaryRemote" )
{
setDictionaryLocal ( false ) ;
updateDictionaryGui () ;
}
else if ( e.getActionCommand() == "BrowseDictionaryPath" )
{
//TODO
}
}
public void itemStateChanged ( ItemEvent e )
{
Object source = e.getItemSelectable () ;
if ( dictionaryEnabled == source )
{
guiSetDictionaryEnabled ( ( ItemEvent.SELECTED == e.getStateChange () ) ? true : false ) ;
}
}
/** This returns only when the user has closed the dialog */
public void show()
{
int size;
String font;
valDictionaryEnabled = dp.getDictionarySettingsEnabled () ;
valDictionaryLocal = dp.getDictionarySettingsLocal () ;
valDictionaryPath = dp.getDictionarySettingsPath () ;
updateDictionaryGui () ;
dialog.show();
pane.setOptionType ( JOptionPane.OK_CANCEL_OPTION ) ;
dialog.show();
}
/**
*
*/
protected boolean validateInput ()
{
int size;
String font;
readDictionaryGui () ;
valDictionaryEnabled = getDictionaryEnabled () ;
valDictionaryLocal = getDictionaryLocal () ;
valDictionaryPath = getDictionaryPath () ;
size = getTibetanFontSize();
if (size>0) dp.setByUserTibetanFontSize(size);
size = getRomanFontSize();
if (size==-1) size = dp.getRomanFontSize();
font = getRomanFont();
dp.setByUserRomanAttributeSet(font, size);
dp.setByUserRomanAttributeSet(font, size);
dp.setByUserDictionarySettings ( valDictionaryEnabled, valDictionaryLocal, valDictionaryPath ) ;
//
// wait for dictionary status to be either READY or ERROR LOADING
//
dp.waitForDictionary () ;
int dictState = dp.getDictionaryLoadState () ;
if ( DictionaryLoadState.READY == dictState )
{
return true ;
}
else if ( DictionaryLoadState.ERROR == dictState )
{
if ( ask ( "Cannot load dictionary with the current settings\n" +
"Choose Yes to disable the dictionary, or No to correct the settings." ) )
{
dp.setByUserDictionarySettings ( false,
valDictionaryLocal,
valDictionaryPath ) ;
return true ;
}
}
else
{
System.err.println ( "Dictionary load state incorrect." ) ;
}
return false ;
}
/**
*
*/
protected boolean ask ( String question )
{
return (JOptionPane.YES_OPTION ==
JOptionPane.showConfirmDialog ( pane, question, "Question", JOptionPane.YES_NO_OPTION ) ) ;
}
}
}