|
From: Matthias K <mat...@us...> - 2006-03-28 17:16:43
|
Update of /cvsroot/jcommander/plugins/org.jcommander.ui.filepanel/src/org/jcommander/ui/filepanel/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26135/src/org/jcommander/ui/filepanel/actions Added Files: CompareAction.java Log Message: First implementation of a diff view (Feature request 1115411) --- NEW FILE: CompareAction.java --- /** * */ package org.jcommander.ui.filepanel.actions; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import org.apache.commons.vfs.*; import org.eclipse.compare.*; import org.eclipse.compare.structuremergeviewer.DiffNode; import org.eclipse.compare.structuremergeviewer.Differencer; import org.eclipse.compare.structuremergeviewer.IStructureComparator; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.program.Program; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.WorkbenchException; import org.jcommander.compare.ComparePlugin; import org.jcommander.compare.CompareTest; import org.jcommander.compare.perspective.ComparePerspectiveFactory; import org.jcommander.ui.filepanel.FilePanelManagerProvider; import org.jcommander.ui.filepanel.controls.FileTab; import org.jcommander.ui.utils.EclipseBroker; import org.jcommander.ui.utils.ImageBroker; import org.jcommander.ui.utils.MessageDisplayer; /** * @author MatthiasK * */ public class CompareAction implements IWorkbenchWindowActionDelegate { /* (non-Javadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() */ public void dispose() { // TODO Auto-generated method stub } // private static class FileInput implements IStructureComparator, ITypedElement, IStreamContentAccessor, IEditableContent { // // private File file; // /** // * // */ // // public String base; // // //protected static final ImageRegistry imgRegistry = new ImageRegistry(); // // public boolean equals(Object other) { // if (other instanceof ITypedElement) // return getName().equals(((ITypedElement) other).getName()); // return super.equals(other); // } // // public int hashCode() { // return getName().hashCode(); // } // // public FileInput(File file, String base) { // // TODO Auto-generated constructor stub // System.out.println(file.getName()); // this.file=file; // this.base=base; // } // // /* (non-Javadoc) // * @see org.eclipse.compare.structuremergeviewer.IStructureComparator#getChildren() // */ // public Object[] getChildren() { // //System.out.println("getchildren"); // if (file.listFiles()==null) // { // //System.out.println("Keine Kinder"); // return null; // // } // //System.out.println("Kinder->"); // //ArrayList list = new ArrayList(); // Object[] children= new Object[file.listFiles().length]; // File[] filelist = file.listFiles(); // // for (int i = 0; i < filelist.length; i++) { // //list.add(new FileInput(filelist[i])); // children[i]=new FileInput(filelist[i],base); // } // return children;//list.toArray(); // } // // /* (non-Javadoc) // * @see org.eclipse.compare.ITypedElement#getName() // */ // public String getName() { // //return file.getAbsolutePath().substring(base.length()); // return file.getName(); // } // // public Image getIconFromProgram(Program program) { // Image image;// = (Image) imgRegistry.get(program.getName()); // //if (image == null) { // ImageData imageData = program.getImageData(); // if (imageData != null) { // image = new Image(null, imageData, imageData.getTransparencyMask()); // //imgRegistry.put(program.getName(), image); // } else { // image = ImageBroker.FILE_ICON; // } // //} // return image; // } // // /* (non-Javadoc) // * @see org.eclipse.compare.ITypedElement#getImage() // */ // public Image getImage() { // Image iconImage = null; // if (file.isDirectory()) return ImageBroker.FOLDER_ICON; // int dot = file.getName().lastIndexOf('.'); // if (dot != -1) { // String extension = file.getName().substring(dot); // Program program = Program.findProgram(extension); // if (program != null) { // //typeString = program.getName(); // //typeString = extension.toUpperCase(); // iconImage = getIconFromProgram(program); // } else { // iconImage = ImageBroker.FILE_ICON; // } // } else { // iconImage = ImageBroker.FILE_ICON; // } // return iconImage; // } // // /* (non-Javadoc) // * @see org.eclipse.compare.ITypedElement#getType() // */ // public String getType() { // // TODO Auto-generated method stub // if (file.isDirectory()){ // System.out.println(FOLDER_TYPE); // return FOLDER_TYPE; // } // else { // String name=file.getName(); // int dot=name.lastIndexOf('.'); // String type=name.substring(dot+1); // System.out.println(type); // return type; // } // //System.out.println((file.isDirectory())?FOLDER_TYPE:TEXT_TYPE); // //return (file.isDirectory())?FOLDER_TYPE:TEXT_TYPE; // } // // /* (non-Javadoc) // * @see org.eclipse.compare.IStreamContentAccessor#getContents() // */ // public InputStream getContents() throws CoreException { // // TODO Auto-generated method stub // if (file.isDirectory()) return null; // try { // return new FileInputStream(file); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return null; // } // // public boolean isEditable() { // // TODO Auto-generated method stub // return true; // } // // public void setContent(byte[] newContent) { // // TODO Auto-generated method stub // // } // // public ITypedElement replace(ITypedElement dest, ITypedElement src) { // // TODO Auto-generated method stub // return null; // } // // } /** * * @author MatthiasK * FileInput encapsulates an FileObject into an class the * eclipse comparefunctions are able to use. */ private static class FileInput implements IStructureComparator, ITypedElement, IStreamContentAccessor, IEditableContent { /** * The FileObject this FileInput represents */ private FileObject file; /** * The basedirectory from which we are comparing. * Not any longer needed, just not yet removed. */ public String base; //protected static final ImageRegistry imgRegistry = new ImageRegistry(); public boolean equals(Object other) { if (other instanceof ITypedElement) return getName().equals(((ITypedElement) other).getName()); return super.equals(other); } public int hashCode() { return getName().hashCode(); } public FileInput(FileObject file, String base) { //System.out.println(file.getName()); this.file=file; this.base=base; } public FileInput(FileObject file) { System.out.println(file.getName()); this.file=file; this.base=""; } /* (non-Javadoc) * @see org.eclipse.compare.structuremergeviewer.IStructureComparator#getChildren() */ public Object[] getChildren() { //System.out.println("getchildren"); try { if (file.getChildren()==null) { return null; } } catch (FileSystemException e) { return null; } Object[] children=null; FileObject[] filelist=null; try { children= new Object[file.getChildren().length]; filelist = file.getChildren(); } catch (FileSystemException e) { e.printStackTrace(); } for (int i = 0; i < filelist.length; i++) { children[i]=new FileInput(filelist[i],base); } return children; } /* (non-Javadoc) * @see org.eclipse.compare.ITypedElement#getName() */ public String getName() { //return file.getAbsolutePath().substring(base.length()); return file.getName().getBaseName(); } private Image getIconFromProgram(Program program) { Image image;// = (Image) imgRegistry.get(program.getName()); //if (image == null) { ImageData imageData = program.getImageData(); if (imageData != null) { image = new Image(null, imageData, imageData.getTransparencyMask()); //imgRegistry.put(program.getName(), image); } else { image = ImageBroker.FILE_ICON; } //} return image; } /* (non-Javadoc) * @see org.eclipse.compare.ITypedElement#getImage() */ public Image getImage() { Image iconImage = null; try { if (file.getType()==FileType.FOLDER) return ImageBroker.FOLDER_ICON; } catch (FileSystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } int dot = file.getName().getBaseName().lastIndexOf('.'); if (dot != -1) { String extension = file.getName().getBaseName().substring(dot); Program program = Program.findProgram(extension); if (program != null) { //typeString = program.getName(); //typeString = extension.toUpperCase(); iconImage = getIconFromProgram(program); } else { iconImage = ImageBroker.FILE_ICON; } } else { iconImage = ImageBroker.FILE_ICON; } return iconImage; } /* (non-Javadoc) * @see org.eclipse.compare.ITypedElement#getType() */ public String getType() { // TODO Auto-generated method stub try { if (file.getType()==FileType.FOLDER) { return FOLDER_TYPE; } else { String name=file.getName().getBaseName(); int dot=name.lastIndexOf('.'); return name.substring(dot+1); } //System.out.println((file.getType()==FileType.FOLDER)?FOLDER_TYPE:TEXT_TYPE); //return (file.getType()==FileType.FOLDER)?FOLDER_TYPE:TEXT_TYPE; } catch (FileSystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /* (non-Javadoc) * @see org.eclipse.compare.IStreamContentAccessor#getContents() */ public InputStream getContents() throws CoreException { // TODO Auto-generated method stub try { if (file.getType()==FileType.FOLDER) return null; return file.getContent().getInputStream(); } catch (FileSystemException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; } public boolean isEditable() { // TODO Auto-generated method stub return true; } public void setContent(byte[] newContent) { // TODO Auto-generated method stub try { System.out.println("setContent"); String nC=new String(newContent); System.out.println(nC); OutputStream oStream=file.getContent().getOutputStream(); oStream.write(nC.getBytes()); oStream.close(); } catch (FileSystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ITypedElement replace(ITypedElement dest, ITypedElement src) { // TODO Implement! // if ((dest==null)&&(src!=null)) { // System.out.println("Add: "+((FileInput)src).file.getName().getURI()); // } else if ((dest!=null)&&(src==null)) { // System.out.println("Remove: "+((FileInput)src).file.getName().getURI()); // } System.out.println("replace"); try{ if (dest!=null) { if (dest instanceof FileInput) { FileInput fDest = (FileInput) dest; if (src==null) { fDest.file.delete(); } else if (src instanceof FileInput) { FileInput fSrc = (FileInput) src; fDest.file.copyFrom(fSrc.file, null); return fDest; } else { System.out.println("Error in FileInput.replace: Src no FileInput"); } } else { System.out.println("Error in FileInput.replace: Dest no FileInput"); } } else if (src!=null) { if (src instanceof FileInput) { FileInput fSrc = (FileInput) src; FileObject fDest=file.resolveFile(fSrc.file.getName().getBaseName()); System.out.println("Copying to "+fDest.getName().getURI()); fDest.copyFrom(fSrc.file,new AllFileSelector()); return new FileInput(fDest,base); } else { System.out.println("Error in FileInput.replace: Src no FileInput"); } } else { System.out.println("Error in FileInput.replace: Src and Dest null"); } } catch (FileSystemException e) { // TODO: handle exception e.printStackTrace(); } return null; } } /* (non-Javadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) */ public void init(IWorkbenchWindow window) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ public void run(IAction action) { /* * Code below copied from CompareByContentAction */ boolean error = true; FileTab tab = FilePanelManagerProvider .getTabManager() .getActiveTabMediator() .getFileTab(); /* Try to find a two file selection in the active panel */ FileObject leftFile = null; FileObject rightFile = null; //TODO: Unnecessary loop? FilePanel.getPanelMediator().getFilePanel()??? FileObject[] selectedFiles = tab.getActiveFilePanel() .getPanelMediator() .getFilePanel() .getFileControl() .getSelectedFiles(); if(selectedFiles.length == 2) { error = false; // We have found an exact selection in the active panel leftFile = selectedFiles[0]; rightFile = selectedFiles[1]; } else if(selectedFiles.length == 1){ // Try to find a matching file in the other panel selectedFiles = tab.getLeftFilePanel() .getPanelMediator() .getFilePanel() .getFileControl() .getSelectedFiles(); FileObject[] otherSelectedFiles = tab.getRightFilePanel() .getPanelMediator() .getFilePanel() .getFileControl() .getSelectedFiles(); if((otherSelectedFiles.length ==1)&&(selectedFiles.length ==1)) { error = false; leftFile = selectedFiles[0]; rightFile = otherSelectedFiles[0]; } } final FileObject fLeftFile=leftFile; final FileObject fRightFile=rightFile; if(!error) { try { IWorkbenchPage page = PlatformUI.getWorkbench().showPerspective(ComparePlugin.COMPARE_PERSPECTIVE_ID, EclipseBroker.getWorkbenchWindow()); } catch (WorkbenchException e) { // TODO Auto-generated catch block e.printStackTrace(); } CompareConfiguration cc = new CompareConfiguration() { /* (non-Javadoc) * @see org.eclipse.compare.CompareConfiguration#getRightLabel(java.lang.Object) */ @Override public String getRightLabel(Object element) { if (element!=null) { DiffNode diffNode = (DiffNode) element; if ((diffNode.getRight()!=null)&&(diffNode.getRight() instanceof FileInput)) { FileInput file = (FileInput) diffNode.getRight(); return file.file.getName().getURI(); } return super.getRightLabel(element); } else return super.getRightLabel(element); } /* (non-Javadoc) * @see org.eclipse.compare.CompareConfiguration#getLeftLabel(java.lang.Object) */ @Override public String getLeftLabel(Object element) { if (element!=null) { DiffNode diffNode = (DiffNode) element; if ((diffNode.getLeft()!=null)&&(diffNode.getLeft() instanceof FileInput)) { FileInput file = (FileInput) diffNode.getLeft(); return file.file.getName().getURI(); } return super.getLeftLabel(element); } else return super.getLeftLabel(element); } }; cc.setLeftEditable(true); cc.setRightEditable(true); CompareEditorInput input = new CompareEditorInput(cc) { protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { Object fRoot; Differencer d = new Differencer(); //System.out.println("Finding diffs"); fRoot = d.findDifferences(false,monitor,null,null, new FileInput(fLeftFile), new FileInput(fRightFile)); //System.out.println("found diffs"); return fRoot; } }; CompareUI.openCompareEditor(input); } else { MessageDisplayer.setStatusErrorMessage("Could not find suitable selections for performing diff"); } } /* (non-Javadoc) * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) */ public void selectionChanged(IAction action, ISelection selection) { // TODO Auto-generated method stub } } |