001: /*******************************************************************************
002: * Copyright (c) 2000, 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.ui.refactoring.reorg;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.OperationCanceledException;
016:
017: import org.eclipse.core.resources.IResource;
018:
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Shell;
022:
023: import org.eclipse.jface.dialogs.IInputValidator;
024: import org.eclipse.jface.dialogs.InputDialog;
025: import org.eclipse.jface.window.Window;
026: import org.eclipse.jface.wizard.Wizard;
027:
028: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
029:
030: import org.eclipse.jdt.core.ICompilationUnit;
031: import org.eclipse.jdt.core.IJavaElement;
032: import org.eclipse.jdt.core.IPackageFragment;
033: import org.eclipse.jdt.core.IPackageFragmentRoot;
034: import org.eclipse.jdt.core.JavaCore;
035:
036: import org.eclipse.jdt.internal.corext.refactoring.Checks;
037: import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor;
038: import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQueries;
039: import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
040: import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
041: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
042: import org.eclipse.jdt.internal.corext.util.Messages;
043:
044: import org.eclipse.jdt.internal.ui.JavaPlugin;
045: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
046:
047: public class NewNameQueries implements INewNameQueries {
048:
049: private static final String INVALID_NAME_NO_MESSAGE = "";//$NON-NLS-1$
050: private final Wizard fWizard;
051: private final Shell fShell;
052:
053: public NewNameQueries() {
054: fShell = null;
055: fWizard = null;
056: }
057:
058: public NewNameQueries(Wizard wizard) {
059: fWizard = wizard;
060: fShell = null;
061: }
062:
063: public NewNameQueries(Shell shell) {
064: fShell = shell;
065: fWizard = null;
066: }
067:
068: private Shell getShell() {
069: Assert.isTrue(fWizard == null || fShell == null);
070: if (fWizard != null)
071: return fWizard.getContainer().getShell();
072:
073: if (fShell != null)
074: return fShell;
075: return JavaPlugin.getActiveWorkbenchShell();
076: }
077:
078: public INewNameQuery createNewCompilationUnitNameQuery(
079: ICompilationUnit cu, String initialSuggestedName) {
080: String[] keys = { JavaCore.removeJavaLikeExtension(cu
081: .getElementName()) };
082: String message = Messages.format(
083: ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
084: return createStaticQuery(
085: createCompilationUnitNameValidator(cu), message,
086: initialSuggestedName, getShell());
087: }
088:
089: public INewNameQuery createNewResourceNameQuery(IResource res,
090: String initialSuggestedName) {
091: String[] keys = { res.getName() };
092: String message = Messages.format(
093: ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
094: return createStaticQuery(createResourceNameValidator(res),
095: message, initialSuggestedName, getShell());
096: }
097:
098: public INewNameQuery createNewPackageNameQuery(
099: IPackageFragment pack, String initialSuggestedName) {
100: String[] keys = { pack.getElementName() };
101: String message = Messages.format(
102: ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
103: return createStaticQuery(createPackageNameValidator(pack),
104: message, initialSuggestedName, getShell());
105: }
106:
107: public INewNameQuery createNewPackageFragmentRootNameQuery(
108: IPackageFragmentRoot root, String initialSuggestedName) {
109: String[] keys = { root.getElementName() };
110: String message = Messages.format(
111: ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
112: return createStaticQuery(
113: createPackageFragmentRootNameValidator(root), message,
114: initialSuggestedName, getShell());
115: }
116:
117: public INewNameQuery createNullQuery() {
118: return createStaticQuery(null);
119: }
120:
121: public INewNameQuery createStaticQuery(final String newName) {
122: return new INewNameQuery() {
123: public String getNewName() {
124: return newName;
125: }
126: };
127: }
128:
129: private static INewNameQuery createStaticQuery(
130: final IInputValidator validator, final String message,
131: final String initial, final Shell shell) {
132: return new INewNameQuery() {
133: public String getNewName()
134: throws OperationCanceledException {
135: InputDialog dialog = new InputDialog(shell,
136: ReorgMessages.ReorgQueries_nameConflictMessage,
137: message, initial, validator) {
138: /* (non-Javadoc)
139: * @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
140: */
141: protected Control createDialogArea(Composite parent) {
142: Control area = super .createDialogArea(parent);
143: TextFieldNavigationHandler.install(getText());
144: return area;
145: }
146: };
147: if (dialog.open() == Window.CANCEL)
148: throw new OperationCanceledException();
149: return dialog.getValue();
150: }
151: };
152: }
153:
154: private static IInputValidator createResourceNameValidator(
155: final IResource res) {
156: IInputValidator validator = new IInputValidator() {
157: public String isValid(String newText) {
158: if (newText == null
159: || "".equals(newText) || res.getParent() == null) //$NON-NLS-1$
160: return INVALID_NAME_NO_MESSAGE;
161: if (res.getParent().findMember(newText) != null)
162: return ReorgMessages.ReorgQueries_resourceWithThisNameAlreadyExists;
163: if (!res.getParent().getFullPath().isValidSegment(
164: newText))
165: return ReorgMessages.ReorgQueries_invalidNameMessage;
166: IStatus status = res.getParent().getWorkspace()
167: .validateName(newText, res.getType());
168: if (status.getSeverity() == IStatus.ERROR)
169: return status.getMessage();
170:
171: if (res.getName().equalsIgnoreCase(newText))
172: return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
173:
174: return null;
175: }
176: };
177: return validator;
178: }
179:
180: private static IInputValidator createCompilationUnitNameValidator(
181: final ICompilationUnit cu) {
182: IInputValidator validator = new IInputValidator() {
183: public String isValid(String newText) {
184: if (newText == null || "".equals(newText)) //$NON-NLS-1$
185: return INVALID_NAME_NO_MESSAGE;
186: String newCuName = JavaModelUtil.getRenamedCUName(cu,
187: newText);
188: IStatus status = JavaConventionsUtil
189: .validateCompilationUnitName(newCuName, cu);
190: if (status.getSeverity() == IStatus.ERROR)
191: return status.getMessage();
192: RefactoringStatus refStatus;
193: refStatus = Checks.checkCompilationUnitNewName(cu,
194: newText);
195: if (refStatus.hasFatalError())
196: return refStatus
197: .getMessageMatchingSeverity(RefactoringStatus.FATAL);
198:
199: if (cu.getElementName().equalsIgnoreCase(newCuName))
200: return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
201:
202: return null;
203: }
204: };
205: return validator;
206: }
207:
208: private static IInputValidator createPackageFragmentRootNameValidator(
209: final IPackageFragmentRoot root) {
210: return new IInputValidator() {
211: IInputValidator resourceNameValidator = createResourceNameValidator(root
212: .getResource());
213:
214: public String isValid(String newText) {
215: return resourceNameValidator.isValid(newText);
216: }
217: };
218: }
219:
220: private static IInputValidator createPackageNameValidator(
221: final IPackageFragment pack) {
222: IInputValidator validator = new IInputValidator() {
223: public String isValid(String newText) {
224: if (newText == null || "".equals(newText)) //$NON-NLS-1$
225: return INVALID_NAME_NO_MESSAGE;
226: IStatus status = JavaConventionsUtil
227: .validatePackageName(newText, pack);
228: if (status.getSeverity() == IStatus.ERROR)
229: return status.getMessage();
230:
231: IJavaElement parent = pack.getParent();
232: try {
233: if (parent instanceof IPackageFragmentRoot) {
234: if (!RenamePackageProcessor
235: .isPackageNameOkInRoot(newText,
236: (IPackageFragmentRoot) parent))
237: return ReorgMessages.ReorgQueries_packagewithThatNameexistsMassage;
238: }
239: } catch (CoreException e) {
240: return INVALID_NAME_NO_MESSAGE;
241: }
242: if (pack.getElementName().equalsIgnoreCase(newText))
243: return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
244:
245: return null;
246: }
247: };
248: return validator;
249: }
250: }
|