KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > TextEditChangePreviewViewer


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.ltk.internal.ui.refactoring;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.ltk.core.refactoring.Change;
24 import org.eclipse.ltk.core.refactoring.GroupCategory;
25 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
26 import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.DisposeEvent;
30 import org.eclipse.swt.events.DisposeListener;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.resource.ImageDescriptor;
37 import org.eclipse.jface.text.IRegion;
38 import org.eclipse.jface.viewers.Viewer;
39
40 import org.eclipse.ui.model.IWorkbenchAdapter;
41
42 import org.eclipse.compare.CompareConfiguration;
43 import org.eclipse.compare.CompareUI;
44 import org.eclipse.compare.CompareViewerSwitchingPane;
45 import org.eclipse.compare.IEncodedStreamContentAccessor;
46 import org.eclipse.compare.IResourceProvider;
47 import org.eclipse.compare.ITypedElement;
48 import org.eclipse.compare.structuremergeviewer.DiffNode;
49 import org.eclipse.compare.structuremergeviewer.ICompareInput;
50
51 import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
52 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
53
54 public class TextEditChangePreviewViewer implements IChangePreviewViewer {
55
56     private ComparePreviewer fViewer;
57     
58     private static class TextEditBasedChangeInput extends ChangePreviewViewerInput {
59         TextEditBasedChangeGroup group;
60         int surroundingLines;
61         
62         TextEditBasedChangeGroup[] groups;
63         IRegion range;
64         
65         GroupCategory groupCategory;
66         
67         public TextEditBasedChangeInput(Change change) {
68             super(change);
69         }
70     }
71     
72     private static class ComparePreviewer extends CompareViewerSwitchingPane {
73         private CompareConfiguration fCompareConfiguration;
74         private String JavaDoc fLabel;
75         private ImageDescriptor fDescriptor;
76         private Image fImage;
77         public ComparePreviewer(Composite parent) {
78             super(parent, SWT.BORDER | SWT.FLAT, true);
79             fCompareConfiguration= new CompareConfiguration();
80             fCompareConfiguration.setLeftEditable(false);
81             fCompareConfiguration.setLeftLabel(RefactoringUIMessages.ComparePreviewer_original_source);
82             fCompareConfiguration.setRightEditable(false);
83             fCompareConfiguration.setRightLabel(RefactoringUIMessages.ComparePreviewer_refactored_source);
84             addDisposeListener(new DisposeListener() {
85                 public void widgetDisposed(DisposeEvent e) {
86                     if (fImage != null && !fImage.isDisposed())
87                         fImage.dispose();
88                 }
89             });
90             Dialog.applyDialogFont(this);
91         }
92         public void setLabel(String JavaDoc label) {
93             fLabel= label;
94         }
95         public void setImageDescriptor(ImageDescriptor imageDescriptor) {
96             fDescriptor= imageDescriptor;
97         }
98         protected Viewer getViewer(Viewer oldViewer, Object JavaDoc input) {
99             return CompareUI.findContentViewer(oldViewer, (ICompareInput)input, this, fCompareConfiguration);
100         }
101         public void setText(String JavaDoc text) {
102             if (fLabel != null) {
103                 super.setText(fLabel);
104             } else {
105                 super.setText(text);
106             }
107             Image current= null;
108             if (fDescriptor != null) {
109                 current= fImage;
110                 fImage= fDescriptor.createImage();
111             } else {
112                 current= fImage;
113                 fImage= null;
114             }
115             setImage(fImage);
116             if (current != null) {
117                 current.dispose();
118             }
119         }
120     }
121     
122     private static class CompareElement implements ITypedElement, IEncodedStreamContentAccessor, IResourceProvider {
123         // we use an encoding that preserves Unicode across the stream
124
private static final String JavaDoc ENCODING= "UTF-8"; //$NON-NLS-1$
125
private String JavaDoc fContent;
126         private String JavaDoc fType;
127         private IResource fResource;
128         public CompareElement(String JavaDoc content, String JavaDoc type, IResource resource) {
129             fContent= content;
130             fType= type;
131             fResource= resource;
132         }
133         public String JavaDoc getName() {
134             return RefactoringUIMessages.ComparePreviewer_element_name;
135         }
136         public Image getImage() {
137             return null;
138         }
139         public String JavaDoc getType() {
140             return fType;
141         }
142         public InputStream JavaDoc getContents() throws CoreException {
143             try {
144                 return new ByteArrayInputStream JavaDoc(fContent.getBytes(ENCODING));
145             } catch (UnsupportedEncodingException JavaDoc e) {
146                 return new ByteArrayInputStream JavaDoc(fContent.getBytes());
147             }
148         }
149         public String JavaDoc getCharset() {
150             return ENCODING;
151         }
152         public IResource getResource() {
153             return fResource;
154         }
155     }
156     
157     public static ChangePreviewViewerInput createInput(TextEditBasedChange change) {
158         return new ChangePreviewViewerInput(change);
159     }
160     
161     public static ChangePreviewViewerInput createInput(Change change, TextEditBasedChangeGroup group, int surroundingLines) {
162         TextEditBasedChangeInput input= new TextEditBasedChangeInput(change);
163         input.group= group;
164         input.surroundingLines= surroundingLines;
165         return input;
166     }
167     
168     public static ChangePreviewViewerInput createInput(Change change, TextEditBasedChangeGroup[] groups, IRegion range) {
169         TextEditBasedChangeInput input= new TextEditBasedChangeInput(change);
170         input.groups= groups;
171         input.range= range;
172         return input;
173     }
174
175     public void createControl(Composite parent) {
176         fViewer= new ComparePreviewer(parent);
177     }
178
179     public Control getControl() {
180         return fViewer;
181     }
182
183     public void setInput(ChangePreviewViewerInput input) {
184         try {
185             Change change= input.getChange();
186             if (input instanceof TextEditBasedChangeInput) {
187                 TextEditBasedChangeInput extended= (TextEditBasedChangeInput)input;
188                 if (extended.group != null && extended.surroundingLines >= 0) {
189                     TextEditBasedChangeGroup group= extended.group;
190                     TextEditBasedChange editChange= group.getTextEditChange();
191                     setInput(editChange, editChange.getCurrentContent(group.getRegion(), true, 2, new NullProgressMonitor()),
192                         editChange.getPreviewContent(new TextEditBasedChangeGroup[] { group }, group.getRegion(), true, 2, new NullProgressMonitor()),
193                         editChange.getTextType());
194                     return;
195                 } else if (extended.groups != null && extended.groups.length > 0 && extended.range != null) {
196                     TextEditBasedChange editChange= extended.groups[0].getTextEditChange();
197                     TextEditBasedChangeGroup[] groups= extended.groups;
198                     if (extended.groupCategory != null) {
199                         
200                     }
201                     setInput(editChange, editChange.getCurrentContent(extended.range, true, 0, new NullProgressMonitor()),
202                         editChange.getPreviewContent(groups, extended.range, true, 0, new NullProgressMonitor()),
203                         editChange.getTextType());
204                     return;
205                 }
206             } else if (change instanceof TextEditBasedChange) {
207                 TextEditBasedChange editChange= (TextEditBasedChange)change;
208                 setInput(editChange, editChange.getCurrentContent(new NullProgressMonitor()), editChange.getPreviewContent(new NullProgressMonitor()), editChange.getTextType());
209                 return;
210             } else {
211                 fViewer.setInput(null);
212             }
213         } catch (CoreException e) {
214             RefactoringUIPlugin.log(e);
215             fViewer.setInput(null);
216         }
217     }
218
219     public void refresh() {
220         fViewer.getViewer().refresh();
221     }
222     
223     private void setInput(TextEditBasedChange change, String JavaDoc left, String JavaDoc right, String JavaDoc type) {
224         Object JavaDoc element= change.getModifiedElement();
225         IResource resource= null;
226         if (element instanceof IAdaptable) {
227             IAdaptable adaptable= (IAdaptable)element;
228             IWorkbenchAdapter workbenchAdapter= (IWorkbenchAdapter)adaptable.getAdapter(IWorkbenchAdapter.class);
229             if (workbenchAdapter != null) {
230                 fViewer.setLabel(workbenchAdapter.getLabel(element));
231                 fViewer.setImageDescriptor(workbenchAdapter.getImageDescriptor(element));
232             } else {
233                 fViewer.setLabel(null);
234                 fViewer.setImageDescriptor(null);
235             }
236             resource= (IResource)adaptable.getAdapter(IResource.class);
237         } else {
238             fViewer.setLabel(null);
239             fViewer.setImageDescriptor(null);
240         }
241         
242         fViewer.setInput(new DiffNode(
243             new CompareElement(left, type, resource),
244             new CompareElement(right, type, resource)));
245     }
246 }
Popular Tags