Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifiable package name preview for move type change #655

Draft
wants to merge 3 commits into
base: freeze
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions plugins/org.eclipse.fordiac.ide.typemanagement/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,15 @@
</or>
</enablement>
</changePreviewViewer>
<changePreviewViewer
class="org.eclipse.fordiac.ide.typemanagement.wizards.MoveToPackageChangeViewer"
id="org.eclipse.fordiac.ide.typemanagement.wizards.MoveToPackageChangeViewer1">
<enablement>
<or>
<instanceof value="org.eclipse.fordiac.ide.typemanagement.refactoring.move.MoveTypeChange"/>
</or>
</enablement>
</changePreviewViewer>
</extension>
<extension
point="org.eclipse.ui.menus">
Expand Down Expand Up @@ -768,5 +777,18 @@
</extension>
<extension
point="org.eclipse.ltk.core.refactoring.refactoringContributions">
</extension>
<extension
point="org.eclipse.ltk.core.refactoring.moveParticipants">
<moveParticipant
class="org.eclipse.fordiac.ide.typemanagement.refactoring.move.MoveTypeRefactoringParticipant"
id="org.eclipse.fordiac.ide.typemanagement.moveParticipant1"
name="Move To Package Handler">
<enablement>
<adapt
type="org.eclipse.core.resources.IFile">
</adapt>
</enablement>
</moveParticipant>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2024 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Mario Kastner
* - initial API and implementation and/or initial documentation
*******************************************************************************/

package org.eclipse.fordiac.ide.typemanagement.refactoring;

import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;

