Skip to content

Commit

Permalink
Task #226 Creating Classes for Importing Data from Comet.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngat_di authored and ngat_di committed May 14, 2024
1 parent 993494a commit 69f87b3
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Require-Bundle: org.eclipse.ui,
de.dlr.sc.virsat.model.extension.cefx,
org.eclipse.ui.navigator,
de.dlr.sc.virsat.model.calculation.ui,
de.dlr.sc.virsat.cef.branding.ui
de.dlr.sc.virsat.cef.branding.ui,
org.eclipse.ui.ide,
de.dlr.sc.virsat.external.lib.comet.sdkj.servicedal
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-ActivationPolicy: lazy
Export-Package: de.dlr.sc.virsat.model.extension.cefx.ui.itemprovider,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions de.dlr.sc.virsat.model.extension.cefx.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2697,6 +2697,23 @@
</with>
</activeWhen>
</handler>
</extension>
<extension
point="org.eclipse.ui.importWizards">
<wizard
category="de.dlr.virsat.global.category.import.virsat"
class="de.dlr.sc.virsat.model.extension.cefx.ui.importWizards.CometImportWizard"
icon="icons/sample.png"
id="de.dlr.sc.virsat.model.extension.cefx.ui.importWizards.CometImportWizard"
name="Comet Import Wizard">
<description>
Import a file from the local file system into the workspace.
</description>
</wizard>
<category
id="de.dlr.virsat.global.category.import.virsat"
name="Virtual Satellite">
</category>
</extension>
<!-- Plugin.XML Protected Region End -->
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*******************************************************************************
* 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.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import cdp4common.dto.Thing;
import cdp4dal.Session;
import cdp4dal.SessionImpl;
import cdp4dal.dal.Credentials;
import cdp4dal.dal.ProxySettings;
import cdp4servicesdal.CdpServicesDal;

import java.net.URI;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Responsible for fetching data from a Comet server using provided credentials and populating an SWT Tree with the data.
*/
public class CometDataFetcher {
private static final String ROOT_NODE_TEXT = "Comet Data";

private URI serverUri;
private Credentials credentials;
private Session session;
private Image nodeImage; // Image used for non-leaf tree nodes

/**
* Constructs a new CometDataFetcher.
*
* @param url Server URL.
* @param username User's login name.
* @param password User's password.
* @param proxySettings proxy settings
*/
public CometDataFetcher(String url, String username, String password, ProxySettings proxySettings) {
this.serverUri = URI.create(url);
this.credentials = new Credentials(username, password, serverUri, proxySettings);
this.session = new SessionImpl(new CdpServicesDal(), this.credentials);
initImages();
}

/**
* Initializes images for the tree nodes from Eclipse's shared image repository.
*/
private void initImages() {
nodeImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
}

/**
* Fetches data from the Comet server and populates the provided SWT Tree.
*
* @param tree The tree to populate with data.
*/
public void fetchData(Tree tree) {
try {
List<Thing> things = session.getDal().open(credentials, new AtomicBoolean()).get();

Display.getDefault().asyncExec(() -> populateTree(tree, things));
} catch (InterruptedException e) {
Display.getDefault().asyncExec(() -> showMessage("Data fetch interrupted.", SWT.ICON_WARNING));
} catch (ExecutionException e) {
Display.getDefault().asyncExec(() -> showMessage("Failed to fetch data from the server. Check server availability and credentials.", SWT.ICON_ERROR));
}
}

/**
* Displays a message box with a specified message and icon.
*
* @param message The message to display.
* @param iconType The type of icon to display
*/
private void showMessage(String message, int iconType) {
MessageBox messageBox = new MessageBox(Display.getDefault().getActiveShell(), iconType | SWT.OK);
messageBox.setMessage(message);
messageBox.open();
}

/**
* Populates the tree with data items, each representing a Thing object.
*
* @param tree The tree to be populated.
* @param things The list of Thing objects.
*/
private void populateTree(Tree tree, List<Thing> things) {
TreeItem rootItem = new TreeItem(tree, SWT.NONE);
rootItem.setText(ROOT_NODE_TEXT);
// Set the image for the root node
rootItem.setImage(nodeImage);
things.forEach(thing -> addTreeItem(rootItem, thing));
expandAll(tree.getItems());
}

/**
* Adds a tree item to the tree for each Thing object.
*
* @param parentItem The parent item under which the new item will be added.
* @param thing The Thing object to represent.
*/
private void addTreeItem(TreeItem parentItem, Thing thing) {
TreeItem item = new TreeItem(parentItem, SWT.NONE);
item.setText(thing.toString());
// Set image only for non-leaf nodes
item.setImage(isLeaf(thing) ? null : nodeImage);
}

/**
* Determines if this Thing is a leaf node in the hierarchy.
* A leaf node is defined as not having any other Thing in its container properties.
*
* @return true if this Thing is a leaf, false otherwise.
*/
private boolean isLeaf(Thing thing) {
// Check if all container lists are empty, which would indicate no children
for (List<?> containerList : thing.getContainerLists()) {
if (!containerList.isEmpty()) {
return false; // Found a non-empty container list, hence it's not a leaf
}
}
return true; // No children found, it's a leaf
}


/**
* Recursively expands all nodes in the tree.
*
* @param items Array of TreeItem objects to expand.
*/
private void expandAll(TreeItem[] items) {
for (TreeItem item : items) {
item.setExpanded(true);
if (item.getItemCount() > 0) {
expandAll(item.getItems());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* 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.Wizard;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
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; // The main page of the wizard

/**
* Constructor for CometImportWizard.
* Sets the window title and specifies that a progress monitor is needed.
*/
public CometImportWizard() {
super();
setWindowTitle("Comet Import Wizard");
setNeedsProgressMonitor(true);
}

/**
* Initializes the wizard with the workbench and current selection.
* This method is called when the wizard is initialized and is responsible
* for adding the pages to the wizard.
*
* @param workbench The current workbench.
* @param selection The current object selection.
*/
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
mainPage = new CometImportWizardPage("Configure Comet Server");
addPage(mainPage);
}

/**
* Called when the user clicks the Finish button on the wizard.
* This method should contain the logic to process the data collected through the wizard pages.
*
* @return true if the data processing is successful and the wizard can be closed; false otherwise.
*/
@Override
public boolean performFinish() {
// This method delegates the finish operation to the mainPage
return mainPage.performFinish();
}
}

Loading

0 comments on commit 69f87b3

Please sign in to comment.