1 package SnowMailClient.Language.Editor; 2 3 import SnowMailClient.Language.*; 4 import snow.sortabletable.*; 5 import snow.utils.gui.*; 6 7 import SnowMailClient.SnowMailClientApp; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import javax.swing. *; 12 import javax.swing.event.*; 13 import javax.swing.table.*; 14 import java.util.*; 15 import java.io.*; 16 import java.net.*; 17 18 19 21 public class TranslationEditor extends JFrame implements ListSelectionListener 22 { 23 private boolean standalone = false; 24 25 private transient SentenceDictionary actualDictionary = null; 26 private transient SentenceDictionary helpTranslation = null; 27 28 private final SentencesTableModel sentencesTableModel = new SentencesTableModel(); 29 SortableTableModel sortableTableModel; 30 31 private final JTextArea textEnglish = new JTextArea("", 4, 70); 32 private final JTextArea textTranslated = new JTextArea("", 5, 70); 33 34 private final JTable sentencesTable = new JTable(); 35 private final SearchStringTableCellRenderer searchStringTableCellRenderer = new SearchStringTableCellRenderer(); 36 37 38 public static final String TITLE = "Translator" + " 3.0 "; 39 40 private final JLabel translationLabel = new JContrastLabel("Translation"); 41 private JButton saveTranslation; 42 43 private final JTextArea textAlternateTranslated = new JTextArea("", 4, 70); 45 private final JLabel alternateTranslationLabel = new JContrastLabel("Translation in the help language"); 46 JPanel panAlternateText; 47 48 TranslationEditor ref = null; 49 50 String [] languagesNamesFoundOnDisk = null; 51 52 public TranslationEditor() 53 { 54 super(); 55 ref = this; 56 57 languagesNamesFoundOnDisk = Language.getInstance().getAvailableLanguages(); 59 60 61 62 this.setTitle( TITLE ); 63 getContentPane().setLayout(new BorderLayout()); 64 65 JPanel trPanel = new JPanel(); 66 trPanel.setLayout(new BoxLayout(trPanel, BoxLayout.Y_AXIS)); 67 68 JScrollPane jsp = new JScrollPane(sentencesTable); 69 70 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 71 jsp, trPanel); 72 splitPane.setOneTouchExpandable(true); 73 74 75 76 getContentPane().add(splitPane, BorderLayout.CENTER); 77 78 79 80 JPanel panText = new JPanel(new BorderLayout()); 81 trPanel.add(panText); 82 panText.add(new JContrastLabel("English original sentence:"), BorderLayout.NORTH); 83 panText.add(wrapLeft(new JScrollPane(textEnglish),20), BorderLayout.CENTER); 84 85 panAlternateText = new JPanel(new BorderLayout()); 86 87 trPanel.add(panAlternateText); 88 panAlternateText.add(alternateTranslationLabel, BorderLayout.NORTH); 89 panAlternateText.add(wrapLeft(new JScrollPane(textAlternateTranslated),20), BorderLayout.CENTER); 90 textAlternateTranslated.setEditable(false); 91 92 textEnglish.setBackground(UIManager.getColor("Label.background")); 93 textAlternateTranslated.setBackground(UIManager.getColor("Label.background")); 94 95 createMenu(); 96 97 textEnglish.setEditable(false); 98 99 JPanel panTransl = new JPanel(new BorderLayout()); 100 trPanel.add(panTransl); 101 panTransl.add(translationLabel, BorderLayout.NORTH); 102 panTransl.add(wrapLeft(new JScrollPane(textTranslated),20), BorderLayout.CENTER); 103 104 saveTranslation = new JButton( "Save Translation"); 105 saveTranslation.setBackground(Color.orange); 106 JPanel btPanel = new JPanel(); 107 btPanel.add(saveTranslation); 108 panTransl.add(btPanel, BorderLayout.SOUTH); 109 110 saveTranslation.addActionListener(new ActionListener() 111 { 112 public void actionPerformed(ActionEvent e) 113 { 114 saveActualTranslation(); 115 } 116 }); 117 118 119 JPanel northPanel = new SnowBackgroundPanel(new FlowLayout(FlowLayout.LEFT,5,5)); 120 121 getContentPane().add(northPanel, BorderLayout.NORTH); 122 123 124 final Vector<Sentence> englishSentences = SourceSentencesParser.readEnglishSourceCodeSentencesFromFile(); 126 System.out.println("Found english sentences: "+englishSentences.size()); 127 128 if(englishSentences.size()==0) 129 { 130 System.out.println("No sentences found in SnowMail/Language/english_sentences.vec, trying to load from jar"); 131 132 try 134 { 135 englishSentences.addAll( SourceSentencesParser.readEnglishSentencesVectorFromJarFile() ); 136 } 137 catch(Exception e) 138 { 139 } 140 141 if(englishSentences.size()==0) 142 { 143 JOptionPane.showMessageDialog(ref, 144 "The original english sentences to translate were not found on your system." 145 +"\nPlease parse the source code with the utility SnowMailClient/Language/SourceSentencesParserEditor.java" 146 +"\nand install the generated english_sentences.vec file in the directory SnowMailClient/Language/" 147 +"\nlocated in the same directory as SnowMailClient.jar.", 148 "The file english_sentences.vec is missing.", JOptionPane.ERROR_MESSAGE); 149 } 150 } 151 152 153 Vector<String > toShow = updateAllDictionaries(englishSentences); 154 155 if(toShow.size()==0) 156 { 157 JOptionPane.showMessageDialog(ref, 158 "There are no sentences to translate, in any language"); 159 } 160 161 162 163 164 final JComboBox langCB = new JComboBox( toShow.toArray(new String [toShow.size()])); 165 northPanel.add(new JContrastLabel("Language ")); 166 northPanel.add(langCB); 167 langCB.addActionListener(new ActionListener() 168 { 169 public void actionPerformed(ActionEvent e) 170 { 171 if(actualDictionary!=null) 172 { 173 try 174 { 175 System.out.println("saving..."); 176 actualDictionary.saveToFile(); 177 } 178 catch(Exception ee) 179 { 180 ee.printStackTrace(); 181 } 182 } 183 else 184 { 185 System.out.println("actual dic = null"); 186 } 187 188 189 actualDictionary = Language.getInstance().getDictionaryFromFile( (String ) langCB.getSelectedItem(), false ); 190 191 192 193 if(actualDictionary==null) 194 { 195 sentencesTable.setEnabled(false); 196 setTitle( TITLE + " [No dictionary loaded]"); 197 } 198 else 199 { 200 boolean isEditable = actualDictionary.getIsEditable(); 201 textTranslated.setEditable(isEditable); 202 saveTranslation.setVisible(isEditable); 203 204 if(!isEditable) 205 { 206 File dicFile = new File("Language/"+ (String ) langCB.getSelectedItem() +".translation"); 207 if(dicFile.exists() && !dicFile.canWrite()) 208 { 209 JOptionPane.showMessageDialog(ref, "The language file "+dicFile.getAbsolutePath()+ 210 "is not editable\nbecause the file flag is read-only.", "Warning", JOptionPane.WARNING_MESSAGE); 211 } 212 else 213 { 214 JOptionPane.showMessageDialog(ref, "The language is not editable", 215 "Warning", JOptionPane.WARNING_MESSAGE); 216 } 217 } 218 219 setTitle(TITLE + " ["+actualDictionary.getFileName()+"] "+actualDictionary.getNumberOfTranslatedSentences()+" translated words"); 220 sentencesTable.setEnabled(true); 221 222 sentencesTableModel.setDictionary(actualDictionary, isEditable); 223 } 224 } 225 }); 226 227 sortableTableModel = new SortableTableModel(sentencesTableModel); 228 sentencesTable.setModel(sortableTableModel); 229 sortableTableModel.installGUI(sentencesTable); 230 int fontSize = UIManager.getFont("Label.font").getSize(); 231 sentencesTable.getColumnModel().getColumn(0).setPreferredWidth(fontSize*12); 232 sentencesTable.getColumnModel().getColumn(1).setPreferredWidth(fontSize*12); 233 234 235 AdvancedSearchPanel sp = new AdvancedSearchPanel("Search:", null, sortableTableModel, true); 236 northPanel.add(sp); 237 238 textEnglish.addFocusListener(new FocusAdapter() 239 { 240 @Override public void focusGained(FocusEvent e) 241 { 242 textEnglish.setSelectionStart(0); 243 textEnglish.setSelectionEnd(textEnglish.getText().length()); 244 } 245 }); 246 247 this.addWindowListener( new WindowAdapter() 248 { 249 @Override public void windowClosing(WindowEvent e) 250 { 251 terminateFrame(); 252 } 253 @Override public void windowClosed(WindowEvent e) 254 { 255 terminateFrame(); 256 } 257 } 258 ); 259 260 sentencesTable.getSelectionModel().addListSelectionListener(this); 261 sentencesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 262 splitPane.setDividerLocation(300); 263 264 this.setSize(900, 700); 265 this.setLocationRelativeTo(null); 266 this.setVisible(true); 267 268 if(langCB.getModel().getSize()>0) 270 { 271 langCB.setSelectedIndex(0); 272 } 273 274 } 276 278 private void createMenu() 279 { 280 this.setJMenuBar(new JMenuBar()); 281 JMenu menuFile = new JMenu("File"); 282 this.getJMenuBar().add(menuFile); 283 284 JMenuItem newLanguage = new JMenuItem("Create New Language Pack"); 285 menuFile.add(newLanguage); 286 newLanguage.addActionListener(new ActionListener() 287 { 288 public void actionPerformed(ActionEvent e) 289 { 290 String rep = JOptionPane.showInputDialog(ref, "Enter the name of the new language"); 291 if(rep!=null) 292 { 293 Language.getInstance().setActualTranslation(rep, false); 294 SentenceDictionary sd = Language.getInstance().getDictionaryFromFile(rep, false); 295 try 296 { 297 sd.saveToFile(); 298 JOptionPane.showMessageDialog(ref, "You must restart the translation editor to see the new language"); 299 } 300 catch(Exception ex) 301 { 302 ex.printStackTrace(); 303 JOptionPane.showMessageDialog(ref, "Error: "+ex.getMessage()); 304 } 305 } 306 } 307 }); 308 309 310 JMenuItem importWords = new JMenuItem("Import translated words from another language pack"); 311 if(SnowMailClientApp.debug) 312 { 313 menuFile.addSeparator(); 314 menuFile.add(importWords); 315 } 316 importWords.addActionListener(new ActionListener() 317 { 318 public void actionPerformed(ActionEvent e) 319 { 320 JFileChooser fileChooser = new JFileChooser("c:/proj/mail/client"); 321 int rep = fileChooser.showOpenDialog(TranslationEditor.this); 322 323 if(rep==JFileChooser.APPROVE_OPTION) 324 { 325 try 326 { 327 File file = fileChooser.getSelectedFile(); 328 SentenceDictionary sd = SentenceDictionary.ReadFromFile_ZIPPED(file); 329 System.out.println("Language read="+sd.getLanguage()); 330 System.out.println("#sentences=" + sd.getNumberOfSentences() 331 +", #translated=" +sd.getNumberOfTranslatedSentences()); 332 333 importSentencesFromAnotherTranslation(sd); 334 } 335 catch(Exception ex) 336 { 337 ex.printStackTrace(); 338 JOptionPane.showMessageDialog(ref, "Error: "+ex.getMessage()); 339 } 340 } 341 } 342 }); 343 344 JMenuItem quit = new JMenuItem("Quit", new Icons.CrossIcon(10,10,true)); 345 menuFile.addSeparator(); 346 menuFile.add(quit); 347 quit.addActionListener(new ActionListener() 348 { 349 public void actionPerformed(ActionEvent e) 350 { 351 terminateFrame(); 352 } 353 }); 354 355 356 JMenu menuLang = new JMenu("Translation Help Language"); 357 this.getJMenuBar().add(menuLang); 358 ButtonGroup bg = new ButtonGroup(); 359 360 int act=-1; 361 for(int i=0; i<this.languagesNamesFoundOnDisk.length; i++) 362 { 363 String at = Language.getInstance().getActualTranslation(); 364 JRadioButtonMenuItem lmi = new JRadioButtonMenuItem( 365 languagesNamesFoundOnDisk[i], 366 at.equals(languagesNamesFoundOnDisk[i])); 367 368 if(at.equals(languagesNamesFoundOnDisk[i])) act = i; 369 370 bg.add(lmi); 371 menuLang.add(lmi); 372 final int ii=i; 373 lmi.addActionListener(new ActionListener() 374 { 375 public void actionPerformed(ActionEvent e) 376 { 377 helpTranslation = Language.getInstance().getDictionaryFromFile(languagesNamesFoundOnDisk[ii], true); 378 } 379 }); 380 } 381 382 if(act!=-1) 383 { 384 helpTranslation = Language.getInstance().getDictionaryFromFile(languagesNamesFoundOnDisk[act], true); 385 } 386 387 JMenu menuUtils = new JMenu("Utilities"); 388 this.getJMenuBar().add(menuUtils); 389 390 JMenuItem viewTextOfAllSentences = new JMenuItem("View text of all translated sentences"); 391 menuUtils.add(viewTextOfAllSentences); 392 viewTextOfAllSentences.addActionListener(new ActionListener() 393 { 394 public void actionPerformed(ActionEvent e) 395 { 396 JTextArea ta = new JTextArea(); 397 if(actualDictionary!=null) 398 { 399 ta.append(actualDictionary.getStringOfAllTranslatedSentences()); 400 } 401 402 JDialog dialog = new JDialog(TranslationEditor.this, "All translated sentences", false); 403 dialog.getContentPane().setLayout(new BorderLayout()); 404 dialog.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); 405 dialog.setSize(400,600); 406 407 CloseControlPanel ccp = new CloseControlPanel(dialog, false, true, "Close"); 408 dialog.getContentPane().add(ccp, BorderLayout.SOUTH); 409 410 dialog.setVisible(true); 411 } 412 }); 413 } 414 415 private void importSentencesFromAnotherTranslation(SentenceDictionary importDic) 416 { 417 java.util.List <Sentence> allActualSentences = actualDictionary.getAllSentences(); 418 java.util.List <Sentence> sentencesThatHasTranslationsInImport = new ArrayList<Sentence>(); 419 for(int i=0; i<allActualSentences.size(); i++) 420 { 421 Sentence s = allActualSentences.get(i); 422 String sent = s.getSentence(); 423 if( !actualDictionary.hasTranslatedSentence(sent) 424 && importDic.hasTranslatedSentence(sent)) 425 { 426 sentencesThatHasTranslationsInImport.add(s); 427 } 428 } 429 430 for(int i=0; i<sentencesThatHasTranslationsInImport.size(); i++) 431 { 432 Sentence s = sentencesThatHasTranslationsInImport.get(i); 433 String sent = s.getSentence(); 434 actualDictionary.addTranslation( 435 sent, importDic.getTranslatedSentence(sent)); 436 } 437 } 438 439 442 private Vector<String > updateAllDictionaries(Vector<Sentence> englishSentences) 443 { 444 Vector<String > languagesNames = new Vector<String >(); 447 for(int i=0; i<languagesNamesFoundOnDisk.length; i++) 448 { 449 String langName = languagesNamesFoundOnDisk[i]; 450 if(!langName.equals(Language.ENGLISH)) 451 { 452 SentenceDictionary sd = Language.getInstance().getDictionaryFromFile(langName, false); 453 if(sd!=null) 454 { 455 languagesNames.addElement(langName); 456 457 if(sd.getIsEditable()) 458 { 459 if(englishSentences.size()>0) 461 { 462 sd.updateSentencesFromSource(englishSentences); 463 464 try 466 { 467 sd.saveToFile(); 468 } 469 catch(Exception e) 470 { 471 e.printStackTrace(); 472 } 473 } 474 } 475 else 476 { 477 System.out.println("No updates performed for "+langName+", file not existing or not editable"); 478 } 479 } 480 } 481 } 482 return languagesNames; 483 } 484 485 486 488 public void valueChanged(ListSelectionEvent e) 489 { 490 int sel = sentencesTable.getSelectedRow(); 492 493 if(sel!=-1 && actualDictionary!=null) 494 { 495 int pos = sortableTableModel.getIndexInUnsortedFromTablePos(sel); 496 Sentence sent = sentencesTableModel.getSentenceAt(pos); 497 498 String eng = sent.getSentence(); 499 String tra = sent.getTranslation(); 500 String classLocation = sent.getLocationClass(); 501 int linePos = sent.getLinePosition(); 502 503 try 504 { 505 int na = Common.getNumberOfParametersInSentence(eng); 506 } 507 catch(Exception ee) 508 { 509 JOptionPane.showMessageDialog(this, ee.getMessage(), "Bad arguments", JOptionPane.WARNING_MESSAGE); 510 } 511 512 textEnglish.setText(eng); 513 514 textEnglish.setSelectionStart(0); 516 textEnglish.setSelectionEnd(eng.length()); 517 519 textTranslated.setText(tra); 520 522 if(helpTranslation!=null) 523 { 524 String helpTr = helpTranslation.getTranslatedSentence(eng); 525 textAlternateTranslated.setText(helpTr); 526 } 527 else 528 { 529 textAlternateTranslated.setText(""); 530 } 531 } 532 else 533 { 534 textEnglish.setText(""); 535 textTranslated.setText(""); 536 textAlternateTranslated.setText(""); 537 } 538 } 539 540 542 private void saveActualTranslation() 543 { 544 String to = textEnglish.getText(); 545 String tr = textTranslated.getText(); 546 547 if(to.equals("") || tr.equals("")) return; 549 550 if(actualDictionary!=null) 551 { 552 int ne=-1; 553 try 554 { 555 ne = Common.getNumberOfParametersInSentence(to); 556 } 557 catch(Exception ee) 558 { 559 } 561 int nt=-1; 562 try 563 { 564 nt = Common.getNumberOfParametersInSentence(tr); 565 if(nt!=ne) throw new Exception ( 566 "Translated sentence has not the same number of parameters as the original sentence." 567 + "\nOrignal has "+ne+" and translated has "+nt 568 ); 569 } 570 catch(Exception ee) 571 { 572 JOptionPane.showMessageDialog(this, 573 "Error: "+ ee.getMessage()); 574 575 return; 576 } 578 579 580 System.out.println("add translation "+to+" => "+tr); 582 final int sr = sentencesTable.getSelectedRow(); 583 584 actualDictionary.addTranslation(to, tr); 585 586 587 EventQueue.invokeLater(new Runnable () 588 { 589 public void run() 590 { 591 if(sr<sentencesTable.getRowCount()) 592 { 593 sentencesTable.getSelectionModel().setSelectionInterval(sr, sr); 594 } 595 596 if(actualDictionary!=null) 597 { 598 setTitle(TITLE + " ["+actualDictionary.getFileName()+"] "+actualDictionary.getNumberOfTranslatedSentences()+" translated words"); 599 } 600 } 601 }); 602 } 603 } 604 605 private void terminateFrame() 606 { 607 if(actualDictionary!=null) 608 try 609 { 610 actualDictionary.saveToFile(); 611 } 612 catch(Exception ee) 613 { 614 ee.printStackTrace(); 615 } 616 617 if(standalone) 618 { 619 System.exit(0); 620 } 621 else 622 { 623 setVisible(false); 624 } 625 } 626 627 630 public static synchronized ImageIcon loadImageIcon(String name) 631 { 632 ClassLoader cl = TranslationEditor.class.getClassLoader(); 633 634 if(cl==null) 635 { 636 System.out.println("ERROR: class loader is null"); 637 return null; 638 } 639 640 URL url = cl.getResource(name); 641 if(url!=null) 642 { 643 return new ImageIcon(url); 645 } 646 else 647 { 648 url = cl.getResource("Language/"+name); 649 if(url!=null) 650 { 651 return new ImageIcon(url); 653 } 654 else 655 { 656 System.out.println("URL is null, can't find image "+name); 657 return null; 658 } 659 } 660 } 661 662 private JPanel wrapLeft(Component comp,int hgap) 663 { 664 JPanel pan= new JPanel(new FlowLayout(FlowLayout.LEFT,hgap,0)); 665 pan.add(comp); 666 return pan; 667 } 668 669 public static void main(String [] a) 670 { 671 EventQueue.invokeLater(new Runnable (){public void run(){ 673 TranslationEditor te = new TranslationEditor(); 674 te.standalone = true; 675 }}); 676 } 677 678 } | Popular Tags |