blob: e297723db5ab5a9917391c38c9acc4f5485860d7 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2013 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
******************************************************************************/
package org.eclipse.scout.sdk.ui.executor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.scout.sdk.Texts;
import org.eclipse.scout.sdk.ui.internal.ScoutSdkUi;
import org.eclipse.scout.sdk.ui.internal.jdt.JdtRenameTransaction;
import org.eclipse.scout.sdk.ui.util.UiUtility;
import org.eclipse.scout.sdk.util.ScoutUtility;
import org.eclipse.scout.sdk.util.type.TypeUtility;
/**
* <h3>{@link TypeRenameExecutor}</h3>
*
* @author Matthias Villiger
* @since 4.1.0 13.10.2014
*/
public class TypeRenameExecutor extends AbstractRenameExecutor {
private IType m_type;
@Override
public boolean canRun(IStructuredSelection selection) {
boolean superRun = super.canRun(selection);
if (!superRun) {
return false;
}
m_type = UiUtility.getTypeFromSelection(selection);
return isEditable(m_type);
}
@Override
protected void fillTransaction(JdtRenameTransaction transaction, String newName) throws CoreException {
transaction.add(m_type, newName);
}
@Override
protected IStatus validate(String newName) {
IStatus inheritedStatus = ScoutUtility.validateJavaName(newName, getReadOnlySuffix());
if (inheritedStatus.matches(IStatus.ERROR)) {
return inheritedStatus;
}
IType declaringType = m_type.getDeclaringType();
if (declaringType != null) {
if (TypeUtility.exists(declaringType.getType(newName))) {
return new Status(IStatus.ERROR, ScoutSdkUi.PLUGIN_ID, Texts.get("Error_nameAlreadyUsed"));
}
}
else {
String packName = m_type.getPackageFragment().getElementName();
if (TypeUtility.existsType(packName + "." + newName)) {
return new Status(IStatus.ERROR, ScoutSdkUi.PLUGIN_ID, Texts.get("Error_nameAlreadyUsed"));
}
}
return inheritedStatus;
}
}