KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 //import java.awt.event.ItemEvent;
24
//import java.awt.event.ItemListener;
25
import java.beans.PropertyDescriptor JavaDoc;
26 import java.beans.PropertyEditorManager JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InterruptedIOException JavaDoc;
31
32 import org.netbeans.api.progress.ProgressHandle;
33 import org.netbeans.api.progress.ProgressHandleFactory;
34
35 import org.openide.explorer.propertysheet.ExPropertyEditor;
36 import org.openide.explorer.propertysheet.PropertyModel;
37 import org.openide.explorer.propertysheet.PropertyPanel;
38 import org.openide.explorer.propertysheet.DefaultPropertyModel;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.loaders.DataObjectNotFoundException;
44 import org.openide.nodes.Node;
45 import org.openide.windows.TopComponent;
46
47 import org.netbeans.api.diff.Difference;
48 import org.netbeans.spi.diff.*;
49 import org.openide.loaders.InstanceDataObject;
50 import org.openide.util.NbBundle;
51 import org.openide.util.RequestProcessor;
52 import org.openide.util.Task;
53 import org.openide.ErrorManager;
54
55 import javax.swing.*;
56
57 /**
58  * This panel is to be used as a wrapper for diff visualizers.
59  * @author Martin Entlicher
60  */

61 public class DiffPresenter extends javax.swing.JPanel JavaDoc {
62     
63     public static final String JavaDoc PROP_PROVIDER = "provider"; // NOI18N
64
public static final String JavaDoc PROP_VISUALIZER = "visualizer"; // NOI18N
65

66     public static final String JavaDoc PROP_TOOLBAR = "DiffPresenter.toolbarPanel"; // NOI18N
67

68     private DiffPresenter.Info diffInfo;
69     private DiffProvider defaultProvider;
70     private DiffVisualizer defaultVisualizer;
71     private JComponent progressPanel;
72
73     /**
74      * Interruptible (to be able to drop streams of deadlocked
75      * external program) request processor.
76      */

77     private final RequestProcessor diffRP = new RequestProcessor("Diff", 1, true);
78
79     private RequestProcessor.Task computationTask = diffRP.post( // NOI28N
80
new Runnable JavaDoc(){public void run(){}}
81     );
82     
83     private boolean added;
84
85     /**
86      * Creates <i>just computing diff</i> presenter. The mode
87      * is left on {@link #initWithDiffInfo} call.
88      */

89     public DiffPresenter() {
90         String JavaDoc label = NbBundle.getMessage(DiffPresenter.class, "diff.prog");
91         ProgressHandle progress = ProgressHandleFactory.createHandle(label);
92         progressPanel = ProgressHandleFactory.createProgressComponent(progress);
93         add(progressPanel);
94         progress.start();
95     }
96
97     /** Creates new DiffPresenter with given content. */
98     public DiffPresenter(DiffPresenter.Info diffInfo) {
99         initWithDiffInfo(diffInfo);
100     }
101
102     /** Seta actual diff content. Can be called just once. */
103     public final void initWithDiffInfo(DiffPresenter.Info diffInfo) {
104         assert this.diffInfo == null;
105         this.diffInfo = diffInfo;
106         if (progressPanel != null) {
107             remove(progressPanel);
108         }
109         initComponents();
110         initMyComponents();
111         providerLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderA11yDesc")); // NOI18N
112
visualizerLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerA11yDesc")); // NOI18N
113
}
114
115     /** This method is called from within the constructor to
116      * initialize the form.
117      * WARNING: Do NOT modify this code. The content of this method is
118      * always regenerated by the Form Editor.
119      */

120     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
121
private void initComponents() {
122         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
123
124         jPanel1 = new javax.swing.JPanel JavaDoc();
125         toolbarPanel = new javax.swing.JPanel JavaDoc();
126         servicesPanel = new javax.swing.JPanel JavaDoc();
127         providerLabel = new javax.swing.JLabel JavaDoc();
128         visualizerLabel = new javax.swing.JLabel JavaDoc();
129         visualizerPanel = new javax.swing.JPanel JavaDoc();
130
131         setLayout(new java.awt.GridBagLayout JavaDoc());
132
133         toolbarPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.CENTER, 0, 0));
134
135         toolbarPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
136         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
137         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138         add(toolbarPanel, gridBagConstraints);
139
140         servicesPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
141
142         org.openide.awt.Mnemonics.setLocalizedText(providerLabel, org.openide.util.NbBundle.getMessage(DiffPresenter.class, "LBL_Provider"));
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.gridx = 0;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
147         servicesPanel.add(providerLabel, gridBagConstraints);
148
149         org.openide.awt.Mnemonics.setLocalizedText(visualizerLabel, org.openide.util.NbBundle.getMessage(DiffPresenter.class, "LBL_Visualizer"));
150         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
151         gridBagConstraints.gridx = 2;
152         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
153         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
154         servicesPanel.add(visualizerLabel, gridBagConstraints);
155
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
160         gridBagConstraints.weightx = 1.0;
161         add(servicesPanel, gridBagConstraints);
162
163         visualizerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
164
165         visualizerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(0, 0, 0)));
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
168         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
169         gridBagConstraints.weightx = 1.0;
170         gridBagConstraints.weighty = 1.0;
171         add(visualizerPanel, gridBagConstraints);
172
173     }// </editor-fold>//GEN-END:initComponents
174

