From e6a29021f5cfbb15a0d14250eca84d330a954fd1 Mon Sep 17 00:00:00 2001 From: Mario Kastner Date: Mon, 16 Sep 2024 13:56:45 +0200 Subject: [PATCH 1/3] Move to Package Refactoring Feature The move to package feature builds up on the eclipse ltk move refactoring. The type file will be moved and its package will be named according to the move destination. --- .../plugin.xml | 22 ++++ .../IFordiacModifiablePreviewChange.java | 28 ++++ .../refactoring/UpdateTypeEntryChange.java | 14 +- .../refactoring/move/MoveTypeChange.java | 95 ++++++++++++++ .../move/MoveTypeRefactoringParticipant.java | 122 ++++++++++++++++++ .../move/UpdateTypeEntryFileChange.java | 62 +++++++++ .../ide/typemanagement/wizards/Messages.java | 11 ++ .../wizards/MoveToPackageChangeViewer.java | 109 ++++++++++++++++ .../wizards/messages.properties | 9 ++ 9 files changed, 465 insertions(+), 7 deletions(-) create mode 100644 plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/IFordiacModifiablePreviewChange.java create mode 100644 plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java create mode 100644 plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java create mode 100644 plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/UpdateTypeEntryFileChange.java create mode 100644 plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/MoveToPackageChangeViewer.java diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/plugin.xml b/plugins/org.eclipse.fordiac.ide.typemanagement/plugin.xml index bee26fa929..7d78493c5e 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/plugin.xml +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/plugin.xml @@ -712,6 +712,15 @@ + + + + + + + @@ -768,5 +777,18 @@ + + + + + + + + diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/IFordiacModifiablePreviewChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/IFordiacModifiablePreviewChange.java new file mode 100644 index 0000000000..8c99e06817 --- /dev/null +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/IFordiacModifiablePreviewChange.java @@ -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(); +} diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java index 3cb26a60c2..f4a6860902 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java @@ -37,10 +37,10 @@ public class UpdateTypeEntryChange extends Change { - IFile file; - TypeEntry typeEntry; - String newName; - String oldName; + protected IFile file; + protected TypeEntry typeEntry; + protected String newName; + protected String oldName; public UpdateTypeEntryChange(final IFile file, final TypeEntry typeEntry, final String newName, final String oldName) { @@ -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(() -> { @@ -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$ @@ -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; diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java new file mode 100644 index 0000000000..03bb1172dd --- /dev/null +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * 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 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, + final URI destination) { + 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(element.getTypeEntry().getPackageName(), oldPackageName)) { + status.addFatalError(Messages.MoveTypeToPackage_NameChanged); + } + if (oldPackageName.equals(newPackageName)) { + status.addWarning(Messages.MoveTypeToPackage_PackageNameIsTheSame); + } + final Optional 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; + } + +} diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java new file mode 100644 index 0000000000..bd42048465 --- /dev/null +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * 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.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +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 URI destinationURI; + private IFile file; + + @Override + protected boolean initialize(final Object element) { + if (element instanceof final IFile file) { + this.file = file; + this.type = TypeLibraryManager.INSTANCE.getTypeEntryForFile(file); + this.oldPackageName = type.getPackageName(); + if (getArguments().getDestination() instanceof final IResource folder) { + this.destinationURI = URI.createPlatformResourceURI(folder.getFullPath().toString(), true) + .appendSegment(type.getURI().lastSegment()); + this.newPackageName = PackageNameHelper.getPackageNameFromFile(getFileFromURI(destinationURI)); + 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 errorMessage = IdentifierVerifier.verifyPackageName(newPackageName); + if (errorMessage.isPresent()) { + status.addFatalError(errorMessage.get()); + } + return status; + } + + @Override + public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException { + final CompositeChange parentChange = new CompositeChange(Messages.MoveTypeToPackage); + parentChange.add(new UpdateTypeEntryFileChange(file, type, getFileFromURI(destinationURI))); + parentChange.add(new MoveTypeChange(newPackageName, this.type, getName(), this.type.getURI(), destinationURI)); + 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 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; + } +} diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/UpdateTypeEntryFileChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/UpdateTypeEntryFileChange.java new file mode 100644 index 0000000000..46fca59d4c --- /dev/null +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/UpdateTypeEntryFileChange.java @@ -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; + } +} diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/Messages.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/Messages.java index 8369db1511..036969423a 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/Messages.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/Messages.java @@ -34,6 +34,17 @@ public class Messages extends NLS { public static String RepairBrokenConnectionWizardPage_Name; public static String RepairBrokenConnectionWizardPage_Title; public static String RepairBrokenConnectionWizardPage_Type; + + public static String MoveToPackageChangePreview_EnterPackageName; + public static String MoveTypeToPackage; + public static String MoveTypeToPackage_RenamePackageTo; + public static String MoveTypeToPackage_PackageNameIsTheSame; + public static String MoveTypeToPackage_PackageNameIsEmpty; + public static String MoveTypeToPackage_InvalidDestination; + public static String MoveTypeToPackage_UpdateInstances; + public static String MoveTypeToPackage_NameChanged; + public static String MoveTypeToPackage_UpdateTypeEntryFile; + static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/MoveToPackageChangeViewer.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/MoveToPackageChangeViewer.java new file mode 100644 index 0000000000..f2279c4762 --- /dev/null +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/MoveToPackageChangeViewer.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * 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.wizards; + +import java.util.Optional; +import java.util.function.Supplier; + +import org.eclipse.fordiac.ide.model.IdentifierVerifier; +import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary; +import org.eclipse.fordiac.ide.model.ui.widgets.PackageSelectionProposalProvider; +import org.eclipse.fordiac.ide.typemanagement.refactoring.IFordiacModifiablePreviewChange; +import org.eclipse.jface.fieldassist.ContentProposalAdapter; +import org.eclipse.jface.fieldassist.ControlDecoration; +import org.eclipse.jface.fieldassist.FieldDecorationRegistry; +import org.eclipse.jface.fieldassist.TextContentAdapter; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput; +import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CLabel; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter; + +public class MoveToPackageChangeViewer implements IChangePreviewViewer { + private Composite control; + private IFordiacModifiablePreviewChange change; + private Text nameText; + + @Override + public void createControl(final Composite parent) { + control = new Composite(parent, SWT.FILL); + + GridLayoutFactory.fillDefaults().numColumns(2).margins(new Point(5, 5)).applyTo(control); + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(control); + + final CLabel label = new CLabel(control, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(label); + label.setText(Messages.MoveToPackageChangePreview_EnterPackageName + ":"); //$NON-NLS-1$ + + this.nameText = new Text(control, SWT.BORDER | SWT.FILL); + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(nameText); + nameText.setEnabled(false); + + final ControlDecoration errorDecorator = createErrorDecorator(nameText); + nameText.addModifyListener(e -> { + if (change != null) { + final String text = nameText.getText(); + final Optional errorMessage = IdentifierVerifier.verifyPackageName(text); + if (errorMessage.isPresent()) { + errorDecorator.setDescriptionText(errorMessage.get()); + errorDecorator.show(); + } else { + change.updateChange(nameText.getText()); + errorDecorator.hide(); + } + } + }); + + final Supplier typeLibrary = () -> change.getType().getTypeLibrary(); + final ContentAssistCommandAdapter packageNameProposalAdapter = new ContentAssistCommandAdapter(nameText, + new TextContentAdapter(), new PackageSelectionProposalProvider(typeLibrary), null, null, true); + packageNameProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); + } + + @Override + public Control getControl() { + return control; + } + + @Override + public void setInput(final ChangePreviewViewerInput input) { + if (input.getChange() instanceof final IFordiacModifiablePreviewChange previewChange) { + this.change = previewChange; + if (nameText != null) { + nameText.setText(change.getText()); + if (change.isEditable()) { + nameText.setEnabled(true); + } + } + } + } + + private static ControlDecoration createErrorDecorator(final Control control) { + final ControlDecoration errorDecorator = new ControlDecoration(control, SWT.TOP | SWT.RIGHT); + final Image img = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR) + .getImage(); + errorDecorator.setImage(img); + errorDecorator.hide(); + return errorDecorator; + } + +} diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/messages.properties b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/messages.properties index c461df524a..e970a9501a 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/messages.properties +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/wizards/messages.properties @@ -15,3 +15,12 @@ RepairBrokenConnectionWizardPage_Dots=... RepairBrokenConnectionWizardPage_Name=Name RepairBrokenConnectionWizardPage_Title=Repair broken Connection RepairBrokenConnectionWizardPage_Type=Type +MoveTypeToPackage=Move type to package +MoveTypeToPackage_RenamePackageTo=Rename package to: {0} +MoveTypeToPackage_PackageNameIsTheSame=The selected package name is the same as before +MoveTypeToPackage_PackageNameIsEmpty=Package name is empty +MoveTypeToPackage_NameChanged=The element package name has changed +MoveTypeToPackage_InvalidDestination=Invalid destination +MoveToPackageChangePreview_EnterPackageName=Enter package name +MoveTypeToPackage_UpdateInstances=Update type instances +MoveTypeToPackage_UpdateTypeEntryFile=Update type entry file From 1eccf5d1d82ffdf77144154b00658f3ee0810082 Mon Sep 17 00:00:00 2001 From: Mario Kastner Date: Mon, 21 Oct 2024 14:40:04 +0200 Subject: [PATCH 2/3] execute move type change as prechange --- .../refactoring/UpdateTypeEntryChange.java | 8 ++++---- .../refactoring/move/MoveTypeChange.java | 5 ++--- .../move/MoveTypeRefactoringParticipant.java | 15 ++++++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java index f4a6860902..b82b4c2b47 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/UpdateTypeEntryChange.java @@ -37,10 +37,10 @@ public class UpdateTypeEntryChange extends Change { - protected IFile file; - protected TypeEntry typeEntry; - protected String newName; - protected 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) { diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java index 03bb1172dd..49ff603875 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeChange.java @@ -38,8 +38,7 @@ public class MoveTypeChange extends AbstractCommandChange implem private String oldPackageName; private final TypeEntry type; - protected MoveTypeChange(final String newPackageName, final TypeEntry type, final String name, final URI uri, - final URI destination) { + 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; @@ -54,7 +53,7 @@ public void initializeValidationData(final LibraryElement element, final IProgre public RefactoringStatus isValid(final LibraryElement element, final IProgressMonitor pm) throws CoreException, OperationCanceledException { final RefactoringStatus status = new RefactoringStatus(); - if (!Objects.equals(element.getTypeEntry().getPackageName(), oldPackageName)) { + if (!Objects.equals(PackageNameHelper.getPackageName(element), oldPackageName)) { status.addFatalError(Messages.MoveTypeToPackage_NameChanged); } if (oldPackageName.equals(newPackageName)) { diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java index bd42048465..52da30a6ca 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java @@ -48,7 +48,7 @@ public class MoveTypeRefactoringParticipant extends MoveParticipant { private String oldPackageName; private String newPackageName; private TypeEntry type; - private URI destinationURI; + private IFile destinationFile; private IFile file; @Override @@ -58,9 +58,10 @@ protected boolean initialize(final Object element) { this.type = TypeLibraryManager.INSTANCE.getTypeEntryForFile(file); this.oldPackageName = type.getPackageName(); if (getArguments().getDestination() instanceof final IResource folder) { - this.destinationURI = URI.createPlatformResourceURI(folder.getFullPath().toString(), true) + final URI destinationURI = URI.createPlatformResourceURI(folder.getFullPath().toString(), true) .appendSegment(type.getURI().lastSegment()); - this.newPackageName = PackageNameHelper.getPackageNameFromFile(getFileFromURI(destinationURI)); + this.destinationFile = getFileFromURI(destinationURI); + this.newPackageName = PackageNameHelper.getPackageNameFromFile(destinationFile); return true; } } @@ -89,11 +90,15 @@ public RefactoringStatus checkConditions(final IProgressMonitor pm, final CheckC 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(file, type, getFileFromURI(destinationURI))); - parentChange.add(new MoveTypeChange(newPackageName, this.type, getName(), this.type.getURI(), destinationURI)); + parentChange.add(new UpdateTypeEntryFileChange(file, type, destinationFile)); parentChange.add(getInstanceChanges(type)); return parentChange; } From 14491435788f2640af36fce2ce24c4c88422cd99 Mon Sep 17 00:00:00 2001 From: Mario Kastner Date: Mon, 21 Oct 2024 21:13:45 +0200 Subject: [PATCH 3/3] avoid construction of URI to get file --- .../move/MoveTypeRefactoringParticipant.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java index 52da30a6ca..bb13f597fc 100644 --- a/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java +++ b/plugins/org.eclipse.fordiac.ide.typemanagement/src/org/eclipse/fordiac/ide/typemanagement/refactoring/move/MoveTypeRefactoringParticipant.java @@ -18,10 +18,12 @@ 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; @@ -49,18 +51,16 @@ public class MoveTypeRefactoringParticipant extends MoveParticipant { private String newPackageName; private TypeEntry type; private IFile destinationFile; - private IFile file; + private IFile currentFile; @Override protected boolean initialize(final Object element) { if (element instanceof final IFile file) { - this.file = file; + this.currentFile = file; this.type = TypeLibraryManager.INSTANCE.getTypeEntryForFile(file); this.oldPackageName = type.getPackageName(); - if (getArguments().getDestination() instanceof final IResource folder) { - final URI destinationURI = URI.createPlatformResourceURI(folder.getFullPath().toString(), true) - .appendSegment(type.getURI().lastSegment()); - this.destinationFile = getFileFromURI(destinationURI); + if (getArguments().getDestination() instanceof final IContainer destination) { + this.destinationFile = destination.getFile(IPath.fromOSString(file.getFullPath().lastSegment())); this.newPackageName = PackageNameHelper.getPackageNameFromFile(destinationFile); return true; } @@ -98,7 +98,7 @@ public Change createPreChange(final IProgressMonitor pm) throws CoreException, O @Override public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException { final CompositeChange parentChange = new CompositeChange(Messages.MoveTypeToPackage); - parentChange.add(new UpdateTypeEntryFileChange(file, type, destinationFile)); + parentChange.add(new UpdateTypeEntryFileChange(currentFile, type, destinationFile)); parentChange.add(getInstanceChanges(type)); return parentChange; }