public interface IFordiacModifiablePreviewChange {

boolean isEditable();

void updateChange(String inputText);

String getText();

TypeEntry getType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@

public class UpdateTypeEntryChange extends Change {

IFile file;
TypeEntry typeEntry;
String newName;
String oldName;
protected final IFile file;
protected final TypeEntry typeEntry;
protected final String newName;
protected final String oldName;

public UpdateTypeEntryChange(final IFile file, final TypeEntry typeEntry, final String newName,
final String oldName) {
Expand All @@ -67,7 +67,7 @@ public RefactoringStatus isValid(final IProgressMonitor pm) throws CoreException
return status;
}

public static void checkEditor(final RefactoringStatus result, final TypeEntry typeEntry, final String oldName) {
public void checkEditor(final RefactoringStatus result, final TypeEntry typeEntry, final String oldName) {
// depending if the in-place renaming is active we may not be in the display
// thread
Display.getDefault().syncExec(() -> {
Expand All @@ -84,7 +84,8 @@ public static void checkEditor(final RefactoringStatus result, final TypeEntry t
});
}

private static boolean shouldSaveFile(final Shell shell, final String oldName) {
@SuppressWarnings("static-method")
protected boolean shouldSaveFile(final Shell shell, final String oldName) {
final int result = MessageDialog.open(MessageDialog.QUESTION, shell, "Rename of Type with unsaved changes!", //$NON-NLS-1$
MessageFormat.format(
"There are unsaved changes for type \"{0}\". Do you want to save them before renaming?", //$NON-NLS-1$
Expand All @@ -98,7 +99,6 @@ public Change perform(final IProgressMonitor pm) throws CoreException {
final IFile newFile = findNewResource(newName);
if (newFile != null) {
FordiacResourceChangeListener.updateTypeEntryByRename(newFile, typeEntry);

return new UpdateTypeEntryChange(newFile, typeEntry, oldName, newName);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2024 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Mario Kastner
* - initial API and implementation and/or initial documentation
*******************************************************************************/

package org.eclipse.fordiac.ide.typemanagement.refactoring.move;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.fordiac.ide.model.IdentifierVerifier;
import org.eclipse.fordiac.ide.model.commands.change.ChangePackageNameCommand;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.typemanagement.refactoring.AbstractCommandChange;
import org.eclipse.fordiac.ide.typemanagement.refactoring.IFordiacModifiablePreviewChange;
import org.eclipse.fordiac.ide.typemanagement.wizards.Messages;
import org.eclipse.gef.commands.Command;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

public class MoveTypeChange extends AbstractCommandChange<LibraryElement> implements IFordiacModifiablePreviewChange {

private String newPackageName;
private String oldPackageName;
private final TypeEntry type;

protected MoveTypeChange(final String newPackageName, final TypeEntry type, final String name, final URI uri) {
super(name, uri, LibraryElement.class);
this.newPackageName = newPackageName;
this.type = type;
}

@Override
public void initializeValidationData(final LibraryElement element, final IProgressMonitor pm) {
oldPackageName = PackageNameHelper.getPackageName(element);
}

@Override
public RefactoringStatus isValid(final LibraryElement element, final IProgressMonitor pm)
throws CoreException, OperationCanceledException {
final RefactoringStatus status = new RefactoringStatus();
if (!Objects.equals(PackageNameHelper.getPackageName(element), oldPackageName)) {
status.addFatalError(Messages.MoveTypeToPackage_NameChanged);
}
if (oldPackageName.equals(newPackageName)) {
status.addWarning(Messages.MoveTypeToPackage_PackageNameIsTheSame);
}
final Optional<String> errorMessage = IdentifierVerifier.verifyPackageName(newPackageName);
if (errorMessage.isPresent()) {
status.addFatalError(errorMessage.get());
}
return status;
}

@Override
protected Command createCommand(final LibraryElement element) {
return new ChangePackageNameCommand(element, newPackageName);
}

@Override
public void updateChange(final String textInput) {
this.newPackageName = textInput;
}

@Override
public String getText() {
return this.newPackageName;
}

@Override
public TypeEntry getType() {
return this.type;
}

@Override
public boolean isEditable() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*******************************************************************************
* Copyright (c) 2024 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Mario Kastner
* - initial API and implementation and/or initial documentation
*******************************************************************************/

package org.eclipse.fordiac.ide.typemanagement.refactoring.move;

import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.model.IdentifierVerifier;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement;
import org.eclipse.fordiac.ide.model.search.types.BlockTypeInstanceSearch;
import org.eclipse.fordiac.ide.model.search.types.DataTypeInstanceSearch;
import org.eclipse.fordiac.ide.model.typelibrary.DataTypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryManager;
import org.eclipse.fordiac.ide.typemanagement.refactoring.UpdateFBInstanceChange;
import org.eclipse.fordiac.ide.typemanagement.wizards.Messages;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;

public class MoveTypeRefactoringParticipant extends MoveParticipant {

private String oldPackageName;
private String newPackageName;
private TypeEntry type;
private IFile destinationFile;
private IFile currentFile;

@Override
protected boolean initialize(final Object element) {
if (element instanceof final IFile file) {
this.currentFile = file;
this.type = TypeLibraryManager.INSTANCE.getTypeEntryForFile(file);
this.oldPackageName = type.getPackageName();
if (getArguments().getDestination() instanceof final IContainer destination) {
this.destinationFile = destination.getFile(IPath.fromOSString(file.getFullPath().lastSegment()));
this.newPackageName = PackageNameHelper.getPackageNameFromFile(destinationFile);
return true;
}
}
return false;
}

@Override
public String getName() {
return MessageFormat.format(Messages.MoveTypeToPackage_RenamePackageTo, newPackageName);
}

@Override
public RefactoringStatus checkConditions(final IProgressMonitor pm, final CheckConditionsContext context)
throws OperationCanceledException {
final RefactoringStatus status = new RefactoringStatus();
if (oldPackageName.contentEquals(newPackageName)) {
status.addWarning(Messages.MoveTypeToPackage_PackageNameIsTheSame);
}
if (!(getArguments().getDestination() instanceof IResource)) {
status.addError(Messages.MoveTypeToPackage_InvalidDestination);
}
final Optional<String> errorMessage = IdentifierVerifier.verifyPackageName(newPackageName);
if (errorMessage.isPresent()) {
status.addFatalError(errorMessage.get());
}
return status;
}

@Override
public Change createPreChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
return new MoveTypeChange(newPackageName, this.type, getName(), this.type.getURI());
}

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
final CompositeChange parentChange = new CompositeChange(Messages.MoveTypeToPackage);
parentChange.add(new UpdateTypeEntryFileChange(currentFile, type, destinationFile));
parentChange.add(getInstanceChanges(type));
return parentChange;
}

static IFile getFileFromURI(final URI uri) {
if (uri.isPlatformResource()) {
final Path filePath = new Path(uri.toPlatformString(true));
return ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
}
return null;
}

private static CompositeChange getInstanceChanges(final TypeEntry typeEntry) {
final CompositeChange change = new CompositeChange(Messages.MoveTypeToPackage_UpdateInstances);
final List<? extends EObject> result = (typeEntry instanceof final DataTypeEntry dtEntry)
? new DataTypeInstanceSearch(dtEntry).performSearch()
: new BlockTypeInstanceSearch(typeEntry).performSearch();

for (final EObject eObject : result) {
if (eObject instanceof final FBNetworkElement elem) {
change.add(new UpdateFBInstanceChange(elem, typeEntry));
}
}
return change;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2024 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Mario Kastner - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.fordiac.ide.typemanagement.refactoring.move;

import java.text.MessageFormat;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.systemmanagement.changelistener.FordiacResourceChangeListener;
import org.eclipse.fordiac.ide.typemanagement.refactoring.UpdateTypeEntryChange;
import org.eclipse.fordiac.ide.typemanagement.wizards.Messages;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

public class UpdateTypeEntryFileChange extends UpdateTypeEntryChange {

private final IFile destinationFile;

public UpdateTypeEntryFileChange(final IFile currentFile, final TypeEntry typeEntry, final IFile destinationFile) {
super(currentFile, typeEntry, typeEntry.getTypeName(), typeEntry.getTypeName());
this.destinationFile = destinationFile;
}

@Override
public String getName() {
return Messages.MoveTypeToPackage_UpdateTypeEntryFile;
}

@Override
protected boolean shouldSaveFile(final Shell shell, final String oldName) {
final int result = MessageDialog.open(MessageDialog.QUESTION, shell, "Moving of Type with unsaved changes!", //$NON-NLS-1$
MessageFormat.format(
"There are unsaved changes for type \"{0}\". Do you want to save them before moving it?", //$NON-NLS-1$
oldName),
SWT.NONE, "Save", "Cancel"); //$NON-NLS-1$//$NON-NLS-2$
return result == 0;
}

@Override
public Change perform(final IProgressMonitor pm) throws CoreException {
if (destinationFile != null) {
FordiacResourceChangeListener.updateTypeEntry(destinationFile, typeEntry);
// returns undo change
return new UpdateTypeEntryFileChange(destinationFile, typeEntry, file);
}
return null;
}
}
Loading
Loading