]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2009 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; | |
14 | import java.io.InputStream; | |
15 | import java.io.UnsupportedEncodingException; | |
16 | import java.util.ResourceBundle; | |
17 | ||
18 | import org.eclipse.swt.graphics.Image; | |
19 | import org.eclipse.swt.widgets.Shell; | |
20 | ||
21 | import org.eclipse.core.runtime.CoreException; | |
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 fContents; | |
51 | ||
52 | TypedElement(ISourceReference s, String contents) { | |
53 | fSource= s; | |
54 | fContents= contents; | |
55 | } | |
56 | ||
57 | public String getName() { | |
58 | return fJavaElementLabelProvider.getText(fSource); | |
59 | } | |
60 | ||
61 | public String getType() { | |
62 | return "JAVA"; //$NON-NLS-1$ | |
63 | } | |
64 | ||
65 | public Image getImage() { | |
66 | return fJavaElementLabelProvider.getImage(fSource); | |
67 | } | |
68 | ||
69 | public InputStream getContents() throws CoreException { | |
70 | byte[] bytes; | |
71 | try { | |
72 | bytes= fContents.getBytes("UTF-16"); //$NON-NLS-1$ | |
73 | } catch (UnsupportedEncodingException e) { | |
74 | bytes= fContents.getBytes(); | |
75 | } | |
76 | return new ByteArrayInputStream(bytes); | |
77 | } | |
78 | ||
79 | public String getCharset() { | |
80 | return "UTF-16"; //$NON-NLS-1$ | |
81 | } | |
82 | } | |
83 | ||
84 | private static final String 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 bundle= ResourceBundle.getBundle(BUNDLE_NAME); | |
99 | CompareDialog d= new CompareDialog(shell, bundle); | |
100 | ||
101 | ||
102 | String left= null; | |
103 | String 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_POST_QUALIFIED | | |
121 | JavaElementLabelProvider.SHOW_ROOT); | |
122 | ||
123 | if (left == null || right == null) { | |
124 | String errorTitle= JavaCompareUtilities.getString(bundle, "errorTitle"); //$NON-NLS-1$ | |
125 | String errorFormat= JavaCompareUtilities.getString(bundle, "errorFormat"); //$NON-NLS-1$ | |
126 | ||
127 | Object element= null; | |
128 | if (left == null) | |
129 | element= fLeft; | |
130 | else | |
131 | element= fRight; | |
132 | ||
133 | String message= Messages.format(errorFormat, new String[] { fJavaElementLabelProvider.getText(element) } ); | |
134 | ||
135 | MessageDialog.openError(shell, errorTitle, message); | |
136 | return; | |
137 | } | |
138 | ||
139 | d.compare(new DiffNode(new TypedElement(fLeft, left), new TypedElement(fRight, right))); | |
140 | ||
141 | fJavaElementLabelProvider.dispose(); | |
142 | fJavaElementLabelProvider= null; | |
143 | } | |
144 | ||
145 | protected boolean isEnabled(ISelection selection) { | |
146 | if (selection instanceof IStructuredSelection) { | |
147 | Object[] sel= ((IStructuredSelection) selection).toArray(); | |
148 | if (sel.length == 2) { | |
149 | for (int i= 0; i < 2; i++) { | |
150 | Object o= sel[i]; | |
151 | if (!(o instanceof ISourceReference)) | |
152 | return false; | |
153 | } | |
154 | fLeft= (ISourceReference) sel[0]; | |
155 | fRight= (ISourceReference) sel[1]; | |
156 | return true; | |
157 | } | |
158 | } | |
159 | return false; | |
160 | } | |
161 | ||
162 | private String getExtendedSource(ISourceReference ref) throws JavaModelException { | |
163 | ||
164 | // get parent | |
165 | if (ref instanceof IJavaElement) { | |
166 | IJavaElement parent= ((IJavaElement) ref).getParent(); | |
167 | if (parent instanceof ISourceReference) { | |
168 | ISourceReference sr= (ISourceReference) parent; | |
169 | String parentContent= sr.getSource(); | |
170 | if (parentContent != null) { | |
171 | ISourceRange parentRange= sr.getSourceRange(); | |
172 | ISourceRange childRange= ref.getSourceRange(); | |
173 | ||
174 | int start= childRange.getOffset() - parentRange.getOffset(); | |
175 | int end= start + childRange.getLength(); | |
176 | ||
177 | // search backwards for beginning of line | |
178 | while (start > 0) { | |
179 | char c= parentContent.charAt(start-1); | |
180 | if (c == '\n' || c == '\r') | |
181 | break; | |
182 | start--; | |
183 | } | |
184 | ||
185 | return parentContent.substring(start, end); | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | return ref.getSource(); | |
191 | } | |
192 | } |