175     
176     // Variables declaration - do not modify//GEN-BEGIN:variables
177
javax.swing.JPanel JavaDoc jPanel1;
178     javax.swing.JLabel JavaDoc providerLabel;
179     javax.swing.JPanel JavaDoc servicesPanel;
180     javax.swing.JPanel JavaDoc toolbarPanel;
181     javax.swing.JLabel JavaDoc visualizerLabel;
182     javax.swing.JPanel JavaDoc visualizerPanel;
183     // End of variables declaration//GEN-END:variables
184

185     private void initMyComponents() {
186         PropertyDescriptor JavaDoc pd;
187         PropertyModel model;
188         PropertyPanel panel;
189         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
190         FileSystem dfs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem();
191         FileObject services = dfs.findResource("Services");
192         DataFolder df = DataFolder.findFolder(services);
193         Object JavaDoc editor = PropertyEditorManager.findEditor (Object JavaDoc.class);
194         if (diffInfo.isChooseProviders() && editor != null) {
195             try {
196                 pd = new PropertyDescriptor JavaDoc (PROP_PROVIDER, getClass ());
197             } catch (java.beans.IntrospectionException JavaDoc intrex) {
198                 return ;
199             }
200             pd.setPropertyEditorClass (editor.getClass());
201             // special attributes to the property editor
202
pd.setValue ("superClass", DiffProvider.class);
203             pd.setValue ("suppressCustomEditor", Boolean.TRUE);
204             FileObject providersFO = services.getFileObject("DiffProviders");
205             try {
206                 DataObject providersDO = DataObject.find(providersFO);
207                 Node providersNode = providersDO.getNodeDelegate();
208                 pd.setValue("node", providersNode);
209             } catch (DataObjectNotFoundException donfex) {}
210             pd.setValue(ExPropertyEditor.PROPERTY_HELP_ID, "org.netbeans.modules.diff.DiffPresenter.providers");
211             model = new DefaultPropertyModel (this, pd);
212             panel = new PropertyPanel (model, PropertyPanel.PREF_INPUT_STATE);
213
214             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 15);
216             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
217             if (!diffInfo.isChooseVisualizers()) gridBagConstraints.weightx = 1.0;
218             gridBagConstraints.gridx = 1;
219             servicesPanel.add(panel, gridBagConstraints);
220             providerLabel.setLabelFor(panel);
221             panel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderPropertyPanelA11yName")); // NOI18N
222
panel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderPropertyPanelA11yDesc")); // NOI18N
223
}
224         if (diffInfo.isChooseVisualizers() && editor != null) {
225             try {
226                 pd = new PropertyDescriptor JavaDoc (PROP_VISUALIZER, getClass ());
227             } catch (java.beans.IntrospectionException JavaDoc intrex) {
228                 return ;
229             }
230             pd.setPropertyEditorClass (editor.getClass());
231             // special attributes to the property editor
232
pd.setValue ("superClass", DiffVisualizer.class);
233             pd.setValue ("suppressCustomEditor", Boolean.TRUE);
234             FileObject visualizersFO = services.getFileObject("DiffVisualizers");
235             try {
236                 DataObject visualizersDO = DataObject.find(visualizersFO);
237                 Node visualizersNode = visualizersDO.getNodeDelegate();
238                 pd.setValue("node", visualizersNode);
239             } catch (DataObjectNotFoundException donfex) {}
240             pd.setValue(ExPropertyEditor.PROPERTY_HELP_ID, "org.netbeans.modules.diff.DiffPresenter.visualizers");
241             model = new DefaultPropertyModel (this, pd);
242             panel = new PropertyPanel (model, PropertyPanel.PREF_INPUT_STATE);
243             panel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerPropertyPanelA11yName")); // NOI18N
244
panel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerPropertyPanelA11yDesc")); // NOI18N
245

246             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
247             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
248             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
249             gridBagConstraints.weightx = 1.0;
250             gridBagConstraints.gridx = 3;
251             servicesPanel.add(panel, gridBagConstraints);
252             visualizerLabel.setLabelFor(panel);
253         }
254         providerLabel.setVisible(diffInfo.isChooseProviders() && editor != null);
255         visualizerLabel.setVisible(diffInfo.isChooseVisualizers() && editor != null);
256         servicesPanel.setVisible((diffInfo.isChooseProviders() || diffInfo.isChooseVisualizers()) && editor != null);
257     }
258     
259     public DiffProvider getProvider() {
260         return defaultProvider;
261     }
262
263     /** Set the diff provider and update the view. */
264     public void setProvider(DiffProvider p) {
265         this.defaultProvider = (DiffProvider) p;
266
267         if (added) {
268             asyncDiff((DiffProvider) p, defaultVisualizer);
269             setDefaultDiffService(p, "Services/DiffProviders"); // NOI18N
270
}
271     }
272     
273     public DiffVisualizer getVisualizer() {
274         return defaultVisualizer;
275     }
276
277     /** Set the diff visualizer and update the view. */
278     public void setVisualizer(DiffVisualizer v) {
279         this.defaultVisualizer = (DiffVisualizer) v;
280
281         if (added) {
282             asyncDiff(defaultProvider, (DiffVisualizer) v);
283             setDefaultDiffService(v, "Services/DiffVisualizers"); // NOI18N
284
}
285     }
286     
287     private static void setDefaultDiffService(Object JavaDoc ds, String JavaDoc folder) {
288         //System.out.println("setDefaultDiffService("+ds+")");
289
FileSystem dfs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem();
290         FileObject services = dfs.findResource(folder);
291         DataFolder df = DataFolder.findFolder(services);
292         DataObject[] children = df.getChildren();
293         //System.out.println(" Got children.");
294
for (int i = 0; i < children.length; i++) {
295             if (children[i] instanceof InstanceDataObject) {
296                 InstanceDataObject ido = (InstanceDataObject) children[i];
297                 if (ido.instanceOf(ds.getClass())) {
298                     //System.out.println(" Found an instance of my class.");
299
try {
300                         if (ds.equals(ido.instanceCreate())) {
301                             //System.out.println(" Have it, settings the order.");
302
df.setOrder(new DataObject[] { ido });
303                             break;
304                         }
305                     } catch (java.io.IOException JavaDoc ioex) {
306                     } catch (ClassNotFoundException JavaDoc cnfex) {}
307                 }
308             }
309         }
310     }
311
312     
313     /*
314     public void addProvidersChangeListener(PropertyChangeListener l) {
315         propSupport.addPropertyChangeListener(PROP_PROVIDER, l);
316     }
317     
318     public void removeProvidersChangeListener(PropertyChangeListener l) {
319         propSupport.removePropertyChangeListener(PROP_PROVIDER, l);
320     }
321     
322     public void addVisualizersChangeListener(PropertyChangeListener l) {
323         propSupport.addPropertyChangeListener(PROP_VISUALIZER, l);
324     }
325     
326     public void removeVisualizersChangeListener(PropertyChangeListener l) {
327         propSupport.removePropertyChangeListener(PROP_VISUALIZER, l);
328     }
329      */

