<commit_before>/*******************************************************************************
 * Copyright (c) 2010, 2014, <NAME>., Budapest University of Technology and Economics
 * 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: 
 *     <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.simulink.importer.ui.handlers;

import hu.bme.mit.massif.communication.ICommandEvaluator;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.simulink.SimulinkModel;
import hu.bme.mit.massif.simulink.api.Exporter;
import hu.bme.mit.massif.simulink.api.exception.SimulinkApiException;
import hu.bme.mit.massif.simulink.importer.ui.MassifSimulinkUIPlugin;
import hu.bme.mit.massif.simulink.importer.ui.dialogs.ExportSettingsDialog;
import hu.bme.mit.massif.simulink.importer.ui.preferences.PreferenceConstants;

import java.io.File;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * @author <NAME>
 * 
 */
public class ExportModelHandler extends AbstractSimulinkHandler {

    protected static final String EXCEPTION_WHILE_EXPORTING = "Exception occurred while exporting model!";
    protected static final String EXCEPTION_WHILE_SAVING = "Exception occurred while saving exported model!";

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands. ExecutionEvent)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        ISelection selection = HandlerUtil.getCurrentSelection(event);

        if (selection instanceof IStructuredSelection) {

            final ICommandEvaluator evaluator = getCommandEvaluator();

            if (evaluator != null) {
                for (Object element : ((IStructuredSelection) selection).toList()) {
                    if (element instanceof IFile) {
                        IFile file = (IFile) element;

                        handleFile(evaluator, file);
                    }
                }
            }
        }
        return null;
    }

    private void handleFile(final ICommandEvaluator evaluator, IFile file) {
        // Reading parameter values
        // Library/model to export
        String modelName = file.getName().substring(0, file.getName().lastIndexOf("."));// bundle.getString("ModelName");
        String modelPath = file.getParent().getLocation().toOSString();

        // Result settings - path and model name
        String resultPath = getPreferenceStringValue(PreferenceConstants.EXPORT_RESULT_MODEL_PATH, "");

        // If the corresponding preferences are not set, the user is prompted to select location and name
        if (resultPath == null || resultPath == "") {

            ExportSettingsDialog dialog = new ExportSettingsDialog(Display.getCurrent().getActiveShell(),
                    new File(modelPath));
            IPreferenceStore store = MassifSimulinkUIPlugin.getDefault().getPreferenceStore();
            dialog.setPreferenceStore(store);
            dialog.open();

            // If the operation was cancelled, then return
            if (dialog.getReturnCode() == Dialog.CANCEL) {
                return;
            }

            dialog.storeSelectedFileExtension();
            
            resultPath = dialog.getTargetDirectory().toString();
        }
        // TODO is final needed? If the model is moved, finalResultPath stays unchanged
        // why?
        final String finalResultPath = resultPath;

        try {
            exportModel(evaluator, modelName, modelPath, finalResultPath);

            // TODO proper exception handling
        } catch (SimulinkApiException e) {
            Status status = new Status(Status.ERROR, MassifSimulinkUIPlugin.PLUGIN_ID, EXCEPTION_WHILE_EXPORTING, e);<commit_msg>[sonar] minor warning fix<commit_after>/*******************************************************************************
 * Copyright (c) 2010, 2014, <NAME>., Budapest University of Technology and Economics
 * 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: 
 *     <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.simulink.importer.ui.handlers;

import hu.bme.mit.massif.communication.ICommandEvaluator;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.simulink.SimulinkModel;
import hu.bme.mit.massif.simulink.api.Exporter;
import hu.bme.mit.massif.simulink.api.exception.SimulinkApiException;
import hu.bme.mit.massif.simulink.importer.ui.MassifSimulinkUIPlugin;
import hu.bme.mit.massif.simulink.importer.ui.dialogs.ExportSettingsDialog;
import hu.bme.mit.massif.simulink.importer.ui.preferences.PreferenceConstants;

import java.io.File;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.handlers.HandlerUtil;

import com.google.common.base.Strings;

/**
 * @author <NAME>
 * 
 */
public class ExportModelHandler extends AbstractSimulinkHandler {

    protected static final String EXCEPTION_WHILE_EXPORTING = "Exception occurred while exporting model!";
    protected static final String EXCEPTION_WHILE_SAVING = "Exception occurred while saving exported model!";

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands. ExecutionEvent)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        ISelection selection = HandlerUtil.getCurrentSelection(event);

        if (selection instanceof IStructuredSelection) {

            final ICommandEvaluator evaluator = getCommandEvaluator();

            if (evaluator != null) {
                for (Object element : ((IStructuredSelection) selection).toList()) {
                    if (element instanceof IFile) {
                        IFile file = (IFile) element;

                        handleFile(evaluator, file);
                    }
                }
            }
        }
        return null;
    }

    private void handleFile(final ICommandEvaluator evaluator, IFile file) {
        // Reading parameter values
        // Library/model to export
        String modelName = file.getName().substring(0, file.getName().lastIndexOf("."));// bundle.getString("ModelName");
        String modelPath = file.getParent().getLocation().toOSString();

        // Result settings - path and model name
        String resultPath = getPreferenceStringValue(PreferenceConstants.EXPORT_RESULT_MODEL_PATH, "");

        // If the corresponding preferences are not set, the user is prompted to select location and name
        if (Strings.isNullOrEmpty(resultPath)) {

            ExportSettingsDialog dialog = new ExportSettingsDialog(Display.getCurrent().getActiveShell(),
                    new File(modelPath));
            IPreferenceStore store = MassifSimulinkUIPlugin.getDefault().getPreferenceStore();
            dialog.setPreferenceStore(store);
            dialog.open();

            // If the operation was cancelled, then return
            if (dialog.getReturnCode() == Dialog.CANCEL) {
                return;
            }

            dialog.storeSelectedFileExtension();
            
            resultPath = dialog.getTargetDirectory().toString();
        }
        // TODO is final needed? If the model is moved, finalResultPath stays unchanged
        // why?
        final String finalResultPath = resultPath;

        try {
            exportModel(evaluator, modelName, modelPath, finalResultPath);

            // TODO proper exception handling
        } catch (SimulinkApiException e) {
            Status status = new Status(Status.ERROR, MassifSimulinkUIPlugin.PLUGIN_ID, EXCEPTION_WHILE_EXPORTING, e);
{'fraction_non_alphanumeric': 0.12497331910352188, 'fraction_numerical': 0.002774813233724653, 'mean_word_length': 3.194717994628469, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331663', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2447, 'n_words': 554}