KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > ShowGoldenFilesPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.semantic;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.JFrame JavaDoc;
37 import javax.swing.text.AttributeSet JavaDoc;
38 import javax.swing.text.StyleConstants JavaDoc;
39 import javax.swing.text.StyleContext JavaDoc;
40 import javax.swing.text.StyledDocument JavaDoc;
41 import org.openide.filesystems.FileUtil;
42
43 /**
44  *
45  * @author Jan Lahoda
46  */

47 public class ShowGoldenFilesPanel extends javax.swing.JPanel JavaDoc {
48
49     private Component JavaDoc c;
50     private File JavaDoc goldenFile;
51     private File JavaDoc testFile;
52     
53     /** Creates new form ShowGoldenFilesPanel */
54     public ShowGoldenFilesPanel(Component JavaDoc c) {
55         this.c = c;
56         initComponents();
57     }
58     
59     /** This method is called from within the constructor to
60      * initialize the form.
61      * WARNING: Do NOT modify this code. The content of this method is
62      * always regenerated by the Form Editor.
63      */

64     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
65
private void initComponents() {
66         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
67         jEditorPane1 = new javax.swing.JEditorPane JavaDoc();
68         jLabel1 = new javax.swing.JLabel JavaDoc();
69         jLabel2 = new javax.swing.JLabel JavaDoc();
70         goldenField = new javax.swing.JTextField JavaDoc();
71         testField = new javax.swing.JTextField JavaDoc();
72         jButton1 = new javax.swing.JButton JavaDoc();
73         jButton2 = new javax.swing.JButton JavaDoc();
74
75         jEditorPane1.addCaretListener(new javax.swing.event.CaretListener JavaDoc() {
76             public void caretUpdate(javax.swing.event.CaretEvent JavaDoc 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 JavaDoc() {
93             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
142

143     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
144
// TODO add your handling code here:
145
InputStream JavaDoc source = null;
146         OutputStream JavaDoc target = null;
147                 
148         try {
149             source = new FileInputStream JavaDoc(testFile);
150             target = new FileOutputStream JavaDoc(goldenFile);
151             
152             FileUtil.copy(source, target);
153         } catch (IOException JavaDoc e) {
154             e.printStackTrace();
155         } finally {
156             if (source != null) {
157                 try {
158                     source.close();
159                 } catch (IOException JavaDoc e) {
160                     e.printStackTrace();
161                 }
162             }
163             if (target != null) {
164                 try {
165                     target.close();
166                 } catch (IOException JavaDoc e) {
167                     e.printStackTrace();
168                 }
169             }
170         }
171         c.setVisible(false);
172     }//GEN-LAST:event_jButton1ActionPerformed
173

174     private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent JavaDoc evt) {//GEN-FIRST:event_jEditorPane1CaretUpdate
175
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     }//GEN-LAST:event_jEditorPane1CaretUpdate
202

203     
204     // Variables declaration - do not modify//GEN-BEGIN:variables
205
public javax.swing.JTextField JavaDoc goldenField;
206     public javax.swing.JButton JavaDoc jButton1;
207     public javax.swing.JButton JavaDoc jButton2;
208     public javax.swing.JEditorPane JavaDoc jEditorPane1;
209     public javax.swing.JLabel JavaDoc jLabel1;
210     public javax.swing.JLabel JavaDoc jLabel2;
211     public javax.swing.JScrollPane JavaDoc jScrollPane1;
212     public javax.swing.JTextField JavaDoc testField;
213     // End of variables declaration//GEN-END:variables
214

215     private List JavaDoc<HighlightImpl> golden;
216     private List JavaDoc<HighlightImpl> test;
217     
218     public void setDocument(StyledDocument JavaDoc doc, List JavaDoc<HighlightImpl> golden, List JavaDoc<HighlightImpl> test, File JavaDoc goldenFile, File JavaDoc testFile) {
219         this.golden = golden;
220         this.test = test;
221         this.goldenFile = goldenFile;
222         this.testFile = testFile;
223         List JavaDoc<HighlightImpl> missing = new ArrayList JavaDoc<HighlightImpl>();
224         List JavaDoc<HighlightImpl> added = new ArrayList JavaDoc<HighlightImpl>();
225         
226         Map JavaDoc<String JavaDoc, HighlightImpl> name2Golden = new HashMap JavaDoc<String JavaDoc, HighlightImpl>();
227         Map JavaDoc<String JavaDoc, HighlightImpl> name2Test = new HashMap JavaDoc<String JavaDoc, 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 JavaDoc<String JavaDoc> missingNames = new HashSet JavaDoc<String JavaDoc>(name2Golden.keySet());
238         
239         missingNames.removeAll(name2Test.keySet());
240         
241         for (String JavaDoc m : missingNames) {
242             missing.add(name2Golden.get(m));
243         }
244         
245         Set JavaDoc<String JavaDoc> addedNames = new HashSet JavaDoc<String JavaDoc>(name2Test.keySet());
246         
247         addedNames.removeAll(name2Golden.keySet());
248         
249         for (String JavaDoc a : addedNames) {
250             added.add(name2Test.get(a));
251         }
252         
253         List JavaDoc<HighlightImpl> modified = new ArrayList JavaDoc<HighlightImpl>();
254         
255         modified.addAll(missing);
256         modified.addAll(added);
257         
258         StyleContext JavaDoc sc = StyleContext.getDefaultStyleContext();
259         AttributeSet JavaDoc 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