KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > compare > BytecodeCompare


1 /*****************************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the BSD License which
4  * accompanies this distribution, and is available at
5  * http://www.opensource.org/licenses/bsd-license.php Contributor: Andrei Loskutov -
6  * initial API and implementation
7  ****************************************************************************************/

8
9 package de.loskutov.bco.compare;
10
11 import org.eclipse.compare.CompareConfiguration;
12 import org.eclipse.compare.CompareEditorInput;
13 import org.eclipse.compare.CompareUI;
14 import org.eclipse.compare.CompareViewerPane;
15 import org.eclipse.compare.CompareViewerSwitchingPane;
16 import org.eclipse.compare.structuremergeviewer.Differencer;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.OperationCanceledException;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.jface.action.ToolBarManager;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.ui.IReusableEditor;
29
30 import de.loskutov.bco.preferences.BCOConstants;
31 import de.loskutov.bco.ui.actions.DefaultToggleAction;
32
33 /**
34  */

35 public class BytecodeCompare extends CompareEditorInput {
36
37     /** Stores reference to the element displayed on the left side of the viewer. */
38     protected TypedElement left;
39     /** Stores reference to the element displayed on the right side of the viewer. */
40     protected TypedElement right;
41     /** Action used in compare view/bytecode view to toggle asmifier mode on/off */
42     protected Action toggleAsmifierModeAction;
43     /** Action used in compare view/bytecode view to hide/show line info. */
44     protected Action hideLineInfoAction;
45     /** Action used in compare view/bytecode view to hide/show local variables. */
46     protected Action hideLocalsAction;
47     protected Action hideStackMapAction;
48     protected Action expandStackMapAction;
49
50     protected IReusableEditor myEditor;
51
52     /**
53      * Constructor for PerforceCompareEditorInput.
54      * @param left element displayed on the left.
55      * @param right element displayed on the right.
56      */

57     public BytecodeCompare(TypedElement left, TypedElement right) {
58         super(new CompareConfiguration());
59         this.left = left;
60         this.right = right;
61
62         toggleAsmifierModeAction = new DefaultToggleAction(BCOConstants.SHOW_ASMIFIER_CODE,
63             new IPropertyChangeListener(){
64                 public void propertyChange(PropertyChangeEvent event) {
65                     if(IAction.CHECKED.equals(event.getProperty())){
66                         boolean asmifier = Boolean.TRUE == event.getNewValue();
67                         toggleMode(BCOConstants.F_SHOW_ASMIFIER_CODE, asmifier, asmifier);
68                     }
69                 }
70             });
71
72         hideLineInfoAction = new DefaultToggleAction(BCOConstants.SHOW_LINE_INFO, new IPropertyChangeListener(){
73                 public void propertyChange(PropertyChangeEvent event) {
74                     if(IAction.CHECKED.equals(event.getProperty())){
75                         toggleMode(
76                             BCOConstants.F_SHOW_LINE_INFO,
77                             Boolean.TRUE == event.getNewValue(),
78                             toggleAsmifierModeAction.isChecked());
79                     }
80                 }
81             });
82
83         hideLocalsAction = new DefaultToggleAction(BCOConstants.SHOW_VARIABLES,
84             new IPropertyChangeListener(){
85                 public void propertyChange(PropertyChangeEvent event) {
86                     if(IAction.CHECKED.equals(event.getProperty())){
87                         toggleMode(
88                             BCOConstants.F_SHOW_VARIABLES,
89                             Boolean.TRUE == event.getNewValue(),
90                             toggleAsmifierModeAction.isChecked());
91                     }
92                 }
93             });
94
95         hideStackMapAction = new DefaultToggleAction(BCOConstants.SHOW_STACKMAP,
96             new IPropertyChangeListener(){
97                 public void propertyChange(PropertyChangeEvent event) {
98                     if(IAction.CHECKED.equals(event.getProperty())){
99                         toggleMode(
100                             BCOConstants.F_SHOW_STACKMAP,
101                             Boolean.TRUE == event.getNewValue(),
102                             toggleAsmifierModeAction.isChecked());
103                     }
104                 }
105             });
106
107         expandStackMapAction = new DefaultToggleAction(BCOConstants.EXPAND_STACKMAP,
108             new IPropertyChangeListener(){
109                 public void propertyChange(PropertyChangeEvent event) {
110                     if(IAction.CHECKED.equals(event.getProperty())){
111                         toggleMode(
112                             BCOConstants.F_EXPAND_STACKMAP,
113                             Boolean.TRUE == event.getNewValue(),
114                             toggleAsmifierModeAction.isChecked());
115                     }
116                 }
117             });
118     }
119
120     /** @see CompareEditorInput#prepareInput(IProgressMonitor) */
121     protected Object JavaDoc prepareInput(IProgressMonitor monitor)
122         throws InterruptedException JavaDoc {
123         if (right == null || left == null) {
124             return null;
125         }
126
127         try {
128             initLabels();
129             Differencer differencer = new Differencer();
130             monitor.beginTask("Bytecode Outline: comparing...", 30); //$NON-NLS-1$
131
IProgressMonitor sub = new SubProgressMonitor(monitor, 10);
132             try {
133                 sub.beginTask("Bytecode Outline: comparing...", 100); //$NON-NLS-1$
134

135                 return differencer.findDifferences(
136                     false, sub, null, null, left, right);
137             } finally {
138                 sub.done();
139             }
140         } catch (OperationCanceledException e) {
141             throw new InterruptedException JavaDoc(e.getMessage());
142         } finally {
143             monitor.done();
144         }
145     }
146
147     /**
148      * Sets up the title and pane labels for the comparison view.
149      */

150     private void initLabels() {
151         CompareConfiguration cc = getCompareConfiguration();
152
153         cc.setLeftLabel(left.getName());
154         cc.setLeftImage(left.getImage());
155
156         cc.setRightLabel(right.getName());
157         cc.setRightImage(right.getImage());
158
159         setTitle("Bytecode compare: " //$NON-NLS-1$
160
+ left.getElementName() + " - " + right.getElementName()); //$NON-NLS-1$
161
}
162
163
164     /**
165      * @see org.eclipse.compare.CompareEditorInput#createContents(org.eclipse.swt.widgets.Composite)
166      */

167     public Control createContents(Composite parent) {
168         Object JavaDoc obj = parent.getData();
169
170         // dirty hook on this place to get reference to editor
171
// CompareEditor extends EditorPart implements IReusableEditor
172
if(obj instanceof IReusableEditor){
173             myEditor = (IReusableEditor)obj;
174         }
175
176         Control control = super.createContents(parent);
177
178         // dirty hook on this place to get reference to CompareViewerPane
179
// from CompareEditorInput: see field
180
// private CompareViewerSwitchingPane fContentInputPane;
181
// see also CompareEditorInput.createContents:
182
// fComposite.setData("Nav", //$NON-NLS-1$
183
Object JavaDoc obj2 = control.getData("Nav"); //$NON-NLS-1$
184
if (obj2 instanceof CompareViewerSwitchingPane[]) {
185             // there are 4 panels, last one is the input pane that we search for
186
CompareViewerSwitchingPane[] panels = (CompareViewerSwitchingPane[])obj2;
187             if(panels.length > 0){
188                 Composite comparePane = panels[panels.length-1];
189                 ToolBarManager toolBarManager2 = CompareViewerPane
190                     .getToolBarManager(comparePane);
191                 boolean separatorExist = false;
192                 if (toolBarManager2.find(hideLineInfoAction.getId()) == null) {
193                     if(!separatorExist) {
194                         separatorExist = true;
195                         toolBarManager2.insert(0, new Separator("bco")); //$NON-NLS-1$
196
}
197                     toolBarManager2.insertBefore("bco", hideLineInfoAction); //$NON-NLS-1$
198
toolBarManager2.update(true);
199                 }
200                 if (toolBarManager2.find(hideLocalsAction.getId()) == null) {
201                     if(!separatorExist) {
202                         separatorExist = true;
203                         toolBarManager2.insert(0, new Separator("bco")); //$NON-NLS-1$
204
}
205                     toolBarManager2.insertBefore("bco", hideLocalsAction); //$NON-NLS-1$
206
toolBarManager2.update(true);
207                 }
208
209                 if (toolBarManager2.find(hideStackMapAction.getId()) == null) {
210                     if(!separatorExist) {
211                         separatorExist = true;
212                         toolBarManager2.insert(0, new Separator("bco")); //$NON-NLS-1$
213
}
214                     toolBarManager2.insertBefore("bco", hideStackMapAction); //$NON-NLS-1$
215
toolBarManager2.update(true);
216                 }
217                 if (toolBarManager2.find(expandStackMapAction.getId()) == null) {
218                     if(!separatorExist) {
219                         separatorExist = true;
220                         toolBarManager2.insert(0, new Separator("bco")); //$NON-NLS-1$
221
}
222                     toolBarManager2.insertBefore("bco", expandStackMapAction); //$NON-NLS-1$
223
toolBarManager2.update(true);
224                 }
225
226                 if (toolBarManager2.find(toggleAsmifierModeAction.getId()) == null) {
227                     if(!separatorExist) {
228                         toolBarManager2.insert(0, new Separator("bco")); //$NON-NLS-1$
229
separatorExist = true;
230                     }
231                     toolBarManager2.insertBefore("bco", toggleAsmifierModeAction); //$NON-NLS-1$
232
toolBarManager2.update(true);
233                 }
234                 try {
235                     toolBarManager2.getControl().getParent().layout(true);
236                     toolBarManager2.getControl().getParent().update();
237                 } catch (NullPointerException JavaDoc e) {
238                     // ignore, i'm just curios why we need this code in 3.2 and expect
239
// some unwanted side effects...
240
}
241             }
242         }
243
244         return control;
245     }
246
247     protected void toggleMode(int mode, boolean value, boolean isASMifierMode) {
248         String JavaDoc contentType = isASMifierMode
249             ? TypedElement.TYPE_ASM_IFIER
250             : TypedElement.TYPE_BYTECODE;
251
252         left.setMode(mode, value);
253         left.setMode(BCOConstants.F_SHOW_ASMIFIER_CODE, isASMifierMode);
254         left.setType(contentType);
255
256         right.setMode(mode, value);
257         right.setMode(BCOConstants.F_SHOW_ASMIFIER_CODE, isASMifierMode);
258         right.setType(contentType);
259         CompareUI.reuseCompareEditor(this, myEditor);
260     }
261 }
Popular Tags