blob: 53ad071c8c7a9eec272db40e2bc4275190d9880a [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pierre Allard,
* Regent L'Archeveque - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.core.environment.earth.surface.ui.composites;
import java.util.Map;
import org.eclipse.apogy.core.environment.earth.surface.EarthSurfaceWorksite;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EarthSurfaceWorksiteImportFromFileComposite extends Composite {
private static final Logger Logger = LoggerFactory.getLogger(EarthSurfaceWorksiteImportFromFileComposite.class);
public static String path = System.getProperty("user.home");
private EarthSurfaceWorksite selectedEarthSurfaceWorksite;
private final Button btnBrowse;
private final Text text;
public EarthSurfaceWorksiteImportFromFileComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(3, false));
Label lblFile = new Label(this, SWT.NONE);
lblFile.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblFile.setText("File:");
this.text = new Text(this, SWT.BORDER);
this.text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.text.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
loadEarthSurfaceWorksite(EarthSurfaceWorksiteImportFromFileComposite.this.text.getText());
} catch (Throwable t) {
String errorMessage = "Failed to import Earth Surface Worksite from file<"
+ EarthSurfaceWorksiteImportFromFileComposite.this.text.getText() + ">!";
Logger.error(errorMessage, t);
MessageDialog.openError(getShell(), "Error", errorMessage);
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
this.btnBrowse = new Button(this, SWT.NONE);
this.btnBrowse.setText("Browse...");
this.btnBrowse.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
fd.setFilterPath(path);
fd.setFilterExtensions(new String[] { "*.ws", "*.sws" });
fd.setOverwrite(true);
String filePath = fd.open();
if (filePath != null && filePath.length() > 0) {
try {
loadEarthSurfaceWorksite(filePath);
EarthSurfaceWorksiteImportFromFileComposite.this.text.setText(filePath);
} catch (Throwable t) {
setSelectedEarthSurfaceWorksite(null);
String errorMessage = "Failed to import Earth Surface Worksite from file<" + filePath + ">!";
Logger.error(errorMessage, t);
MessageDialog.openError(getShell(), "Error", errorMessage);
}
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
private void setSelectedEarthSurfaceWorksite(EarthSurfaceWorksite selectedEarthSurfaceWorksite) {
this.selectedEarthSurfaceWorksite = selectedEarthSurfaceWorksite;
newEarthSurfaceWorksiteSelected(selectedEarthSurfaceWorksite);
}
public EarthSurfaceWorksite getSelectedEarthSurfaceWorksite() {
return this.selectedEarthSurfaceWorksite;
}
private void loadEarthSurfaceWorksite(String filePath) throws Throwable {
if (filePath != null) {
String urlString = filePath;
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("key", new XMIResourceFactoryImpl());
ResourceSet resSet = new ResourceSetImpl();
Resource resource = resSet.createResource(URI.createFileURI(urlString));
resource.load(m);
EarthSurfaceWorksite earthSurfaceWorksite = (EarthSurfaceWorksite) resource.getContents().get(0);
setSelectedEarthSurfaceWorksite(earthSurfaceWorksite);
}
}
/**
* Method called when a EarthSurfaceWorksite is selected.
*
* @param earthSurfaceWorksite The selected EarthSurfaceWorksite, can be null.
*/
protected void newEarthSurfaceWorksiteSelected(EarthSurfaceWorksite earthSurfaceWorksite) {
}
}