| /******************************************************************************* |
| * 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: |
| <<<<<<< HEAD |
| * Pierre Allard - initial API and implementation |
| * Regent L'Archeveque |
| * |
| ======= |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| >>>>>>> refs/heads/eclipse_pa |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.geometry.data3d.ui.commands; |
| |
| import java.util.Iterator; |
| |
| import org.eclipse.apogy.common.geometry.data3d.CartesianCoordinatesSet; |
| import org.eclipse.apogy.common.geometry.data3d.Data3DIO; |
| import org.eclipse.core.commands.AbstractHandler; |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.commands.IHandler; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.widgets.FileDialog; |
| import org.eclipse.swt.widgets.MessageBox; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.handlers.HandlerUtil; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class SaveCartesianCoordinatesSetToFileCommandHandler extends AbstractHandler implements IHandler { |
| private static final Logger Logger = LoggerFactory.getLogger(SaveCartesianCoordinatesSetToFileCommandHandler.class); |
| |
| protected static String currentDir = System.getProperty("user.dir"); |
| |
| @Override |
| public Object execute(ExecutionEvent event) throws ExecutionException { |
| Iterator<?> selections = ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).iterator(); |
| |
| while (selections.hasNext()) { |
| Object selection = selections.next(); |
| |
| if (selection instanceof CartesianCoordinatesSet) { |
| final CartesianCoordinatesSet coordinatesSet = (CartesianCoordinatesSet) selection; |
| |
| // Open pop-up and save. |
| IWorkbench wb = PlatformUI.getWorkbench(); |
| Shell shell = wb.getActiveWorkbenchWindow().getShell(); |
| saveToFile(shell, coordinatesSet); |
| } |
| } |
| return null; |
| } |
| |
| protected void saveToFile(final Shell shell, final CartesianCoordinatesSet coordinatesSet) { |
| FileDialog fileChooser = new FileDialog(shell, SWT.SAVE); |
| fileChooser.setText("Save Coordinates Set (" + coordinatesSet.getPoints().size() + " points) to file"); |
| fileChooser.setFilterPath(currentDir); |
| fileChooser.setFilterExtensions(new String[] { "*.xyz;*.csv;" }); |
| String filename = fileChooser.open(); |
| if (filename != null) { |
| try { |
| if (filename.endsWith(".xyz")) { |
| saveAsXYZ(coordinatesSet, filename); |
| } else if (filename.endsWith(".csv")) { |
| saveAsCSV(coordinatesSet, filename); |
| } else { |
| String fileExtension = ""; |
| if (filename.lastIndexOf(".") > 0) { |
| fileExtension = filename.substring(filename.lastIndexOf(".")); |
| } |
| String message = "Failed to save the coordinates. The specified file extension <" + fileExtension |
| + "> is not supported."; |
| |
| Logger.error(message); |
| |
| MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); |
| messageBox.setMessage(message); |
| messageBox.open(); |
| } |
| } catch (Exception e) { |
| Logger.error(e.getMessage(), e); |
| } |
| currentDir = fileChooser.getFilterPath(); |
| } |
| } |
| |
| protected void saveAsXYZ(CartesianCoordinatesSet coordinatesSet, String fileName) { |
| Logger.info("Saving coordinates to file <" + fileName + ">."); |
| try { |
| Data3DIO.INSTANCE.saveCoordinatesSetToXYZ(coordinatesSet, fileName); |
| Logger.info("Saved coordinates to file <" + fileName + ">."); |
| } catch (Throwable t) { |
| Logger.error("Failed to save coordinates to file <" + fileName + ">.", t); |
| } |
| } |
| |
| protected void saveAsCSV(CartesianCoordinatesSet coordinatesSet, String fileName) { |
| Logger.info("Saving coordinates to file <" + fileName + ">."); |
| try { |
| Data3DIO.INSTANCE.saveCoordinatesSetToCSV(coordinatesSet, fileName); |
| Logger.info("Saved coordinates to file <" + fileName + ">."); |
| } catch (Throwable t) { |
| Logger.error("Failed to save coordinates to file <" + fileName + ">.", t); |
| } |
| } |
| } |