1 11 package org.eclipse.compare.internal; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 import java.util.ResourceBundle ; 16 import java.lang.reflect.InvocationTargetException ; 17 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.swt.graphics.Image; 23 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.text.IDocument; 28 import org.eclipse.jface.text.BadLocationException; 29 30 import org.eclipse.ui.*; 31 import org.eclipse.ui.part.FileEditorInput; 32 import org.eclipse.ui.texteditor.ITextEditor; 33 import org.eclipse.ui.texteditor.IDocumentProvider; 34 import org.eclipse.ui.actions.WorkspaceModifyOperation; 35 36 import org.eclipse.compare.*; 37 import org.eclipse.compare.ITypedElement; 38 import org.eclipse.compare.IStreamContentAccessor; 39 40 41 public class EditionAction extends BaseCompareAction { 42 43 47 class DocumentBufferNode implements ITypedElement, IEncodedStreamContentAccessor { 48 private static final String UTF_16= "UTF-16"; private IDocument fDocument; 50 private IFile fFile; 51 52 DocumentBufferNode(IDocument document, IFile file) { 53 fDocument= document; 54 fFile= file; 55 } 56 57 public String getName() { 58 return fFile.getName(); 59 } 60 61 public String getType() { 62 return fFile.getFileExtension(); 63 } 64 65 public Image getImage() { 66 return null; 67 } 68 69 public InputStream getContents() { 70 return new ByteArrayInputStream (Utilities.getBytes(fDocument.get(), UTF_16)); 71 } 72 73 public String getCharset() { 74 return UTF_16; 75 } 76 } 77 78 private String fBundleName; 79 private boolean fReplaceMode; 80 protected boolean fPrevious= false; 81 protected String fHelpContextId; 82 83 EditionAction(boolean replaceMode, String bundleName) { 84 fReplaceMode= replaceMode; 85 fBundleName= bundleName; 86 } 87 88 protected boolean isEnabled(ISelection selection) { 89 return Utilities.getFiles(selection).length == 1; } 91 92 protected void run(ISelection selection) { 93 IFile[] files= Utilities.getFiles(selection); 94 for (int i= 0; i < files.length; i++) 95 doFromHistory(files[i]); 96 } 97 98 private void doFromHistory(final IFile file) { 99 100 ResourceBundle bundle= ResourceBundle.getBundle(fBundleName); 101 String title= Utilities.getString(bundle, "title"); 103 Shell parentShell= CompareUIPlugin.getShell(); 104 105 IFileState states[]= null; 106 try { 107 states= file.getHistory(null); 108 } catch (CoreException ex) { 109 MessageDialog.openError(parentShell, title, ex.getMessage()); 110 return; 111 } 112 113 if (states == null || states.length <= 0) { 114 String msg= Utilities.getString(bundle, "noLocalHistoryError"); MessageDialog.openInformation(parentShell, title, msg); 116 return; 117 } 118 119 ITypedElement base= new ResourceNode(file); 120 121 IDocument document= getDocument(file); 122 ITypedElement target= base; 123 if (document != null) 124 target= new DocumentBufferNode(document, file); 125 126 ITypedElement[] editions= new ITypedElement[states.length+1]; 127 editions[0]= base; 128 for (int i= 0; i < states.length; i++) 129 editions[i+1]= new HistoryItem(base, states[i]); 130 131 EditionSelectionDialog d= new EditionSelectionDialog(parentShell, bundle); 132 d.setEditionTitleArgument(file.getName()); 133 d.setEditionTitleImage(CompareUIPlugin.getImage(file)); 134 if (fHelpContextId != null) 136 d.setHelpContextId(fHelpContextId); 137 138 if (fReplaceMode) { 139 140 ITypedElement ti= null; 141 if (fPrevious) 142 ti= d.selectPreviousEdition(target, editions, null); 143 else 144 ti= d.selectEdition(target, editions, null); 145 146 if (ti instanceof IStreamContentAccessor) { 147 IStreamContentAccessor sa= (IStreamContentAccessor)ti; 148 149 if (Utilities.validateResource(file, parentShell, title)) { 150 try { 151 152 if (document != null) 153 updateDocument(document, sa); 154 else 155 updateWorkspace(bundle, parentShell, sa, file); 156 157 } catch (InterruptedException x) { 158 160 } catch (InvocationTargetException x) { 161 String reason= x.getTargetException().getMessage(); 162 MessageDialog.openError(parentShell, title, Utilities.getFormattedString(bundle, "replaceError", reason)); } 164 } 165 } 166 } else { 167 d.setCompareMode(true); 168 169 d.selectEdition(target, editions, null); 170 } 171 } 172 173 private void updateWorkspace(final ResourceBundle bundle, Shell shell, 174 final IStreamContentAccessor sa, final IFile file) 175 throws InvocationTargetException , InterruptedException { 176 WorkspaceModifyOperation operation= new WorkspaceModifyOperation() { 177 public void execute(IProgressMonitor pm) throws InvocationTargetException { 178 try { 179 String taskName= Utilities.getString(bundle, "taskName"); pm.beginTask(taskName, IProgressMonitor.UNKNOWN); 181 file.setContents(sa.getContents(), false, true, pm); 182 } catch (CoreException e) { 183 throw new InvocationTargetException (e); 184 } finally { 185 pm.done(); 186 } 187 } 188 }; 189 190 ProgressMonitorDialog pmdialog= new ProgressMonitorDialog(shell); 191 pmdialog.run(false, true, operation); 192 } 193 194 private void updateDocument(IDocument document, IStreamContentAccessor sa) throws InvocationTargetException { 195 try { 196 String text= Utilities.readString(sa); 197 document.replace(0, document.getLength(), text); 198 } catch (CoreException e) { 199 throw new InvocationTargetException (e); 200 } catch (BadLocationException e) { 201 throw new InvocationTargetException (e); 202 } 203 } 204 205 private IDocument getDocument(IFile file) { 206 IWorkbench wb= PlatformUI.getWorkbench(); 207 if (wb == null) 208 return null; 209 IWorkbenchWindow[] ws= wb.getWorkbenchWindows(); 210 if (ws == null) 211 return null; 212 213 FileEditorInput test= new FileEditorInput(file); 214 215 for (int i= 0; i < ws.length; i++) { 216 IWorkbenchWindow w= ws[i]; 217 IWorkbenchPage[] wps= w.getPages(); 218 if (wps != null) { 219 for (int j= 0; j < wps.length; j++) { 220 IWorkbenchPage wp= wps[j]; 221 IEditorPart ep= wp.findEditor(test); 222 if (ep instanceof ITextEditor) { 223 ITextEditor te= (ITextEditor) ep; 224 IDocumentProvider dp= te.getDocumentProvider(); 225 if (dp != null) { 226 IDocument doc= dp.getDocument(ep); 227 if (doc != null) 228 return doc; 229 } 230 } 231 } 232 } 233 } 234 return null; 235 } 236 } 237 238 | Popular Tags |