KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.swt.widgets.*;
21
22 import org.eclipse.jface.action.*;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24
25 import org.eclipse.compare.*;
26 import org.eclipse.compare.structuremergeviewer.StructureDiffViewer;
27 import org.eclipse.compare.structuremergeviewer.IDiffContainer;
28 import org.eclipse.compare.structuremergeviewer.Differencer;
29 import org.eclipse.compare.structuremergeviewer.ICompareInput;
30 import org.eclipse.compare.structuremergeviewer.DiffNode;
31
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaCore;
35
36 class JavaStructureDiffViewer extends StructureDiffViewer {
37     
38     /**
39      * Toggles a boolean property of an <code>CompareConfiguration</code>.
40      */

41     static class ChangePropertyAction extends Action {
42
43         private CompareConfiguration fCompareConfiguration;
44         private String JavaDoc fPropertyKey;
45         private ResourceBundle JavaDoc fBundle;
46         private String JavaDoc fPrefix;
47
48
49         public ChangePropertyAction(ResourceBundle JavaDoc bundle, CompareConfiguration cc, String JavaDoc rkey, String JavaDoc pkey) {
50             fPropertyKey= pkey;
51             fBundle= bundle;
52             fPrefix= rkey;
53             JavaCompareUtilities.initAction(this, fBundle, fPrefix);
54             setCompareConfiguration(cc);
55         }
56
57         public void run() {
58             boolean b= !JavaCompareUtilities.getBoolean(fCompareConfiguration, fPropertyKey, false);
59             setChecked(b);
60             if (fCompareConfiguration != null)
61                 fCompareConfiguration.setProperty(fPropertyKey, new Boolean JavaDoc(b));
62         }
63
64         public void setChecked(boolean state) {
65             super.setChecked(state);
66             JavaCompareUtilities.initToggleAction(this, fBundle, fPrefix, state);
67         }
68         
69         public void setCompareConfiguration(CompareConfiguration cc) {
70             fCompareConfiguration= cc;
71             setChecked(JavaCompareUtilities.getBoolean(fCompareConfiguration, fPropertyKey, false));
72         }
73     }
74
75     private static final String JavaDoc SMART= "SMART"; //$NON-NLS-1$
76

77     private ActionContributionItem fSmartActionItem;
78     private JavaStructureCreator fStructureCreator;
79     private boolean fThreeWay;
80
81     public JavaStructureDiffViewer(Composite parent, CompareConfiguration configuration) {
82         super(parent, configuration);
83         fStructureCreator= new JavaStructureCreator();
84         setStructureCreator(fStructureCreator);
85     }
86     
87     /**
88      * Overridden to find and expand the first class.
89      */

90     protected void initialSelection() {
91         Object JavaDoc firstClass= null;
92         Object JavaDoc o= getRoot();
93         if (o != null) {
94             Object JavaDoc[] children= getSortedChildren(o);
95             if (children != null && children.length > 0) {
96                 for (int i= 0; i < children.length; i++) {
97                     o= children[i];
98                     Object JavaDoc[] sortedChildren= getSortedChildren(o);
99                     if (sortedChildren != null && sortedChildren.length > 0) {
100                         for (int j= 0; j < sortedChildren.length; j++) {
101                             o= sortedChildren[j];
102                             if (o instanceof DiffNode) {
103                                 DiffNode dn= (DiffNode) o;
104                                 ITypedElement e= dn.getId();
105                                 if (e instanceof JavaNode) {
106                                     JavaNode jn= (JavaNode) e;
107                                     int tc= jn.getTypeCode();
108                                     if (tc == JavaNode.CLASS || tc == JavaNode.INTERFACE) {
109                                         firstClass= dn;
110                                     }
111                                 }
112                             }
113                         }
114                     }
115                 }
116             }
117         }
118         if (firstClass != null)
119             expandToLevel(firstClass, 1);
120         else
121             expandToLevel(2);
122     }
123
124     protected void compareInputChanged(ICompareInput input) {
125         
126         fThreeWay= input != null ? input.getAncestor() != null
127                                  : false;
128         setSmartButtonVisible(fThreeWay);
129         
130         if (input != null) {
131             Map JavaDoc compilerOptions= getCompilerOptions(input.getAncestor());
132             if (compilerOptions == null)
133                 compilerOptions= getCompilerOptions(input.getLeft());
134             if (compilerOptions == null)
135                 compilerOptions= getCompilerOptions(input.getRight());
136             if (compilerOptions != null)
137                 fStructureCreator.setDefaultCompilerOptions(compilerOptions);
138         }
139         
140         super.compareInputChanged(input);
141     }
142     
143     private Map JavaDoc getCompilerOptions(ITypedElement input) {
144         if (input instanceof IResourceProvider) {
145             IResource resource= ((IResourceProvider) input).getResource();
146             if (resource != null) {
147                 IJavaElement element= JavaCore.create(resource);
148                 if (element != null) {
149                     IJavaProject javaProject= element.getJavaProject();
150                     if (javaProject != null)
151                         return javaProject.getOptions(true);
152                 }
153             }
154         }
155         return null;
156     }
157     
158     /**
159      * Overriden to create a "smart" button in the viewer's pane control bar.
160      * <p>
161      * Clients can override this method and are free to decide whether they want to call
162      * the inherited method.
163      *
164      * @param toolBarManager the toolbar manager for which to add the buttons
165      */

166     protected void createToolItems(ToolBarManager toolBarManager) {
167         
168         super.createToolItems(toolBarManager);
169         
170         IAction a= new ChangePropertyAction(getBundle(), getCompareConfiguration(), "action.Smart.", SMART); //$NON-NLS-1$
171
fSmartActionItem= new ActionContributionItem(a);
172         fSmartActionItem.setVisible(fThreeWay);
173         toolBarManager.appendToGroup("modes", fSmartActionItem); //$NON-NLS-1$
174
}
175     
176     protected void postDiffHook(Differencer differencer, IDiffContainer root, IProgressMonitor monitor) {
177         if (fStructureCreator.canRewriteTree()) {
178             boolean smart= JavaCompareUtilities.getBoolean(getCompareConfiguration(), SMART, false);
179             if (smart && root != null)
180                 fStructureCreator.rewriteTree(differencer, root);
181         }
182     }
183     
184     /**
185      * Tracks property changes of the configuration object.
186      * Clients may override to track their own property changes.
187      * In this case they must call the inherited method.
188      */

189     protected void propertyChange(PropertyChangeEvent event) {
190         if (event.getProperty().equals(SMART))
191             diff();
192         else
193             super.propertyChange(event);
194     }
195     
196     private void setSmartButtonVisible(boolean visible) {
197         if (fSmartActionItem == null)
198             return;
199         Control c= getControl();
200         if (c == null || c.isDisposed())
201             return;
202             
203         fSmartActionItem.setVisible(visible);
204         ToolBarManager tbm= CompareViewerPane.getToolBarManager(c.getParent());
205         if (tbm != null) {
206             tbm.update(true);
207             ToolBar tb= tbm.getControl();
208             if (!tb.isDisposed())
209                 tb.getParent().layout(true);
210         }
211     }
212 }
213
Popular Tags