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.reorg;
011:
012: import org.eclipse.core.resources.IResource;
013:
014: import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
015:
016: import org.eclipse.jdt.core.ICompilationUnit;
017: import org.eclipse.jdt.core.IPackageFragment;
018: import org.eclipse.jdt.core.IPackageFragmentRoot;
019:
020: /**
021: * Logged implementation of new name queries.
022: *
023: * @since 3.3
024: */
025: public final class LoggedNewNameQueries implements INewNameQueries {
026:
027: /** Default implementation of a new name query */
028: private final class NewNameQuery implements INewNameQuery {
029:
030: /** The name */
031: private final String fName;
032:
033: /** The object */
034: private final Object fObject;
035:
036: /**
037: * Creates a new new name query.
038: *
039: * @param object
040: * the object
041: * @param name
042: * the initial suggested name
043: */
044: public NewNameQuery(final Object object, String name) {
045: fObject = object;
046: fName = name;
047: }
048:
049: /**
050: * Returns the new name of the compilation unit, without any extension.
051: *
052: * @return the new name, or <code>null</code>
053: */
054: private String getCompilationUnitName() {
055: String name = fLog.getNewName(fObject);
056: if (name != null) {
057: int index = name.lastIndexOf('.');
058: if (index > 0)
059: name = name.substring(0, index);
060: }
061: return name;
062: }
063:
064: /**
065: * {@inheritDoc}
066: */
067: public String getNewName() {
068: String name = null;
069: if (fObject instanceof ICompilationUnit)
070: name = getCompilationUnitName();
071: else
072: name = fLog.getNewName(fObject);
073: if (name == null)
074: name = fName;
075: return fName;
076: }
077: }
078:
079: /** The reorg execution log */
080: private final ReorgExecutionLog fLog;
081:
082: /**
083: * Creates a new logged new name queries.
084: *
085: * @param log
086: * the reorg execution log
087: */
088: public LoggedNewNameQueries(final ReorgExecutionLog log) {
089: fLog = log;
090: }
091:
092: /**
093: * {@inheritDoc}
094: */
095: public INewNameQuery createNewCompilationUnitNameQuery(
096: final ICompilationUnit unit,
097: final String initialSuggestedName) {
098: return new NewNameQuery(unit, initialSuggestedName);
099: }
100:
101: /**
102: * {@inheritDoc}
103: */
104: public INewNameQuery createNewPackageFragmentRootNameQuery(
105: final IPackageFragmentRoot root,
106: final String initialSuggestedName) {
107: return new NewNameQuery(root, initialSuggestedName);
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public INewNameQuery createNewPackageNameQuery(
114: final IPackageFragment fragment,
115: final String initialSuggestedName) {
116: return new NewNameQuery(fragment, initialSuggestedName);
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public INewNameQuery createNewResourceNameQuery(
123: final IResource resource, final String initialSuggestedName) {
124: return new NewNameQuery(resource, initialSuggestedName);
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: public INewNameQuery createNullQuery() {
131: return createStaticQuery(null);
132: }
133:
134: /**
135: * {@inheritDoc}
136: */
137: public INewNameQuery createStaticQuery(final String name) {
138: return new INewNameQuery() {
139:
140: public String getNewName() {
141: return name;
142: }
143: };
144: }
145: }
|