All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jdplus.toolkit.desktop.plugin.ui.variables.actions.ImportVariablesAction Maven / Gradle / Ivy

/*
 * Copyright 2013 National Bank of Belgium
 * 
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved 
 * by the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * 
 * http://ec.europa.eu/idabc/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and 
 * limitations under the Licence.
 */
package jdplus.toolkit.desktop.plugin.ui.variables.actions;

import jdplus.toolkit.desktop.plugin.Config;
import jdplus.toolkit.desktop.plugin.Converter;
import jdplus.toolkit.desktop.plugin.interchange.Importable;
import jdplus.toolkit.desktop.plugin.interchange.InterchangeManager;
import jdplus.toolkit.desktop.plugin.nodes.SingleNodeAction;
import jdplus.toolkit.desktop.plugin.ui.variables.VariablesDocumentManager;
import jdplus.toolkit.desktop.plugin.workspace.Workspace;
import jdplus.toolkit.desktop.plugin.workspace.WorkspaceFactory;
import jdplus.toolkit.desktop.plugin.workspace.WorkspaceItem;
import jdplus.toolkit.desktop.plugin.workspace.nodes.ItemWsNode;
import jdplus.toolkit.base.api.timeseries.regression.ModellingContext;
import jdplus.toolkit.base.api.timeseries.regression.TsDataSuppliers;
import jdplus.toolkit.base.api.util.NameManager;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
import org.openide.util.actions.Presenter;

import javax.swing.*;
import java.util.List;

/**
 *
 * @author Philippe Charles
 */
@ActionID(category = "Edit", id = "demetra.desktop.ui.variables.actions.ImportVariablesAction")
@ActionRegistration(displayName = "#CTL_ImportVariablesAction", lazy = false)
@ActionReferences({
    @ActionReference(path = VariablesDocumentManager.PATH, position = 1430)
})
@Messages("CTL_ImportVariablesAction=Import from")
public final class ImportVariablesAction extends SingleNodeAction implements Presenter.Popup {

    private static final Converter CONVERTER = new VariablesConfig().reverse();
    private static final List IMPORTABLES = List.of(new ImportableVariables());

    public ImportVariablesAction() {
        super(ItemWsNode.class);
    }

    @Override
    public JMenuItem getPopupPresenter() {
        JMenuItem result = InterchangeManager.get().newImportMenu(IMPORTABLES);
        result.setText(Bundle.CTL_ImportVariablesAction());
        return result;
    }

    @Override
    protected void performAction(ItemWsNode activatedNode) {
    }

    @Override
    protected boolean enable(ItemWsNode activatedNode) {
        return true;
    }

    @Override
    public String getName() {
        return null;
    }

    private static final class ImportableVariables implements Importable {

        @Override
        public String getDomain() {
            return VariablesConfig.DOMAIN;
        }

        @Override
        public void importConfig(Config config) throws IllegalArgumentException {
            TsDataSuppliers value = CONVERTER.doForward(config);
            if (value == null || value.isEmpty()) {
                return;
            }
            Workspace ws = WorkspaceFactory.getInstance().getActiveWorkspace();
            VariablesDocumentManager mgr = WorkspaceFactory.getInstance().getManager(VariablesDocumentManager.class);
            WorkspaceItem item = mgr.create(ws);
            TsDataSuppliers element = item.getElement();
            String[] all = value.unlockedNames();
            if (all != null) {
                for (String s : all) {
                    element.set(s, value.get(s));
                }
            }
            // add the variables into the active processingcontext
            NameManager vars = ModellingContext.getActiveContext().getTsVariableManagers();
            String name = config.getName();
            if (!name.equals(item.getDisplayName()) && !vars.contains(name)) {
                vars.rename(item.getDisplayName(), name);
                item.setDisplayName(name);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy