KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
21
22 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamingNameSuggestor;
23 import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating;
24 import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating;
25 import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
26 import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating;
27 import org.eclipse.jdt.internal.corext.refactoring.tagging.ISimilarDeclarationUpdating;
28 import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating;
29 import org.eclipse.jdt.internal.corext.util.Messages;
30
31 import org.eclipse.jdt.ui.JavaElementLabels;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35 /**
36  * Helper class to generate a refactoring descriptor comment.
37  *
38  * @since 3.2
39  */

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

48     /**
49      * Creates a composite setting.
50      *
51      * @param caption
52      * the caption
53      * @param settings
54      * the settings
55      * @return the composite setting
56      */

57     public static String JavaDoc createCompositeSetting(final String JavaDoc caption, final String JavaDoc[] settings) {
58         Assert.isNotNull(caption);
59         Assert.isNotNull(settings);
60         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(128);
61         buffer.append(caption);
62         for (int index= 0; index < settings.length; index++) {
63             if (settings[index] != null && !"".equals(settings[index])) { //$NON-NLS-1$
64
buffer.append(LINE_DELIMITER);
65                 buffer.append(ELEMENT_DELIMITER);
66                 buffer.append(settings[index]);
67             } else {
68                 buffer.append(LINE_DELIMITER);
69                 buffer.append(ELEMENT_DELIMITER);
70                 buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available);
71             }
72         }
73         return buffer.toString();
74     }
75
76     /** The header of the comment */
77     private final String JavaDoc fHeader;
78
79     /** The settings list */
80     private final List JavaDoc fSettings= new ArrayList JavaDoc(6);
81
82     /**
83      * Creates a new java refactoring descriptor comment.
84      *
85      * @param object
86      * the refactoring object to generate a comment for
87      * @param header
88      * the header of the comment (typically the unique description of
89      * the refactoring with fully qualified element names)
90      */

91     public JavaRefactoringDescriptorComment(final Object JavaDoc object, final String JavaDoc header) {
92         Assert.isNotNull(object);
93         Assert.isNotNull(header);
94         fHeader= header;
95         initializeInferredSettings(object);
96     }
97
98     /**
99      * Adds the specified setting to this comment.
100      *
101      * @param index
102      * the index
103      * @param setting
104      * the setting to add
105      */

106     public void addSetting(final int index, final String JavaDoc setting) {
107         Assert.isTrue(index >= 0);
108         Assert.isNotNull(setting);
109         Assert.isTrue(!"".equals(setting)); //$NON-NLS-1$
110
fSettings.add(index, setting);
111     }
112
113     /**
114      * Adds the specified setting to this comment.
115      *
116      * @param setting
117      * the setting to add
118      */

119     public void addSetting(final String JavaDoc setting) {
120         Assert.isNotNull(setting);
121         Assert.isTrue(!"".equals(setting)); //$NON-NLS-1$
122
fSettings.add(setting);
123     }
124
125     /**
126      * Returns this comment in a human-readable string representation.
127      *
128      * @return this comment in string representation
129      */

130     public String JavaDoc asString() {
131         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(256);
132         buffer.append(fHeader);
133         for (final Iterator JavaDoc iterator= fSettings.iterator(); iterator.hasNext();) {
134             final String JavaDoc setting= (String JavaDoc) iterator.next();
135             buffer.append(LINE_DELIMITER);
136             buffer.append(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_inferred_setting_pattern, setting));
137         }
138         return buffer.toString();
139     }
140
141     /**
142      * Returns the number of settings.
143      *
144      * @return the number of settings
145      */

146     public int getCount() {
147         return fSettings.size();
148     }
149
150     /**
151      * Initializes the inferred settings.
152      *
153      * @param object
154      * the refactoring object
155      */

