<commit_before>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ui.editors;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.preferences.DBPPropertySource;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.runtime.properties.PropertySourceEditable;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.SimpleCommandContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * DatabaseEditorInput
 */
public abstract class DatabaseEditorInput<NODE extends DBNDatabaseNode> implements IPersistableElement, IDatabaseEditorInput, IDataSourceContainerProvider
{
    private final NODE node;
    private final DBCExecutionContext executionContext;
    private final DBECommandContext commandContext;
    private String defaultPageId;
    private String defaultFolderId;
    private Map<String, Object> attributes = new LinkedHashMap<>();

    protected DatabaseEditorInput(@Nullable NODE node)
    {
        this(node, null);
    }

    protected DatabaseEditorInput(@Nullable NODE node, @Nullable DBECommandContext commandContext)
    {
        this.node = node;
        DBPDataSource dataSource = node == null ? null : node.getDataSource();
        if (dataSource != null) {
            this.executionContext = dataSource.getDefaultContext(false);
            this.commandContext = commandContext != null ?
                commandContext :
                new SimpleCommandContext(
                    this.executionContext,
                    false);
        } else {
            this.executionContext = null;
            this.commandContext = null;
        }
    }

    @Override
    public boolean exists()
    {
        return false;
    }

    @Override
    public ImageDescriptor getImageDescriptor()
    {
        return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
    }

    @Override
    public String getName()
    {
        if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_EDITOR_FULL_NAME)) {
            return node.getNodeFullName();
        } else {
            return node.getName();
        }
    }

    @Override
    public IPersistableElement getPersistable()
    {
        if (getExecutionContext() == null ||
            getDatabaseObject() == null ||
            !getDatabaseObject().isPersisted() ||
            !DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.UI_KEEP_DATABASE_EDITORS))
        {
            return null;
        }

        return this;
    }

    @Override
    public String getToolTipText()
    {
        return node.getNodeDescription();
    }

    @Override
    public <T> T getAdapter(Class<T> adapter)
    {
        if (IWorkbenchAdapter.class.equals(adapter)) {
            return adapter.cast(new WorkbenchAdapter() {
                @Override
                public ImageDescriptor getImageDescriptor(Object object) {
                    return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
                }
                @Override
                public String getLabel(Object o) {
                    return node.getName();
                }
                @Override
                public Object getParent(Object o) {
                    return node.getParentNode();
                }
            });
        }

        return null;
    }

    @Override
    public DBPDataSourceContainer getDataSourceContainer()
    {
        if (executionContext != null) {
            return executionContext.getDataSource().getContainer();
        } else if (node instanceof DBNDataSource) {
            return node.getDataSourceContainer();
        } else {
            return null;
        }
    }

    @Override
    public DBCExecutionContext getExecutionContext() {
        return executionContext;
    }

    @Override
    public NODE getNavigatorNode()
    {
        return node;
    }
<commit_msg>#2556 adapt DatabaseEditorInput to DBSObject<commit_after>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ui.editors;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.preferences.DBPPropertySource;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.runtime.properties.PropertySourceEditable;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.SimpleCommandContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * DatabaseEditorInput
 */
public abstract class DatabaseEditorInput<NODE extends DBNDatabaseNode> implements IPersistableElement, IDatabaseEditorInput, IDataSourceContainerProvider
{
    private final NODE node;
    private final DBCExecutionContext executionContext;
    private final DBECommandContext commandContext;
    private String defaultPageId;
    private String defaultFolderId;
    private Map<String, Object> attributes = new LinkedHashMap<>();

    protected DatabaseEditorInput(@Nullable NODE node)
    {
        this(node, null);
    }

    protected DatabaseEditorInput(@Nullable NODE node, @Nullable DBECommandContext commandContext)
    {
        this.node = node;
        DBPDataSource dataSource = node == null ? null : node.getDataSource();
        if (dataSource != null) {
            this.executionContext = dataSource.getDefaultContext(false);
            this.commandContext = commandContext != null ?
                commandContext :
                new SimpleCommandContext(
                    this.executionContext,
                    false);
        } else {
            this.executionContext = null;
            this.commandContext = null;
        }
    }

    @Override
    public boolean exists()
    {
        return false;
    }

    @Override
    public ImageDescriptor getImageDescriptor()
    {
        return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
    }

    @Override
    public String getName()
    {
        if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_EDITOR_FULL_NAME)) {
            return node.getNodeFullName();
        } else {
            return node.getName();
        }
    }

    @Override
    public IPersistableElement getPersistable()
    {
        if (getExecutionContext() == null ||
            getDatabaseObject() == null ||
            !getDatabaseObject().isPersisted() ||
            !DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.UI_KEEP_DATABASE_EDITORS))
        {
            return null;
        }

        return this;
    }

    @Override
    public String getToolTipText()
    {
        return node.getNodeDescription();
    }

    @Override
    public <T> T getAdapter(Class<T> adapter)
    {
        if (IWorkbenchAdapter.class.equals(adapter)) {
            return adapter.cast(new WorkbenchAdapter() {
                @Override
                public ImageDescriptor getImageDescriptor(Object object) {
                    return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
                }
                @Override
                public String getLabel(Object o) {
                    return node.getName();
                }
                @Override
                public Object getParent(Object o) {
                    return node.getParentNode();
                }
            });
        } else if (DBSObject.class.equals(adapter)) {
            return adapter.cast(getDatabaseObject());
        }

        return null;
    }

    @Override
    public DBPDataSourceContainer getDataSourceContainer()
    {
        if (executionContext != null) {
            return executionContext.getDataSource().getContainer();
        } else if (node instanceof DBNDataSource) {
            return node.getDataSourceContainer();
        } else {
            return null;
        }
    }

    @Override
    public DBCExecutionContext getExecutionContext() {
        return executionContext;
    }

    @Override
    public NODE getNavigatorNode()
    {
        return node;
    }

{'fraction_non_alphanumeric': 0.08522780428477875, 'fraction_numerical': 0.0026195153896529143, 'mean_word_length': 2.684936228886591, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6444470', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2832, 'n_words': 651}