KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > builtin > DefaultDiff


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
20 package org.netbeans.modules.diff.builtin;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.io.StringWriter JavaDoc;
30
31 import org.openide.NotifyDescriptor;
32 import org.openide.util.NbBundle;
33 import org.openide.util.Lookup;
34 import org.openide.windows.*;
35
36 import org.netbeans.api.diff.Diff;
37 //import org.netbeans.api.diff.DiffWrapperPanel;
38
import org.netbeans.api.diff.Difference;
39 import org.netbeans.api.diff.DiffView;
40 import org.netbeans.api.diff.StreamSource;
41 import org.netbeans.spi.diff.*;
42 import org.netbeans.modules.diff.builtin.visualizer.DiffViewImpl;
43 import org.netbeans.modules.diff.builtin.visualizer.editable.EditableDiffView;
44 import org.openide.DialogDisplayer;
45
46 /**
47  *
48  * @author Martin Entlicher
49  */

50 public class DefaultDiff extends Diff implements Serializable JavaDoc {
51     
52     private boolean showDiffSelector = true;
53     
54     /** Creates a new instance of DefaultDiff */
55     public DefaultDiff() {
56     }
57     
58     /**
59      * Get the display name of this diff provider.
60      */

61     public String JavaDoc getDisplayName() {
62         return NbBundle.getMessage(DefaultDiff.class, "DefaultDiff.displayName");
63     }
64     
65     /**
66      * Show the visual representation of the diff between two sources.
67      * @param name1 the name of the first source
68      * @param title1 the title of the first source
69      * @param r1 the first source
70      * @param name2 the name of the second source
71      * @param title2 the title of the second source
72      * @param r2 the second resource compared with the first one.
73      * @param MIMEType the mime type of these sources
74      * @return The Component representing the diff visual representation
75      * or null, when the representation is outside the IDE.
76      * @throws IOException when the reading from input streams fails.
77      */

78     public Component JavaDoc createDiff(String JavaDoc name1, String JavaDoc title1, Reader JavaDoc r1,
79                                 String JavaDoc name2, String JavaDoc title2, Reader JavaDoc r2,
80                                 String JavaDoc MIMEType) throws IOException JavaDoc {
81                                     /*
82         this.name1 = name1;
83         this.name2 = name2;
84         this.title1 = title1;
85         this.title2 = title2;
86         this.MIMEType = MIMEType;
87         StringWriter out1 = new StringWriter();
88         StringWriter out2 = new StringWriter();
89         cpStream(r1, out1);
90         cpStream(r2, out2);
91         this.buffer1 = out1.toString();
92         this.buffer2 = out2.toString();
93                                      */

94         //diffPanel = new DiffWrapperPanel(showDiffSelector, showDiffSelector);
95
DiffInfo diffInfo = new DiffInfo(name1, name2, title1, title2,
96             MIMEType, showDiffSelector, showDiffSelector, r1, r2);
97         // I need to know the initial differences to know whether the files actually
98
// differ or not.
99
DiffProvider provider = (DiffProvider) Lookup.getDefault().lookup(DiffProvider.class);
100         try {
101             Difference[] diffs = provider.computeDiff(diffInfo.createFirstReader(), diffInfo.createSecondReader());
102             diffInfo.setInitialDiffs(diffs);
103         } catch (IOException JavaDoc ioex) {}
104         DiffPresenter diffPanel = new DiffPresenter(diffInfo);
105         TopComponent tp = new DiffTopComponent(diffPanel);
106         diffInfo.setPresentingComponent(tp);
107         diffPanel.setProvider(provider);
108         diffPanel.setVisualizer((DiffVisualizer) Lookup.getDefault().lookup(DiffVisualizer.class));
109         return tp;
110         //if (!initPanel()) return null;
111
//return tp;
112
}
113
114     public DiffView createDiff(StreamSource s1, StreamSource s2) throws IOException JavaDoc {
115         if (System.getProperty("netbeans.experimental.diff55") != null) return new DiffViewImpl(s1, s2);
116         return new EditableDiffView(s1, s2);
117     }
118
119     /** Getter for property showDiffSelector.
120      * @return Value of property showDiffSelector.
121      */

122     public boolean isShowDiffSelector() {
123         return showDiffSelector;
124     }
125     
126     /** Setter for property showDiffSelector.
127      * @param showDiffSelector New value of property showDiffSelector.
128      */

129     public void setShowDiffSelector(boolean showDiffSelector) {
130         this.showDiffSelector = showDiffSelector;
131     }
132     
133     /*
134     private boolean initPanel() throws IOException {
135         Lookup.Result providersResult = Lookup.getDefault().lookup(new Lookup.Template(DiffProvider.class));
136         Lookup.Item[] providers = new Lookup.Item[providersResult.allItems().size()];
137         int i = 0;
138         for (Iterator it = providersResult.allItems().iterator(); it.hasNext(); i++) {
139             providers[i] = (Lookup.Item) it.next();
140         }
141         Lookup.Result visualizersResult = Lookup.getDefault().lookup(new Lookup.Template(DiffVisualizer.class));
142         Lookup.Item[] visualizers = new Lookup.Item[visualizersResult.allInstances().size()];
143         i = 0;
144         for (Iterator it = visualizersResult.allItems().iterator(); it.hasNext(); i++) {
145             visualizers[i] = (Lookup.Item) it.next();
146         }
147         if (providers.length == 0 || visualizers.length == 0) {
148             if (providers.length == 0) {
149                 TopManager.getDefault().notify(new NotifyDescriptor.Message(
150                     NbBundle.getMessage(DefaultDiff.class, "MSG_ProvidersMissing")));
151             } else if (visualizers.length == 0) {
152                 TopManager.getDefault().notify(new NotifyDescriptor.Message(
153                     NbBundle.getMessage(DefaultDiff.class, "MSG_VisualizersMissing")));
154             }
155             return false;
156         }
157         providersMap = new HashMap();
158         visualizersMap = new HashMap();
159         String[] providersDisplayNames = new String[providers.length];
160         for (i = 0; i < providers.length; i++) {
161             providersDisplayNames[i] = providers[i].getDisplayName();
162             providersMap.put(providersDisplayNames[i], providers[i]);
163         }
164         String[] visualizersDisplayNames = new String[visualizers.length];
165         for (i = 0; i < visualizers.length; i++) {
166             visualizersDisplayNames[i] = visualizers[i].getDisplayName();
167             visualizersMap.put(visualizersDisplayNames[i], visualizers[i]);
168         }
169         defaultProviderName = providersDisplayNames[0];
170         defaultVisualizerName = visualizersDisplayNames[0];
171         diffPanel.showProvidersChooser(true);
172         diffPanel.showVisualizerChooser(true);
173         diffPanel.setProviders(providersDisplayNames);
174         diffPanel.setVisualizers(visualizersDisplayNames);
175         diffPanel.setVisualizer((DiffVisualizer) visualizers[0].getInstance());
176         diffPanel.addProvidersChangeListener(new ProvidersChangeListener());
177         diffPanel.addVisualizersChangeListener(new VisualizerChangeListener());
178         showDiff();
179         return true;
180     }
181      */

182     
183     /*
184     private boolean initPanel() throws IOException {
185         DiffProvider provider = (DiffProvider) Lookup.getDefault().lookup(DiffProvider.class);
186         DiffVisualizer visualizer = (DiffVisualizer) Lookup.getDefault().lookup(DiffVisualizer.class);
187         diffPanel.setProvider(provider);
188         diffPanel.setVisualizer(visualizer);
189         ServicesChangeListener l = new ServicesChangeListener();
190         diffPanel.addProvidersChangeListener(l);
191         diffPanel.addVisualizersChangeListener(l);
192         showDiff(provider, visualizer);
193         return true;
194     }
195     
196     private synchronized void showDiff(DiffProvider p, DiffVisualizer v) throws IOException {
197         Difference[] diffs = p.computeDiff(new StringReader(buffer1),
198                                            new StringReader(buffer2));
199         Component c = v.createView(diffs, name1, title1, new StringReader(buffer1),
200                                    name2, title2, new StringReader(buffer2), MIMEType);
201         diffPanel.setVisualizer(c);
202         tp.setName(c.getName());
203         if (c instanceof TopComponent) {
204             TopComponent vtp = (TopComponent) c;
205             tp.setToolTipText(vtp.getToolTipText());
206             tp.setIcon(vtp.getIcon());
207         }
208         c.requestFocus();
209     }
210      */

211     
212     /*
213     private synchronized void showDiff() throws IOException {
214         //System.out.println("showDiff("+defaultProviderName+", "+defaultVisualizerName+")");
215         Lookup.Item pItem = (Lookup.Item) providersMap.get(defaultProviderName);
216         Lookup.Item vItem = (Lookup.Item) visualizersMap.get(defaultVisualizerName);
217         DiffProvider p = (DiffProvider) pItem.getInstance();
218         DiffVisualizer v = (DiffVisualizer) vItem.getInstance();
219         Difference[] diffs = p.computeDiff(new StringReader(buffer1),
220                                            new StringReader(buffer2));
221         Component c = v.createView(diffs, name1, title1, new StringReader(buffer1),
222                                    name2, title2, new StringReader(buffer2), MIMEType);
223         diffPanel.setVisualizer(c);
224         diffPanel.setDefaultProvider(defaultProviderName);
225         diffPanel.setDefaultVisualizer(defaultVisualizerName);
226         tp.setName(c.getName());
227         if (c instanceof TopComponent) {
228             TopComponent vtp = (TopComponent) c;
229             tp.setToolTipText(vtp.getToolTipText());
230             tp.setIcon(vtp.getIcon());
231         }
232         c.requestFocus();
233     }
234      */

235     
236     private static void cpStream(Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc {
237         char[] buff = new char[1024];
238         int n;
239         while ((n = in.read(buff)) > 0) {
240             out.write(buff, 0, n);
241         }
242         in.close();
243         out.close();
244     }
245     
246     /*
247     private class ServicesChangeListener implements java.beans.PropertyChangeListener {
248         
249         /**
250          * This method gets called when a bound property is changed.
251          * @param evt A PropertyChangeEvent object describing the event source
252          * and the property that has changed.
253          *
254         public void propertyChange(java.beans.PropertyChangeEvent evt) {
255             try {
256                 showDiff(diffPanel.getProvider(), diffPanel.getVisualizer());
257             } catch (IOException ioex) {
258                 TopManager.getDefault().notifyException(ioex);
259             }
260         }
261         
262     }
263      */

264     
265     /*
266     private class ProvidersChangeListener implements ItemListener {
267         
268         /**
269          * Invoked when an item has been selected or deselected.
270          * The code written for this method performs the operations
271          * that need to occur when an item is selected (or deselected).
272          *
273         public void itemStateChanged(ItemEvent e) {
274             if (e.SELECTED == e.getStateChange()) {
275                 defaultProviderName = (String) e.getItem();
276                 try {
277                     showDiff();
278                 } catch (IOException ioex) {
279                     TopManager.getDefault().notifyException(ioex);
280                 }
281             }
282         }
283         
284     }
285     
286     private class VisualizerChangeListener implements ItemListener {
287         
288         /**
289          * Invoked when an item has been selected or deselected.
290          * The code written for this method performs the operations
291          * that need to occur when an item is selected (or deselected).
292          *
293         public void itemStateChanged(ItemEvent e) {
294             if (e.SELECTED == e.getStateChange()) {
295                 //defaultVisualizerName = (String) e.getItem();
296                 System.out.println("visualizer = "+e.getItem());
297                 try {
298                     showDiff();
299                 } catch (IOException ioex) {
300                     TopManager.getDefault().notifyException(ioex);
301                 }
302             }
303         }
304         
305     }
306      */

307     
308     private static class DiffInfo extends DiffPresenter.Info {
309         
310         private String JavaDoc buffer1, buffer2;
311         private Difference[] diffs;
312         
313         public DiffInfo(String JavaDoc name1, String JavaDoc name2, String JavaDoc title1, String JavaDoc title2,
314                         String JavaDoc mimeType, boolean chooseProviders, boolean chooseVisualizers,
315                         Reader JavaDoc r1, Reader JavaDoc r2) throws IOException JavaDoc {
316             super(name1, name2, title1, title2, mimeType, chooseProviders, chooseVisualizers);
317             StringWriter JavaDoc out1 = new StringWriter JavaDoc();
318             StringWriter JavaDoc out2 = new StringWriter JavaDoc();
319             cpStream(r1, out1);
320             cpStream(r2, out2);
321             this.buffer1 = out1.toString();
322             this.buffer2 = out2.toString();
323         }
324         
325         public Reader JavaDoc createFirstReader() {
326             return new StringReader JavaDoc(buffer1);
327         }
328         
329         public Reader JavaDoc createSecondReader() {
330             return new StringReader JavaDoc(buffer2);
331         }
332         
333         void setInitialDiffs(Difference[] diffs) {
334             this.diffs = diffs;
335         }
336         
337         public Difference[] getInitialDifferences() {
338             Difference[] diffs = this.diffs;
339             this.diffs = null;
340             return diffs;
341         }
342         
343     }
344     
345     public static class DiffTopComponent extends TopComponent {
346         
347         public DiffTopComponent(Component JavaDoc c) {
348             setLayout(new BorderLayout JavaDoc());
349             add(c, BorderLayout.CENTER);
350             getAccessibleContext().setAccessibleName(NbBundle.getMessage(DiffTopComponent.class, "ACSN_Diff_Top_Component")); // NOI18N
351
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DiffTopComponent.class, "ACSD_Diff_Top_Component")); // NOI18N
352
}
353         
354         public int getPersistenceType(){
355             return TopComponent.PERSISTENCE_NEVER;
356         }
357         
358         protected String JavaDoc preferredID(){
359             return "DiffTopComponent"; //NOI18N
360
}
361         
362         public void open(Workspace workspace) {
363             super.open(workspace);
364             //diffPanel.open();
365
requestActive();
366         }
367         
368     }
369     
370 }
371
Popular Tags