KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaCompareAction


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.jdt.internal.ui.compare;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27
28 import org.eclipse.ui.IActionDelegate;
29
30 import org.eclipse.compare.IEncodedStreamContentAccessor;
31 import org.eclipse.compare.ITypedElement;
32 import org.eclipse.compare.structuremergeviewer.DiffNode;
33
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.ISourceRange;
36 import org.eclipse.jdt.core.ISourceReference;
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.ui.JavaElementLabelProvider;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 public class JavaCompareAction implements IActionDelegate {
46     
47     class TypedElement implements ITypedElement, IEncodedStreamContentAccessor {
48         
49         private ISourceReference fSource;
50         private String JavaDoc fContents;
51         
52         TypedElement(ISourceReference s, String JavaDoc contents) {
53             fSource= s;
54             fContents= contents;
55         }
56         
57         public String JavaDoc getName() {
58             return fJavaElementLabelProvider.getText(fSource);
59         }
60         
61         public String JavaDoc getType() {
62             return "JAVA"; //$NON-NLS-1$
63
}
64         
65         public Image getImage() {
66             return fJavaElementLabelProvider.getImage(fSource);
67         }
68         
69         public InputStream JavaDoc getContents() throws CoreException {
70             byte[] bytes;
71             try {
72                 bytes= fContents.getBytes("UTF-16"); //$NON-NLS-1$
73
} catch (UnsupportedEncodingException JavaDoc e) {
74                 bytes= fContents.getBytes();
75             }
76             return new ByteArrayInputStream JavaDoc(bytes);
77         }
78         
79         public String JavaDoc getCharset() {
80             return "UTF-16"; //$NON-NLS-1$
81
}
82     }
83     
84     private static final String JavaDoc BUNDLE_NAME= "org.eclipse.jdt.internal.ui.compare.CompareAction"; //$NON-NLS-1$
85

86     private ISourceReference fLeft;
87     private ISourceReference fRight;
88     
89     private JavaElementLabelProvider fJavaElementLabelProvider;
90     
91
92     public void selectionChanged(IAction action, ISelection selection) {
93         action.setEnabled(isEnabled(selection));
94     }
95     
96     public void run(IAction action) {
97         Shell shell= JavaPlugin.getActiveWorkbenchShell();
98         ResourceBundle JavaDoc bundle= ResourceBundle.getBundle(BUNDLE_NAME);
99         CompareDialog d= new CompareDialog(shell, bundle);
100                     
101         
102         String JavaDoc left= null;
103         String JavaDoc right= null;
104         
105         try {
106             left= getExtendedSource(fLeft);
107         } catch (JavaModelException ex) {
108             JavaPlugin.log(ex);
109         }
110         
111         try {
112             right= getExtendedSource(fRight);
113         } catch (JavaModelException ex) {
114             JavaPlugin.log(ex);
115         }
116         
117         fJavaElementLabelProvider= new JavaElementLabelProvider(
118                     JavaElementLabelProvider.SHOW_PARAMETERS |
119                     JavaElementLabelProvider.SHOW_OVERLAY_ICONS |
120                     JavaElementLabelProvider.SHOW_ROOT);
121         
122         if (left == null || right == null) {
123             String JavaDoc errorTitle= JavaCompareUtilities.getString(bundle, "errorTitle"); //$NON-NLS-1$
124
String JavaDoc errorFormat= JavaCompareUtilities.getString(bundle, "errorFormat"); //$NON-NLS-1$
125

126             Object JavaDoc element= null;
127             if (left == null)
128                 element= fLeft;
129             else
130                 element= fRight;
131                  
132             String JavaDoc message= Messages.format(errorFormat, new String JavaDoc[] { fJavaElementLabelProvider.getText(element) } );
133             
134             MessageDialog.openError(shell, errorTitle, message);
135             return;
136         }
137         
138         d.compare(new DiffNode(new TypedElement(fLeft, left), new TypedElement(fRight, right)));
139         
140         fJavaElementLabelProvider.dispose();
141         fJavaElementLabelProvider= null;
142     }
143     
144     protected boolean isEnabled(ISelection selection) {
145         if (selection instanceof IStructuredSelection) {
146             Object JavaDoc[] sel= ((IStructuredSelection) selection).toArray();
147             if (sel.length == 2) {
148                 for (int i= 0; i < 2; i++) {
149                     Object JavaDoc o= sel[i];
150                     if (!(o instanceof ISourceReference))
151                         return false;
152                 }
153                 fLeft= (ISourceReference) sel[0];
154                 fRight= (ISourceReference) sel[1];
155                 return true;
156             }
157         }
158         return false;
159     }
160
161     private String JavaDoc getExtendedSource(ISourceReference ref) throws JavaModelException {
162         
163         // get parent
164
if (ref instanceof IJavaElement) {
165             IJavaElement parent= ((IJavaElement) ref).getParent();
166             if (parent instanceof ISourceReference) {
167                 ISourceReference sr= (ISourceReference) parent;
168                 String JavaDoc parentContent= sr.getSource();
169                 if (parentContent != null) {
170                     ISourceRange parentRange= sr.getSourceRange();
171                     ISourceRange childRange= ref.getSourceRange();
172                     
173                     int start= childRange.getOffset() - parentRange.getOffset();
174                     int end= start + childRange.getLength();
175                     
176                     // search backwards for beginning of line
177
while (start > 0) {
178                         char c= parentContent.charAt(start-1);
179                         if (c == '\n' || c == '\r')
180                             break;
181                         start--;
182                     }
183                     
184                     return parentContent.substring(start, end);
185                 }
186             }
187         }
188         
189         return ref.getSource();
190     }
191 }
192
Popular Tags