1 19 package org.netbeans.modules.java.editor.semantic; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import javax.swing.JComponent ; 36 import javax.swing.JFrame ; 37 import javax.swing.text.AttributeSet ; 38 import javax.swing.text.StyleConstants ; 39 import javax.swing.text.StyleContext ; 40 import javax.swing.text.StyledDocument ; 41 import org.openide.filesystems.FileUtil; 42 43 47 public class ShowGoldenFilesPanel extends javax.swing.JPanel { 48 49 private Component c; 50 private File goldenFile; 51 private File testFile; 52 53 54 public ShowGoldenFilesPanel(Component c) { 55 this.c = c; 56 initComponents(); 57 } 58 59 64 private void initComponents() { 66 jScrollPane1 = new javax.swing.JScrollPane (); 67 jEditorPane1 = new javax.swing.JEditorPane (); 68 jLabel1 = new javax.swing.JLabel (); 69 jLabel2 = new javax.swing.JLabel (); 70 goldenField = new javax.swing.JTextField (); 71 testField = new javax.swing.JTextField (); 72 jButton1 = new javax.swing.JButton (); 73 jButton2 = new javax.swing.JButton (); 74 75 jEditorPane1.addCaretListener(new javax.swing.event.CaretListener () { 76 public void caretUpdate(javax.swing.event.CaretEvent evt) { 77 jEditorPane1CaretUpdate(evt); 78 } 79 }); 80 81 jScrollPane1.setViewportView(jEditorPane1); 82 83 jLabel1.setText("Golden:"); 84 85 jLabel2.setText("Test:"); 86 87 goldenField.setText("jTextField1"); 88 89 testField.setText("jTextField2"); 90 91 jButton1.setText("Accept"); 92 jButton1.addActionListener(new java.awt.event.ActionListener () { 93 public void actionPerformed(java.awt.event.ActionEvent evt) { 94 jButton1ActionPerformed(evt); 95 } 96 }); 97 98 jButton2.setText("Refuse"); 99 100 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 101 this.setLayout(layout); 102 layout.setHorizontalGroup( 103 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 104 .add(layout.createSequentialGroup() 105 .addContainerGap() 106 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 107 .add(layout.createSequentialGroup() 108 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 109 .add(jLabel1) 110 .add(jLabel2)) 111 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 112 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 113 .add(testField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE) 114 .add(goldenField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))) 115 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE) 116 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 117 .add(jButton1) 118 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 119 .add(jButton2))) 120 .addContainerGap()) 121 ); 122 layout.setVerticalGroup( 123 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 124 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 125 .addContainerGap() 126 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE) 127 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 128 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 129 .add(jLabel1) 130 .add(goldenField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 131 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 132 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 133 .add(jLabel2) 134 .add(testField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 135 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 136 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 137 .add(jButton2) 138 .add(jButton1)) 139 .addContainerGap()) 140 ); 141 } 143 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { InputStream source = null; 146 OutputStream target = null; 147 148 try { 149 source = new FileInputStream (testFile); 150 target = new FileOutputStream (goldenFile); 151 152 FileUtil.copy(source, target); 153 } catch (IOException e) { 154 e.printStackTrace(); 155 } finally { 156 if (source != null) { 157 try { 158 source.close(); 159 } catch (IOException e) { 160 e.printStackTrace(); 161 } 162 } 163 if (target != null) { 164 try { 165 target.close(); 166 } catch (IOException e) { 167 e.printStackTrace(); 168 } 169 } 170 } 171 c.setVisible(false); 172 } 174 private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt) { int pos = jEditorPane1.getCaretPosition(); 176 HighlightImpl goldenHighlight = null; 177 HighlightImpl testHighlight = null; 178 179 for (HighlightImpl h : golden) { 180 if (h.getStart() <= pos && pos <= h.getEnd()) { 181 goldenHighlight = h; 182 break; 183 } 184 } 185 186 for (HighlightImpl h : test) { 187 if (h.getStart() <= pos && pos <= h.getEnd()) { 188 testHighlight = h; 189 break; 190 } 191 } 192 193 if (goldenHighlight != null) 194 goldenField.setText(goldenHighlight.getHighlightTestData()); 195 else 196 goldenField.setText("<none>"); 197 if (testHighlight != null ) 198 testField.setText(testHighlight.getHighlightTestData()); 199 else 200 testField.setText("<none>"); 201 } 203 204 public javax.swing.JTextField goldenField; 206 public javax.swing.JButton jButton1; 207 public javax.swing.JButton jButton2; 208 public javax.swing.JEditorPane jEditorPane1; 209 public javax.swing.JLabel jLabel1; 210 public javax.swing.JLabel jLabel2; 211 public javax.swing.JScrollPane jScrollPane1; 212 public javax.swing.JTextField testField; 213 215 private List <HighlightImpl> golden; 216 private List <HighlightImpl> test; 217 218 public void setDocument(StyledDocument doc, List <HighlightImpl> golden, List <HighlightImpl> test, File goldenFile, File testFile) { 219 this.golden = golden; 220 this.test = test; 221 this.goldenFile = goldenFile; 222 this.testFile = testFile; 223 List <HighlightImpl> missing = new ArrayList <HighlightImpl>(); 224 List <HighlightImpl> added = new ArrayList <HighlightImpl>(); 225 226 Map <String , HighlightImpl> name2Golden = new HashMap <String , HighlightImpl>(); 227 Map <String , HighlightImpl> name2Test = new HashMap <String , HighlightImpl>(); 228 229 for (HighlightImpl g : golden) { 230 name2Golden.put(g.getHighlightTestData(), g); 231 } 232 233 for (HighlightImpl t : test) { 234 name2Test.put(t.getHighlightTestData(), t); 235 } 236 237 Set <String > missingNames = new HashSet <String >(name2Golden.keySet()); 238 239 missingNames.removeAll(name2Test.keySet()); 240 241 for (String m : missingNames) { 242 missing.add(name2Golden.get(m)); 243 } 244 245 Set <String > addedNames = new HashSet <String >(name2Test.keySet()); 246 247 addedNames.removeAll(name2Golden.keySet()); 248 249 for (String a : addedNames) { 250 added.add(name2Test.get(a)); 251 } 252 253 List <HighlightImpl> modified = new ArrayList <HighlightImpl>(); 254 255 modified.addAll(missing); 256 modified.addAll(added); 257 258 StyleContext sc = StyleContext.getDefaultStyleContext(); 259 AttributeSet as = sc.getEmptySet(); 260 261 as = sc.addAttribute(as, StyleConstants.Foreground, Color.RED); 262 as = sc.addAttribute(as, StyleConstants.Bold, Boolean.TRUE); 263 264 for (HighlightImpl h : modified) { 265 doc.setCharacterAttributes(h.getStart(), h.getEnd() - h.getStart(), as, false); 266 } 267 268 jEditorPane1.setContentType("text/html"); 269 jEditorPane1.setDocument(doc); 270 } 271 272 } 273 | Popular Tags |