Advertisement
KosIvantsov

show_QA_check.groovy

Jun 12th, 2013
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.00 KB | None | 0 0
  1. import groovy.swing.SwingBuilder
  2. import java.awt.Component
  3. import javax.swing.JButton
  4. import javax.swing.JTable
  5. import javax.swing.table.*
  6. import javax.swing.event.*
  7. import java.awt.event.*
  8. import javax.swing.JOptionPane.*
  9. import org.omegat.util.Platform.*
  10. import java.awt.BorderLayout as BL
  11. /*
  12.  The rules are based on the Checkmate Quality check
  13.  http://www.opentag.com/okapi/wiki/index.php?title=CheckMate_-_Quality_Check_Configuration
  14.  Each rule is a block of groovy code, 'source' and 'target' are the two parameters of this block
  15.  */
  16.  
  17. def prop = project.projectProperties
  18. if (!prop) {
  19.   final def title = 'Check rules'
  20.   final def msg   = 'Please try again after you open a project.'
  21.   showMessageDialog null, msg, title, INFORMATION_MESSAGE
  22.   return
  23. }
  24.  
  25. data=[]
  26. console.println("Check rules.\n");
  27.  
  28. // Prefs
  29. maxCharLengthAbove=240
  30. minCharLengthAbove=40
  31.  
  32. rules = [
  33.            
  34.             // Text unit verification
  35.             targetLeadingWhiteSpaces: { s, t ->  t =~ /^\s+/ },
  36.             targetTrailingWhiteSpaces: { s, t -> t =~ /\s+$/ },
  37.             // Segment verification
  38.             doubledWords: { s, t -> t =~ /(?i)(\b\w+)\s+\1\b/ },
  39.            // Length
  40.            targetShorter: { s, t -> (t.length() / s.length() * 100) < minCharLengthAbove },
  41.            targetLonger: { s, t -> (t.length() / s.length() * 100) > maxCharLengthAbove }
  42.         ];
  43.  
  44. segment_count = 0;
  45.  
  46. files = project.projectFiles;
  47.  
  48. for (i in 0 ..< files.size()) {
  49.     fi = files[i];
  50.    
  51.     //console.println(fi.filePath);
  52.     for (j in 0 ..< fi.entries.size()) {
  53.         ste = fi.entries[j];
  54.         source = ste.getSrcText();
  55.         target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
  56.        
  57.         if ( target == null ) {
  58.             continue;
  59.         }
  60.        
  61.         rules.each { k, v ->
  62.             if (rules[k](source, target)) {
  63.                 console.println(ste.entryNum() + "\t" + k + /*"\t[" + source + "]" + */"\t[" + target + "]");
  64.                 data.add([ seg: ste.entryNum(), rule: k, source: source, target: target ]);
  65.                 segment_count++;
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. console.println("Segments found : " + segment_count);
  72.  
  73.  
  74. swing = new SwingBuilder()
  75.  
  76. frame = swing.frame(title:'Check rules', preferredSize: [800, 500]) {
  77.     scrollPane {
  78.         table() {
  79.             tableModel(list:data) {
  80.                 propertyColumn(editable: true, header:'Segment', propertyName:'seg', minWidth: 80, maxWidth: 80, preferredWidth: 80,
  81.                         cellEditor: new TableCellEditor()
  82.                         {
  83.                             public void cancelCellEditing()                             {}
  84.                             public boolean stopCellEditing()                            {   return false;   }
  85.                             public Object getCellEditorValue()                          {   return value;   }
  86.                             public boolean isCellEditable(EventObject anEvent)          {   return true;    }
  87.                             public boolean shouldSelectCell(EventObject anEvent)        {   return true;   }
  88.                             public void addCellEditorListener(CellEditorListener l)     {}
  89.                             public void removeCellEditorListener(CellEditorListener l)  {}
  90.                             public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  91.                             {
  92.                                 println("value: " + value);
  93.                                 org.omegat.core.Core.getEditor().gotoEntry(value);
  94.                             }
  95.                            
  96.                         },
  97.                         cellRenderer: new TableCellRenderer()
  98.                         {
  99.                             public Component getTableCellRendererComponent(JTable table,
  100.                             Object value,
  101.                             boolean isSelected,
  102.                             boolean hasFocus,
  103.                             int row,
  104.                             int column)
  105.                             {
  106.                                 def btn = new JButton()
  107.                                 btn.setText(value.toString())
  108.                                 return btn
  109.                                
  110.                             }
  111.                         }
  112.                         )                
  113.                 propertyColumn(editable: false, header:'Rule',propertyName:'rule', minWidth: 120, maxWidth: 200, preferredWidth: 150)
  114.                 propertyColumn(editable: false, header:'Source',propertyName:'source', minWidth: 150, preferredWidth: 350)
  115.                 propertyColumn(editable: false, header:'Target',propertyName:'target', minWidth: 150, preferredWidth: 350)
  116.             }
  117.         }
  118.        
  119.     }
  120.      panel(constraints: BL.SOUTH){
  121.             button('Quit', actionPerformed:{
  122.                 frame.visible = false
  123.             })
  124.         }
  125. }
  126. frame.pack()
  127. frame.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement