`
jay_kid
  • 浏览: 65084 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

diff Viewer

J# 
阅读更多
diffViewer

Const.java
package constData;

public class Const {

	final public static int STR_ARRAY_A = 0;
	final public static int STR_ARRAY_B = 1;
}


DiffProcessor.java
package processor;

public class DiffProcessor {
	
	final public static int CHAR_SAME=0;
	final public static int CHAR_DIFFERENT=1;
	final public static int STR_1_ARRAY = 0;
	final public static int STR_2_ARRAY = 1;
		
	private String str1;
	private String str2;
	
	private int[] str1Array;
	private int[] str2Array;
	
	public DiffProcessor(){
		super();
	}
		
	public DiffProcessor(String str1,String str2){
		init(str1, str2);
	}

	private void init(String str1, String str2) {
		this.str1 = str1;
		this.str2 = str2;
		str1Array = new int[str1.length()];
		str2Array = new int[str2.length()];
	}
	
	public void reSetString(String str1, String str2){
		init(str1,str2);
	}
	
	public int[][] process(){
		LCS(0,str1.length()-1, 0, str2.length()-1);
		int[][] rInt = new int[2][];
		rInt[STR_1_ARRAY] = this.str1Array;
		rInt[STR_2_ARRAY] = this.str2Array;
		return rInt;
		
	}
	

	private static void copyArrayValue (int[] array1, int[] array2){
		if (array1.length == array2.length){
			for (int i = 0; i< array1.length; i++){
				array1[i] = array2[i];
			}
		} else {
			return;
		}
	}
	
	private void LCS(int str1BeginIndex, int str1EndIndex, int str2BeginIndex, int str2EndIndex){
//		System.out.println("$$$$$$$$$$$$$$$$$$$Enter$$$$$$$$$$$$$$$$$");
//		System.out.println("Comming Index str1BeginIndex=" + str1BeginIndex);
//		System.out.println("Comming Index str1EndIndex  =" + str1EndIndex);
//		System.out.println("Comming Index str2BeginIndex=" + str2BeginIndex);
//		System.out.println("Comming Index str2EndIndex  =" + str2EndIndex);
		
		int arrayX = str1EndIndex-str1BeginIndex+1;
		int arrayY = str2EndIndex-str2BeginIndex+1;
		
		int[][] LCSArray = new int[2][arrayY];
		int maxValue = 0;
		int xMax = 0;
		int yMax = 0;
		for (int x = 0; x < arrayX; x++){
			for (int y = 0; y < arrayY; y++){
				if (str1.charAt(x+str1BeginIndex)==str2.charAt(y+str2BeginIndex)){
					if (x>0 && y>0){
						LCSArray[1][y]=LCSArray[0][y-1] +1;
						if (LCSArray[1][y]> maxValue){
							maxValue = LCSArray[1][y];
							xMax = x;
							yMax = y;
						}
					}else{
						LCSArray[1][y]=1;
					}
				}else {
					LCSArray[1][y]=0;
				}
//				System.out.print(LCSArray[1][y]);
			}
			copyArrayValue(LCSArray[0], LCSArray[1]);
			//LCSArray[0] = LCSArray[1];
//			System.out.println();
		}
		if (maxValue != 0){
//			System.out.println("xMax:" + xMax);
//			System.out.println("yMax:" + yMax);
//			System.out.println("maxValue:" + maxValue);
//			System.out.println("yMax-maxValue+1,yMax+1:" + str2.substring(yMax-maxValue+1,yMax+1));
			//return str1.substring(xMax-maxValue+1, xMax+1);
			
			// for the same part, mark str1Array and str2Array corresponding part to 0
			int xSamePartStartIndex = str1BeginIndex + xMax - maxValue + 1;
			int xSamePartEndIndex =  xSamePartStartIndex + maxValue -1;
			int ySamePartStartIndex = str2BeginIndex + yMax - maxValue + 1;
			int ySamePartEndIndex = ySamePartStartIndex + maxValue -1;
			
			for (int i = xSamePartStartIndex; i <= xSamePartEndIndex; i++){
				str1Array[i]=0;
			}
			for (int i = ySamePartStartIndex; i <= ySamePartEndIndex; i++){
				str2Array[i]=0;
			}	
			
			
			// for the difference part including pre-diff and post-diff, continue use LCS() to find out the same part;
//			System.out.println("xSamePartStartIndex:" + xSamePartStartIndex);
//			System.out.println("xSamePartEndIndex:" + xSamePartEndIndex);
//			System.out.println("ySamePartStartIndex:" + ySamePartStartIndex);
//			System.out.println("ySamePartEndIndex:" + ySamePartEndIndex);
//			System.out.println("==========================================");
//			System.out.println("xSamePartStartIndex - str1BeginIndex:" + (xSamePartStartIndex - str1BeginIndex));
//			System.out.println("str1EndIndex - xSamePartEndIndex:" + (str1EndIndex - xSamePartEndIndex));
//			System.out.println("ySamePartStartIndex - str2BeginIndex:" + (ySamePartStartIndex - str2BeginIndex));
//			System.out.println("str2EndIndex - ySamePartEndIndex:" + (str2EndIndex - ySamePartEndIndex));
			if (xSamePartStartIndex - str1BeginIndex <= 0){
				for (int i = str2BeginIndex; i<ySamePartStartIndex; i++){
					str2Array[i] = 1;
				}
			} else if (ySamePartStartIndex - str2BeginIndex <= 0){
				for (int i = str1BeginIndex; i<xSamePartStartIndex; i++){
					str1Array[i] = 1;
				}
			} else {
				LCS(str1BeginIndex, xSamePartStartIndex-1, str2BeginIndex, ySamePartStartIndex-1 );
			}
			
			if (str1EndIndex - xSamePartEndIndex <=0){
				for (int i = ySamePartEndIndex+1; i<=str2EndIndex; i++){
					str2Array[i] = 1;
				}
			} else if(str2EndIndex - ySamePartEndIndex <=0){
				for (int i = xSamePartEndIndex+1; i<=str1EndIndex; i++){
					str1Array[i] = 1;
				}
			} else {
				LCS(xSamePartEndIndex+1, str1EndIndex, ySamePartEndIndex+1, str2EndIndex);
			}
				
		} else {
//			System.out.println("===There are no any same part between===" );
//			System.out.println("String A:" + str1.substring(str1BeginIndex, str1EndIndex+1));
//			System.out.println("String B:" + str2.substring(str2BeginIndex, str2EndIndex+1));
			//for the different part, mark str1Array and str2Array corresponding part to 1
			for (int i = str1BeginIndex; i <= str1EndIndex; i++){
				str1Array[i]=1;
			}
			for (int i = str2BeginIndex; i <= str2EndIndex; i++){
				str2Array[i]=1;
			}
			return;
		}
	}



}


ProcessorManager.java
package processor;

public class ProcessorManager {
	
	public static ProcessorManager processorManager;
	private DiffProcessor diffProcessor;
	
	public DiffProcessor getDiffProcessor() {
		return diffProcessor;
	}

	public void setDiffProcessor(DiffProcessor diffProcessor) {
		this.diffProcessor = diffProcessor;
	}

	public static synchronized ProcessorManager getInstance(){
		if (processorManager == null){
			processorManager = new ProcessorManager();
		}
		return processorManager;
	}

}


UIManager.java
package ui;

import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class UIManager {
	private static UIManager uiManager;
	public static synchronized UIManager getInstance(){
		if (uiManager == null){
			uiManager = new UIManager();
		}
		return uiManager;
	}
	private JTextPane jTextPaneA;
	private JTextPane jTextPaneB;
	
	MutableAttributeSet diffAttr;
	MutableAttributeSet defaultAttr;

	public UIManager(){
		iniAttr();
	}
	
	public void iniAttr(){
		diffAttr = new SimpleAttributeSet();
		defaultAttr = new SimpleAttributeSet();
		StyleConstants.setForeground(diffAttr, Color.red);
		StyleConstants.setForeground(defaultAttr, Color.black);		
	}

	public MutableAttributeSet getDefaultAttr() {
		return defaultAttr;
	}

	public void setDefaultAttr(MutableAttributeSet defaultAttr) {
		this.defaultAttr = defaultAttr;
	}

	public MutableAttributeSet getDiffAttr() {
		return diffAttr;
	}
	
	public JTextPane getJTextPaneA() {
		return jTextPaneA;
	}
	public JTextPane getJTextPaneB() {
		return jTextPaneB;
	}
	public void setDiffAttr(MutableAttributeSet diffAttr) {
		this.diffAttr = diffAttr;
	}
	public void setJTextPaneA(JTextPane textPaneA) {
		jTextPaneA = textPaneA;
	}
	public void setJTextPaneB(JTextPane textPaneB) {
		jTextPaneB = textPaneB;
	}
	
}


ViewerFrame.java
package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoundedRangeModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;

import constData.Const;

import processor.DiffProcessor;
import processor.ProcessorManager;


public class ViewerFrame extends JFrame{
 
	private JTextPane jTextPaneA;
	private JTextPane jTextPaneB;
	
    public ViewerFrame()   
    {   
    	
        VierMenuBar menuTest = new VierMenuBar();    
        CenterPanel centerPanel = new CenterPanel();  
        BottomPanel bottomPanel = new BottomPanel();
           

        Container c = this.getContentPane();   
        // c.setLayout(new BorderLayout())   
        this.setJMenuBar(menuTest);   
            
        c.add(centerPanel,BorderLayout.CENTER);   
        c.add(bottomPanel,BorderLayout.SOUTH);   
           
        this.addWindowListener(new WindowAdapter()   
            {   
                public void WindowClosing(WindowEvent e)   
                {       
                    dispose();   
                    System.exit(0);   
                }   
            });   
               
           
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(700,500);   
        setTitle("DiffViewer1.0");     
        setLocation(200,150);   
        setVisible(true);        
    } 
    

    class VierMenuBar extends JMenuBar   
    {   
        private JDialog aboutDialog;   
               
 
        public VierMenuBar()   
        {   
            JMenu fileMenu = new JMenu("File");   
            JMenuItem exitMenuItem = new JMenuItem("Exit",KeyEvent.VK_E);   
            JMenuItem aboutMenuItem = new JMenuItem("About",KeyEvent.VK_A);            
                                                   
            fileMenu.add(exitMenuItem);   
            fileMenu.add(aboutMenuItem);   
               
            this.add(fileMenu);        
               
                       
            aboutDialog = new JDialog();   
            initAboutDialog();   
                           
            exitMenuItem.addActionListener(new ActionListener()   
            {   
                public void actionPerformed(ActionEvent e)   
                {   
                    dispose();   
                    System.exit(0);   
                }   
            });   
               
            aboutMenuItem.addActionListener(new ActionListener()   
            {   
                public void actionPerformed(ActionEvent e)   
                {   
                    aboutDialog.setVisible(true);   
                }   
            });            
                           
        }   
            
        public JDialog getAboutDialog()   
        {   
            return aboutDialog;   
        }   
           

        public void initAboutDialog()   
        {   
            aboutDialog.setTitle("About");   
               
            Container con =aboutDialog.getContentPane();   
                
            Icon icon = new ImageIcon("smile.gif");   
            JLabel aboutLabel = new JLabel("<html><b><font size=5>"+   
            "<center>Jay"+"<br>Test Version",icon,JLabel.CENTER);   
                            
            con.add(aboutLabel,BorderLayout.CENTER);   
               
            aboutDialog.setSize(450,225);   
            aboutDialog.setLocation(300,300);                          
            aboutDialog.addWindowListener(new WindowAdapter()   
            {   
                public void WindowClosing(WindowEvent e)   
                {   
                    dispose();   
                }                      
            });            
        }   
    }   
       
    class CenterPanel extends JPanel   
    {   
        public CenterPanel()   
        {   
        	JScrollPane jScrollPane1 = new JScrollPane();
        	JScrollPane jScrollPane2 = new JScrollPane();
        	       	
            JTextPane jTextPaneA = new JTextPane();
            JTextPane jTextPaneB = new JTextPane();
            UIManager.getInstance().setJTextPaneA(jTextPaneA);
            UIManager.getInstance().setJTextPaneB(jTextPaneB);
            
            
            jTextPaneA.setDocument(new DefaultStyledDocument());
            jTextPaneB.setDocument(new DefaultStyledDocument());
            
        	jScrollPane1.setViewportView(jTextPaneA);
        	jScrollPane2.setViewportView(jTextPaneB);
               

               
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jScrollPane1,jScrollPane2);   
            this.setLayout(new BorderLayout());                     
            this.add(splitPane,BorderLayout.CENTER);   
            this.setEnabled(true);             
        }   
    }   
    
    class BottomPanel extends JPanel   
    {   
        public BottomPanel()   
        {
           JButton compareButton = new JButton("Compare");  
           compareButton.addActionListener(new ActionListener(){
        	   
			public void actionPerformed(ActionEvent e) {
				System.out.println("Press the button!!");
				JTextPane jTextPaneA = UIManager.getInstance().getJTextPaneA();
				JTextPane jTextPaneB = UIManager.getInstance().getJTextPaneB();
				
				DefaultStyledDocument docA = (DefaultStyledDocument)jTextPaneA.getDocument();
				DefaultStyledDocument docB = (DefaultStyledDocument)jTextPaneB.getDocument();
				
				try {
					String str1 = docA.getText(0, docA.getLength());
					String str2 = docB.getText(0, docB.getLength());
					
					DiffProcessor processor = ProcessorManager.getInstance().getDiffProcessor();
					processor.reSetString(str1, str2);
					
					int[][] result = processor.process();
//					for (int i=0; i<result.length; i++){
//						for (int j=0; j<result[i].length; j++){
//							System.out.print(result[i][j]);
//						}
//						System.out.println();
//					}
				
					MutableAttributeSet diffAttr = UIManager.getInstance().getDiffAttr();
					MutableAttributeSet defaultAttr = UIManager.getInstance().getDefaultAttr();
					
					// set jTextPaneA 
					for (int i = 0; i<result[Const.STR_ARRAY_A].length; i++){
						if (result[Const.STR_ARRAY_A][i] == 1){
							docA.setCharacterAttributes(i, 1, diffAttr, true);
						} else {
							docA.setCharacterAttributes(i, 1, defaultAttr, true);
						}
					}
					
					// set jTextPaneB
					for (int i=0; i<result[Const.STR_ARRAY_B].length ; i++){
						if (result[Const.STR_ARRAY_B][i] == 1){
							docB.setCharacterAttributes(i, 1, diffAttr, true);
						} else {
							docB.setCharacterAttributes(i, 1, defaultAttr, true);
						}
					}
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
				
			}
			
           });
           this.setLayout(new BorderLayout());
           this.add(compareButton,BorderLayout.CENTER);                  
        }   
    }   
     
    public static void main(String args[]){
    	new ViewerFrame();
    	ProcessorManager.getInstance().setDiffProcessor(new DiffProcessor());
    }
}
分享到:
评论

相关推荐

    CodeReview:CodeReview是一个Git GUI工具,用于执行以Python3和Qt5编写的代码审查(Diff Viewer)。

    它提供了强大的Diff Viewer功能,帮助开发者直观地查看和理解代码变更,从而提高代码质量和团队协作效率。 **Git基础与用途** Git是一个分布式版本控制系统,由Linus Torvalds为Linux内核开发。它允许开发者跟踪...

    CVS Branch Diff Viewer-开源

    【CVS Branch Diff Viewer】是一款开源的工具,主要用于帮助用户以直观的图形界面查看CVS(Concurrent Versions System)项目中的分支差异。CVS是一种版本控制系统,它允许开发者管理代码的不同版本,跟踪每一次修改...

    react-diff-viewer:使用Diff和React制作的简单美观的文本diff查看器组件

    查看v2.0安装yarn add react-diff-viewer# ornpm i react-diff-viewer用法import React , { PureComponent } from 'react' ;import ReactDiffViewer from 'react-diff-viewer' ;const oldCode = `const a = 10const...

    Github Jupyter diff viewer-crx插件

    ipynb)的diff查看器在github中仅显示在拉出请求或提交中更改文件时显示源代码(Python和Markdown)的差异。 如果要在私有存储库中使用它,则必须在选项中输入GitHub Personal Access令牌。 查看文件时使用拉拔请求...

    svn_excel_diff

    设置方法:右键--&gt;TortoiseSVN--&gt;Settings---&gt;Diff Viewer--&gt;Advanced--&gt;.xls--&gt;edit:把路径设计svn_excel_diff.exe所以路径,如:D:/svn_excel_diff.exe. 即可使用。有问题联系:boluns@126.com

    SVN 比较工具

    7. **关联Diff viewer**:在TortoiseSVN的设置中,选择"Settings" -&gt; "External Programs",然后配置"Diff viewer"为Beyond Compare,这样在进行SVN的文件比较时,会自动调用Beyond Compare来显示差异。 8. **使用...

    diffviewer:在线差异工具

    DiffViewer:在线差异工具 DiffViewer 是一种在线工具,可让您快速查看两段文本之间的差异。 实际上,这是一个简单的网站,使用 、 和来展示使用库是多么容易。 截屏 执照 这个工具是在下发布的。

    Fork-1.0.91.1.dmg

    适用于Mac和Windows的快速友好的git客户端...Fork的Diff Viewer提供了清晰的视图,可以快速发现源代码中的更改。 在不离开应用程序的情况下管理您的存储库。 使用非官方视图,您可以找到更改特定文件行的最后一个人。

    svn diff excel

    设置方法:右键--&gt;TortoiseSVN--&gt;Settings---&gt;Diff Viewer--&gt;Advanced--&gt;.xls--&gt;edit:把路径设计svn_excel_diff.exe所以路径,如:D:/svn_excel_diff.exe. 注:现在文件名和路径名为中文还是会有问题。

    diff-viewer:用于src-reader处理的文件的差异查看器

    差异查看器diff-viewer允许我们以类似GitHub的样式可视化文件的两个版本之间的差异。 它专注于将当前版本与创建的后处理版本进行。用法打开一个新的浏览器窗口或标签,然后输入URL:_ * 例子:屏幕截图灵感改编自的...

    react-diff:给定两个输入,突出显示差异

    var Diff = require ( 'react-diff' ) ; var Component = React . createClass ( { render : function ( ) { return ( &lt; Diff xss=removed xss=removed xss=removed&gt; ) ; } } ) ; 执照 麻省理工学院

    git&TortoiseGit&TortoiseSVN安装包

    此外,TortoiseGit还提供了图形化的历史查看器(Log Viewer)和差异比较工具(Diff Viewer),方便用户查看代码变更记录和对比不同版本的差异。 **TortoiseSVN** TortoiseSVN是Subversion的Windows客户端,...

    review board 搭建内容

    - **Diff Viewer插件**:增强Review Board的代码差异查看功能,例如支持颜色高亮、代码折叠等。 - **Email通知**:配置邮件服务器,使Review Board能够发送审查请求的通知,提高团队沟通效率。 - **第三方集成**:...

    diff-viewer-crx插件

    工具查看浏览器上的diff屏幕上半部分有一个编辑器,下半部分为分开,以便显示结果编辑器部件分为左右,然后在更改输入到左编辑器的内容之前,将在更改为右侧的内容后显示差异... 在浏览器工具上查看diff屏幕上半...

    SVN SVN工具 windows系统x64版本

    3. **差异视图(Diff Viewer)**:显示文件的差异,帮助用户理解文件的更改内容。 4. **冲突解决工具**:内置冲突解决工具,帮助用户处理版本冲突问题。 5. **URL支持**:可以通过URL直接访问远程仓库,方便远程...

    SVN集成 Beyond Compare 文档

    - 打开Eclipse,依次点击`Window -&gt; Preferences -&gt; SVN -&gt; Diff Viewer`。 - 在弹出的界面中点击`Add`按钮,添加一个新的外部比较器。 - `Extension or mine-type`设置为`*`,表示所有类型的文件都可以使用...

    react-diff-view:git diff组件

    React差异视图一个git diff组件,用于消耗git统一diff输出。概述拆分视图统一检视优化选择功能齐全一个干净而小巧的核心,可以显示差异。 支持拆分(并排)视图和统一(一列)视图。 可容忍的性能。 灵活的装饰组件...

    Github Side-by-Side Diffs-crx插件

    在github中支持并排差异,以及Github的Diff Viewer的其他改进。 功能包括: 显示更多上下文:增加插入和删除周围显示的行数,以获取有关更改的更多上下文。 并排视图:查看Diff,以便插入和删除是并排而不是默认的...

    Notepads轻量级文本编辑器

    一款新 Windows 10 记事本,包含C++编写,包含源代码和安装包。  流利的设计,内置标签系统。  速度快得惊人,重量轻得惊人。  在命令行或... 内置diff viewer(预览你的修改)。  会话快照和多实例。

    puppet-catalog-diff-viewer:puppet-catalog-diff工具的查看器

    $ docker run -ti -p 8080:8080 camptocamp/puppet-catalog-diff-viewer 将允许您访问的目录差异查看器。 服务器端存储 如果可以从reportlist.json读取,则将会自动填充可用报告的下拉列表。 该文件包含data json...

Global site tag (gtag.js) - Google Analytics