Added a flexible mechanism for persistent boolean-, integer-, and

string-valued preferences built atop java.util.Properties.

How it works: the jvm is asked first, and then the user's prefs file, if it exists,
then the system-wide prefs file, and then the built-in preferences.  Finally, for
robustness, a default may be optionally hard-coded in the source.

I made several things configurable, too:

the default Tibetan keyboard
the default font sizes and faces
whether you want developer-only features enabled
Savant's file extension (.savant)
etc.

The only known problems are the following:

The default location for the user's preferences file is windows-specific,
arbitrary, and not in the user documentation.  Likewise for the location of the
system-wide preferences file.  You can change them using 'java -D', though.

There is no "Save preferences" option yet, and closing the program does
not save preferences either.
This commit is contained in:
dchandler 2002-10-14 04:06:05 +00:00
parent b914309dba
commit 08e4e2fc57
8 changed files with 591 additions and 101 deletions

View file

@ -33,6 +33,7 @@ import java.io.*;
import javax.swing.text.rtf.*;
import org.thdl.util.ThdlDebug;
import org.thdl.util.ThdlOptions;
import org.thdl.util.StatusBar;
/**
@ -284,6 +285,20 @@ public RTFEditorKit rtfEd = null;
}
*/
private static int defaultTibFontSize() {
// FIXME: at program exit, or when the user selects "Save
// preferences", or somehow, save the value the users chooses:
return ThdlOptions.getIntegerOption("thdl.default.tibetan.font.size",
36);
}
private static int defaultRomanFontSize() {
// FIXME: at program exit, or when the user selects "Save
// preferences", or somehow, save the value the users chooses:
return ThdlOptions.getIntegerOption("thdl.default.roman.font.size",
14);
}
/**
* This method sets up the editor, assigns fonts and point sizes,
* sets the document, the caret, and adds key and focus listeners.
@ -298,15 +313,16 @@ public RTFEditorKit rtfEd = null;
styleContext = new StyleContext();
doc = new TibetanDocument(styleContext);
doc.setTibetanFontSize(36);
doc.setTibetanFontSize(defaultTibFontSize());
setDocument(doc);
Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontFamily(defaultStyle, "TibetanMachineWeb");
StyleConstants.setFontSize(defaultStyle, 36);
StyleConstants.setFontSize(defaultStyle, defaultTibFontSize()); // FIXME make pref
romanFontFamily = "Serif";
romanFontSize = 14;
romanFontFamily = ThdlOptions.getStringOption("thdl.default.roman.font.face",
"Serif"); // FIXME write out this preference.
romanFontSize = defaultRomanFontSize(); // FIXME make pref
setRomanAttributeSet(romanFontFamily, romanFontSize);
// newDocument();
@ -403,12 +419,12 @@ public RTFEditorKit rtfEd = null;
styleContext = new StyleContext();
doc = new TibetanDocument(styleContext);
doc.setTibetanFontSize(36);
doc.setTibetanFontSize(defaultTibFontSize());
setDocument(doc);
Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontFamily(defaultStyle, "TibetanMachineWeb");
StyleConstants.setFontSize(defaultStyle, 36);
StyleConstants.setFontSize(defaultStyle, defaultTibFontSize());
}
/**
@ -1006,9 +1022,9 @@ public void paste(int offset) {
e.consume();
initKeyboard();
if (isTibetan)
doc.appendDuff(caret.getDot(),"\n",TibetanMachineWeb.getAttributeSet(1));
doc.appendDuff(caret.getDot(),"\n",TibetanMachineWeb.getAttributeSet(1)); // FIXME does this work on all platforms?
else
append("\n", romanAttributeSet);
append("\n", romanAttributeSet); // FIXME does this work on all platforms?
break;
}
}
@ -1455,10 +1471,10 @@ public void paste(int offset) {
*/
public void toTibetanMachineWeb(String wylie, int offset) {
try {
StringTokenizer sTok = new StringTokenizer(wylie, "\n\t", true);
StringTokenizer sTok = new StringTokenizer(wylie, "\n\t", true); // FIXME does this work on all platforms?
while (sTok.hasMoreTokens()) {
String next = sTok.nextToken();
if (next.equals("\n") || next.equals("\t")) {
if (next.equals("\n") || next.equals("\t")) { // FIXME does this work on all platforms?
try {
doc.insertString(offset, next, null);
offset++;

View file

@ -33,6 +33,7 @@ import javax.swing.text.rtf.*;
import org.thdl.tib.text.*;
import org.thdl.util.ThdlDebug;
import org.thdl.util.ThdlOptions;
import org.thdl.util.StatusBar;
import org.thdl.util.ThdlActionListener;
import org.thdl.util.RTFPane;
@ -80,7 +81,6 @@ public class Jskad extends JPanel implements DocumentListener {
private final String keybd4HelpFile = "Sambhota_keymap_one.rtf";
private String fontName = "";
private JComboBox fontFamilies, fontSizes;
private JFileChooser fileChooser;
private javax.swing.filechooser.FileFilter rtfFilter;
@ -90,7 +90,6 @@ public class Jskad extends JPanel implements DocumentListener {
private static int numberOfTibsRTFOpen = 0;
private static int x_size;
private static int y_size;
private TibetanKeyboard sambhota1Keyboard = null, duff1Keyboard = null, duff2Keyboard = null;
/**
* The text editing window which this Jskad object embeds.
@ -115,10 +114,12 @@ public class Jskad extends JPanel implements DocumentListener {
* is a JApplet then the File menu is omitted from the menu bar.
*/
public Jskad(final Object parent) {
if (Boolean.getBoolean("thdl.Jskad.disable.status.bar")) {
if (ThdlOptions.getBooleanOption("thdl.Jskad.disable.status.bar")) {
statusBar = null;
} else {
statusBar = new StatusBar("Welcome to Jskad!");
statusBar
= new StatusBar(ThdlOptions.getStringOption("thdl.Jskad.initial.status.message",
"Welcome to Jskad!"));
}
parentObject = parent;
numberOfTibsRTFOpen++;
@ -378,49 +379,35 @@ public class Jskad extends JPanel implements DocumentListener {
toolBar.add(new JLabel("Keyboard:"));
toolBar.addSeparator();
/* Initialize dp before calling installKeyboard. */
if (ThdlOptions.getBooleanOption(Jskad.enableKeypressStatusProp)) {
dp = new DuffPane(statusBar);
} else {
dp = new DuffPane();
}
String[] keyboard_options = {keybd1Description,
keybd2Description,
keybd3Description,
keybd4Description};
final JComboBox keyboards = new JComboBox(keyboard_options);
int initialKeyboard
= ThdlOptions.getIntegerOption("thdl.default.tibetan.keyboard", 0);
try {
keyboards.setSelectedIndex(initialKeyboard);
} catch (IllegalArgumentException e) {
keyboards.setSelectedIndex(0); // good ol' Wylie
}
installKeyboard(initialKeyboard);
keyboards.addActionListener(new ThdlActionListener() {
public void theRealActionPerformed(ActionEvent e) {
switch (keyboards.getSelectedIndex()) {
case 0: //Extended Wylie
installKeyboard("wylie");
break;
case 1: //TCC 1
if (duff1Keyboard == null)
duff1Keyboard = installKeyboard("tcc1");
else
dp.registerKeyboard(duff1Keyboard);
break;
case 2: //TCC 2
if (duff2Keyboard == null)
duff2Keyboard = installKeyboard("tcc2");
else
dp.registerKeyboard(duff2Keyboard);
break;
case 3: //Sambhota
if (sambhota1Keyboard == null)
sambhota1Keyboard = installKeyboard("sambhota1");
else
dp.registerKeyboard(sambhota1Keyboard);
break;
}
installKeyboard(keyboards.getSelectedIndex());
}
});
toolBar.add(keyboards);
toolBar.add(Box.createHorizontalGlue());
if (Boolean.getBoolean(Jskad.enableKeypressStatusProp)) {
dp = new DuffPane(statusBar);
} else {
dp = new DuffPane();
}
JScrollPane sp = new JScrollPane(dp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
dp.getDocument().addDocumentListener(this);
@ -786,7 +773,7 @@ public class Jskad extends JPanel implements DocumentListener {
try {
BufferedReader in = new BufferedReader(new FileReader(txt_fileChosen));
DuffPane dp2;
if (Boolean.getBoolean(Jskad.enableKeypressStatusProp)) {
if (ThdlOptions.getBooleanOption(Jskad.enableKeypressStatusProp)) {
dp2 = new DuffPane(statusBar);
} else {
dp2 = new DuffPane();
@ -812,7 +799,41 @@ public class Jskad extends JPanel implements DocumentListener {
}
}
private TibetanKeyboard installKeyboard(String name) {
private TibetanKeyboard sambhota1Keyboard = null, duff1Keyboard = null, duff2Keyboard = null;
private void installKeyboard(int keyboardIndex) {
switch (keyboardIndex) {
case 1: //TCC 1
if (duff1Keyboard == null)
duff1Keyboard = installKeyboard("tcc1");
else
dp.registerKeyboard(duff1Keyboard);
break;
case 2: //TCC 2
if (duff2Keyboard == null)
duff2Keyboard = installKeyboard("tcc2");
else
dp.registerKeyboard(duff2Keyboard);
break;
case 3: //Sambhota
if (sambhota1Keyboard == null)
sambhota1Keyboard = installKeyboard("sambhota1");
else
dp.registerKeyboard(sambhota1Keyboard);
break;
default: //Extended Wylie
if (0 != keyboardIndex
&& ThdlOptions.getBooleanOption("thdl.debug")) {
throw new Error("You set thdl.default.tibetan.keyboard to an illegal value: "
+ keyboardIndex);
}
installKeyboard("wylie");
break;
}
}
private TibetanKeyboard installKeyboard(String name) {
TibetanKeyboard tk = null;
if (!name.equalsIgnoreCase("wylie")) {
@ -833,6 +854,7 @@ public class Jskad extends JPanel implements DocumentListener {
}
catch (TibetanKeyboard.InvalidKeyboardException ike) {
System.out.println("invalid keyboard file or file not found");
ThdlDebug.noteIffyCode();
return null;
}
}
@ -840,6 +862,7 @@ public class Jskad extends JPanel implements DocumentListener {
return null;
}
ThdlDebug.verify("dp != null", dp != null);
dp.registerKeyboard(tk);
return tk;
}
@ -1074,22 +1097,27 @@ public class Jskad extends JPanel implements DocumentListener {
* you discover a bug, please send us an email, making sure to include
* the jskad.log file as an attachment. */
public static void main(String[] args) {
ThdlDebug.attemptToSetUpLogFile("jskad", ".log");
try {
ThdlDebug.attemptToSetUpLogFile("jskad", ".log");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
JFrame f = new JFrame("Jskad");
Dimension d = f.getToolkit().getScreenSize();
x_size = d.width/4*3;
y_size = d.height/4*3;
f.setSize(x_size, y_size);
f.setLocation(d.width/8, d.height/8);
f.getContentPane().add(new Jskad(f));
f.setVisible(true);
JFrame f = new JFrame("Jskad");
Dimension d = f.getToolkit().getScreenSize();
x_size = d.width/4*3;
y_size = d.height/4*3;
f.setSize(x_size, y_size);
f.setLocation(d.width/8, d.height/8);
f.getContentPane().add(new Jskad(f));
f.setVisible(true);
} catch (ThdlLazyException e) {
System.err.println("Jskad has a BUG:");
e.getRealException().printStackTrace(System.err);
}
}
}