330
331     /* Start lazy diff computation */
332     public void addNotify() {
333         super.addNotify();
334         added = true;
335         asyncDiff(defaultProvider, defaultVisualizer);
336     }
337
338     /* On close kill background task. */
339     public void removeNotify() {
340         super.removeNotify();
341         computationTask.cancel();
342     }
343
344     /**
345      * Asynchronously computes and shows diff.
346      */

347     private synchronized void asyncDiff(final DiffProvider p, final DiffVisualizer v) {
348         if (v == null) {
349             return;
350         }
351
352         Difference[] diffs;
353         if (p != null) {
354             diffs = diffInfo.getInitialDifferences();
355             if (diffs == null) {
356                 JPanel panel = new JPanel();
357                 panel.setLayout(new BorderLayout());
358                 String JavaDoc message = NbBundle.getMessage(DiffPresenter.class, "BK0001");
359                 JLabel label = new JLabel(message);
360                 label.setHorizontalAlignment(JLabel.CENTER);
361                 panel.add(label, BorderLayout.CENTER);
362                 setVisualizer(panel);
363             }
364         } else {
365             diffs = diffInfo.getDifferences();
366         }
367
368         final Difference[] fdiffs = diffs;
369         Runnable JavaDoc computation = new Runnable JavaDoc() {
370             public void run() {
371                 try {
372                     Difference[] adiffs = fdiffs;
373                     String JavaDoc message = NbBundle.getMessage(DiffPresenter.class, "BK0001");
374                     ProgressHandle ph = ProgressHandleFactory.createHandle(message);
375                     if (adiffs == null) {
376                         try {
377                             ph.start();
378                             adiffs = p.computeDiff(
379                                     diffInfo.createFirstReader(),
380                                     diffInfo.createSecondReader()
381                             );
382                         } finally {
383                             ph.finish();
384                         }
385                     }
386
387                     if (adiffs == null) {
388                         return;
389                     }
390
391                     final Difference[] fadiffs = adiffs;
392                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
393                         public void run() {
394                             try {
395                                 viewVisualizer(v, fadiffs);
396                             } catch (IOException JavaDoc ioex) {
397                                 ErrorManager.getDefault().notify(ErrorManager.USER, ioex);
398                             }
399                         }
400                     });
401
402                 } catch (InterruptedIOException JavaDoc ex) {
403                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
404                 } catch (IOException JavaDoc ex) {
405                     ErrorManager.getDefault().notify(ErrorManager.USER, ex);
406                 }
407             }
408         };
409
410         computationTask.cancel();
411         computationTask = diffRP.post(computation);
412     }
413     
414     private void viewVisualizer(DiffVisualizer v, Difference[] diffs) throws IOException JavaDoc {
415         assert SwingUtilities.isEventDispatchThread();
416         Component c = v.createView(diffs, diffInfo.getName1(), diffInfo.getTitle1(),
417             diffInfo.createFirstReader(), diffInfo.getName2(), diffInfo.getTitle2(),
418             diffInfo.createSecondReader(), diffInfo.getMimeType());
419         setVisualizer((JComponent)c);
420         TopComponent tp = diffInfo.getPresentingComponent();
421         if (tp != null) {
422             tp.setName(c.getName());
423             if (c instanceof TopComponent) {
424                 TopComponent vtp = (TopComponent) c;
425                 tp.setToolTipText(vtp.getToolTipText());
426                 tp.setIcon(vtp.getIcon());
427             }
428         }
429         c.requestFocus();
430     }
431
432     private void setVisualizer(JComponent visualizer) {
433         visualizerPanel.removeAll();
434         if (visualizer != null) {
435             toolbarPanel.removeAll();
436             JComponent toolbar = (JComponent) visualizer.getClientProperty(PROP_TOOLBAR);
437             if (toolbar != null) {
438                 toolbarPanel.add(toolbar);
439             }
440             visualizerPanel.add(visualizer, java.awt.BorderLayout.CENTER);
441         }
442         revalidate();
443         repaint();
444     }
445     
446     /**
447      * This class contains informations about the differences.
448      */

