KickJava   Java API By Example, From Geeks To Geeks.

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


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