From ca496af394cf2e0dba735aedef3c5cc40f8d3139 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Tue, 8 Oct 2024 15:17:10 +0200 Subject: [PATCH 01/11] Task #233 Creating of the project with the Java 17 --- .../META-INF/MANIFEST.MF | 4 +- .../icons/sample.png | Bin 0 -> 332 bytes .../icons/sample@2x.png | Bin 0 -> 526 bytes .../plugin.xml | 13 + .../ui/importWizards/CometDataFetcher.java | 197 ++++++++++++ .../ui/importWizards/CometImportWizard.java | 126 ++++++++ .../importWizards/CometImportWizardPage.java | 281 ++++++++++++++++++ .../ImportFromCometToCTCommand.java | 147 +++++++++ .../cefx/ui/importWizards/ImportHandler.java | 99 ++++++ .../importWizards/SelectedElementsPage.java | 84 ++++++ .../META-INF/MANIFEST.MF | 3 +- 11 files changed, 952 insertions(+), 2 deletions(-) create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/icons/sample.png create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/icons/sample@2x.png create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/SelectedElementsPage.java diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/META-INF/MANIFEST.MF b/de.dlr.sc.virsat.model.extension.cefx.ui/META-INF/MANIFEST.MF index 0ff2fba0..897a6e8b 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/META-INF/MANIFEST.MF +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/META-INF/MANIFEST.MF @@ -28,7 +28,9 @@ Require-Bundle: org.eclipse.ui, de.dlr.sc.virsat.build.ui, de.dlr.sc.virsat.model.calculation.ui, de.dlr.sc.virsat.cef.branding.ui, - de.dlr.sc.virsat.model.extension.cefx + de.dlr.sc.virsat.model.extension.cefx, + de.dlr.sc.virsat.external.lib.comet.sdkj.servicedal;bundle-version="4.17.0", + org.eclipse.ui.ide Bundle-Activator: de.dlr.sc.virsat.model.extension.cefx.ui.Activator Bundle-Vendor: DLR (German Aerospace Center) Automatic-Module-Name: de.dlr.sc.virsat.model.extension.cef.ui diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/icons/sample.png b/de.dlr.sc.virsat.model.extension.cefx.ui/icons/sample.png new file mode 100644 index 0000000000000000000000000000000000000000..02c4b79e10986d56bda97051eb0da2e9119c97f5 GIT binary patch literal 332 zcmV-S0ki&zP)o}PQ;SW4Q?|D@o|!n4bZ+dmjHl(fFnOU>N3Hz|3`e^PLI_Tklq z9dEUPF8GjIQ2+RLZpDrNq~L=3C%1s!j|YcCWZ+aA5&{s6WBjJ7phjchl+7dD zK%??C5t{FV)M8Cck-?KSfC{eT(i|B$Wp$+QOcA_!0hFXcX&$6FDqzZKU<%z0%uwCY e0aKi5l4}7X=XWJ6VmxR70000{ZT)6VX-X|e!skhH1SmmK(Q+8xOk9EsoDx9;%V9VIujLL74J^=XMJoq9 z1|@9VK_4(VhK80y5@pbo6ckI-X8va_ZhIdO!f9Fi&L*dB`%g;@wBtO?KClDIJKpgF zIsXR&gB_67@QMe>IbYQF_DWvOz5n#UMXhhIg6u>s=Yd5ht>Yhtu@=)Y0h1l*%f Qr~m)}07*qoM6N<$f|JJHQ2+n{ literal 0 HcmV?d00001 diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/plugin.xml b/de.dlr.sc.virsat.model.extension.cefx.ui/plugin.xml index 8e8e83ae..3dec6e64 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/plugin.xml +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/plugin.xml @@ -2832,6 +2832,19 @@ + + + + + Import a file from the local file system into the workspace. + + diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java new file mode 100644 index 00000000..f8ca5bef --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import cdp4common.engineeringmodeldata.ElementDefinition; +import cdp4common.engineeringmodeldata.ElementUsage; +import cdp4common.engineeringmodeldata.EngineeringModel; +import cdp4common.engineeringmodeldata.Iteration; +import cdp4common.engineeringmodeldata.Parameter; +import cdp4common.engineeringmodeldata.ParameterSwitchKind; +import cdp4common.engineeringmodeldata.ParameterValueSet; +import cdp4common.sitedirectorydata.DomainOfExpertise; +import cdp4dal.Session; +import cdp4dal.SessionImpl; +import cdp4dal.dal.Credentials; +import cdp4servicesdal.CdpServicesDal; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; + +import java.io.IOException; +import java.net.URI; +import java.security.GeneralSecurityException; +import java.util.Optional; + +/** + * The CometDataFetcher class is responsible for fetching and populating + * data from a COMET system into a SWT Tree widget. + */ +public class CometDataFetcher { + + private final Credentials credentials; + private final Session session; + private final URI uri; + + /** + * Constructs a new CometDataFetcher instance. + * + * @param url The URL of the COMET system. + * @param username The username for authentication. + * @param password The password for authentication. + */ + public CometDataFetcher(String url, String username, String password) { + this.uri = URI.create(url); + CdpServicesDal dal = new CdpServicesDal(); + this.credentials = new Credentials(username, password, this.uri, null); + this.session = new SessionImpl(dal, this.credentials); + } + + /** + * Initiates the process to fetch data and populate the provided Tree. + */ + public void fetchData(Tree tree) { + new Thread(() -> { + try { + openSession(); + Display.getDefault().asyncExec(() -> populateTreeWithIteration(tree)); + } catch (IOException e) { + // Handle I/O exceptions, such as network issues + handleException(e, tree); + } catch (GeneralSecurityException e) { + // Handle security-related exceptions, such as authentication failures + handleException(e, tree); + } catch (RuntimeException e) { + // Handle any other runtime exceptions that might occur + handleException(e, tree); + } + }).start(); + } + + /** + * Opens the session for communication with the COMET system. + */ + public void openSession() throws IOException, GeneralSecurityException { + session.open().join(); + } + + /** + * Populates the provided Tree with data from the selected iteration + * of the engineering model. + * + * @param tree The SWT Tree widget to populate. + */ + private void populateTreeWithIteration(Tree tree) { + var siteDirectory = session.getAssembler().retrieveSiteDirectory(); + var model = siteDirectory.getModel().get(0); + + var iterationIid = model.getIterationSetup().get(0).getIterationIid(); + var domainOfExpertiseIid = model.getActiveDomain().get(0).getIid(); + + EngineeringModel engineeringModel = new EngineeringModel(model.getEngineeringModelIid(), session.getAssembler().getCache(), uri); + Iteration iteration = new Iteration(iterationIid, session.getAssembler().getCache(), uri); + iteration.setContainer(engineeringModel); + + DomainOfExpertise domainOfExpertise = new DomainOfExpertise(domainOfExpertiseIid, session.getAssembler().getCache(), uri); + + session.read(iteration, domainOfExpertise).join(); + + Optional openIteration = session.getOpenIterations().keySet().stream().findFirst(); + + if (openIteration.isPresent()) { + openIteration.get().getElement().stream() + .filter(this::isTopLevelElement) // Filter for the top-level element + .findFirst() + .ifPresent(element -> { + TreeItem topLevelItem = new TreeItem(tree, SWT.NONE); + topLevelItem.setText(element.getName()); + addChildren(topLevelItem, element); + topLevelItem.setExpanded(true); + }); + } else { + showErrorMessage(tree, "No open iteration found."); + } + } + + /** + * Custom method to determine if an ElementDefinition is a top-level element. + * A top-level element is typically not referenced by any other elements. + */ + private boolean isTopLevelElement(ElementDefinition element) { + return element.referencingElementUsages().isEmpty(); + } + + + + + /** + * Recursively adds child elements of a parent ElementDefinition to a TreeItem. + */ + private void addChildren(TreeItem parentItem, ElementDefinition parentElement) { + parentElement.getParameter().stream() + .filter(parameter -> "mass".equalsIgnoreCase(parameter.getParameterType().getName())) + .findFirst() + .ifPresent(parameter -> { + String massValue = fetchParameterValue(parameter); + TreeItem massItem = new TreeItem(parentItem, SWT.NONE); + massItem.setText("Mass: " + massValue + " kg"); + }); + + for (ElementUsage usage : parentElement.getContainedElement()) { + TreeItem usageItem = new TreeItem(parentItem, SWT.NONE); + usageItem.setText(usage.getName()); + addChildren(usageItem, usage.getElementDefinition()); + } + } + + /** + * Fetches the value of the specified Parameter. + * + */ + private String fetchParameterValue(Parameter parameter) { + try { + ParameterValueSet valueSet = parameter.getValueSet().get(0); + + if (valueSet.getValueSwitch() == ParameterSwitchKind.COMPUTED) { + return valueSet.getComputed().get(0); + } else if (valueSet.getValueSwitch() == ParameterSwitchKind.MANUAL) { + return valueSet.getManual().get(0); + } else { + return "No valid mass found"; + } + } catch (Exception e) { + e.printStackTrace(); + return "N/A"; + } + } + + /** + * Displays an error message in a MessageBox. + */ + private void showErrorMessage(Tree tree, String message) { + Display.getDefault().asyncExec(() -> { + MessageBox messageBox = new MessageBox(tree.getShell(), SWT.ICON_ERROR); + messageBox.setMessage(message); + messageBox.open(); + }); + } + + /** + * Handles exceptions by printing the stack trace and displaying an error message. + */ + private void handleException(Exception e, Tree tree) { + e.printStackTrace(); + showErrorMessage(tree, "An error occurred: " + e.getMessage()); + } +} \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java new file mode 100644 index 00000000..bff4789d --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.IImportWizard; +import org.eclipse.ui.IWorkbench; + +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; +import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; +import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; + +import java.util.List; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.edit.command.AddCommand; +import org.eclipse.jface.viewers.IStructuredSelection; + +/** + * This wizard handles the import of data from a Comet server. + * It guides the user through server configuration and initiates the data import process. + */ +public class CometImportWizard extends Wizard implements IImportWizard { + + private CometImportWizardPage mainPage; + private SelectedElementsPage selectedElementsPage; + + public CometImportWizard() { + super(); + setWindowTitle("CometImportWizard"); + setNeedsProgressMonitor(true); + } + + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + mainPage = new CometImportWizardPage("Configure Comet Server"); + selectedElementsPage = new SelectedElementsPage("Selected Elements"); + addPage(mainPage); + addPage(selectedElementsPage); + } + + @Override + public boolean performFinish() { + TreeItem rootItem = selectedElementsPage.getTree().getItem(0); + + if (rootItem != null) { + ConfigurationTree configurationTree = importElements(rootItem); + + // Save the configuration tree + saveConfigurationTree(configurationTree); + } + + return true; + } + + + private ConfigurationTree importElements(TreeItem rootItem) { + ImportHandler importHandler = new ImportHandler(); + return importHandler.importElements(rootItem); + } + + /** + * Saves the imported ConfigurationTree to the editing domain. + */ + + + public void saveConfigurationTree(ConfigurationTree configurationTree) { + if (configurationTree == null) { + throw new IllegalStateException("ConfigurationTree is null. Import failed or was not executed correctly."); + } + + StructuralElementInstance rootInstance = configurationTree.getStructuralElementInstance(); + if (rootInstance == null) { + throw new IllegalStateException("StructuralElementInstance is null in the ConfigurationTree."); + } + + // Ensure that the rootInstance is associated with a resource + if (rootInstance.eResource() == null) { + throw new IllegalStateException("StructuralElementInstance's resource is null."); + } + + // Check if editing domain retrieval is successful + VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(rootInstance); + if (editingDomain == null) { + throw new IllegalStateException("Editing domain is null. Unable to get the editing domain for the StructuralElementInstance."); + } + + try { + Command addCommand = AddCommand.create(editingDomain, rootInstance.eResource(), rootInstance.eResource().getContents(), rootInstance); + editingDomain.getCommandStack().execute(addCommand); + editingDomain.saveAll(); + + ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); + } catch (CoreException e) { + e.printStackTrace(); + } + } + + + @Override + public IWizardPage getNextPage(IWizardPage page) { + if (page == mainPage) { + // Get the selected TreeItems from the mainPage + List checkedItems = mainPage.getCheckedItems(); + + // Pass the selected items to the SelectedElementsPage + selectedElementsPage.setSelectedElements(checkedItems); + return selectedElementsPage; + } + return super.getNextPage(page); + } + +} \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java new file mode 100644 index 00000000..b3c8b1ac --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java @@ -0,0 +1,281 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.ProgressMonitorDialog; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.statushandlers.StatusManager; + +import de.dlr.sc.virsat.build.ui.Activator; + +import java.util.ArrayList; +import java.util.List; + +/** + * The CometImportWizardPage class represents a wizard page used to configure + * and fetch data from a COMET server. It provides a UI for entering server details, + * initiating data fetch, and displaying the fetched data in a tree structure. + */ +public class CometImportWizardPage extends WizardPage { + + private static final int GRID_COLUMNS = 2; + private static final int VERTICALSPAN = 1; + private static final int TREE_HEIGHT_HINT = 400; + private static final int TREE_WIDTH_HINT = 600; + + private Text serverUrlText; + private Text loginText; + private Text passwordText; + private Tree targetTree; + private CometDataFetcher fetcher; + private boolean isDataFetched = false; + + /** + * Constructs a new CometImportWizardPage with the specified name. + * + * @param pageName The name of the wizard page. + */ + protected CometImportWizardPage(String pageName) { + super(pageName); + setTitle("Comet Server Configuration"); + setDescription(""); + } + + /** + * Creates the controls (UI elements) for this wizard page. + */ + @Override + public void createControl(Composite parent) { + Composite container = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(GRID_COLUMNS, false); + container.setLayout(layout); + + // Create red color and bold font + Color red = Display.getCurrent().getSystemColor(SWT.COLOR_RED); + FontData[] fontData = getFont().getFontData(); + for (FontData fd : fontData) { + fd.setStyle(SWT.BOLD); + } + Font boldFont = new Font(Display.getCurrent(), fontData); + + // Create a custom description label with red color and bold font + Label customDescription = new Label(container, SWT.NONE); + customDescription.setText("Enter details and fetch data."); + customDescription.setForeground(red); + customDescription.setFont(boldFont); + + GridData descriptionGridData = new GridData(SWT.FILL, SWT.CENTER, true, false, GRID_COLUMNS, VERTICALSPAN); + customDescription.setLayoutData(descriptionGridData); + + container.addDisposeListener(event -> boldFont.dispose()); + + // Create blue labels for the text fields + Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE); + createLabelAndTextField(container, "Server URL:", SWT.BORDER, blue); + createLabelAndTextField(container, "Login:", SWT.BORDER, blue); + createLabelAndTextField(container, "Password:", SWT.BORDER | SWT.PASSWORD, blue); + + createFetchButton(container); + + targetTree = new Tree(container, SWT.CHECK | SWT.MULTI | SWT.BORDER); + targetTree.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + if (e.detail == SWT.CHECK) { + TreeItem item = (TreeItem) e.item; + checkChildren(item, item.getChecked()); + } + } + }); + GridData treeGridData = new GridData(SWT.FILL, SWT.FILL, true, true, GRID_COLUMNS, VERTICALSPAN); + treeGridData.heightHint = TREE_HEIGHT_HINT; + treeGridData.widthHint = TREE_WIDTH_HINT; + targetTree.setLayoutData(treeGridData); + + setControl(container); + } + + /** + * Creates a "Fetch Data" button within the provided container. The button is aligned to the right side of the container. + */ + private void createFetchButton(Composite container) { + Button fetchButton = new Button(container, SWT.PUSH); + fetchButton.setText("Fetch Data"); + + // Set the button to be aligned to the right side + GridData fetchButtonGridData = new GridData(SWT.RIGHT, SWT.CENTER, false, false); + fetchButtonGridData.horizontalSpan = GRID_COLUMNS; + fetchButton.setLayoutData(fetchButtonGridData); + + fetchButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + handleFetchData(); + } + }); + } + + /** + * Handles the process of fetching data from a server, triggered by the "Fetch Data" button. + * This method validates the input fields, retrieves user input (server URL, username, and password), + * and uses a ProgressMonitorDialog to display the progress of connecting to the COMET server + * and fetching data. It ensures that the data is fetched only once. + */ + private void handleFetchData() { + // Check if data has already been fetched + if (isDataFetched) { + setMessage("Data has already been fetched.", SWT.ICON_INFORMATION); + return; + } + + // Validate the input fields before proceeding + if (!validateInput()) { + return; + } + + // These variables will hold the input values from the UI + final String[] serverUrl = new String[1]; + final String[] username = new String[1]; + final String[] password = new String[1]; + + // Access UI elements from the UI thread + Display.getDefault().syncExec(() -> { + serverUrl[0] = serverUrlText.getText(); + username[0] = loginText.getText(); + password[0] = passwordText.getText(); + }); + + Shell shell = getShell(); + + // Use a ProgressMonitorDialog to show progress to the user + ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(shell); + try { + progressDialog.run(true, true, new IRunnableWithProgress() { + @Override + public void run(IProgressMonitor monitor) { + try { + monitor.beginTask("Connecting to COMET server...", IProgressMonitor.UNKNOWN); + + // Instantiate the CometDataFetcher with the retrieved values + fetcher = new CometDataFetcher(serverUrl[0], username[0], password[0]); + + // Fetch and display data + fetcher.fetchData(targetTree); + + // After successful data fetch, set the flag to true + isDataFetched = true; + + } catch (Exception e) { + Status status = new Status(Status.ERROR, Activator.getPluginId(), "Failed to fetch data from COMET server.", e); + StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW); + } finally { + monitor.done(); + } + } + }); + } catch (Exception e) { + Status status = new Status(Status.ERROR, Activator.getPluginId(), "Failed to run progress dialog.", e); + StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW); + } + } + + /** + * Creates a label and a text field with the specified label text and style. + */ + private void createLabelAndTextField(Composite parent, String labelText, int style, Color labelColor) { + Label label = new Label(parent, SWT.NONE); + label.setText(labelText); + label.setForeground(labelColor); // Set label color to blue + Text text = new Text(parent, style); + text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + if (labelText.equals("Server URL:")) { + serverUrlText = text; + } else if (labelText.equals("Login:")) { + loginText = text; + } else if (labelText.equals("Password:")) { + passwordText = text; + } + } + + /** + * Validates the user input in the text fields. Ensures that all fields are filled. + */ + private boolean validateInput() { + if (serverUrlText.getText().isEmpty() || loginText.getText().isEmpty() || passwordText.getText().isEmpty()) { + setMessage("All fields are required.", SWT.ICON_WARNING); + return false; + } + if (!serverUrlText.getText().startsWith("http://") && !serverUrlText.getText().startsWith("https://")) { + setMessage("Server URL must start with http:// or https://", SWT.ICON_WARNING); + return false; + } + return true; + } + + /** + * Retrieves a list of all TreeItems that are checked in the Tree. + */ + public List getCheckedItems() { + List checkedItems = new ArrayList<>(); + for (TreeItem item : targetTree.getItems()) { + getCheckedItemsRecursively(item, checkedItems); + } + return checkedItems; + } + + /** + * Recursively collects checked TreeItems and adds them to the provided list. + */ + private void getCheckedItemsRecursively(TreeItem item, List checkedItems) { + if (item.getChecked()) { + checkedItems.add(item); + } + for (TreeItem child : item.getItems()) { + getCheckedItemsRecursively(child, checkedItems); + } + } + + /** + * Recursively checks or unchecks all child TreeItems of the specified TreeItem. + */ + private void checkChildren(TreeItem item, boolean checked) { + for (TreeItem child : item.getItems()) { + child.setChecked(checked); + checkChildren(child, checked); + } + } + + /** + * Performs the finish operation when the wizard is completed. + */ + public boolean performFinish() { + return true; + } +} \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java new file mode 100644 index 00000000..ab57a163 --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.transaction.RecordingCommand; +import de.dlr.sc.virsat.model.dvlm.concepts.Concept; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.extension.cefx.model.SystemParameters; +import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; +import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; +import de.dlr.sc.virsat.model.extension.cefx.model.SubSystemMassParameters; +import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; +import org.eclipse.emf.edit.command.AddCommand; +import org.eclipse.swt.widgets.TreeItem; + +import java.util.Collections; +import java.util.List; + +public class ImportFromCometToCTCommand { + + private static final int SYSTEM_MARGIN_DEFAULT = 20; + + private VirSatTransactionalEditingDomain domain; + private StructuralElementInstance parentInstance; + private Concept activeConceptCefx; + private Concept activeConceptPs; + private List selectedItems; + + /** + * Constructor to initialize the ImportFromCometToCTCommand object. + */ + public ImportFromCometToCTCommand(VirSatTransactionalEditingDomain domain, StructuralElementInstance parentInstance, Concept activeConceptCefx, Concept activeConceptPs, List selectedItems) { + if (domain == null || parentInstance == null || activeConceptCefx == null || activeConceptPs == null || selectedItems == null || selectedItems.isEmpty()) { + throw new IllegalArgumentException("Invalid arguments provided to ImportFromCometToCTCommand constructor."); + } + + this.domain = domain; + this.parentInstance = parentInstance; + this.activeConceptCefx = activeConceptCefx; + this.activeConceptPs = activeConceptPs; + this.selectedItems = selectedItems; + } + + /** + * Creates a command that imports the selected elements from the Comet tool into the ConfigurationTree. + */ + public Command createImportCommand() { + if (selectedItems.isEmpty()) { + throw new IllegalStateException("No selected items to import."); + } + + // A compound command to hold all the subcommands + CompoundCommand compoundCommand = new CompoundCommand(); + + try { + // Create a new ConfigurationTree using the root name of the first selected item + String rootName = selectedItems.get(0).getText(); + if (rootName == null || rootName.isEmpty()) { + throw new IllegalStateException("Root name of the selected item is null or empty."); + } + + ConfigurationTree newCT = new ConfigurationTree(activeConceptPs); + newCT.setName(rootName); + + // Create a structural instance for the new ConfigurationTree and set its parent + StructuralElementInstance newCTInstance = newCT.getStructuralElementInstance(); + newCTInstance.setParent(parentInstance); + + // Add the new ConfigurationTree instance to the parent instance + Command addCTCommand = AddCommand.create(domain, parentInstance, null, Collections.singleton(newCTInstance)); + compoundCommand.append(addCTCommand); + + // For each selected item, add it to the ConfigurationTree + for (TreeItem item : selectedItems) { + String itemName = item.getText(); + if (itemName == null || itemName.isEmpty()) { + continue; + } + + // Create a new ElementConfiguration for the selected item + ElementConfiguration config = new ElementConfiguration(activeConceptPs); + config.setName(itemName); + + // Create a structural instance for the configuration and set its parent + StructuralElementInstance configInstance = config.getStructuralElementInstance(); + configInstance.setParent(newCTInstance); + + // Add the configuration instance to the new ConfigurationTree instance + Command addConfigCommand = AddCommand.create(domain, newCTInstance, null, Collections.singleton(configInstance)); + compoundCommand.append(addConfigCommand); + + // Add subsystem-specific parameters using a recording command + compoundCommand.append(new RecordingCommand(domain) { + @Override + protected void doExecute() { + addSubSystemParameters(activeConceptCefx, config); + } + }); + } + + // Add system parameters to the ConfigurationTree + compoundCommand.append(new RecordingCommand(domain) { + @Override + protected void doExecute() { + addSystemParameters(activeConceptCefx, newCT); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Failed to create import command: " + e.getMessage(), e); + } + + // Return the compound command that can be executed + return compoundCommand; + } + + /** + * Adds system parameters to the specified ConfigurationTree. + */ + private void addSystemParameters(Concept concept, ConfigurationTree system) { + SystemParameters systemParameters = new SystemParameters(concept); + systemParameters.setName("SystemParameters"); + systemParameters.setSystemMargin(SYSTEM_MARGIN_DEFAULT); + system.add(systemParameters); + } + + /** + * Adds subsystem-specific mass parameters to the specified ElementConfiguration. + */ + private void addSubSystemParameters(Concept concept, ElementConfiguration subSystem) { + SubSystemMassParameters subSystemMassParameters = new SubSystemMassParameters(concept); + subSystemMassParameters.setName("MassParameters"); + subSystem.add(subSystemMassParameters); + } +} + diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java new file mode 100644 index 00000000..898e40a3 --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; +import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; +import de.dlr.sc.virsat.model.concept.types.structural.IBeanStructuralElementInstance; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElement; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralFactory; +import org.eclipse.swt.widgets.TreeItem; + +public class ImportHandler { + + /** + * Imports the selected tree items and maps them. + */ + public ConfigurationTree importElements(TreeItem rootItem) { + // Create the root element as a ConfigurationTree + ConfigurationTree rootConfigurationTree = new ConfigurationTree(); + StructuralElementInstance rootInstance = createStructuralElementInstance(rootItem.getText()); + + if (rootInstance == null) { + throw new IllegalStateException("Root StructuralElementInstance could not be created."); + } + + rootConfigurationTree.setStructuralElementInstance(rootInstance); + rootConfigurationTree.setName(rootItem.getText()); + + // Recursively add children as ElementConfigurations + addChildren(rootConfigurationTree, rootItem); + + return rootConfigurationTree; + } + + /** + * Recursively adds children as ElementConfiguration instances. + */ + private void addChildren(IBeanStructuralElementInstance parentConfig, TreeItem parentItem) { + for (TreeItem childItem : parentItem.getItems()) { + // Create a new ElementConfiguration for each child + ElementConfiguration elementConfig = new ElementConfiguration(); + StructuralElementInstance childInstance = createStructuralElementInstance(childItem.getText()); + + if (childInstance == null) { + throw new IllegalStateException("Child StructuralElementInstance could not be created."); + } + + elementConfig.setStructuralElementInstance(childInstance); + elementConfig.setName(childItem.getText()); + + // Add the new ElementConfiguration as a child of the parent + parentConfig.add(elementConfig); + + // Recursively process children of this child item + addChildren(elementConfig, childItem); + } + } + + /** + * Helper method to create a StructuralElementInstance using a factory. + */ + private StructuralElementInstance createStructuralElementInstance(String name) { + // Use the StructuralFactory to create an instance + StructuralElementInstance sei = StructuralFactory.eINSTANCE.createStructuralElementInstance(); + + if (sei == null) { + throw new IllegalStateException("Failed to create StructuralElementInstance."); + } + sei.setName(name); + + // Create or find the corresponding StructuralElement to set the type + StructuralElement structuralElement = findOrCreateStructuralElement(name); + if (structuralElement == null) { + throw new IllegalStateException("Failed to find or create a StructuralElement for " + name); + } + + sei.setType(structuralElement); + + return sei; + } + + private StructuralElement findOrCreateStructuralElement(String name) { + // Find an existing StructuralElement by name or create a new one + StructuralElement structuralElement = StructuralFactory.eINSTANCE.createStructuralElement(); + structuralElement.setName(name); + + return structuralElement; + } +} + diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/SelectedElementsPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/SelectedElementsPage.java new file mode 100644 index 00000000..81844bb8 --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/SelectedElementsPage.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; + +import java.util.List; + +public class SelectedElementsPage extends WizardPage { + + private Tree selectedElementsTree; + private List selectedTreeItems; + protected SelectedElementsPage(String pageName) { + super(pageName); + setTitle("Selected Elements"); + setDescription("Review the selected elements and tree structure."); + } + + @Override + public void createControl(Composite parent) { + Composite container = new Composite(parent, SWT.NONE); + container.setLayout(new GridLayout(1, false)); + + Label label = new Label(container, SWT.NONE); + label.setText("Selected Elements:"); + + selectedElementsTree = new Tree(container, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + selectedElementsTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + + setControl(container); + } + + /** + * Populates the Tree with the selected elements. + * This method will be called when the user clicks "Next" from the previous page. + */ + public void setSelectedElements(List selectedItems) { + + // Clear any existing items + selectedElementsTree.removeAll(); + + // Create root TreeItem based on the first selected item + if (!selectedItems.isEmpty()) { + TreeItem root = new TreeItem(selectedElementsTree, SWT.NONE); + root.setText(selectedItems.get(0).getText()); + addChildren(root, selectedItems.get(0)); + root.setExpanded(true); + } + } + + /** + * Recursively adds children of the TreeItems. + */ + private void addChildren(TreeItem parent, TreeItem original) { + for (TreeItem item : original.getItems()) { + TreeItem child = new TreeItem(parent, SWT.NONE); + child.setText(item.getText()); + addChildren(child, item); + } + } + + public Tree getTree() { + return selectedElementsTree; + } + + public List getSelectedItems() { + return this.selectedTreeItems; + } +} diff --git a/de.dlr.sc.virsat.model.extension.cefx/META-INF/MANIFEST.MF b/de.dlr.sc.virsat.model.extension.cefx/META-INF/MANIFEST.MF index bcbc9dde..f7ab074b 100644 --- a/de.dlr.sc.virsat.model.extension.cefx/META-INF/MANIFEST.MF +++ b/de.dlr.sc.virsat.model.extension.cefx/META-INF/MANIFEST.MF @@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.ui, de.dlr.sc.virsat.build, de.dlr.sc.virsat.commons, de.dlr.sc.virsat.model.extension.ps;visibility:=reexport, - de.dlr.sc.virsat.model.calculation + de.dlr.sc.virsat.model.calculation, + de.dlr.sc.virsat.external.lib.comet.sdkj.servicedal;bundle-version="4.17.0" Bundle-RequiredExecutionEnvironment: JavaSE-17 Bundle-ActivationPolicy: lazy Export-Package: de.dlr.sc.virsat.model.extension.cefx, From 5fee878de2324300c578853010db665cc4b6c6c9 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Thu, 10 Oct 2024 16:26:46 +0200 Subject: [PATCH 02/11] Task #233 creating of the import target selection page. --- .../ui/importWizards/CometImportWizard.java | 30 ++++---- .../importWizards/ImportTargetSelection.java | 72 +++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index bff4789d..838fc38f 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -22,6 +22,7 @@ import java.util.List; +import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -37,6 +38,8 @@ public class CometImportWizard extends Wizard implements IImportWizard { private CometImportWizardPage mainPage; private SelectedElementsPage selectedElementsPage; + private ImportTargetSelection targetSelectionPage; + private IContainer model; public CometImportWizard() { super(); @@ -46,9 +49,12 @@ public CometImportWizard() { @Override public void init(IWorkbench workbench, IStructuredSelection selection) { + this.model = ResourcesPlugin.getWorkspace().getRoot(); mainPage = new CometImportWizardPage("Configure Comet Server"); + targetSelectionPage = new ImportTargetSelection(model); selectedElementsPage = new SelectedElementsPage("Selected Elements"); addPage(mainPage); + addPage(targetSelectionPage); addPage(selectedElementsPage); } @@ -88,9 +94,9 @@ public void saveConfigurationTree(ConfigurationTree configurationTree) { } // Ensure that the rootInstance is associated with a resource - if (rootInstance.eResource() == null) { - throw new IllegalStateException("StructuralElementInstance's resource is null."); - } +// if (rootInstance.eResource() == null) { +// throw new IllegalStateException("StructuralElementInstance's resource is null."); +// } // Check if editing domain retrieval is successful VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(rootInstance); @@ -99,7 +105,7 @@ public void saveConfigurationTree(ConfigurationTree configurationTree) { } try { - Command addCommand = AddCommand.create(editingDomain, rootInstance.eResource(), rootInstance.eResource().getContents(), rootInstance); + Command addCommand = AddCommand.create(editingDomain, ,); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); @@ -112,14 +118,14 @@ public void saveConfigurationTree(ConfigurationTree configurationTree) { @Override public IWizardPage getNextPage(IWizardPage page) { - if (page == mainPage) { - // Get the selected TreeItems from the mainPage - List checkedItems = mainPage.getCheckedItems(); - - // Pass the selected items to the SelectedElementsPage - selectedElementsPage.setSelectedElements(checkedItems); - return selectedElementsPage; - } +// if (page == mainPage) { +// // Get the selected TreeItems from the mainPage +// List checkedItems = mainPage.getCheckedItems(); +// +// // Pass the selected items to the SelectedElementsPage +// selectedElementsPage.setSelectedElements(checkedItems); +// return selectedElementsPage; +// } return super.getNextPage(page); } diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java new file mode 100644 index 00000000..b7120e3f --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; + +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; +import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; +import de.dlr.sc.virsat.project.ui.contentProvider.VirSatFilteredWrappedTreeContentProvider; +import de.dlr.sc.virsat.uiengine.ui.wizard.AImportExportPage; + +public class ImportTargetSelection extends AImportExportPage { + + protected ImportTargetSelection(String pageName) { + super(pageName); + + } + protected ImportTargetSelection(IContainer iContainer) { + super("Select import target's element", SWT.OPEN); + setModel(iContainer); + setDescription("Please select a tree to import to and a file to import."); + } + + + @Override + public void createControl(Composite parent) { + super.createControl(parent); + createTreeViewer(); + + Label label = new Label((Composite) getControl(), SWT.NONE); + label.setText("All Geometry files will be imported into the documents folder of the containing model element."); + } + + /** + * Create a tree viewer with filters to show only relevant tree elements for CAD + * import /export + */ + protected void createTreeViewer() { + TreeViewer treeViewer = createTreeUI(); + VirSatFilteredWrappedTreeContentProvider filteredCp = (VirSatFilteredWrappedTreeContentProvider) treeViewer + .getContentProvider(); + filteredCp.addStructuralElementIdFilter(ConfigurationTree.FULL_QUALIFIED_STRUCTURAL_ELEMENT_NAME); + filteredCp.addStructuralElementIdFilter(ElementConfiguration.FULL_QUALIFIED_STRUCTURAL_ELEMENT_NAME); + } + + @Override + public boolean isSelectionValid() { + Object selection = getSelection(); + return selection instanceof StructuralElementInstance; + } + + /** + * Checks if the page has been sufficiently filled with user data + * + * @return true iff the page is complete + */ + public boolean isComplete() { + return isSelectionValid(); + } +} From f5f2c78d3ec5063d3062b2fe037c72e842a5e8e3 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Tue, 29 Oct 2024 13:02:54 +0100 Subject: [PATCH 03/11] Task #233 implement the perform finish method. --- .../ui/importWizards/CometImportWizard.java | 58 +++++++++---------- .../importWizards/ImportTargetSelection.java | 4 +- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 838fc38f..57956fd3 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -15,7 +15,11 @@ import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; +import de.dlr.sc.virsat.model.dvlm.Repository; +import de.dlr.sc.virsat.model.dvlm.concepts.Concept; +import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.extension.cefx.Activator; import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; @@ -37,7 +41,6 @@ public class CometImportWizard extends Wizard implements IImportWizard { private CometImportWizardPage mainPage; - private SelectedElementsPage selectedElementsPage; private ImportTargetSelection targetSelectionPage; private IContainer model; @@ -52,26 +55,31 @@ public void init(IWorkbench workbench, IStructuredSelection selection) { this.model = ResourcesPlugin.getWorkspace().getRoot(); mainPage = new CometImportWizardPage("Configure Comet Server"); targetSelectionPage = new ImportTargetSelection(model); - selectedElementsPage = new SelectedElementsPage("Selected Elements"); + addPage(mainPage); addPage(targetSelectionPage); - addPage(selectedElementsPage); } @Override public boolean performFinish() { - TreeItem rootItem = selectedElementsPage.getTree().getItem(0); + // Retrieve selected source tree items from the first page + List selectedItems = mainPage.getCheckedItems(); - if (rootItem != null) { - ConfigurationTree configurationTree = importElements(rootItem); + // Retrieve the selected target location from the second page + StructuralElementInstance targetInstance = (StructuralElementInstance) targetSelectionPage.getSelection(); - // Save the configuration tree - saveConfigurationTree(configurationTree); + // Perform the actual import based on the selected source and target + for (TreeItem item : selectedItems) { + ConfigurationTree configurationTree = importElements(item); + saveConfigurationTree(configurationTree, targetInstance); } - + return true; } - + + public StructuralElementInstance getSelectedTarget() { + return (StructuralElementInstance) getSelectedTarget(); + } private ConfigurationTree importElements(TreeItem rootItem) { ImportHandler importHandler = new ImportHandler(); @@ -82,8 +90,7 @@ private ConfigurationTree importElements(TreeItem rootItem) { * Saves the imported ConfigurationTree to the editing domain. */ - - public void saveConfigurationTree(ConfigurationTree configurationTree) { + public void saveConfigurationTree(ConfigurationTree configurationTree, StructuralElementInstance targetInstance) { if (configurationTree == null) { throw new IllegalStateException("ConfigurationTree is null. Import failed or was not executed correctly."); } @@ -93,22 +100,18 @@ public void saveConfigurationTree(ConfigurationTree configurationTree) { throw new IllegalStateException("StructuralElementInstance is null in the ConfigurationTree."); } - // Ensure that the rootInstance is associated with a resource -// if (rootInstance.eResource() == null) { -// throw new IllegalStateException("StructuralElementInstance's resource is null."); -// } - - // Check if editing domain retrieval is successful + // Retrieve the editing domain from the registry VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(rootInstance); - if (editingDomain == null) { - throw new IllegalStateException("Editing domain is null. Unable to get the editing domain for the StructuralElementInstance."); - } + Repository currentRepository = editingDomain.getResourceSet().getRepository(); + ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); + System.out.println("Editing domain retrieved successfully: " + editingDomain); + + // Proceed with saving and executing commands try { - Command addCommand = AddCommand.create(editingDomain, ,); + Command addCommand = AddCommand.create(editingDomain, targetInstance, null, rootInstance); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); - ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException e) { e.printStackTrace(); @@ -116,16 +119,9 @@ public void saveConfigurationTree(ConfigurationTree configurationTree) { } + @Override public IWizardPage getNextPage(IWizardPage page) { -// if (page == mainPage) { -// // Get the selected TreeItems from the mainPage -// List checkedItems = mainPage.getCheckedItems(); -// -// // Pass the selected items to the SelectedElementsPage -// selectedElementsPage.setSelectedElements(checkedItems); -// return selectedElementsPage; -// } return super.getNextPage(page); } diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java index b7120e3f..4f491288 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportTargetSelection.java @@ -40,11 +40,11 @@ public void createControl(Composite parent) { createTreeViewer(); Label label = new Label((Composite) getControl(), SWT.NONE); - label.setText("All Geometry files will be imported into the documents folder of the containing model element."); + label.setText("The selected structural element will receive the imported configuration, and all related files will be stored in the appropriate location."); } /** - * Create a tree viewer with filters to show only relevant tree elements for CAD + * Create a tree viewer with filters to show only relevant tree elements * import /export */ protected void createTreeViewer() { From de0d3e34f01130b181d281cc66366c98fa7afe20 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Tue, 29 Oct 2024 14:47:06 +0100 Subject: [PATCH 04/11] Task #233 fix the error of editing domain. --- .../ui/importWizards/CometImportWizard.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 57956fd3..abe5c7dc 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -15,11 +15,8 @@ import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; -import de.dlr.sc.virsat.model.dvlm.Repository; -import de.dlr.sc.virsat.model.dvlm.concepts.Concept; -import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; -import de.dlr.sc.virsat.model.extension.cefx.Activator; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralPackage; import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; @@ -81,9 +78,20 @@ public StructuralElementInstance getSelectedTarget() { return (StructuralElementInstance) getSelectedTarget(); } + /** + * Imports elements from a specified root item in the source tree into a new + * ConfigurationTree structure. This method first checks if the target + * selected on the ImportTargetSelection page is a valid instance of + * StructuralElementInstance. If valid, it initializes a new + * ConfigurationTree using this target element as its root. + */ private ConfigurationTree importElements(TreeItem rootItem) { - ImportHandler importHandler = new ImportHandler(); - return importHandler.importElements(rootItem); + if (targetSelectionPage.getSelection()instanceof StructuralElementInstance) { + + return new ConfigurationTree((StructuralElementInstance) targetSelectionPage.getSelection()); + } + + return null; } /** @@ -102,14 +110,13 @@ public void saveConfigurationTree(ConfigurationTree configurationTree, Structura // Retrieve the editing domain from the registry VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(rootInstance); - Repository currentRepository = editingDomain.getResourceSet().getRepository(); - ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); System.out.println("Editing domain retrieved successfully: " + editingDomain); // Proceed with saving and executing commands + try { - Command addCommand = AddCommand.create(editingDomain, targetInstance, null, rootInstance); + Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, rootInstance); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); From 0e2acea291e07dec093a092b3805e40777aa790c Mon Sep 17 00:00:00 2001 From: ngat_di Date: Fri, 8 Nov 2024 17:44:50 +0100 Subject: [PATCH 05/11] Task #233 update the insert's method. --- .../ui/importWizards/CometImportWizard.java | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index abe5c7dc..40c21b14 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -15,9 +15,13 @@ import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; +import de.dlr.sc.virsat.model.dvlm.Repository; +import de.dlr.sc.virsat.model.dvlm.concepts.Concept; +import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; import de.dlr.sc.virsat.model.dvlm.structural.StructuralPackage; import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; +import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; @@ -57,6 +61,13 @@ public void init(IWorkbench workbench, IStructuredSelection selection) { addPage(targetSelectionPage); } + + public Concept getPsConcept(VirSatTransactionalEditingDomain domain) { + Repository currentRepository = domain.getResourceSet().getRepository(); + ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); + Concept activeConcept = acHelper.getConcept(de.dlr.sc.virsat.model.extension.ps.Activator.getPluginId()); + return activeConcept; + } @Override public boolean performFinish() { // Retrieve selected source tree items from the first page @@ -64,19 +75,26 @@ public boolean performFinish() { // Retrieve the selected target location from the second page StructuralElementInstance targetInstance = (StructuralElementInstance) targetSelectionPage.getSelection(); - + + VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); + + Concept psConcept = getPsConcept(editingDomain); + // Perform the actual import based on the selected source and target - for (TreeItem item : selectedItems) { - ConfigurationTree configurationTree = importElements(item); - saveConfigurationTree(configurationTree, targetInstance); - } + //for (TreeItem item : selectedItems) { + //ConfigurationTree configurationTree = importElements(item); + + ElementConfiguration elementConfigOfElementFromComet = new ElementConfiguration(psConcept); + elementConfigOfElementFromComet.setName(selectedItems.get(0).getText()); + saveDataFromComet(elementConfigOfElementFromComet.getStructuralElementInstance(), targetInstance); + // } return true; } public StructuralElementInstance getSelectedTarget() { return (StructuralElementInstance) getSelectedTarget(); - } + } /** * Imports elements from a specified root item in the source tree into a new @@ -85,38 +103,38 @@ public StructuralElementInstance getSelectedTarget() { * StructuralElementInstance. If valid, it initializes a new * ConfigurationTree using this target element as its root. */ - private ConfigurationTree importElements(TreeItem rootItem) { - if (targetSelectionPage.getSelection()instanceof StructuralElementInstance) { - - return new ConfigurationTree((StructuralElementInstance) targetSelectionPage.getSelection()); - } - - return null; - } +// private ConfigurationTree importElements(TreeItem rootItem) { +// +// if (targetSelectionPage.getSelection()instanceof StructuralElementInstance) { +// +// ConfigurationTree confTreeOfElementFromComet = new ConfigurationTree(); +// confTreeOfElementFromComet.setName(rootItem.getText(0)); +// +// return new ConfigurationTree(confTreeOfElementFromComet.getStructuralElementInstance()); +// } +// +// return null; +// } /** * Saves the imported ConfigurationTree to the editing domain. */ - public void saveConfigurationTree(ConfigurationTree configurationTree, StructuralElementInstance targetInstance) { - if (configurationTree == null) { - throw new IllegalStateException("ConfigurationTree is null. Import failed or was not executed correctly."); - } + public void saveDataFromComet(StructuralElementInstance elementFromComet, StructuralElementInstance targetInstance) { - StructuralElementInstance rootInstance = configurationTree.getStructuralElementInstance(); - if (rootInstance == null) { + if (elementFromComet == null) { throw new IllegalStateException("StructuralElementInstance is null in the ConfigurationTree."); } // Retrieve the editing domain from the registry - VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(rootInstance); + VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); System.out.println("Editing domain retrieved successfully: " + editingDomain); // Proceed with saving and executing commands try { - Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, rootInstance); + Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, elementFromComet); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); From d95f92a6dec0a464001c0d7014b291ff707c5760 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Thu, 21 Nov 2024 19:02:17 +0100 Subject: [PATCH 06/11] Task #234 implementation of the comet data container. --- .../ui/importWizards/CometImportWizard.java | 133 +++++++++--------- .../importWizards/CometImportWizardPage.java | 50 +++++-- .../cefx/ui/importWizards/TreeNode.java | 35 +++++ 3 files changed, 147 insertions(+), 71 deletions(-) create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 40c21b14..0ade9f38 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -11,7 +11,6 @@ import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.Wizard; -import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; @@ -20,7 +19,6 @@ import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; import de.dlr.sc.virsat.model.dvlm.structural.StructuralPackage; -import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; @@ -40,20 +38,20 @@ * It guides the user through server configuration and initiates the data import process. */ public class CometImportWizard extends Wizard implements IImportWizard { - - private CometImportWizardPage mainPage; + + private CometImportWizardPage mainPage; private ImportTargetSelection targetSelectionPage; private IContainer model; public CometImportWizard() { super(); - setWindowTitle("CometImportWizard"); + setWindowTitle("Comet Import Wizard"); setNeedsProgressMonitor(true); } @Override public void init(IWorkbench workbench, IStructuredSelection selection) { - this.model = ResourcesPlugin.getWorkspace().getRoot(); + this.model = ResourcesPlugin.getWorkspace().getRoot(); mainPage = new CometImportWizardPage("Configure Comet Server"); targetSelectionPage = new ImportTargetSelection(model); @@ -61,80 +59,92 @@ public void init(IWorkbench workbench, IStructuredSelection selection) { addPage(targetSelectionPage); } - - public Concept getPsConcept(VirSatTransactionalEditingDomain domain) { - Repository currentRepository = domain.getResourceSet().getRepository(); - ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); - Concept activeConcept = acHelper.getConcept(de.dlr.sc.virsat.model.extension.ps.Activator.getPluginId()); - return activeConcept; - } + public Concept getPsConcept(VirSatTransactionalEditingDomain domain) { + Repository currentRepository = domain.getResourceSet().getRepository(); + ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); + return acHelper.getConcept(de.dlr.sc.virsat.model.extension.ps.Activator.getPluginId()); + } + @Override public boolean performFinish() { // Retrieve selected source tree items from the first page - List selectedItems = mainPage.getCheckedItems(); - + List selectedItems = mainPage.getCheckedTreeNodes(); + if (selectedItems == null || selectedItems.isEmpty()) { + System.out.println("No items selected. Aborting operation."); + return false; + } + // Retrieve the selected target location from the second page StructuralElementInstance targetInstance = (StructuralElementInstance) targetSelectionPage.getSelection(); - VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); - - Concept psConcept = getPsConcept(editingDomain); + if (targetInstance == null) { + System.out.println("No target instance selected. Aborting operation."); + return false; + } - // Perform the actual import based on the selected source and target - //for (TreeItem item : selectedItems) { - //ConfigurationTree configurationTree = importElements(item); - - ElementConfiguration elementConfigOfElementFromComet = new ElementConfiguration(psConcept); - elementConfigOfElementFromComet.setName(selectedItems.get(0).getText()); - saveDataFromComet(elementConfigOfElementFromComet.getStructuralElementInstance(), targetInstance); - // } + VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); + + // Process each selected item + try { + for (TreeNode item : selectedItems) { + createElementConfigurationHierarchy(item, targetInstance, editingDomain); + } + } catch (Exception e) { + System.err.println("Error occurred while creating configuration hierarchy: " + e.getMessage()); + e.printStackTrace(); + return false; + } + System.out.println("All items processed successfully."); return true; } - - public StructuralElementInstance getSelectedTarget() { - return (StructuralElementInstance) getSelectedTarget(); - } /** - * Imports elements from a specified root item in the source tree into a new - * ConfigurationTree structure. This method first checks if the target - * selected on the ImportTargetSelection page is a valid instance of - * StructuralElementInstance. If valid, it initializes a new - * ConfigurationTree using this target element as its root. - */ -// private ConfigurationTree importElements(TreeItem rootItem) { -// -// if (targetSelectionPage.getSelection()instanceof StructuralElementInstance) { -// -// ConfigurationTree confTreeOfElementFromComet = new ConfigurationTree(); -// confTreeOfElementFromComet.setName(rootItem.getText(0)); -// -// return new ConfigurationTree(confTreeOfElementFromComet.getStructuralElementInstance()); -// } -// -// return null; -// } + * Creates a hierarchy of ElementConfiguration instances corresponding to the structure of the provided TreeItem. + */ + + private void createElementConfigurationHierarchy(TreeNode item, StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain) { + if (item == null) { + System.out.println("TreeNode is null. Skipping..."); + return; + } + System.out.println("Processing TreeNode: " + item.getName()); + + // Create an ElementConfiguration for the current tree item + ElementConfiguration elementConfig = new ElementConfiguration(getPsConcept(editingDomain)); + elementConfig.setName(item.getName()); + System.out.println("Created ElementConfiguration: " + elementConfig.getName()); + + // Save this ElementConfiguratiookn under the parentInstance + try { + saveDataFromComet(elementConfig.getStructuralElementInstance(), parentInstance, editingDomain); + System.out.println("Saved ElementConfiguration: " + elementConfig.getName() + " under parent."); + } catch (Exception e) { + System.err.println("Failed to save ElementConfiguration: " + elementConfig.getName()); + e.printStackTrace(); + return; + } + + // Recursively process child nodes to maintain the nested structure + for (TreeNode childItem : item.getChildren()) { + createElementConfigurationHierarchy(childItem, elementConfig.getStructuralElementInstance(), editingDomain); + + } + } /** - * Saves the imported ConfigurationTree to the editing domain. + * Saves the data represented by a StructuralElementInstance to a target parent instance. + * This method creates and executes an AddCommand to attach the provided + * element as a child to the specified target instance. */ - - public void saveDataFromComet(StructuralElementInstance elementFromComet, StructuralElementInstance targetInstance) { - + private void saveDataFromComet(StructuralElementInstance elementFromComet, StructuralElementInstance targetInstance, VirSatTransactionalEditingDomain editingDomain) { if (elementFromComet == null) { - throw new IllegalStateException("StructuralElementInstance is null in the ConfigurationTree."); + throw new IllegalStateException("StructuralElementInstance is null in the Configuration Tree."); } - // Retrieve the editing domain from the registry - VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); - - System.out.println("Editing domain retrieved successfully: " + editingDomain); - - // Proceed with saving and executing commands - try { Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, elementFromComet); + targetInstance.getAssignedDiscipline(); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); @@ -143,11 +153,8 @@ public void saveDataFromComet(StructuralElementInstance elementFromComet, Struct } } - - @Override public IWizardPage getNextPage(IWizardPage page) { return super.getNextPage(page); } - } \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java index b3c8b1ac..decebf4c 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java @@ -110,7 +110,11 @@ public void createControl(Composite parent) { public void widgetSelected(SelectionEvent e) { if (e.detail == SWT.CHECK) { TreeItem item = (TreeItem) e.item; + checkChildren(item, item.getChecked()); + + List updatedNodes = getCheckedTreeNodes(); + System.out.println("Updated TreeNodes: " + updatedNodes); } } }); @@ -242,24 +246,54 @@ private boolean validateInput() { /** * Retrieves a list of all TreeItems that are checked in the Tree. */ - public List getCheckedItems() { - List checkedItems = new ArrayList<>(); + public List getCheckedTreeNodes() { + List checkedNodes = new ArrayList<>(); for (TreeItem item : targetTree.getItems()) { - getCheckedItemsRecursively(item, checkedItems); + TreeNode node = collectCheckedItemsRecursively(item); + if (node != null) { + checkedNodes.add(node); + } } - return checkedItems; + System.out.println("Checked nodes: " + checkedNodes); + return checkedNodes; } /** * Recursively collects checked TreeItems and adds them to the provided list. */ - private void getCheckedItemsRecursively(TreeItem item, List checkedItems) { - if (item.getChecked()) { - checkedItems.add(item); + private TreeNode collectCheckedItemsRecursively(TreeItem item) { + System.out.println("Checking item: " + item.getText() + ", isChecked: " + item.getChecked()); + + if (!item.getChecked() && item.getItems().length == 0) { + return null; } + TreeNode node = new TreeNode(cleanName(item.getText())); + System.out.println("Creating TreeNode: " + node.getName()); + for (TreeItem child : item.getItems()) { - getCheckedItemsRecursively(child, checkedItems); + TreeNode childNode = collectCheckedItemsRecursively(child); + if (childNode != null) { + node.addChild(childNode); + System.out.println("Added child: " + childNode.getName() + " to parent: " + node.getName()); + } + } + + if (!item.getChecked() && node.getChildren().isEmpty()) { + return null; + } + + return node; + } + + /** + * Clean a name. + * + */ + private String cleanName(String name) { + if (name == null || name.isEmpty()) { + return name; } + return name.replaceAll("[^a-zA-Z0-9]", ""); } /** diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java new file mode 100644 index 00000000..f7547644 --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import java.util.ArrayList; +import java.util.List; + +public class TreeNode { + private String name; + private List children; + + public TreeNode(String name) { + this.name = name; + this.children = new ArrayList<>(); + } + + public String getName() { + return name; + } + + public List getChildren() { + return children; + } + + public void addChild(TreeNode child) { + this.children.add(child); + } +} \ No newline at end of file From 560a9d63533b4c6c696c4c8e1091b19b99e0c12b Mon Sep 17 00:00:00 2001 From: ngat_di Date: Fri, 22 Nov 2024 11:26:36 +0100 Subject: [PATCH 07/11] Task #234 using of TreeNode as the data structure. --- .../cefx/ui/importWizards/CometImportWizard.java | 9 +++++---- .../ui/importWizards/CometImportWizardPage.java | 5 ++--- .../extension/cefx/ui/importWizards/TreeNode.java | 13 ++++++++++++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 0ade9f38..4f501def 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -100,8 +100,8 @@ public boolean performFinish() { } /** - * Creates a hierarchy of ElementConfiguration instances corresponding to the structure of the provided TreeItem. - */ + * Creates a hierarchy of ElementConfiguration instances corresponding to the structure of the provided TreeNode. + */ private void createElementConfigurationHierarchy(TreeNode item, StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain) { if (item == null) { @@ -117,6 +117,8 @@ private void createElementConfigurationHierarchy(TreeNode item, StructuralElemen // Save this ElementConfiguratiookn under the parentInstance try { + parentInstance.getAssignedDiscipline(); + saveDataFromComet(elementConfig.getStructuralElementInstance(), parentInstance, editingDomain); System.out.println("Saved ElementConfiguration: " + elementConfig.getName() + " under parent."); } catch (Exception e) { @@ -141,10 +143,9 @@ private void saveDataFromComet(StructuralElementInstance elementFromComet, Struc if (elementFromComet == null) { throw new IllegalStateException("StructuralElementInstance is null in the Configuration Tree."); } - + elementFromComet.setAssignedDiscipline(targetInstance.getAssignedDiscipline()); try { Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, elementFromComet); - targetInstance.getAssignedDiscipline(); editingDomain.getCommandStack().execute(addCommand); editingDomain.saveAll(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java index decebf4c..5b804be9 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java @@ -259,7 +259,7 @@ public List getCheckedTreeNodes() { } /** - * Recursively collects checked TreeItems and adds them to the provided list. + * Recursively collects checked items from a TreeItem structure and constructs a hierarchical TreeNode structure. */ private TreeNode collectCheckedItemsRecursively(TreeItem item) { System.out.println("Checking item: " + item.getText() + ", isChecked: " + item.getChecked()); @@ -286,8 +286,7 @@ private TreeNode collectCheckedItemsRecursively(TreeItem item) { } /** - * Clean a name. - * + * Cleans a name by removing spaces and special characters, keeping only alphanumeric characters. */ private String cleanName(String name) { if (name == null || name.isEmpty()) { diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java index f7547644..538d847c 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java @@ -29,7 +29,18 @@ public List getChildren() { return children; } + /** + * Checks if a child with the given name already exists in this TreeNode. + */ public void addChild(TreeNode child) { - this.children.add(child); + for (TreeNode existingChild : children) { + if (existingChild.getName().equals(child.getName())) { + System.out.println("Duplicate child ignored: " + child.getName()); + return; + } + } + children.add(child); + System.out.println("Added child: " + child.getName()); } + } \ No newline at end of file From a8a77a0053a12ac66d0af7b1bcfc016f3ae78f14 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Wed, 27 Nov 2024 16:08:03 +0100 Subject: [PATCH 08/11] Task #234 Fix the issues of importing of the mass parameter. --- .../ui/importWizards/CometImportWizard.java | 126 +++++++++++++++--- .../importWizards/CometImportWizardPage.java | 22 +-- .../cefx/ui/importWizards/TreeNode.java | 37 +++-- 3 files changed, 144 insertions(+), 41 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 4f501def..8b1594a9 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -19,11 +19,16 @@ import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; import de.dlr.sc.virsat.model.dvlm.structural.StructuralPackage; +import de.dlr.sc.virsat.model.extension.cefx.model.Parameter; +import de.dlr.sc.virsat.model.extension.cefx.model.SubSystemMassParameters; +import de.dlr.sc.virsat.model.extension.cefx.model.SystemMassParameters; import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IResource; @@ -43,12 +48,19 @@ public class CometImportWizard extends Wizard implements IImportWizard { private ImportTargetSelection targetSelectionPage; private IContainer model; + /** + * Default constructor for the CometImportWizard. + * Initializes the wizard with a title and sets the progress monitor flag. + */ public CometImportWizard() { super(); setWindowTitle("Comet Import Wizard"); setNeedsProgressMonitor(true); } + /** + * Initializes the wizard with the workbench and selected resource. + */ @Override public void init(IWorkbench workbench, IStructuredSelection selection) { this.model = ResourcesPlugin.getWorkspace().getRoot(); @@ -59,12 +71,19 @@ public void init(IWorkbench workbench, IStructuredSelection selection) { addPage(targetSelectionPage); } + /** + * Retrieves the active Concept associated with PS. + */ public Concept getPsConcept(VirSatTransactionalEditingDomain domain) { Repository currentRepository = domain.getResourceSet().getRepository(); ActiveConceptHelper acHelper = new ActiveConceptHelper(currentRepository); return acHelper.getConcept(de.dlr.sc.virsat.model.extension.ps.Activator.getPluginId()); } + /** + * Executes the "Finish" operation of the wizard. + * Processes the selected tree nodes and performs the required import operations. + */ @Override public boolean performFinish() { // Retrieve selected source tree items from the first page @@ -73,15 +92,15 @@ public boolean performFinish() { System.out.println("No items selected. Aborting operation."); return false; } - + // Retrieve the selected target location from the second page StructuralElementInstance targetInstance = (StructuralElementInstance) targetSelectionPage.getSelection(); - + if (targetInstance == null) { System.out.println("No target instance selected. Aborting operation."); return false; } - + VirSatTransactionalEditingDomain editingDomain = VirSatEditingDomainRegistry.INSTANCE.getEd(targetInstance); // Process each selected item @@ -98,27 +117,34 @@ public boolean performFinish() { System.out.println("All items processed successfully."); return true; } - + /** * Creates a hierarchy of ElementConfiguration instances corresponding to the structure of the provided TreeNode. */ - private void createElementConfigurationHierarchy(TreeNode item, StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain) { if (item == null) { System.out.println("TreeNode is null. Skipping..."); return; } - System.out.println("Processing TreeNode: " + item.getName()); - - // Create an ElementConfiguration for the current tree item + + // Log the current processing item + System.out.println("Processing TreeNode: " + item.getOriginalName()); + + // Check if this node represents a mass value + double massValue = extractMassValue(item.getOriginalName()); + if (massValue > 0) { + // If this is a mass node, add it as a parameter and skip further processing + addMassParameter(parentInstance, editingDomain, massValue); + System.out.println("Added mass parameter with value: " + massValue + " for TreeNode: " + item.getOriginalName()); + return; + } + + // Create an ElementConfiguration for non-mass nodes ElementConfiguration elementConfig = new ElementConfiguration(getPsConcept(editingDomain)); - elementConfig.setName(item.getName()); - System.out.println("Created ElementConfiguration: " + elementConfig.getName()); + elementConfig.setName(item.getCleanedName()); - // Save this ElementConfiguratiookn under the parentInstance try { - parentInstance.getAssignedDiscipline(); - + // Save this ElementConfiguration under the parent instance saveDataFromComet(elementConfig.getStructuralElementInstance(), parentInstance, editingDomain); System.out.println("Saved ElementConfiguration: " + elementConfig.getName() + " under parent."); } catch (Exception e) { @@ -127,11 +153,78 @@ private void createElementConfigurationHierarchy(TreeNode item, StructuralElemen return; } - // Recursively process child nodes to maintain the nested structure + // Recursively process child nodes for (TreeNode childItem : item.getChildren()) { createElementConfigurationHierarchy(childItem, elementConfig.getStructuralElementInstance(), editingDomain); + } + } + + /** + * Adds a mass parameter to the parent instance based on the extracted mass value. + */ + private void addMassParameter(StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain, double massValue) { + try { + // Check if the parent instance represents a SubSystem or System + String parentName = parentInstance.getName().toLowerCase(); + if (parentName.contains("subsystem")) { + // Create and configure SubSystemMassParameters + SubSystemMassParameters subSystemMassParameters = new SubSystemMassParameters(getPsConcept(editingDomain)); + subSystemMassParameters.setName("SubSystemMassParameter"); + Parameter massTotal = subSystemMassParameters.getMassTotalBean().getValue(); + if (massTotal == null) { + massTotal = new Parameter(); // Create a new Parameter instance + } + massTotal.setDefaultValue(massValue); + subSystemMassParameters.getMassTotalBean().setValue(massTotal); + + // Add the SubSystemMassParameters to the parent instance + Command addCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, subSystemMassParameters); + editingDomain.getCommandStack().execute(addCommand); + System.out.println("Added SubSystemMassParameter with value: " + massValue); + } else { + // Create and configure SystemMassParameters + SystemMassParameters systemMassParameters = new SystemMassParameters(getPsConcept(editingDomain)); + systemMassParameters.setName("SystemMassParameter"); + Parameter massTotal = systemMassParameters.getMassTotalBean().getValue(); + if (massTotal == null) { + massTotal = new Parameter(); // Create a new Parameter instance + } + massTotal.setDefaultValue(massValue); + systemMassParameters.getMassTotalBean().setValue(massTotal); + + // Add the SystemMassParameters to the parent instance + Command addCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, systemMassParameters); + editingDomain.getCommandStack().execute(addCommand); + System.out.println("Added SystemMassParameter with value: " + massValue); + } + } catch (Exception e) { + System.err.println("Error adding mass parameter: " + e.getMessage()); + e.printStackTrace(); + } + } + /** + * Extracts the numeric mass value from a given text string. + */ + private double extractMassValue(String text) { + if (text == null || text.isEmpty()) { + return 0; + } + + try { + // Regex to detect numbers followed by "kg" + Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*kg", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(text); + + if (matcher.find()) { + // Extract and parse the numeric value + return Double.parseDouble(matcher.group(1)); + } + } catch (Exception e) { + System.err.println("Error parsing mass value: " + e.getMessage()); } + // Return 0 if no valid mass is found + return 0; } /** @@ -154,8 +247,11 @@ private void saveDataFromComet(StructuralElementInstance elementFromComet, Struc } } + /** + * Retrieves the next wizard page after the current one. + */ @Override public IWizardPage getNextPage(IWizardPage page) { return super.getNextPage(page); } -} \ No newline at end of file +} diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java index 5b804be9..bfa99459 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java @@ -216,7 +216,7 @@ public void run(IProgressMonitor monitor) { private void createLabelAndTextField(Composite parent, String labelText, int style, Color labelColor) { Label label = new Label(parent, SWT.NONE); label.setText(labelText); - label.setForeground(labelColor); // Set label color to blue + label.setForeground(labelColor); Text text = new Text(parent, style); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); if (labelText.equals("Server URL:")) { @@ -267,34 +267,26 @@ private TreeNode collectCheckedItemsRecursively(TreeItem item) { if (!item.getChecked() && item.getItems().length == 0) { return null; } - TreeNode node = new TreeNode(cleanName(item.getText())); - System.out.println("Creating TreeNode: " + node.getName()); + + // Use the original text + TreeNode node = new TreeNode(item.getText()); + System.out.println("Creating TreeNode: " + node.getCleanedName()); for (TreeItem child : item.getItems()) { TreeNode childNode = collectCheckedItemsRecursively(child); if (childNode != null) { node.addChild(childNode); - System.out.println("Added child: " + childNode.getName() + " to parent: " + node.getName()); + System.out.println("Added child: " + childNode.getCleanedName() + " to parent: " + node.getCleanedName()); } } if (!item.getChecked() && node.getChildren().isEmpty()) { - return null; + return null; } return node; } - /** - * Cleans a name by removing spaces and special characters, keeping only alphanumeric characters. - */ - private String cleanName(String name) { - if (name == null || name.isEmpty()) { - return name; - } - return name.replaceAll("[^a-zA-Z0-9]", ""); - } - /** * Recursively checks or unchecks all child TreeItems of the specified TreeItem. */ diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java index 538d847c..f8c6eda6 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java @@ -13,16 +13,22 @@ import java.util.List; public class TreeNode { - private String name; + private String originalName; // Holds the original name + private String cleanedName; // Holds the cleaned name private List children; - public TreeNode(String name) { - this.name = name; + public TreeNode(String originalName) { + this.originalName = originalName; + this.cleanedName = cleanName(originalName); // Automatically clean the name during initialization this.children = new ArrayList<>(); } - public String getName() { - return name; + public String getOriginalName() { + return originalName; + } + + public String getCleanedName() { + return cleanedName; } public List getChildren() { @@ -30,17 +36,26 @@ public List getChildren() { } /** - * Checks if a child with the given name already exists in this TreeNode. + * Adds a child to this TreeNode. Prevents duplicates based on the cleaned name. */ public void addChild(TreeNode child) { for (TreeNode existingChild : children) { - if (existingChild.getName().equals(child.getName())) { - System.out.println("Duplicate child ignored: " + child.getName()); - return; + if (existingChild.getCleanedName().equals(child.getCleanedName())) { + System.out.println("Duplicate child ignored: " + child.getCleanedName()); + return; } } children.add(child); - System.out.println("Added child: " + child.getName()); + System.out.println("Added child: " + child.getCleanedName()); } -} \ No newline at end of file + /** + * Cleans a name by removing special characters and spaces, leaving only alphanumeric characters. + */ + private String cleanName(String name) { + if (name == null || name.isEmpty()) { + return name; + } + return name.replaceAll("[^a-zA-Z0-9]", ""); // Removes all non-alphanumeric characters + } +} From c2bcb2f29a80c6ab45a341a8693f2b8bd10cb389 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Fri, 13 Dec 2024 19:32:12 +0100 Subject: [PATCH 09/11] Task #213 Fix problems of the Equipement mass and Equipement mass parameter. --- .../ui/importWizards/CometDataFetcher.java | 40 +++++----- .../ui/importWizards/CometImportWizard.java | 79 ++++++++++--------- .../cefx/ui/importWizards/TreeNode.java | 2 +- 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java index f8ca5bef..18cf0997 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java @@ -140,12 +140,14 @@ private boolean isTopLevelElement(ElementDefinition element) { */ private void addChildren(TreeItem parentItem, ElementDefinition parentElement) { parentElement.getParameter().stream() - .filter(parameter -> "mass".equalsIgnoreCase(parameter.getParameterType().getName())) - .findFirst() - .ifPresent(parameter -> { - String massValue = fetchParameterValue(parameter); - TreeItem massItem = new TreeItem(parentItem, SWT.NONE); - massItem.setText("Mass: " + massValue + " kg"); + .filter(parameter -> "mass".equalsIgnoreCase(parameter.getParameterType().getName()) + || "mass margin".equalsIgnoreCase(parameter.getParameterType().getName())) + .forEach(parameter -> { + String value = fetchParameterValue(parameter); + if (value != null) { // Ensure we only add valid manual values + TreeItem item = new TreeItem(parentItem, SWT.NONE); + item.setText(parameter.getParameterType().getName() + ": " + value + " kg"); + } }); for (ElementUsage usage : parentElement.getContainedElement()) { @@ -160,20 +162,16 @@ private void addChildren(TreeItem parentItem, ElementDefinition parentElement) { * */ private String fetchParameterValue(Parameter parameter) { - try { - ParameterValueSet valueSet = parameter.getValueSet().get(0); - - if (valueSet.getValueSwitch() == ParameterSwitchKind.COMPUTED) { - return valueSet.getComputed().get(0); - } else if (valueSet.getValueSwitch() == ParameterSwitchKind.MANUAL) { - return valueSet.getManual().get(0); - } else { - return "No valid mass found"; - } - } catch (Exception e) { - e.printStackTrace(); - return "N/A"; - } + try { + ParameterValueSet valueSet = parameter.getValueSet().get(0); + + if (valueSet.getValueSwitch() == ParameterSwitchKind.MANUAL) { + return valueSet.getManual().get(0); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; } /** @@ -192,6 +190,6 @@ private void showErrorMessage(Tree tree, String message) { */ private void handleException(Exception e, Tree tree) { e.printStackTrace(); - showErrorMessage(tree, "An error occurred: " + e.getMessage()); + showErrorMessage(tree, "An error occurred: " + e.getMessage()); } } \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index 8b1594a9..ddbbebe6 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -19,9 +19,9 @@ import de.dlr.sc.virsat.model.dvlm.concepts.util.ActiveConceptHelper; import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; import de.dlr.sc.virsat.model.dvlm.structural.StructuralPackage; +import de.dlr.sc.virsat.model.extension.cefx.model.EquipmentMassParameters; +import de.dlr.sc.virsat.model.extension.cefx.model.EquipmentParameters; import de.dlr.sc.virsat.model.extension.cefx.model.Parameter; -import de.dlr.sc.virsat.model.extension.cefx.model.SubSystemMassParameters; -import de.dlr.sc.virsat.model.extension.cefx.model.SystemMassParameters; import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; import de.dlr.sc.virsat.project.editingDomain.VirSatEditingDomainRegistry; import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; @@ -132,11 +132,12 @@ private void createElementConfigurationHierarchy(TreeNode item, StructuralElemen // Check if this node represents a mass value double massValue = extractMassValue(item.getOriginalName()); - if (massValue > 0) { - // If this is a mass node, add it as a parameter and skip further processing - addMassParameter(parentInstance, editingDomain, massValue); - System.out.println("Added mass parameter with value: " + massValue + " for TreeNode: " + item.getOriginalName()); - return; + if (massValue > 0) { + boolean isMargin = item.getOriginalName().toLowerCase().contains("margin"); + addMassParameter(parentInstance, editingDomain, massValue, isMargin); + System.out.println("Added mass parameter with value: " + massValue + " for TreeNode: " + item.getOriginalName()); + return; + } // Create an ElementConfiguration for non-mass nodes @@ -160,43 +161,49 @@ private void createElementConfigurationHierarchy(TreeNode item, StructuralElemen } /** - * Adds a mass parameter to the parent instance based on the extracted mass value. + * Adds the mass parameter and mass margin to the parent instance. + * This method creates and inserts both EquipmentParameters and EquipmentMassParameters. */ - private void addMassParameter(StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain, double massValue) { + private void addMassParameter(StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain, double massValue, boolean isMargin) { try { - // Check if the parent instance represents a SubSystem or System - String parentName = parentInstance.getName().toLowerCase(); - if (parentName.contains("subsystem")) { - // Create and configure SubSystemMassParameters - SubSystemMassParameters subSystemMassParameters = new SubSystemMassParameters(getPsConcept(editingDomain)); - subSystemMassParameters.setName("SubSystemMassParameter"); - Parameter massTotal = subSystemMassParameters.getMassTotalBean().getValue(); - if (massTotal == null) { - massTotal = new Parameter(); // Create a new Parameter instance + // Create EquipmentParameters + EquipmentParameters equipmentParameters = new EquipmentParameters(getPsConcept(editingDomain)); + equipmentParameters.setName("EquipmentParameters"); + + // Create EquipmentMassParameters + EquipmentMassParameters equipmentMassParameters = new EquipmentMassParameters(getPsConcept(editingDomain)); + equipmentMassParameters.setName("EquipmentMassParameters"); + + if (isMargin) { + // Add the value to massTotalWithMargin in EquipmentMassParameters + Parameter massTotalWithMargin = equipmentMassParameters.getMassTotalWithMarginBean().getValue(); + if (massTotalWithMargin == null) { + massTotalWithMargin = new Parameter(); // Create a new Parameter instance } - massTotal.setDefaultValue(massValue); - subSystemMassParameters.getMassTotalBean().setValue(massTotal); + massTotalWithMargin.setDefaultValue(massValue); + equipmentMassParameters.getMassTotalWithMarginBean().setValue(massTotalWithMargin); - // Add the SubSystemMassParameters to the parent instance - Command addCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, subSystemMassParameters); - editingDomain.getCommandStack().execute(addCommand); - System.out.println("Added SubSystemMassParameter with value: " + massValue); + System.out.println("Added Mass Total with Margin: " + massValue); } else { - // Create and configure SystemMassParameters - SystemMassParameters systemMassParameters = new SystemMassParameters(getPsConcept(editingDomain)); - systemMassParameters.setName("SystemMassParameter"); - Parameter massTotal = systemMassParameters.getMassTotalBean().getValue(); - if (massTotal == null) { - massTotal = new Parameter(); // Create a new Parameter instance + // Add the value to mass in EquipmentMassParameters + Parameter mass = equipmentMassParameters.getMassBean().getValue(); + if (mass == null) { + mass = new Parameter(); // Create a new Parameter instance } - massTotal.setDefaultValue(massValue); - systemMassParameters.getMassTotalBean().setValue(massTotal); + mass.setDefaultValue(massValue); + equipmentMassParameters.getMassBean().setValue(mass); - // Add the SystemMassParameters to the parent instance - Command addCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, systemMassParameters); - editingDomain.getCommandStack().execute(addCommand); - System.out.println("Added SystemMassParameter with value: " + massValue); + System.out.println("Added Mass: " + massValue); } + + // Add EquipmentParameters to the parent instance + Command addEquipmentParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, equipmentParameters); + editingDomain.getCommandStack().execute(addEquipmentParamsCommand); + + // Add EquipmentMassParameters to the parent instance + Command addEquipmentMassParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, equipmentMassParameters); + editingDomain.getCommandStack().execute(addEquipmentMassParamsCommand); + } catch (Exception e) { System.err.println("Error adding mass parameter: " + e.getMessage()); e.printStackTrace(); diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java index f8c6eda6..79076ad2 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/TreeNode.java @@ -19,7 +19,7 @@ public class TreeNode { public TreeNode(String originalName) { this.originalName = originalName; - this.cleanedName = cleanName(originalName); // Automatically clean the name during initialization + this.cleanedName = cleanName(originalName); this.children = new ArrayList<>(); } From 9ce83b36d49899846e0266e7e04027e6e8d553d2 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Fri, 24 Jan 2025 16:33:04 +0100 Subject: [PATCH 10/11] Task #233 code cleaning and updating. --- .../ui/importWizards/CometDataFetcher.java | 4 +- .../ui/importWizards/CometImportWizard.java | 104 ++++++++++-------- .../importWizards/CometImportWizardPage.java | 7 +- .../cefx/ui/importWizards/DLRLogger.java | 55 +++++++++ .../ui/importWizards/DLRStructureManager.java | 51 +++++++++ 5 files changed, 172 insertions(+), 49 deletions(-) create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRLogger.java create mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRStructureManager.java diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java index 18cf0997..ea144d6b 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometDataFetcher.java @@ -8,7 +8,6 @@ * SPDX-License-Identifier: EPL-2.0 *******************************************************************************/ - package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; import cdp4common.engineeringmodeldata.ElementDefinition; @@ -104,7 +103,6 @@ private void populateTreeWithIteration(Tree tree) { iteration.setContainer(engineeringModel); DomainOfExpertise domainOfExpertise = new DomainOfExpertise(domainOfExpertiseIid, session.getAssembler().getCache(), uri); - session.read(iteration, domainOfExpertise).join(); Optional openIteration = session.getOpenIterations().keySet().stream().findFirst(); @@ -192,4 +190,6 @@ private void handleException(Exception e, Tree tree) { e.printStackTrace(); showErrorMessage(tree, "An error occurred: " + e.getMessage()); } + + } \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java index ddbbebe6..93c07d4d 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizard.java @@ -10,6 +10,7 @@ package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; import org.eclipse.jface.wizard.IWizardPage; + import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; @@ -35,6 +36,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CompoundCommand; import org.eclipse.emf.edit.command.AddCommand; import org.eclipse.jface.viewers.IStructuredSelection; @@ -47,7 +49,10 @@ public class CometImportWizard extends Wizard implements IImportWizard { private CometImportWizardPage mainPage; private ImportTargetSelection targetSelectionPage; private IContainer model; - + + private static final int GRAMS_TO_KILOGRAMS_CONVERSION_FACTOR = 1000; + private static final int NUMBER_FOR_COMPUTATION = 3; + /** * Default constructor for the CometImportWizard. * Initializes the wizard with a title and sets the progress monitor flag. @@ -70,7 +75,6 @@ public void init(IWorkbench workbench, IStructuredSelection selection) { addPage(mainPage); addPage(targetSelectionPage); } - /** * Retrieves the active Concept associated with PS. */ @@ -88,16 +92,22 @@ public Concept getPsConcept(VirSatTransactionalEditingDomain domain) { public boolean performFinish() { // Retrieve selected source tree items from the first page List selectedItems = mainPage.getCheckedTreeNodes(); + if (selectedItems == null || selectedItems.isEmpty()) { - System.out.println("No items selected. Aborting operation."); + + DLRLogger.showErrorDialog("Operation Aborted", "No items selected."); + DLRLogger.logError("No items selected. Aborting operation.", null); + return false; } // Retrieve the selected target location from the second page StructuralElementInstance targetInstance = (StructuralElementInstance) targetSelectionPage.getSelection(); - + if (targetInstance == null) { - System.out.println("No target instance selected. Aborting operation."); + + DLRLogger.showErrorDialog("Operation Aborted", "No target instance selected."); + DLRLogger.logError("No target instance selected. Aborting operation.", null); return false; } @@ -106,29 +116,31 @@ public boolean performFinish() { // Process each selected item try { for (TreeNode item : selectedItems) { + createElementConfigurationHierarchy(item, targetInstance, editingDomain); } } catch (Exception e) { - System.err.println("Error occurred while creating configuration hierarchy: " + e.getMessage()); - e.printStackTrace(); + + DLRLogger.logError("Error occurred while creating configuration hierarchy", e); + DLRLogger.showErrorDialog("Error", "An error occurred during processing. See logs for details."); return false; } - System.out.println("All items processed successfully."); + DLRLogger.logInfo("All items processed successfully."); return true; } - + /** * Creates a hierarchy of ElementConfiguration instances corresponding to the structure of the provided TreeNode. */ private void createElementConfigurationHierarchy(TreeNode item, StructuralElementInstance parentInstance, VirSatTransactionalEditingDomain editingDomain) { if (item == null) { - System.out.println("TreeNode is null. Skipping..."); + DLRLogger.logError("TreeNode is null. Skipping...", null); return; } // Log the current processing item - System.out.println("Processing TreeNode: " + item.getOriginalName()); + DLRLogger.logInfo("Processing TreeNode: " + item.getOriginalName()); // Check if this node represents a mass value double massValue = extractMassValue(item.getOriginalName()); @@ -145,11 +157,14 @@ private void createElementConfigurationHierarchy(TreeNode item, StructuralElemen elementConfig.setName(item.getCleanedName()); try { + // Save this ElementConfiguration under the parent instance saveDataFromComet(elementConfig.getStructuralElementInstance(), parentInstance, editingDomain); - System.out.println("Saved ElementConfiguration: " + elementConfig.getName() + " under parent."); + DLRLogger.logInfo("Saved ElementConfiguration: " + elementConfig.getName() + " under parent."); + } catch (Exception e) { - System.err.println("Failed to save ElementConfiguration: " + elementConfig.getName()); + + DLRLogger.logError("Failed to save ElementConfiguration: " + elementConfig.getName(), e); e.printStackTrace(); return; } @@ -176,37 +191,28 @@ private void addMassParameter(StructuralElementInstance parentInstance, VirSatTr if (isMargin) { // Add the value to massTotalWithMargin in EquipmentMassParameters - Parameter massTotalWithMargin = equipmentMassParameters.getMassTotalWithMarginBean().getValue(); - if (massTotalWithMargin == null) { - massTotalWithMargin = new Parameter(); // Create a new Parameter instance - } - massTotalWithMargin.setDefaultValue(massValue); - equipmentMassParameters.getMassTotalWithMarginBean().setValue(massTotalWithMargin); + equipmentParameters.getMarginMaturityBean().setValue(massValue); - System.out.println("Added Mass Total with Margin: " + massValue); + DLRLogger.logInfo("Added Mass Total with Margin: " + massValue); } else { // Add the value to mass in EquipmentMassParameters + Parameter mass = equipmentMassParameters.getMassBean().getValue(); - if (mass == null) { - mass = new Parameter(); // Create a new Parameter instance - } mass.setDefaultValue(massValue); - equipmentMassParameters.getMassBean().setValue(mass); - - System.out.println("Added Mass: " + massValue); + + DLRLogger.logInfo("Added Mass: " + massValue); } // Add EquipmentParameters to the parent instance - Command addEquipmentParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, equipmentParameters); + Command addEquipmentParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CATEGORY_ASSIGNMENTS, equipmentParameters); editingDomain.getCommandStack().execute(addEquipmentParamsCommand); // Add EquipmentMassParameters to the parent instance - Command addEquipmentMassParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, equipmentMassParameters); + Command addEquipmentMassParamsCommand = AddCommand.create(editingDomain, parentInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CATEGORY_ASSIGNMENTS, equipmentMassParameters); editingDomain.getCommandStack().execute(addEquipmentMassParamsCommand); } catch (Exception e) { - System.err.println("Error adding mass parameter: " + e.getMessage()); - e.printStackTrace(); + DLRLogger.logError("Error adding mass parameter", e); } } @@ -217,43 +223,52 @@ private double extractMassValue(String text) { if (text == null || text.isEmpty()) { return 0; } - try { - // Regex to detect numbers followed by "kg" - Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*kg", Pattern.CASE_INSENSITIVE); + Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*(kg|g)", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); if (matcher.find()) { - // Extract and parse the numeric value - return Double.parseDouble(matcher.group(1)); + double value = Double.parseDouble(matcher.group(1)); + if (matcher.group(NUMBER_FOR_COMPUTATION).equalsIgnoreCase("g")) { + value /= GRAMS_TO_KILOGRAMS_CONVERSION_FACTOR; + } + return value; } } catch (Exception e) { - System.err.println("Error parsing mass value: " + e.getMessage()); + DLRLogger.logError("Error parsing mass value", e); } - // Return 0 if no valid mass is found - return 0; + return 0; } - + /** * Saves the data represented by a StructuralElementInstance to a target parent instance. - * This method creates and executes an AddCommand to attach the provided - * element as a child to the specified target instance. */ private void saveDataFromComet(StructuralElementInstance elementFromComet, StructuralElementInstance targetInstance, VirSatTransactionalEditingDomain editingDomain) { if (elementFromComet == null) { throw new IllegalStateException("StructuralElementInstance is null in the Configuration Tree."); } + elementFromComet.setAssignedDiscipline(targetInstance.getAssignedDiscipline()); + try { - Command addCommand = AddCommand.create(editingDomain, targetInstance, StructuralPackage.STRUCTURAL_ELEMENT_INSTANCE__CHILDREN, elementFromComet); - editingDomain.getCommandStack().execute(addCommand); + CompoundCommand cmd = new CompoundCommand(); + + cmd.append(DLRStructureManager.createAddChildSEICommand(targetInstance, elementFromComet, editingDomain)); + + // Execute the compound command + editingDomain.getCommandStack().execute(cmd); + + // Save changes and refresh workspace editingDomain.saveAll(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); + + } catch (CoreException e) { - e.printStackTrace(); + DLRLogger.logError("Error occurred during saveDataFromComet operation", e); } } + /** * Retrieves the next wizard page after the current one. */ @@ -261,4 +276,5 @@ private void saveDataFromComet(StructuralElementInstance elementFromComet, Struc public IWizardPage getNextPage(IWizardPage page) { return super.getNextPage(page); } + } diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java index bfa99459..b0527983 100644 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/CometImportWizardPage.java @@ -254,7 +254,7 @@ public List getCheckedTreeNodes() { checkedNodes.add(node); } } - System.out.println("Checked nodes: " + checkedNodes); + DLRLogger.logInfo("Checked nodes: " + checkedNodes); return checkedNodes; } @@ -262,7 +262,7 @@ public List getCheckedTreeNodes() { * Recursively collects checked items from a TreeItem structure and constructs a hierarchical TreeNode structure. */ private TreeNode collectCheckedItemsRecursively(TreeItem item) { - System.out.println("Checking item: " + item.getText() + ", isChecked: " + item.getChecked()); + DLRLogger.logInfo("Checking item: " + item.getText() + ", isChecked: " + item.getChecked()); if (!item.getChecked() && item.getItems().length == 0) { return null; @@ -276,7 +276,8 @@ private TreeNode collectCheckedItemsRecursively(TreeItem item) { TreeNode childNode = collectCheckedItemsRecursively(child); if (childNode != null) { node.addChild(childNode); - System.out.println("Added child: " + childNode.getCleanedName() + " to parent: " + node.getCleanedName()); + DLRLogger.logInfo("Added child: " + childNode.getCleanedName() + " to parent: " + node.getCleanedName()); + } } diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRLogger.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRLogger.java new file mode 100644 index 00000000..e7cb5858 --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRLogger.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Display; + +public class DLRLogger { + + private static final String PLUGIN_ID = "de.dlr.sc.virsat.model.extension.cefx"; + + /** + * Constructor + **/ + + private DLRLogger() { + + } + + /** + * Logs an informational message. + * + */ + public static void logInfo(String message) { + ILog log = Platform.getLog(DLRLogger.class); + log.log(new Status(IStatus.INFO, PLUGIN_ID, message)); + } + + /** + * Logs an error message with an exception. + */ + public static void logError(String message, Throwable exception) { + ILog log = Platform.getLog(DLRLogger.class); + log.log(new Status(IStatus.ERROR, PLUGIN_ID, message, exception)); + } + + /** + * Displays an error message in a dialog. + */ + public static void showErrorDialog(String title, String message) { + Display.getDefault().asyncExec(() -> MessageDialog.openError(null, title, message)); + } +} \ No newline at end of file diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRStructureManager.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRStructureManager.java new file mode 100644 index 00000000..7666709d --- /dev/null +++ b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/DLRStructureManager.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. + * + * 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 + *******************************************************************************/ + +package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; + +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.ecore.EObject; + +import de.dlr.sc.virsat.model.dvlm.general.IAssignedDiscipline; +import de.dlr.sc.virsat.model.dvlm.roles.Discipline; +import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; +import de.dlr.sc.virsat.model.dvlm.structural.command.CreateAddStructuralElementInstanceCommand; +import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; +import de.dlr.sc.virsat.project.resources.command.CreateSeiResourceAndFileCommand; + +public class DLRStructureManager { + + + /** + * private Constructor + */ + private DLRStructureManager() { + + } + + /** + * Set child discipline to parent (if any) and create a command to add child + * + * @return command that will create file structure for a child and add it to the + * parent + */ + public static CompoundCommand createAddChildSEICommand(EObject parent, StructuralElementInstance child, + VirSatTransactionalEditingDomain domain) { + Discipline discipline = null; + if (parent instanceof IAssignedDiscipline) { + discipline = ((IAssignedDiscipline) parent).getAssignedDiscipline(); + } + child.setAssignedDiscipline(discipline); + CompoundCommand cmd = new CompoundCommand(); + cmd.append(new CreateSeiResourceAndFileCommand(domain.getResourceSet(), child)); + cmd.append(CreateAddStructuralElementInstanceCommand.create(domain, parent, child)); + return cmd; + } +} From f8779bc7c552b575d1c6a49839dfd252797c8fa4 Mon Sep 17 00:00:00 2001 From: ngat_di Date: Fri, 24 Jan 2025 16:35:50 +0100 Subject: [PATCH 11/11] Task #234 Remove unused classes. --- .../ImportFromCometToCTCommand.java | 147 ------------------ .../cefx/ui/importWizards/ImportHandler.java | 99 ------------ 2 files changed, 246 deletions(-) delete mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java delete mode 100644 de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java deleted file mode 100644 index ab57a163..00000000 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportFromCometToCTCommand.java +++ /dev/null @@ -1,147 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. - * - * 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 - *******************************************************************************/ - -package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; - -import org.eclipse.emf.common.command.Command; -import org.eclipse.emf.common.command.CompoundCommand; -import org.eclipse.emf.transaction.RecordingCommand; -import de.dlr.sc.virsat.model.dvlm.concepts.Concept; -import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; -import de.dlr.sc.virsat.model.extension.cefx.model.SystemParameters; -import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; -import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; -import de.dlr.sc.virsat.model.extension.cefx.model.SubSystemMassParameters; -import de.dlr.sc.virsat.project.editingDomain.VirSatTransactionalEditingDomain; -import org.eclipse.emf.edit.command.AddCommand; -import org.eclipse.swt.widgets.TreeItem; - -import java.util.Collections; -import java.util.List; - -public class ImportFromCometToCTCommand { - - private static final int SYSTEM_MARGIN_DEFAULT = 20; - - private VirSatTransactionalEditingDomain domain; - private StructuralElementInstance parentInstance; - private Concept activeConceptCefx; - private Concept activeConceptPs; - private List selectedItems; - - /** - * Constructor to initialize the ImportFromCometToCTCommand object. - */ - public ImportFromCometToCTCommand(VirSatTransactionalEditingDomain domain, StructuralElementInstance parentInstance, Concept activeConceptCefx, Concept activeConceptPs, List selectedItems) { - if (domain == null || parentInstance == null || activeConceptCefx == null || activeConceptPs == null || selectedItems == null || selectedItems.isEmpty()) { - throw new IllegalArgumentException("Invalid arguments provided to ImportFromCometToCTCommand constructor."); - } - - this.domain = domain; - this.parentInstance = parentInstance; - this.activeConceptCefx = activeConceptCefx; - this.activeConceptPs = activeConceptPs; - this.selectedItems = selectedItems; - } - - /** - * Creates a command that imports the selected elements from the Comet tool into the ConfigurationTree. - */ - public Command createImportCommand() { - if (selectedItems.isEmpty()) { - throw new IllegalStateException("No selected items to import."); - } - - // A compound command to hold all the subcommands - CompoundCommand compoundCommand = new CompoundCommand(); - - try { - // Create a new ConfigurationTree using the root name of the first selected item - String rootName = selectedItems.get(0).getText(); - if (rootName == null || rootName.isEmpty()) { - throw new IllegalStateException("Root name of the selected item is null or empty."); - } - - ConfigurationTree newCT = new ConfigurationTree(activeConceptPs); - newCT.setName(rootName); - - // Create a structural instance for the new ConfigurationTree and set its parent - StructuralElementInstance newCTInstance = newCT.getStructuralElementInstance(); - newCTInstance.setParent(parentInstance); - - // Add the new ConfigurationTree instance to the parent instance - Command addCTCommand = AddCommand.create(domain, parentInstance, null, Collections.singleton(newCTInstance)); - compoundCommand.append(addCTCommand); - - // For each selected item, add it to the ConfigurationTree - for (TreeItem item : selectedItems) { - String itemName = item.getText(); - if (itemName == null || itemName.isEmpty()) { - continue; - } - - // Create a new ElementConfiguration for the selected item - ElementConfiguration config = new ElementConfiguration(activeConceptPs); - config.setName(itemName); - - // Create a structural instance for the configuration and set its parent - StructuralElementInstance configInstance = config.getStructuralElementInstance(); - configInstance.setParent(newCTInstance); - - // Add the configuration instance to the new ConfigurationTree instance - Command addConfigCommand = AddCommand.create(domain, newCTInstance, null, Collections.singleton(configInstance)); - compoundCommand.append(addConfigCommand); - - // Add subsystem-specific parameters using a recording command - compoundCommand.append(new RecordingCommand(domain) { - @Override - protected void doExecute() { - addSubSystemParameters(activeConceptCefx, config); - } - }); - } - - // Add system parameters to the ConfigurationTree - compoundCommand.append(new RecordingCommand(domain) { - @Override - protected void doExecute() { - addSystemParameters(activeConceptCefx, newCT); - } - }); - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Failed to create import command: " + e.getMessage(), e); - } - - // Return the compound command that can be executed - return compoundCommand; - } - - /** - * Adds system parameters to the specified ConfigurationTree. - */ - private void addSystemParameters(Concept concept, ConfigurationTree system) { - SystemParameters systemParameters = new SystemParameters(concept); - systemParameters.setName("SystemParameters"); - systemParameters.setSystemMargin(SYSTEM_MARGIN_DEFAULT); - system.add(systemParameters); - } - - /** - * Adds subsystem-specific mass parameters to the specified ElementConfiguration. - */ - private void addSubSystemParameters(Concept concept, ElementConfiguration subSystem) { - SubSystemMassParameters subSystemMassParameters = new SubSystemMassParameters(concept); - subSystemMassParameters.setName("MassParameters"); - subSystem.add(subSystemMassParameters); - } -} - diff --git a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java b/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java deleted file mode 100644 index 898e40a3..00000000 --- a/de.dlr.sc.virsat.model.extension.cefx.ui/src/de/dlr/sc/virsat/model/extension/cefx/ui/importWizards/ImportHandler.java +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany. - * - * 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 - *******************************************************************************/ - -package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards; - -import de.dlr.sc.virsat.model.extension.ps.model.ConfigurationTree; -import de.dlr.sc.virsat.model.extension.ps.model.ElementConfiguration; -import de.dlr.sc.virsat.model.concept.types.structural.IBeanStructuralElementInstance; -import de.dlr.sc.virsat.model.dvlm.structural.StructuralElement; -import de.dlr.sc.virsat.model.dvlm.structural.StructuralElementInstance; -import de.dlr.sc.virsat.model.dvlm.structural.StructuralFactory; -import org.eclipse.swt.widgets.TreeItem; - -public class ImportHandler { - - /** - * Imports the selected tree items and maps them. - */ - public ConfigurationTree importElements(TreeItem rootItem) { - // Create the root element as a ConfigurationTree - ConfigurationTree rootConfigurationTree = new ConfigurationTree(); - StructuralElementInstance rootInstance = createStructuralElementInstance(rootItem.getText()); - - if (rootInstance == null) { - throw new IllegalStateException("Root StructuralElementInstance could not be created."); - } - - rootConfigurationTree.setStructuralElementInstance(rootInstance); - rootConfigurationTree.setName(rootItem.getText()); - - // Recursively add children as ElementConfigurations - addChildren(rootConfigurationTree, rootItem); - - return rootConfigurationTree; - } - - /** - * Recursively adds children as ElementConfiguration instances. - */ - private void addChildren(IBeanStructuralElementInstance parentConfig, TreeItem parentItem) { - for (TreeItem childItem : parentItem.getItems()) { - // Create a new ElementConfiguration for each child - ElementConfiguration elementConfig = new ElementConfiguration(); - StructuralElementInstance childInstance = createStructuralElementInstance(childItem.getText()); - - if (childInstance == null) { - throw new IllegalStateException("Child StructuralElementInstance could not be created."); - } - - elementConfig.setStructuralElementInstance(childInstance); - elementConfig.setName(childItem.getText()); - - // Add the new ElementConfiguration as a child of the parent - parentConfig.add(elementConfig); - - // Recursively process children of this child item - addChildren(elementConfig, childItem); - } - } - - /** - * Helper method to create a StructuralElementInstance using a factory. - */ - private StructuralElementInstance createStructuralElementInstance(String name) { - // Use the StructuralFactory to create an instance - StructuralElementInstance sei = StructuralFactory.eINSTANCE.createStructuralElementInstance(); - - if (sei == null) { - throw new IllegalStateException("Failed to create StructuralElementInstance."); - } - sei.setName(name); - - // Create or find the corresponding StructuralElement to set the type - StructuralElement structuralElement = findOrCreateStructuralElement(name); - if (structuralElement == null) { - throw new IllegalStateException("Failed to find or create a StructuralElement for " + name); - } - - sei.setType(structuralElement); - - return sei; - } - - private StructuralElement findOrCreateStructuralElement(String name) { - // Find an existing StructuralElement by name or create a new one - StructuralElement structuralElement = StructuralFactory.eINSTANCE.createStructuralElement(); - structuralElement.setName(name); - - return structuralElement; - } -} -