449     public static abstract class Info extends Object JavaDoc {
450         
451         private String JavaDoc name1;
452         private String JavaDoc name2;
453         private String JavaDoc title1;
454         private String JavaDoc title2;
455         private String JavaDoc mimeType;
456         private boolean chooseProviders;
457         private boolean chooseVisualizers;
458         private TopComponent tp;
459         
460         public Info(String JavaDoc name1, String JavaDoc name2, String JavaDoc title1, String JavaDoc title2,
461                     String JavaDoc mimeType, boolean chooseProviders, boolean chooseVisualizers) {
462             this.name1 = name1;
463             this.name2 = name2;
464             this.title1 = title1;
465             this.title2 = title2;
466             this.mimeType = mimeType;
467             this.chooseProviders = chooseProviders;
468             this.chooseVisualizers = chooseVisualizers;
469         }
470         
471         public String JavaDoc getName1() {
472             return name1;
473         }
474         
475         public String JavaDoc getName2() {
476             return name2;
477         }
478         
479         public String JavaDoc getTitle1() {
480             return title1;
481         }
482         
483         public String JavaDoc getTitle2() {
484             return title2;
485         }
486         
487         public String JavaDoc getMimeType() {
488             return mimeType;
489         }
490         
491         public boolean isChooseProviders() {
492             return chooseProviders;
493         }
494         
495         public boolean isChooseVisualizers() {
496             return chooseVisualizers;
497         }
498         
499         public Difference[] getDifferences() {
500             return null;
501         }
502
503         public Difference[] getInitialDifferences() {
504             return null;
505         }
506
507         public abstract Reader JavaDoc createFirstReader() throws FileNotFoundException JavaDoc;
508         
509         public abstract Reader JavaDoc createSecondReader() throws FileNotFoundException JavaDoc;
510         
511         public void setPresentingComponent(TopComponent tp) {
512             this.tp = tp;
513         }
514         
515         public TopComponent getPresentingComponent() {
516             return tp;
517         }
518         
519     }
520     
521 }
522
Popular Tags