比较器(eclipse中compare each那个)

时间:2021-02-06 21:54:18

只需要依赖插件:org.eclipse.compare;bundle-version="3.5.200"

例子,建一个RCP邮件那个demo,用下面的代码替换掉MessagePopupAction类

参考http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-compare/


package wwww;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.BufferedContent;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IModificationDate;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchWindow;

public class MessagePopupAction extends Action {

	private final IWorkbenchWindow window;

	MessagePopupAction(final String text, final IWorkbenchWindow window) {
		super(text);
		this.window = window;
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_OPEN_MESSAGE);
		// Associate the action with a pre-defined command, to allow key bindings.
		setActionDefinitionId(ICommandIds.CMD_OPEN_MESSAGE);
		setImageDescriptor(Activator.getImageDescriptor("/icons/sample3.gif"));
	}

	@Override
	public void run() {
		final CompareConfiguration config = new CompareConfiguration();
		config.setProperty(CompareConfiguration.SHOW_PSEUDO_CONFLICTS, Boolean.FALSE);

		// left
		config.setLeftEditable(true);
		config.setLeftLabel("Left");

		// right
		config.setRightEditable(true);
		config.setRightLabel("Right");

		final CompareEditorInput editorInput = new CompareEditorInput(config) {

			CompareItem left = new CompareItem("C:/A.txt");

			CompareItem right = new CompareItem("C:/Inject.log");

			@Override
			protected Object prepareInput(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
				return new DiffNode(null, Differencer.CONFLICTING, null, left, right);
			}

			@Override
			public void saveChanges(final IProgressMonitor pm) throws CoreException {
				super.saveChanges(pm);

				left.writeFile();
				right.writeFile();
			}
		};

		editorInput.setTitle("文件比较");

		// CompareUI.openCompareEditor(editorInput);
		CompareUI.openCompareDialog(editorInput);
	}

	class CompareItem extends BufferedContent implements ITypedElement, IModificationDate, IEditableContent {

		private final String fileName;

		private final long time;

		CompareItem(final String fileName) {
			this.fileName = fileName;
			time = System.currentTimeMillis();
		}

		/**
		 * @see org.eclipse.compare.BufferedContent#createStream()
		 */
		@Override
		protected InputStream createStream() throws CoreException {
			try {
				return new FileInputStream(new File(fileName));
			} catch (final FileNotFoundException e) {
				e.printStackTrace();
			}
			return new ByteArrayInputStream(new byte[0]);
		}

		/**
		 * @see org.eclipse.compare.IModificationDate#getModificationDate()
		 */
		@Override
		public long getModificationDate() {
			return time;
		}

		/**
		 * @see org.eclipse.compare.ITypedElement#getImage()
		 */
		@Override
		public Image getImage() {
			return CompareUI.DESC_CTOOL_NEXT.createImage();
		}

		/**
		 * @see org.eclipse.compare.ITypedElement#getName()
		 */
		@Override
		public String getName() {
			return fileName;
		}

		/**
		 * @see org.eclipse.compare.ITypedElement#getType()
		 */
		@Override
		public String getType() {
			return ITypedElement.TEXT_TYPE;
		}

		/**
		 * @see org.eclipse.compare.IEditableContent#isEditable()
		 */
		@Override
		public boolean isEditable() {
			return true;
		}

		/**
		 * @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
		 */
		@Override
		public ITypedElement replace(final ITypedElement dest, final ITypedElement src) {
			return null;
		}

		public void writeFile() {
			this.writeFile(fileName, getContent());
		}

		private void writeFile(final String fileName, final byte[] newContent) {
			FileOutputStream fos = null;
			try {
				final File file = new File(fileName);
				if (file.exists()) {
					file.delete();
				}

				file.createNewFile();

				fos = new FileOutputStream(file);
				fos.write(newContent);
				fos.flush();

			} catch (final IOException e) {
				e.printStackTrace();

			} finally {
				try {
					fos.close();
				} catch (final IOException e) {
					e.printStackTrace();
				}

				fos = null;
			}
		}
	}
}