1 11 package org.eclipse.jdt.internal.corext.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 21 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; 22 23 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamingNameSuggestor; 24 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy; 25 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy; 26 import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating; 27 import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating; 28 import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating; 29 import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating; 30 import org.eclipse.jdt.internal.corext.refactoring.tagging.ISimilarDeclarationUpdating; 31 import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating; 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 import org.eclipse.jdt.ui.JavaElementLabels; 35 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 38 43 public final class JDTRefactoringDescriptorComment { 44 45 46 private static final String ELEMENT_DELIMITER= RefactoringCoreMessages.JavaRefactoringDescriptorComment_element_delimiter; 47 48 49 private static final String LINE_DELIMITER= System.getProperty("line.separator", "\n"); 51 60 public static String createCompositeSetting(final String caption, final String [] settings) { 61 Assert.isNotNull(caption); 62 Assert.isNotNull(settings); 63 final StringBuffer buffer= new StringBuffer (128); 64 for (int index= 0; index < settings.length; index++) { 65 if (settings[index] != null && !"".equals(settings[index])) { buffer.append(LINE_DELIMITER); 67 buffer.append(ELEMENT_DELIMITER); 68 buffer.append(settings[index]); 69 } else { 70 buffer.append(LINE_DELIMITER); 71 buffer.append(ELEMENT_DELIMITER); 72 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available); 73 } 74 } 75 if (buffer.length() > 0) 76 buffer.insert(0, caption); 77 return buffer.toString(); 78 } 79 80 81 private final String fHeader; 82 83 84 private final String fProject; 85 86 87 private final List fSettings= new ArrayList (6); 88 89 100 public JDTRefactoringDescriptorComment(final String project, final Object object, final String header) { 101 Assert.isNotNull(object); 102 Assert.isNotNull(header); 103 fProject= project; 104 fHeader= header; 105 initializeInferredSettings(object); 106 } 107 108 116 public void addSetting(final int index, final String setting) { 117 Assert.isTrue(index >= 0); 118 Assert.isNotNull(setting); 119 Assert.isTrue(!"".equals(setting)); fSettings.add(index, setting); 121 } 122 123 129 public void addSetting(final String setting) { 130 if (setting != null && !"".equals(setting)) fSettings.add(setting); 132 } 133 134 139 public String asString() { 140 final StringBuffer buffer= new StringBuffer (256); 141 buffer.append(fHeader); 142 if (fProject != null && !"".equals(fProject)) { buffer.append(LINE_DELIMITER); 144 buffer.append(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptorComment_original_project, fProject)); 145 } 146 for (final Iterator iterator= fSettings.iterator(); iterator.hasNext();) { 147 final String setting= (String ) iterator.next(); 148 buffer.append(LINE_DELIMITER); 149 buffer.append(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_inferred_setting_pattern, setting)); 150 } 151 return buffer.toString(); 152 } 153 154 159 public int getCount() { 160 return fSettings.size(); 161 } 162 163 169 private void initializeInferredSettings(final Object object) { 170 if (object instanceof INameUpdating) { 171 final INameUpdating updating= (INameUpdating) object; 172 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern, JavaElementLabels.getTextLabel(updating.getElements()[0], JavaElementLabels.ALL_FULLY_QUALIFIED))); 173 try { 174 final Object element= updating.getNewElement(); 175 if (element != null) 176 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern, JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_FULLY_QUALIFIED))); 177 else { 178 final String oldLabel= JavaElementLabels.getTextLabel(updating.getElements()[0], JavaElementLabels.ALL_FULLY_QUALIFIED); 179 final String newName= updating.getCurrentElementName(); 180 if (newName.length() < oldLabel.length()) { 181 final String newLabel= oldLabel.substring(0, oldLabel.length() - newName.length()); 182 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern, newLabel + updating.getNewElementName())); 183 } 184 } 185 } catch (CoreException exception) { 186 JavaPlugin.log(exception); 187 } 188 } else if (object instanceof RefactoringProcessor) { 189 final RefactoringProcessor processor= (RefactoringProcessor) object; 190 final Object [] elements= processor.getElements(); 191 if (elements != null) { 192 if (elements.length == 1 && elements[0] != null) 193 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern, JavaElementLabels.getTextLabel(elements[0], JavaElementLabels.ALL_FULLY_QUALIFIED))); 194 else if (elements.length > 1) { 195 final StringBuffer buffer= new StringBuffer (128); 196 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_original_elements); 197 for (int index= 0; index < elements.length; index++) { 198 if (elements[index] != null) { 199 buffer.append(LINE_DELIMITER); 200 buffer.append(ELEMENT_DELIMITER); 201 buffer.append(JavaElementLabels.getTextLabel(elements[index], JavaElementLabels.ALL_FULLY_QUALIFIED)); 202 } else { 203 buffer.append(LINE_DELIMITER); 204 buffer.append(ELEMENT_DELIMITER); 205 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available); 206 } 207 } 208 fSettings.add(buffer.toString()); 209 } 210 } 211 } else if (object instanceof IReorgPolicy) { 212 final IReorgPolicy policy= (IReorgPolicy) object; 213 Object destination= policy.getJavaElementDestination(); 214 if (destination != null) 215 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptorComment_destination_pattern, JavaElementLabels.getTextLabel(destination, JavaElementLabels.ALL_FULLY_QUALIFIED))); 216 else { 217 destination= policy.getResourceDestination(); 218 if (destination != null) 219 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptorComment_destination_pattern, JavaElementLabels.getTextLabel(destination, JavaElementLabels.ALL_FULLY_QUALIFIED))); 220 } 221 final List list= new ArrayList (); 222 list.addAll(Arrays.asList(policy.getJavaElements())); 223 list.addAll(Arrays.asList(policy.getResources())); 224 final Object [] elements= list.toArray(); 225 if (elements != null) { 226 if (elements.length == 1 && elements[0] != null) 227 fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern, JavaElementLabels.getTextLabel(elements[0], JavaElementLabels.ALL_FULLY_QUALIFIED))); 228 else if (elements.length > 1) { 229 final StringBuffer buffer= new StringBuffer (128); 230 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_original_elements); 231 for (int index= 0; index < elements.length; index++) { 232 if (elements[index] != null) { 233 buffer.append(LINE_DELIMITER); 234 buffer.append(ELEMENT_DELIMITER); 235 buffer.append(JavaElementLabels.getTextLabel(elements[index], JavaElementLabels.ALL_FULLY_QUALIFIED)); 236 } else { 237 buffer.append(LINE_DELIMITER); 238 buffer.append(ELEMENT_DELIMITER); 239 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available); 240 } 241 } 242 fSettings.add(buffer.toString()); 243 } 244 } 245 if (object instanceof IMovePolicy) { 246 final IMovePolicy extended= (IMovePolicy) object; 247 if (extended.isTextualMove()) 248 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptorComment_textual_move_only); 249 } 250 } 251 if (object instanceof IReferenceUpdating) { 252 final IReferenceUpdating updating= (IReferenceUpdating) object; 253 if (updating.canEnableUpdateReferences() && updating.getUpdateReferences()) 254 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_update_references); 255 } 256 if (object instanceof ISimilarDeclarationUpdating) { 257 final ISimilarDeclarationUpdating updating= (ISimilarDeclarationUpdating) object; 258 if (updating.canEnableSimilarDeclarationUpdating() && updating.getUpdateSimilarDeclarations()) { 259 final int strategy= updating.getMatchStrategy(); 260 if (strategy == RenamingNameSuggestor.STRATEGY_EXACT) 261 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar); 262 else if (strategy == RenamingNameSuggestor.STRATEGY_EMBEDDED) 263 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_embedded); 264 else if (strategy == RenamingNameSuggestor.STRATEGY_SUFFIX) 265 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_suffix); 266 } 267 } 268 if (object instanceof IQualifiedNameUpdating) { 269 final IQualifiedNameUpdating updating= (IQualifiedNameUpdating) object; 270 if (updating.canEnableQualifiedNameUpdating() && updating.getUpdateQualifiedNames()) { 271 final String patterns= updating.getFilePatterns(); 272 if (patterns != null && !"".equals(patterns)) fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names_pattern, patterns.trim())); 274 else 275 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names); 276 } 277 } 278 if (object instanceof ITextUpdating) { 279 final ITextUpdating updating= (ITextUpdating) object; 280 if (updating.canEnableTextUpdating()) 281 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_textual_occurrences); 282 } 283 if (object instanceof IDelegateUpdating) { 284 final IDelegateUpdating updating= (IDelegateUpdating) object; 285 if (updating.canEnableDelegateUpdating() && updating.getDelegateUpdating()) { 286 if (updating.getDeprecateDelegates()) 287 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original_deprecated); 288 else 289 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original); 290 } 291 } 292 } 293 294 300 public void removeSetting(final int index) { 301 Assert.isTrue(index >= 0); 302 fSettings.remove(index); 303 } 304 } 305 | Popular Tags |