diff --git a/source/org/thdl/tib/scanner/BinaryFileGenerator.java b/source/org/thdl/tib/scanner/BinaryFileGenerator.java index 0833370..96d326f 100644 --- a/source/org/thdl/tib/scanner/BinaryFileGenerator.java +++ b/source/org/thdl/tib/scanner/BinaryFileGenerator.java @@ -152,11 +152,9 @@ public class BinaryFileGenerator extends SimplifiedLinkedList { private long posHijos; private String sil, def[]; - private static String delimiter; - private static int delimiterType; - private final static int delimiterGeneric=0; - private final static int delimiterAcip=1; - private final static int delimiterDash=2; + public final static int delimiterGeneric=0; + public final static int delimiterAcip=1; + public final static int delimiterDash=2; /** Number of dictionary. If 0, partial word (no definition). */ @@ -168,8 +166,6 @@ public class BinaryFileGenerator extends SimplifiedLinkedList { wordRaf = null; defRaf = null; - delimiter = null; - delimiterType=delimiterDash; } public BinaryFileGenerator() @@ -181,7 +177,7 @@ public class BinaryFileGenerator extends SimplifiedLinkedList sourceDef = null; } - public BinaryFileGenerator(String sil, String def, int numDef) + private BinaryFileGenerator(String sil, String def, int numDef) { super(); int marker = sil.indexOf(" "); @@ -208,18 +204,18 @@ public class BinaryFileGenerator extends SimplifiedLinkedList return sil; } - private static String deleteQuotes(String s) + private static String deleteQuotes(String s) + { + int length = s.length(); + if (length>2) { - int length = s.length(); - if (length>2) - { - if ((s.charAt(0)=='\"') && (s.charAt(length-1)=='\"')) - return s.substring(1,length-2); - } - return s; + if ((s.charAt(0)=='\"') && (s.charAt(length-1)=='\"')) + return s.substring(1,length-1); } - - public void addFile(String archivo, int defNum) throws Exception + return s; + } + + public void addFile(String archivo, int delimiterType, String delimiter, int defNum) throws Exception { final short newDefiniendum=1, halfDefiniendum=2, definition=3; short status=newDefiniendum; @@ -671,11 +667,30 @@ public class BinaryFileGenerator extends SimplifiedLinkedList System.out.println(" -acip: use this to process dictionaries entered using the ACIP standard"); System.out.println(" to mark page numbers, comments, etc. Make sure to convert it to"); System.out.println(" THDL's extended Wylie scheme first using the AcipToWylie class."); + } + + public void generateDatabase(String name) throws Exception + { + File wordF = new File(name + ".wrd"), defF = new File(name + ".def"); + wordF.delete(); + defF.delete(); + wordRaf = new RandomAccessFile(wordF,"rw"); + defRaf = new RandomAccessFile(defF,"rw"); + print(); + wordRaf.writeInt((int)posHijos); + } public static void main(String args[]) throws Exception { - int i, n=0, a; + int delimiterType; + String delimiter; + + int i, n=0, a; + + delimiter = null; + delimiterType=delimiterDash; + if (args.length==0) { printSintax(); @@ -701,7 +716,7 @@ public class BinaryFileGenerator extends SimplifiedLinkedList printSintax(); return; } - sl.addFile(args[1] + ".txt",0); + sl.addFile(args[1] + ".txt",delimiterType, delimiter, 0); a=1; } else @@ -709,7 +724,7 @@ public class BinaryFileGenerator extends SimplifiedLinkedList a=0; if (args.length==1) { - sl.addFile(args[0] + ".txt",0); + sl.addFile(args[0] + ".txt", delimiterType, delimiter, 0); } else { @@ -737,17 +752,11 @@ public class BinaryFileGenerator extends SimplifiedLinkedList { delimiterType=delimiterDash; } - sl.addFile(args[i] + ".txt", n); + sl.addFile(args[i] + ".txt", delimiterType, delimiter, n); n++; i++; } } } - File wordF = new File(args[a] + ".wrd"), defF = new File(args[a] + ".def"); - wordF.delete(); - defF.delete(); - wordRaf = new RandomAccessFile(wordF,"rw"); - defRaf = new RandomAccessFile(defF,"rw"); - sl.print(); - wordRaf.writeInt((int)sl.posHijos); + sl.generateDatabase(args[a]); } } diff --git a/source/org/thdl/tib/scanner/CreateDatabaseWizard.java b/source/org/thdl/tib/scanner/CreateDatabaseWizard.java new file mode 100644 index 0000000..70e27e2 --- /dev/null +++ b/source/org/thdl/tib/scanner/CreateDatabaseWizard.java @@ -0,0 +1,259 @@ +package org.thdl.tib.scanner; + +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import org.thdl.util.*; +import org.thdl.tib.scanner.*; +import javax.swing.*; + +class CreateDatabaseWizard extends Dialog implements ActionListener, ItemListener +{ + private Label dictNum; + private int numDict, delimiterType; + private Label fileName; + private TextField dicDesc, dicAbb, delimiter; + private Choice dicType; + private List listDicts; + private Button add, ok, cancel, browse; + private Frame owner; + private SimplifiedLinkedList dicts; + private boolean pocketpc; + private String response; + + CreateDatabaseWizard(Frame owner, boolean pocketpc) + { + super(owner, "Generation of Binary Tree Dictionary", true); + this.pocketpc = pocketpc; + this.owner = owner; + response = ""; + dicts = new SimplifiedLinkedList(); + + this.setLayout(new BorderLayout()); + if (pocketpc) + { + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + this.setSize(d); + } else this.setSize(400,300); + Panel p1, p2, p3; + p1 = new Panel(new GridLayout(4, 1)); + p2 = new Panel(new FlowLayout(FlowLayout.LEFT)); + p2.add(new Label("Dictionary ")); + numDict = 1; + dictNum = new Label(Integer.toString(numDict)); + p2.add(dictNum); + p2.add(new Label(":")); + p1.add(p2); + p2 = new Panel(new BorderLayout()); + p2.add(new Label("File name:"), BorderLayout.WEST); + fileName = new Label(); + p2.add(fileName, BorderLayout.CENTER); + browse = new Button("Browse..."); + browse.addActionListener(this); + p2.add(browse, BorderLayout.EAST); + p1.add(p2); + p2 = new Panel(new BorderLayout()); + if (pocketpc) p2.add(new Label("Desc:"), BorderLayout.WEST); + else p2.add(new Label("Description:"), BorderLayout.WEST); + dicDesc = new TextField(20); + p2.add(dicDesc, BorderLayout.CENTER); + p3 = new Panel(new FlowLayout()); + if (pocketpc) p3.add(new Label("Abv:")); + else p3.add(new Label("Abbreviation:")); + dicAbb = new TextField(6); + p3.add(dicAbb); + p2.add(p3, BorderLayout.EAST); + p1.add(p2); + p2 = new Panel(new BorderLayout()); + p2.add(new Label("Type:"), BorderLayout.WEST); + dicType = new Choice(); + dicType.add("Dash separated"); + dicType.add("Tab separated"); + dicType.add("Transcribed ACIP"); + dicType.add("Other:"); + dicType.addItemListener(this); + p2.add(dicType, BorderLayout.CENTER); + p3 = new Panel(new FlowLayout()); + delimiter = new TextField(" - "); + delimiter.setEnabled(false); + p3.add(delimiter); + add = new Button("Add"); + add.setEnabled(false); + add.addActionListener(this); + p3.add(add); + p2.add(p3, BorderLayout.EAST); + p1.add(p2); + this.add(p1, BorderLayout.NORTH); + listDicts = new List(); + this.add(listDicts, BorderLayout.CENTER); + + p1 = new Panel(new FlowLayout()); + ok = new Button("Generate database"); + ok.addActionListener(this); + ok.setEnabled(false); + p1.add(ok); + cancel = new Button("Cancel"); + cancel.addActionListener(this); + p1.add(cancel); + this.add(p1, BorderLayout.SOUTH); + } + + public void actionPerformed(ActionEvent event) + { + FileDialog fd; + DictionaryRecord dr; + Button obj = (Button) event.getSource(); + String fileName, path=""; + int pos; + + if (obj == cancel) + { + this.setVisible(false); + } + if (obj == browse) + { + fd = new FileDialog(owner, "Select dictionary to open", FileDialog.LOAD); + fd.show(); + fileName = fd.getFile(); + if (fileName != null) + { + path = fd.getDirectory() + fileName; + this.fileName.setText(path); + // strip extension + pos = fileName.lastIndexOf('.'); + if (pos>0) fileName = fileName.substring(0, pos); + this.dicDesc.setText(fileName); + if (fileName.length()>4) this.dicAbb.setText(fileName.substring(0,4)); + else this.dicAbb.setText(fileName); + add.setEnabled(true); + } + } + else if (obj == add) + { + dr = new DictionaryRecord(dicDesc.getText()+ "," + dicAbb.getText(), this.fileName.getText(), delimiterType, delimiter.getText()); + listDicts.add(dr.header + " - " + dicType.getSelectedItem() + "; " + dr.fileName); + ok.setEnabled(true); + numDict++; + dictNum.setText(Integer.toString(numDict)); + dicts.addLast(dr); + + dicDesc.setText(""); + dicAbb.setText(""); + this.fileName.setText(""); + + add.setEnabled(false); + } + else if (obj == ok) + { + BinaryFileGenerator bfg = new BinaryFileGenerator(); + AcipToWylie a2w=null; + + // getting the database name + if (pocketpc) + { + fd = new FileDialog(owner, "Save database to", FileDialog.SAVE); + fd.show(); + fileName = fd.getFile(); + if (fileName!= null) + { + // dropping the extension + pos = fileName.lastIndexOf('.'); + if (pos>0) fileName = fileName.substring(0, pos); + path = fd.getDirectory() + fileName; + } + } + else + { + DictionaryFileFilter dicFilter = new DictionaryFileFilter(); + JFileChooser fileChooser = new JFileChooser("Save database to"); + fileChooser.addChoosableFileFilter(dicFilter); + /*setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + + if (fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) + { + setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); + return; + }*/ + if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) + { + path = fileChooser.getSelectedFile().getPath(); + pos = path.lastIndexOf('.'); + if (pos>0) path = path.substring(0, pos); + } + } + + PrintWriter pr=null; + Object dictsArray[] = dicts.toArray(); + try + { + pr = new PrintWriter(new FileOutputStream(path + ".dic")); + } + catch (Exception e) + { + } + + for (pos=0; pos0) fileName = fileName.substring(0, pos); + response = fd.getDirectory() + fileName; + localDict.setText(response); + ok.setEnabled(true); + } + } + else + { + if (dicFilter == null) dicFilter = new DictionaryFileFilter(); + JFileChooser fileChooser = new JFileChooser("Select dictionary to open"); + fileChooser.addChoosableFileFilter(dicFilter); + /*setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + + if (fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) + { + setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); + return; + }*/ + if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) + { + response = fileChooser.getSelectedFile().getPath(); + response = response.substring(0, response.length()-4); + localDict.setText(response); + ok.setEnabled(true); + } + } + } + } + + /** Implement the disabling of other guys here + */ + public void itemStateChanged(ItemEvent e) + { + Object obj = e.getSource(); + if (obj instanceof Checkbox) + { + Checkbox chx = (Checkbox) obj; + //System.out.println(obj); + if (chx == useOnline) + { + localDict.setEnabled(false); + browse.setEnabled(false); + availDictsOnline.setEnabled(true); + ok.setEnabled(true); + response = dictsOnline[availDictsOnline.getSelectedIndex()]; + } + else if (chx == useOffline) + { + localDict.setEnabled(true); + browse.setEnabled(true); + if (availDictsOnline!=null) availDictsOnline.setEnabled(false); + ok.setEnabled(!localDict.getText().equals("")); + response = localDict.getText(); + } + else if (chx == createNewDictDB) + { + localDict.setEnabled(false); + browse.setEnabled(false); + if (availDictsOnline!=null) availDictsOnline.setEnabled(false); + ok.setEnabled(true); + response=""; + } + } + else if (obj instanceof Choice) + { + Choice ch = (Choice) obj; + response = dictsOnline[ch.getSelectedIndex()]; + } + } + + public String getResponse() + { + return response; + } + + public boolean getDefaultOption() + { + return ckbDefault.getState(); + } +} \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/WindowScannerFilter.java b/source/org/thdl/tib/scanner/WindowScannerFilter.java index 37f1a96..3aaaf74 100644 --- a/source/org/thdl/tib/scanner/WindowScannerFilter.java +++ b/source/org/thdl/tib/scanner/WindowScannerFilter.java @@ -28,6 +28,7 @@ import java.awt.datatransfer.*; import javax.swing.text.JTextComponent; import javax.swing.JOptionPane; import org.thdl.tib.input.DuffPane; +import org.thdl.util.*; /** Provides a graphical interfase to input Tibetan text (Roman or Tibetan script) and displays the words (Roman or Tibetan script) @@ -44,35 +45,91 @@ import org.thdl.tib.input.DuffPane; */ public class WindowScannerFilter implements WindowListener, FocusListener, ActionListener, ItemListener { + private static String defOpenOption = "thdl.scanner.default.open"; + private ScannerPanel sp; - private MenuItem mnuExit, mnuCut, mnuCopy, mnuPaste, mnuDelete, mnuSelectAll, mnuAbout, mnuClear; + private MenuItem mnuExit, mnuCut, mnuCopy, mnuPaste, mnuDelete, mnuSelectAll, mnuAbout, mnuClear, mnuOpen; private CheckboxMenuItem tibScript, mnuDicts; private Object objModified; - private Frame frame; - private Dialog diagAbout; - private boolean usingSwing; + private AboutDialog diagAbout; + private Frame mainWindow; + private boolean pocketpc; + + public WindowScannerFilter(boolean pocketpc) + { + String response; + this.pocketpc = pocketpc; + + if (!pocketpc) response = ThdlOptions.getStringOption(defOpenOption); + else response=null; + if (response==null || response.equals("")) + { + mainWindow = new Frame("Tibetan Scanner"); + mainWindow.show(); + mainWindow.toFront(); + WhichDictionaryFrame wdf = new WhichDictionaryFrame(mainWindow, pocketpc); + + wdf.show(); + response = wdf.getResponse(); + if (response.equals("")) System.exit(0); + else + { + if (!pocketpc && wdf.getDefaultOption()) + { + ThdlOptions.setUserPreference(defOpenOption, response); + try + { + ThdlOptions.saveUserPreferences(); + } + catch (Exception e) + { + } + } + } + } + makeWindow (response); + } + + public WindowScannerFilter() + { + this(false); + } public WindowScannerFilter(String file) { - this (file, false); + pocketpc = false; + mainWindow = null; + makeWindow (file); } - - public WindowScannerFilter(String file, boolean ipaq) + + public WindowScannerFilter(String file, boolean pocketpc) { - frame = new Frame("Tibetan Scanner"); - frame.setLayout(new GridLayout(1,1)); - frame.setBackground(Color.white); + this.pocketpc = pocketpc; + mainWindow = null; + makeWindow (file); + } + + public void makeWindow(String file) + { + if (mainWindow==null) mainWindow = new Frame("Tibetan Scanner"); + else mainWindow.setVisible(false); + mainWindow.setLayout(new GridLayout(1,1)); + mainWindow.setBackground(Color.white); + diagAbout = null; - usingSwing=!ipaq; + mnuAbout = null; Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); - if (usingSwing) + if (!pocketpc) sp = new DuffScannerPanel(file); else sp = new SimpleScannerPanel(file, d.width >d.height); MenuBar mb = new MenuBar(); - Menu m = new Menu ("File"); + Menu m = new Menu ("File"); + mnuOpen = new MenuItem("Open..."); + mnuOpen.addActionListener(this); + m.add(mnuOpen); mnuExit = new MenuItem("Exit"); mnuExit.addActionListener(this); m.add(mnuExit); @@ -104,7 +161,7 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio mnuClear.addActionListener(this); mb.add(m); m = new Menu("View"); - if (usingSwing) + if (!pocketpc) { tibScript = new CheckboxMenuItem("Tibetan Script", true); m.add(tibScript); @@ -120,47 +177,70 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio } mb.add(m); - m = new Menu("Help"); - mnuAbout = new MenuItem("About..."); - m.add(mnuAbout); - mnuAbout.addActionListener(this); - mb.add(m); + if (!pocketpc) + { + m = new Menu("Help"); + mnuAbout = new MenuItem("About..."); + m.add(mnuAbout); + mnuAbout.addActionListener(this); + mb.add(m); + } // disable menus - focusLost(null); + focusLost(null); + + sp.addFocusListener(this); - frame.setMenuBar(mb); - frame.add(sp); - frame.addWindowListener(this); - frame.setSize(d); - // frame.setSize(240,320); - //else frame.setSize(500,600); - frame.show(); + mainWindow.setMenuBar(mb); + mainWindow.add(sp); + mainWindow.addWindowListener(this); + mainWindow.setSize(d); + // mainWindow.setSize(240,320); + //else mainWindow.setSize(500,600); + mainWindow.show(); + mainWindow.toFront(); - if (usingSwing) + if (pocketpc || !ThdlOptions.getBooleanOption(AboutDialog.windowAboutOption)) { - //JOptionPane.showMessageDialog(frame, TibetanScanner.aboutUnicode, "About", JOptionPane.PLAIN_MESSAGE); - diagAbout = new AboutDialog(frame, true); + diagAbout = new AboutDialog(mainWindow, pocketpc); + diagAbout.show(); + + if (!pocketpc && diagAbout.omitNextTime()) + { + ThdlOptions.setUserPreference(AboutDialog.windowAboutOption, true); + try + { + ThdlOptions.saveUserPreferences(); + } + catch(Exception e) + { + } + } } - else if (diagAbout==null) - { - diagAbout = new AboutDialog(frame, false); - } - diagAbout.show(); } - + public static void main(String[] args) { - if (args.length!=1 && args.length!=2) + switch(args.length) { + case 0: + new WindowScannerFilter(); + break; + case 1: + if (args[0].length()>0 && args[0].charAt(0)=='-') new WindowScannerFilter(true); + else new WindowScannerFilter(args[0]); + break; + case 2: + new WindowScannerFilter(args[1], true); + break; + default: + System.out.println("Syntax error!"); System.out.println("Sintaxis: java WindowScannerFilter [options] arch-dict"); System.out.println("Options:"); System.out.println(" -simple: runs the non-swing version."); System.out.println(TibetanScanner.copyrightASCII); - return; } - new WindowScannerFilter(args[args.length-1], args.length == 2); } @@ -288,30 +368,44 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio /* FIXME: what happens if this throws an exception? We'll just see it on the console--it won't terminate the program. And the user may not see the console! See ThdlActionListener. -DC */ - public void actionPerformed(ActionEvent e) + public void actionPerformed(ActionEvent event) { - Object clicked = e.getSource(); + Object clicked = event.getSource(); StringSelection ss; String s = null; if (clicked == mnuAbout) { - if (usingSwing) - { - //JOptionPane.showMessageDialog(frame, TibetanScanner.aboutUnicode, "About", JOptionPane.PLAIN_MESSAGE); - diagAbout = new AboutDialog(frame, true); - } - else if (diagAbout==null) - { - diagAbout = new AboutDialog(frame, false); - } + if (diagAbout==null) diagAbout = new AboutDialog(mainWindow, pocketpc); diagAbout.show(); + if (!pocketpc) + { + ThdlOptions.setUserPreference(AboutDialog.windowAboutOption, diagAbout.omitNextTime()); + try + { + ThdlOptions.saveUserPreferences(); + } + catch(Exception e) + { + } + } } else if (clicked == mnuClear) { sp.clear(); } - else + else if (clicked == mnuOpen) + { + mainWindow.setVisible(false); + mainWindow.dispose(); + if (!pocketpc) ThdlOptions.setUserPreference(defOpenOption, ""); + new WindowScannerFilter(pocketpc); + } + else if (clicked == mnuExit) + { + System.exit(0); + } + else { if (objModified==null) return; @@ -319,11 +413,7 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio { TextArea t = (TextArea) objModified; - if (clicked == mnuExit) - { - System.exit(0); - } - else if (clicked == mnuCut) + if (clicked == mnuCut) { ss = new StringSelection(t.getSelectedText()); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss); @@ -387,7 +477,7 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio else if (clicked == mnuSelectAll) { t.selectAll(); - } + } } } }