1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.util.Map ; 14 import java.util.ResourceBundle ; 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 41 static class ChangePropertyAction extends Action { 42 43 private CompareConfiguration fCompareConfiguration; 44 private String fPropertyKey; 45 private ResourceBundle fBundle; 46 private String fPrefix; 47 48 49 public ChangePropertyAction(ResourceBundle bundle, CompareConfiguration cc, String rkey, String 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 (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 SMART= "SMART"; 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 90 protected void initialSelection() { 91 Object firstClass= null; 92 Object o= getRoot(); 93 if (o != null) { 94 Object [] 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 [] 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 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 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 166 protected void createToolItems(ToolBarManager toolBarManager) { 167 168 super.createToolItems(toolBarManager); 169 170 IAction a= new ChangePropertyAction(getBundle(), getCompareConfiguration(), "action.Smart.", SMART); fSmartActionItem= new ActionContributionItem(a); 172 fSmartActionItem.setVisible(fThreeWay); 173 toolBarManager.appendToGroup("modes", fSmartActionItem); } 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 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 |