org.chromium.debug.ui/src/org/chromium/debug/ui/actions/CompareChangesAction.java
changeset 355 8726e95bcbba
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
       
     1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.debug.ui.actions;
       
     6 
       
     7 import java.io.ByteArrayInputStream;
       
     8 import java.io.InputStream;
       
     9 import java.lang.reflect.InvocationTargetException;
       
    10 
       
    11 import org.chromium.debug.core.model.VmResource;
       
    12 import org.eclipse.compare.CompareConfiguration;
       
    13 import org.eclipse.compare.CompareEditorInput;
       
    14 import org.eclipse.compare.CompareUI;
       
    15 import org.eclipse.compare.IModificationDate;
       
    16 import org.eclipse.compare.IStreamContentAccessor;
       
    17 import org.eclipse.compare.ITypedElement;
       
    18 import org.eclipse.compare.structuremergeviewer.DiffNode;
       
    19 import org.eclipse.compare.structuremergeviewer.Differencer;
       
    20 import org.eclipse.core.resources.IFile;
       
    21 import org.eclipse.core.runtime.CoreException;
       
    22 import org.eclipse.core.runtime.IProgressMonitor;
       
    23 import org.eclipse.swt.graphics.Image;
       
    24 
       
    25 /**
       
    26  * A very preliminary implementation of action that should let user compare his script with
       
    27  * its current state on remote VM.
       
    28  */
       
    29 public class CompareChangesAction extends V8ScriptAction {
       
    30   @Override
       
    31   protected void execute(FilePair filePair) {
       
    32     LiveEditCompareInput input = new LiveEditCompareInput(filePair.getFile(), filePair.getVmResource());
       
    33     CompareUI.openCompareEditor(input);
       
    34   }
       
    35 
       
    36   private static class LiveEditCompareInput extends CompareEditorInput {
       
    37     private final IFile file;
       
    38     private final VmResource script;
       
    39 
       
    40     LiveEditCompareInput(IFile file, VmResource vmResource) {
       
    41       super(createCompareConfiguration());
       
    42       this.file = file;
       
    43       this.script = vmResource;
       
    44     }
       
    45 
       
    46     private static CompareConfiguration createCompareConfiguration() {
       
    47       return new CompareConfiguration();
       
    48     }
       
    49 
       
    50     @Override
       
    51     protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException,
       
    52         InterruptedException {
       
    53 
       
    54       abstract class CompareItem implements ITypedElement, IStreamContentAccessor,
       
    55           IModificationDate {
       
    56         public Image getImage() {
       
    57           return null;
       
    58         }
       
    59         public String getType() {
       
    60           return TEXT_TYPE;
       
    61         }
       
    62         public long getModificationDate() {
       
    63           return 0;
       
    64         }
       
    65       }
       
    66       CompareItem left = new CompareItem() {
       
    67         public String getName() {
       
    68           return "Local file " + file.getName(); //$NON-NLS-1$
       
    69         }
       
    70         public InputStream getContents() throws CoreException {
       
    71           return file.getContents();
       
    72         }
       
    73       };
       
    74       CompareItem right = new CompareItem() {
       
    75         public String getName() {
       
    76           return "File in VM " + script.getFileName(); //$NON-NLS-1$
       
    77         }
       
    78         public InputStream getContents() throws CoreException {
       
    79           return new ByteArrayInputStream(script.getScript().getSource().getBytes());
       
    80         }
       
    81       };
       
    82       DiffNode diffNode = new DiffNode(null, Differencer.PSEUDO_CONFLICT, null, left, right);
       
    83       return diffNode;
       
    84     }
       
    85   }
       
    86 }