1 19 20 package org.netbeans.modules.diff.builtin; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.io.IOException ; 25 import java.io.Reader ; 26 import java.io.Writer ; 27 import java.io.Serializable ; 28 import java.io.StringReader ; 29 import java.io.StringWriter ; 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.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 50 public class DefaultDiff extends Diff implements Serializable { 51 52 private boolean showDiffSelector = true; 53 54 55 public DefaultDiff() { 56 } 57 58 61 public String getDisplayName() { 62 return NbBundle.getMessage(DefaultDiff.class, "DefaultDiff.displayName"); 63 } 64 65 78 public Component createDiff(String name1, String title1, Reader r1, 79 String name2, String title2, Reader r2, 80 String MIMEType) throws IOException { 81 94 DiffInfo diffInfo = new DiffInfo(name1, name2, title1, title2, 96 MIMEType, showDiffSelector, showDiffSelector, r1, r2); 97 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 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 } 113 114 public DiffView createDiff(StreamSource s1, StreamSource s2) throws IOException { 115 if (System.getProperty("netbeans.experimental.diff55") != null) return new DiffViewImpl(s1, s2); 116 return new EditableDiffView(s1, s2); 117 } 118 119 122 public boolean isShowDiffSelector() { 123 return showDiffSelector; 124 } 125 126 129 public void setShowDiffSelector(boolean showDiffSelector) { 130 this.showDiffSelector = showDiffSelector; 131 } 132 133 182 183 211 212 235 236 private static void cpStream(Reader in, Writer out) throws IOException { 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 264 265 307 308 private static class DiffInfo extends DiffPresenter.Info { 309 310 private String buffer1, buffer2; 311 private Difference[] diffs; 312 313 public DiffInfo(String name1, String name2, String title1, String title2, 314 String mimeType, boolean chooseProviders, boolean chooseVisualizers, 315 Reader r1, Reader r2) throws IOException { 316 super(name1, name2, title1, title2, mimeType, chooseProviders, chooseVisualizers); 317 StringWriter out1 = new StringWriter (); 318 StringWriter out2 = new StringWriter (); 319 cpStream(r1, out1); 320 cpStream(r2, out2); 321 this.buffer1 = out1.toString(); 322 this.buffer2 = out2.toString(); 323 } 324 325 public Reader createFirstReader() { 326 return new StringReader (buffer1); 327 } 328 329 public Reader createSecondReader() { 330 return new StringReader (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 c) { 348 setLayout(new BorderLayout ()); 349 add(c, BorderLayout.CENTER); 350 getAccessibleContext().setAccessibleName(NbBundle.getMessage(DiffTopComponent.class, "ACSN_Diff_Top_Component")); getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DiffTopComponent.class, "ACSD_Diff_Top_Component")); } 353 354 public int getPersistenceType(){ 355 return TopComponent.PERSISTENCE_NEVER; 356 } 357 358 protected String preferredID(){ 359 return "DiffTopComponent"; } 361 362 public void open(Workspace workspace) { 363 super.open(workspace); 364 requestActive(); 366 } 367 368 } 369 370 } 371 | Popular Tags |