KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > JDTRefactoringDescriptorComment


1 /*******************************************************************************
2  * Copyright (c) 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.corext.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
39  * Helper class to generate a refactoring descriptor comment.
40  *
41  * @since 3.2
42  */

43 public final class JDTRefactoringDescriptorComment {
44
45     /** The element delimiter */
46     private static final String JavaDoc ELEMENT_DELIMITER= RefactoringCoreMessages.JavaRefactoringDescriptorComment_element_delimiter;
47
48     /** The line delimiter */
49     private static final String JavaDoc LINE_DELIMITER= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
50

51     /**
52      * Creates a composite setting.
53      *
54      * @param caption
55      * the caption
56      * @param settings
57      * the settings
58      * @return the composite setting
59      */

60     public static String JavaDoc createCompositeSetting(final String JavaDoc caption, final String JavaDoc[] settings) {
61         Assert.isNotNull(caption);
62         Assert.isNotNull(settings);
63         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(128);
64         for (int index= 0; index < settings.length; index++) {
65             if (settings[index] != null && !"".equals(settings[index])) { //$NON-NLS-1$
66
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     /** The header of the comment */
81     private final String JavaDoc fHeader;
82
83     /** The project name, or <code>null</code> */
84     private final String JavaDoc fProject;
85
86     /** The settings list */
87     private final List JavaDoc fSettings= new ArrayList JavaDoc(6);
88
89     /**
90      * Creates a new JDT refactoring descriptor comment.
91      *
92      * @param project
93      * the project name, or <code>null</code>
94      * @param object
95      * the refactoring object to generate a comment for
96      * @param header
97      * the header of the comment (typically the unique description of
98      * the refactoring with fully qualified element names)
99      */

100     public JDTRefactoringDescriptorComment(final String JavaDoc project, final Object JavaDoc object, final String JavaDoc header) {
101         Assert.isNotNull(object);
102         Assert.isNotNull(header);
103         fProject= project;
104         fHeader= header;
105         initializeInferredSettings(object);
106     }
107
108     /**
109      * Adds the specified setting to this comment.
110      *
111      * @param index
112      * the index
113      * @param setting
114      * the setting to add
115      */

116     public void addSetting(final int index, final String JavaDoc setting) {
117         Assert.isTrue(index >= 0);
118         Assert.isNotNull(setting);
119         Assert.isTrue(!"".equals(setting)); //$NON-NLS-1$
120
fSettings.add(index, setting);
121     }
122
123     /**
124      * Adds the specified setting to this comment.
125      *
126      * @param setting
127      * the setting to add, or <code>null</code> for no setting
128      */

129     public void addSetting(final String JavaDoc setting) {
130         if (setting != null && !"".equals(setting)) //$NON-NLS-1$
131
fSettings.add(setting);
132     }
133
134     /**
135      * Returns this comment in a human-readable string representation.
136      *
137      * @return this comment in string representation
138      */

139     public String JavaDoc asString() {
140         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(256);
141         buffer.append(fHeader);
142         if (fProject != null && !"".equals(fProject)) { //$NON-NLS-1$
143
buffer.append(LINE_DELIMITER);
144             buffer.append(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptorComment_original_project, fProject));
145         }
146         for (final Iterator JavaDoc iterator= fSettings.iterator(); iterator.hasNext();) {
147             final String JavaDoc setting= (String JavaDoc) 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     /**
155      * Returns the number of settings.
156      *
157      * @return the number of settings
158      */

159     public int getCount() {
160         return fSettings.size();
161     }
162
163     /**
164      * Initializes the inferred settings.
165      *
166      * @param object
167      * the refactoring object
168      */

169     private void initializeInferredSettings(final Object JavaDoc 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 JavaDoc 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 JavaDoc oldLabel= JavaElementLabels.getTextLabel(updating.getElements()[0], JavaElementLabels.ALL_FULLY_QUALIFIED);
179                     final String JavaDoc newName= updating.getCurrentElementName();
180                     if (newName.length() < oldLabel.length()) {
181                         final String JavaDoc 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 JavaDoc[] 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 JavaDoc buffer= new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc list= new ArrayList JavaDoc();
222             list.addAll(Arrays.asList(policy.getJavaElements()));
223             list.addAll(Arrays.asList(policy.getResources()));
224             final Object JavaDoc[] 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 JavaDoc buffer= new StringBuffer JavaDoc(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 JavaDoc patterns= updating.getFilePatterns();
272                 if (patterns != null && !"".equals(patterns)) //$NON-NLS-1$
273
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     /**
295      * Removes the setting at the specified index.
296      *
297      * @param index
298      * the index
299      */

300     public void removeSetting(final int index) {
301         Assert.isTrue(index >= 0);
302         fSettings.remove(index);
303     }
304 }
305
Popular Tags