<commit_before>/*
 * Copyright (C) 2010-2014 <NAME>
 * <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jkiss.dbeaver.ui.properties;

import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.DBPObject;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBECommandReflector;
import org.jkiss.dbeaver.model.edit.DBEObjectEditor;
import org.jkiss.dbeaver.model.edit.DBEObjectMaker;
import org.jkiss.dbeaver.model.edit.prop.DBECommandProperty;
import org.jkiss.dbeaver.model.edit.prop.DBEPropertyHandler;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;

/**
 * PropertySourceEditable
 */
public class PropertySourceEditable extends PropertySourceAbstract implements DBPObject, IPropertySourceEditable
{
    private DBECommandContext commandContext;
    private PropertyChangeCommand lastCommand = null;
    //private final List<IPropertySourceListener> listeners = new ArrayList<IPropertySourceListener>();
    private final CommandReflector commandReflector = new CommandReflector();

    public PropertySourceEditable(DBECommandContext commandContext, Object sourceObject, Object object)
    {
        super(sourceObject, object, true);
        this.commandContext = commandContext;
        //this.objectManager = editorInput.getObjectManager(DBEObjectEditor.class);
    }

    @Override
    public boolean isEditable(Object object)
    {
        DBEObjectEditor objectEditor = getObjectEditor(DBEObjectEditor.class);
        return commandContext != null && objectEditor != null &&
            object instanceof DBPObject && objectEditor.canEditObject((DBPObject) object);
    }

    private <T> T getObjectEditor(Class<T> managerType)
    {
        final Object editableValue = getEditableValue();
        if (editableValue == null) {
            return null;
        }
        return DBeaverCore.getInstance().getEditorsRegistry().getObjectManager(
            editableValue.getClass(),
            managerType);
    }

    @Override
    public DBECommandContext getCommandContext()
    {
        return commandContext;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void setPropertyValue(Object editableValue, ObjectPropertyDescriptor prop, Object newValue)
    {
        if (prop.getValueTransformer() != null) {
            newValue = prop.getValueTransformer().transform(editableValue, newValue);
        }
        DBSObjectCache cache = null;
        if (prop.getId().equals(DBConstants.PROP_ID_NAME) && editableValue instanceof DBSObject) {
            DBEObjectMaker objectManager = getObjectEditor(DBEObjectMaker.class);
            if (objectManager != null) {
                DBSObject object = (DBSObject) editableValue;
                cache = objectManager.getObjectsCache(object);
            }
        }
        final Object oldValue = getPropertyValue(editableValue, prop);
        if (cache != null) {
            cache.removeObject((DBSObject) editableValue);
        }
        try {
            if (!updatePropertyValue(editableValue, prop, newValue, false)) {
                return;
            }
        } finally {
            if (cache != null) {
                cache.cacheObject((DBSObject) editableValue);
            }
        }
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop) {
            final DBEObjectEditor<DBPObject> objectEditor = getObjectEditor(DBEObjectEditor.class);
            if (objectEditor == null) {
                log.error("Can't obtain object editor for " + getEditableValue());<commit_msg>Object rename handle<commit_after>/*
 * Copyright (C) 2010-2014 <NAME>
 * <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jkiss.dbeaver.ui.properties;

import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.DBPObject;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBECommandReflector;
import org.jkiss.dbeaver.model.edit.DBEObjectEditor;
import org.jkiss.dbeaver.model.edit.DBEObjectMaker;
import org.jkiss.dbeaver.model.edit.prop.DBECommandProperty;
import org.jkiss.dbeaver.model.edit.prop.DBEPropertyHandler;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;

/**
 * PropertySourceEditable
 */
public class PropertySourceEditable extends PropertySourceAbstract implements DBPObject, IPropertySourceEditable
{
    private DBECommandContext commandContext;
    private PropertyChangeCommand lastCommand = null;
    //private final List<IPropertySourceListener> listeners = new ArrayList<IPropertySourceListener>();
    private final CommandReflector commandReflector = new CommandReflector();

    public PropertySourceEditable(DBECommandContext commandContext, Object sourceObject, Object object)
    {
        super(sourceObject, object, true);
        this.commandContext = commandContext;
        //this.objectManager = editorInput.getObjectManager(DBEObjectEditor.class);
    }

    @Override
    public boolean isEditable(Object object)
    {
        DBEObjectEditor objectEditor = getObjectEditor(DBEObjectEditor.class);
        return commandContext != null && objectEditor != null &&
            object instanceof DBPObject && objectEditor.canEditObject((DBPObject) object);
    }

    private <T> T getObjectEditor(Class<T> managerType)
    {
        final Object editableValue = getEditableValue();
        if (editableValue == null) {
            return null;
        }
        return DBeaverCore.getInstance().getEditorsRegistry().getObjectManager(
            editableValue.getClass(),
            managerType);
    }

    @Override
    public DBECommandContext getCommandContext()
    {
        return commandContext;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void setPropertyValue(Object editableValue, ObjectPropertyDescriptor prop, Object newValue)
    {
        if (prop.getValueTransformer() != null) {
            newValue = prop.getValueTransformer().transform(editableValue, newValue);
        }
        DBSObjectCache cache = null;
        if (prop.getId().equals(DBConstants.PROP_ID_NAME) && editableValue instanceof DBSObject) {
            DBEObjectMaker objectManager = getObjectEditor(DBEObjectMaker.class);
            if (objectManager != null) {
                DBSObject object = (DBSObject) editableValue;
                cache = objectManager.getObjectsCache(object);
            }
        }
        final Object oldValue = getPropertyValue(editableValue, prop);
        if (cache != null) {
            // If we perform rename then we should remove object from cache and then add it back
            // To update name-based cache
            cache.removeObject((DBSObject) editableValue);
        }
        try {
            if (!updatePropertyValue(editableValue, prop, newValue, false)) {
                return;
            }
        } finally {
            if (cache != null) {
                // Add it back
                cache.cacheObject((DBSObject) editableValue);
            }
        }
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop) {
            final DBEObjectEditor<DBPObject> objectEditor = getObjectEditor(DBEObjectEditor.class);
            if (objectEditor == null) {
                log.error("Can't obtain object editor for " + getEditableValue());
{'fraction_non_alphanumeric': 0.08666453469779341, 'fraction_numerical': 0.005116725295810681, 'mean_word_length': 3.2337545126353793, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '835513', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2522, 'n_words': 662}