001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.CoreException;
019:
020: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
021:
022: import org.eclipse.jdt.internal.corext.refactoring.rename.RenamingNameSuggestor;
023: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy;
024: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
025: import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating;
026: import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating;
027: import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
028: import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating;
029: import org.eclipse.jdt.internal.corext.refactoring.tagging.ISimilarDeclarationUpdating;
030: import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating;
031: import org.eclipse.jdt.internal.corext.util.Messages;
032:
033: import org.eclipse.jdt.ui.JavaElementLabels;
034:
035: import org.eclipse.jdt.internal.ui.JavaPlugin;
036:
037: /**
038: * Helper class to generate a refactoring descriptor comment.
039: *
040: * @since 3.2
041: */
042: public final class JDTRefactoringDescriptorComment {
043:
044: /** The element delimiter */
045: private static final String ELEMENT_DELIMITER = RefactoringCoreMessages.JavaRefactoringDescriptorComment_element_delimiter;
046:
047: /** The line delimiter */
048: private static final String LINE_DELIMITER = System.getProperty(
049: "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
050:
051: /**
052: * Creates a composite setting.
053: *
054: * @param caption
055: * the caption
056: * @param settings
057: * the settings
058: * @return the composite setting
059: */
060: public static String createCompositeSetting(final String caption,
061: final String[] settings) {
062: Assert.isNotNull(caption);
063: Assert.isNotNull(settings);
064: final StringBuffer buffer = new StringBuffer(128);
065: for (int index = 0; index < settings.length; index++) {
066: if (settings[index] != null && !"".equals(settings[index])) { //$NON-NLS-1$
067: buffer.append(LINE_DELIMITER);
068: buffer.append(ELEMENT_DELIMITER);
069: buffer.append(settings[index]);
070: } else {
071: buffer.append(LINE_DELIMITER);
072: buffer.append(ELEMENT_DELIMITER);
073: buffer
074: .append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available);
075: }
076: }
077: if (buffer.length() > 0)
078: buffer.insert(0, caption);
079: return buffer.toString();
080: }
081:
082: /** The header of the comment */
083: private final String fHeader;
084:
085: /** The project name, or <code>null</code> */
086: private final String fProject;
087:
088: /** The settings list */
089: private final List fSettings = new ArrayList(6);
090:
091: /**
092: * Creates a new JDT refactoring descriptor comment.
093: *
094: * @param project
095: * the project name, or <code>null</code>
096: * @param object
097: * the refactoring object to generate a comment for
098: * @param header
099: * the header of the comment (typically the unique description of
100: * the refactoring with fully qualified element names)
101: */
102: public JDTRefactoringDescriptorComment(final String project,
103: final Object object, final String header) {
104: Assert.isNotNull(object);
105: Assert.isNotNull(header);
106: fProject = project;
107: fHeader = header;
108: initializeInferredSettings(object);
109: }
110:
111: /**
112: * Adds the specified setting to this comment.
113: *
114: * @param index
115: * the index
116: * @param setting
117: * the setting to add
118: */
119: public void addSetting(final int index, final String setting) {
120: Assert.isTrue(index >= 0);
121: Assert.isNotNull(setting);
122: Assert.isTrue(!"".equals(setting)); //$NON-NLS-1$
123: fSettings.add(index, setting);
124: }
125:
126: /**
127: * Adds the specified setting to this comment.
128: *
129: * @param setting
130: * the setting to add, or <code>null</code> for no setting
131: */
132: public void addSetting(final String setting) {
133: if (setting != null && !"".equals(setting)) //$NON-NLS-1$
134: fSettings.add(setting);
135: }
136:
137: /**
138: * Returns this comment in a human-readable string representation.
139: *
140: * @return this comment in string representation
141: */
142: public String asString() {
143: final StringBuffer buffer = new StringBuffer(256);
144: buffer.append(fHeader);
145: if (fProject != null && !"".equals(fProject)) { //$NON-NLS-1$
146: buffer.append(LINE_DELIMITER);
147: buffer
148: .append(Messages
149: .format(
150: RefactoringCoreMessages.JavaRefactoringDescriptorComment_original_project,
151: fProject));
152: }
153: for (final Iterator iterator = fSettings.iterator(); iterator
154: .hasNext();) {
155: final String setting = (String) iterator.next();
156: buffer.append(LINE_DELIMITER);
157: buffer
158: .append(Messages
159: .format(
160: RefactoringCoreMessages.JavaRefactoringDescriptor_inferred_setting_pattern,
161: setting));
162: }
163: return buffer.toString();
164: }
165:
166: /**
167: * Returns the number of settings.
168: *
169: * @return the number of settings
170: */
171: public int getCount() {
172: return fSettings.size();
173: }
174:
175: /**
176: * Initializes the inferred settings.
177: *
178: * @param object
179: * the refactoring object
180: */
181: private void initializeInferredSettings(final Object object) {
182: if (object instanceof INameUpdating) {
183: final INameUpdating updating = (INameUpdating) object;
184: fSettings
185: .add(Messages
186: .format(
187: RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern,
188: JavaElementLabels
189: .getTextLabel(
190: updating
191: .getElements()[0],
192: JavaElementLabels.ALL_FULLY_QUALIFIED)));
193: try {
194: final Object element = updating.getNewElement();
195: if (element != null)
196: fSettings
197: .add(Messages
198: .format(
199: RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern,
200: JavaElementLabels
201: .getTextLabel(
202: element,
203: JavaElementLabels.ALL_FULLY_QUALIFIED)));
204: else {
205: final String oldLabel = JavaElementLabels
206: .getTextLabel(
207: updating.getElements()[0],
208: JavaElementLabels.ALL_FULLY_QUALIFIED);
209: final String newName = updating
210: .getCurrentElementName();
211: if (newName.length() < oldLabel.length()) {
212: final String newLabel = oldLabel.substring(0,
213: oldLabel.length() - newName.length());
214: fSettings
215: .add(Messages
216: .format(
217: RefactoringCoreMessages.JavaRefactoringDescriptor_renamed_element_pattern,
218: newLabel
219: + updating
220: .getNewElementName()));
221: }
222: }
223: } catch (CoreException exception) {
224: JavaPlugin.log(exception);
225: }
226: } else if (object instanceof RefactoringProcessor) {
227: final RefactoringProcessor processor = (RefactoringProcessor) object;
228: final Object[] elements = processor.getElements();
229: if (elements != null) {
230: if (elements.length == 1 && elements[0] != null)
231: fSettings
232: .add(Messages
233: .format(
234: RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern,
235: JavaElementLabels
236: .getTextLabel(
237: elements[0],
238: JavaElementLabels.ALL_FULLY_QUALIFIED)));
239: else if (elements.length > 1) {
240: final StringBuffer buffer = new StringBuffer(128);
241: buffer
242: .append(RefactoringCoreMessages.JavaRefactoringDescriptor_original_elements);
243: for (int index = 0; index < elements.length; index++) {
244: if (elements[index] != null) {
245: buffer.append(LINE_DELIMITER);
246: buffer.append(ELEMENT_DELIMITER);
247: buffer
248: .append(JavaElementLabels
249: .getTextLabel(
250: elements[index],
251: JavaElementLabels.ALL_FULLY_QUALIFIED));
252: } else {
253: buffer.append(LINE_DELIMITER);
254: buffer.append(ELEMENT_DELIMITER);
255: buffer
256: .append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available);
257: }
258: }
259: fSettings.add(buffer.toString());
260: }
261: }
262: } else if (object instanceof IReorgPolicy) {
263: final IReorgPolicy policy = (IReorgPolicy) object;
264: Object destination = policy.getJavaElementDestination();
265: if (destination != null)
266: fSettings
267: .add(Messages
268: .format(
269: RefactoringCoreMessages.JavaRefactoringDescriptorComment_destination_pattern,
270: JavaElementLabels
271: .getTextLabel(
272: destination,
273: JavaElementLabels.ALL_FULLY_QUALIFIED)));
274: else {
275: destination = policy.getResourceDestination();
276: if (destination != null)
277: fSettings
278: .add(Messages
279: .format(
280: RefactoringCoreMessages.JavaRefactoringDescriptorComment_destination_pattern,
281: JavaElementLabels
282: .getTextLabel(
283: destination,
284: JavaElementLabels.ALL_FULLY_QUALIFIED)));
285: }
286: final List list = new ArrayList();
287: list.addAll(Arrays.asList(policy.getJavaElements()));
288: list.addAll(Arrays.asList(policy.getResources()));
289: final Object[] elements = list.toArray();
290: if (elements != null) {
291: if (elements.length == 1 && elements[0] != null)
292: fSettings
293: .add(Messages
294: .format(
295: RefactoringCoreMessages.JavaRefactoringDescriptor_original_element_pattern,
296: JavaElementLabels
297: .getTextLabel(
298: elements[0],
299: JavaElementLabels.ALL_FULLY_QUALIFIED)));
300: else if (elements.length > 1) {
301: final StringBuffer buffer = new StringBuffer(128);
302: buffer
303: .append(RefactoringCoreMessages.JavaRefactoringDescriptor_original_elements);
304: for (int index = 0; index < elements.length; index++) {
305: if (elements[index] != null) {
306: buffer.append(LINE_DELIMITER);
307: buffer.append(ELEMENT_DELIMITER);
308: buffer
309: .append(JavaElementLabels
310: .getTextLabel(
311: elements[index],
312: JavaElementLabels.ALL_FULLY_QUALIFIED));
313: } else {
314: buffer.append(LINE_DELIMITER);
315: buffer.append(ELEMENT_DELIMITER);
316: buffer
317: .append(RefactoringCoreMessages.JavaRefactoringDescriptor_not_available);
318: }
319: }
320: fSettings.add(buffer.toString());
321: }
322: }
323: if (object instanceof IMovePolicy) {
324: final IMovePolicy extended = (IMovePolicy) object;
325: if (extended.isTextualMove())
326: fSettings
327: .add(RefactoringCoreMessages.JavaRefactoringDescriptorComment_textual_move_only);
328: }
329: }
330: if (object instanceof IReferenceUpdating) {
331: final IReferenceUpdating updating = (IReferenceUpdating) object;
332: if (updating.canEnableUpdateReferences()
333: && updating.getUpdateReferences())
334: fSettings
335: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_update_references);
336: }
337: if (object instanceof ISimilarDeclarationUpdating) {
338: final ISimilarDeclarationUpdating updating = (ISimilarDeclarationUpdating) object;
339: if (updating.canEnableSimilarDeclarationUpdating()
340: && updating.getUpdateSimilarDeclarations()) {
341: final int strategy = updating.getMatchStrategy();
342: if (strategy == RenamingNameSuggestor.STRATEGY_EXACT)
343: fSettings
344: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar);
345: else if (strategy == RenamingNameSuggestor.STRATEGY_EMBEDDED)
346: fSettings
347: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_embedded);
348: else if (strategy == RenamingNameSuggestor.STRATEGY_SUFFIX)
349: fSettings
350: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_rename_similar_suffix);
351: }
352: }
353: if (object instanceof IQualifiedNameUpdating) {
354: final IQualifiedNameUpdating updating = (IQualifiedNameUpdating) object;
355: if (updating.canEnableQualifiedNameUpdating()
356: && updating.getUpdateQualifiedNames()) {
357: final String patterns = updating.getFilePatterns();
358: if (patterns != null && !"".equals(patterns)) //$NON-NLS-1$
359: fSettings
360: .add(Messages
361: .format(
362: RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names_pattern,
363: patterns.trim()));
364: else
365: fSettings
366: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_qualified_names);
367: }
368: }
369: if (object instanceof ITextUpdating) {
370: final ITextUpdating updating = (ITextUpdating) object;
371: if (updating.canEnableTextUpdating())
372: fSettings
373: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_textual_occurrences);
374: }
375: if (object instanceof IDelegateUpdating) {
376: final IDelegateUpdating updating = (IDelegateUpdating) object;
377: if (updating.canEnableDelegateUpdating()
378: && updating.getDelegateUpdating()) {
379: if (updating.getDeprecateDelegates())
380: fSettings
381: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original_deprecated);
382: else
383: fSettings
384: .add(RefactoringCoreMessages.JavaRefactoringDescriptor_keep_original);
385: }
386: }
387: }
388:
389: /**
390: * Removes the setting at the specified index.
391: *
392: * @param index
393: * the index
394: */
395: public void removeSetting(final int index) {
396: Assert.isTrue(index >= 0);
397: fSettings.remove(index);
398: }
399: }
|