KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > EditionAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
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     /**
44      * Implements the IStreamContentAccessor and ITypedElement protocols
45      * for a Document.
46      */

47     class DocumentBufferNode implements ITypedElement, IEncodedStreamContentAccessor {
48         private static final String JavaDoc UTF_16= "UTF-16"; //$NON-NLS-1$
49
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 JavaDoc getName() {
58             return fFile.getName();
59         }
60         
61         public String JavaDoc getType() {
62             return fFile.getFileExtension();
63         }
64         
65         public Image getImage() {
66             return null;
67         }
68         
69         public InputStream JavaDoc getContents() {
70             return new ByteArrayInputStream JavaDoc(Utilities.getBytes(fDocument.get(), UTF_16));
71         }
72
73         public String JavaDoc getCharset() {
74             return UTF_16;
75         }
76     }
77
78     private String JavaDoc fBundleName;
79     private boolean fReplaceMode;
80     protected boolean fPrevious= false;
81     protected String JavaDoc fHelpContextId;
82     
83     EditionAction(boolean replaceMode, String JavaDoc bundleName) {
84         fReplaceMode= replaceMode;
85         fBundleName= bundleName;
86     }
87
88     protected boolean isEnabled(ISelection selection) {
89         return Utilities.getFiles(selection).length == 1; // we don't support multiple selection for now
90
}
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 JavaDoc bundle= ResourceBundle.getBundle(fBundleName);
101         String JavaDoc title= Utilities.getString(bundle, "title"); //$NON-NLS-1$
102

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 JavaDoc msg= Utilities.getString(bundle, "noLocalHistoryError"); //$NON-NLS-1$
115
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         //d.setHideIdenticalEntries(false);
135
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 JavaDoc x) {
158                         // Do nothing. Operation has been canceled by user.
159

160                     } catch (InvocationTargetException JavaDoc x) {
161                         String JavaDoc reason= x.getTargetException().getMessage();
162                         MessageDialog.openError(parentShell, title, Utilities.getFormattedString(bundle, "replaceError", reason)); //$NON-NLS-1$
163
}
164                 }
165             }
166         } else {
167             d.setCompareMode(true);
168
169             d.selectEdition(target, editions, null);
170         }
171     }
172     
173     private void updateWorkspace(final ResourceBundle JavaDoc bundle, Shell shell,
174                         final IStreamContentAccessor sa, final IFile file)
175                                     throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
176         WorkspaceModifyOperation operation= new WorkspaceModifyOperation() {
177             public void execute(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
178                 try {
179                     String JavaDoc taskName= Utilities.getString(bundle, "taskName"); //$NON-NLS-1$
180
pm.beginTask(taskName, IProgressMonitor.UNKNOWN);
181                     file.setContents(sa.getContents(), false, true, pm);
182                 } catch (CoreException e) {
183                     throw new InvocationTargetException JavaDoc(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 JavaDoc {
195         try {
196             String JavaDoc text= Utilities.readString(sa);
197             document.replace(0, document.getLength(), text);
198         } catch (CoreException e) {
199             throw new InvocationTargetException JavaDoc(e);
200         } catch (BadLocationException e) {
201             throw new InvocationTargetException JavaDoc(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