156     private void initializeInferredSettings(final Object JavaDoc object) {
157         if (object instanceof INameUpdating) {
158             final INameUpdating updating= (INameUpdating) object;
159             fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern, JavaElementLabels.getTextLabel(updating.getElements()[0], JavaElementLabels.ALL_FULLY_QUALIFIED)));
160             try {
161                 final Object JavaDoc element= updating.getNewElement();
162                 if (element != null)
163                     fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern, JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_FULLY_QUALIFIED)));
164                 else {
165                     final String JavaDoc oldLabel= JavaElementLabels.getTextLabel(updating.getElements()[0], JavaElementLabels.ALL_FULLY_QUALIFIED);
166                     final String JavaDoc newName= updating.getCurrentElementName();
167                     if (newName.length() < oldLabel.length()) {
168                         final String JavaDoc newLabel= oldLabel.substring(0, oldLabel.length() - newName.length());
169                         fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern, newLabel + updating.getNewElementName()));
170                     }
171                 }
172             } catch (CoreException exception) {
173                 JavaPlugin.log(exception);
174             }
175         } else if (object instanceof RefactoringProcessor) {
176             final RefactoringProcessor processor= (RefactoringProcessor) object;
177             final Object JavaDoc[] elements= processor.getElements();
178             if (elements != null) {
179                 if (elements.length == 1 && elements[0] != null)
180                     fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern, JavaElementLabels.getTextLabel(elements[0], JavaElementLabels.ALL_FULLY_QUALIFIED)));
181                 else if (elements.length > 1) {
182                     final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(128);
183                     buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_original_elements);
184                     for (int index= 0; index < elements.length; index++) {
185                         if (elements[index] != null) {
186                             buffer.append(LINE_DELIMITER);
187                             buffer.append(ELEMENT_DELIMITER);
188                             buffer.append(JavaElementLabels.getTextLabel(elements[index], JavaElementLabels.ALL_FULLY_QUALIFIED));
189                         } else {
190                             buffer.append(LINE_DELIMITER);
191                             buffer.append(ELEMENT_DELIMITER);
192                             buffer.append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available);
193                         }
194                     }
195                     fSettings.add(buffer.toString());
196                 }
197             }
198         }
199         if (object instanceof IReferenceUpdating) {
200             final IReferenceUpdating updating= (IReferenceUpdating) object;
201             if (updating.canEnableUpdateReferences() && updating.getUpdateReferences())
202                 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_update_references);
203         }
204         if (object instanceof ISimilarDeclarationUpdating) {
205             final ISimilarDeclarationUpdating updating= (ISimilarDeclarationUpdating) object;
206             if (updating.canEnableSimilarDeclarationUpdating() && updating.getUpdateSimilarDeclarations()) {
207                 final int strategy= updating.getMatchStrategy();
208                 if (strategy == RenamingNameSuggestor.STRATEGY_EXACT)
209                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar);
210                 else if (strategy == RenamingNameSuggestor.STRATEGY_EMBEDDED)
211                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_embedded);
212                 else if (strategy == RenamingNameSuggestor.STRATEGY_SUFFIX)
213                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_suffix);
214             }
215         }
216         if (object instanceof IQualifiedNameUpdating) {
217             final IQualifiedNameUpdating updating= (IQualifiedNameUpdating) object;
218             if (updating.canEnableQualifiedNameUpdating() && updating.getUpdateQualifiedNames()) {
219                 final String JavaDoc patterns= updating.getFilePatterns();
220                 if (patterns != null && !"".equals(patterns)) //$NON-NLS-1$
221
fSettings.add(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names_pattern, patterns.trim()));
222                 else
223                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names);
224             }
225         }
226         if (object instanceof ITextUpdating) {
227             final ITextUpdating updating= (ITextUpdating) object;
228             if (updating.canEnableTextUpdating())
229                 fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_textual_occurrences);
230         }
231         if (object instanceof IDelegateUpdating) {
232             final IDelegateUpdating updating= (IDelegateUpdating) object;
233             if (updating.canEnableDelegateUpdating() && updating.getDelegateUpdating()) {
234                 if (updating.getDeprecateDelegates())
235                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original_deprecated);
236                 else
237                     fSettings.add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original);
238             }
239         }
240     }
241
242     /**
243      * Removes the setting at the specified index.
244      *
245      * @param index
246      * the index
247      */

248     public void removeSetting(final int index) {
249         Assert.isTrue(index >= 0);
250         fSettings.remove(index);
251     }
252 }
253
Popular Tags