Skip to main content

Usign NetBeans Diff Module

Posted by edgars on August 3, 2006 at 12:51 PM PDT

I am doing some related issues with Diff Module in Greenbox NetBeans Plugin.

Basically, when user generates a code by second time, the NetBeans will ask to him "Hey you did some changes in original file, would you like to see the diff between old file and new file you are generating?", If User presse "yes" button, The Diff module will appear to user.

To invoke the Diff Panel on NetBeans you can use something similar to following source code:

  DiffVisualizer diff = (DiffVisualizer) Lookup.getDefault().lookup(DiffVisualizer.class);
    DiffProvider dprov = (DiffProvider) Lookup.getDefault().lookup(DiffProvider.class);
    StreamSource src = (StreamSource) Lookup.getDefault().lookup(StreamSource.class); 
    DiffPresenter v = (DiffPresenter) Lookup.getDefault().lookup(DiffPresenter.class);
class MyInfo extends DiffPresenter.Info {
   private Difference[] differences;
   
public MyInfo(String name1, String name2, String title1, String title2,
              String mimeType, boolean chooseProviders, boolean chooseVisualizers) {
    super(name1,name2,title1,title2,mimeType,chooseProviders,chooseVisualizers);
  }
  public void setDifferences(Difference[] adiff) {
     differences = adiff;
  }

  public Difference[] getDifferences() {
      return differences;
  }

  public Difference[] getInitialDifferences() {
      return differences;
  }

  public  Reader createFirstReader() throws FileNotFoundException {
    Reader f = new FileReader("/Users/edgarsilva/summa/dev/GreenboxBaseProject/web/WEB-INF/spring-context.xml");
    return f;
  }
 
  public  Reader createSecondReader() throws FileNotFoundException{
  
     
      String springContext = new String("");
      try {       
              springContext= ClassGeneratorBase.getInstance().generateSourceByTemplateName(getClasse(),"springContext",initializeGreenboxContextAndVariables(),"/templates");
      } catch (Exception e ) {
         e.printStackTrace();
      }
     
      return new StringReader(springContext);
  }
   
    }
   
      MyInfo i = new MyInfo("old","new","old","new","text/xml",true,true);
        try {
           
            s1 =  new FileReader("/Users/edgarsilva/summa/dev/GreenboxBaseProject/web/WEB-INF/spring-context.xml");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
            String springContext = new String("");
            try {       
                    springContext= ClassGeneratorBase.getInstance().generateSourceByTemplateName(getClasse(),"springContext",initializeGreenboxContextAndVariables(),"/templates");
            } catch (Exception e ) {
               e.printStackTrace();
            }
     
      s2 = new StringReader(springContext);
   
try {
    v = new DiffPresenter(i);
   
    v.setProvider(dprov);
    v.setVisualizer(diff);
    diffPanel.add(v,BorderLayout.CENTER);
     
}catch (Exception e) {
    NbUtils.showMessage(e.getMessage());
}

The main point, you should pay atention is the fact to extend the class DiffPresenter.Info , in order to allow it works correctlly.

Any different tip will be appreciated in the comments section.

The Following Image shows the results of this testing code:

diff


cya

Related Topics >>