<commit_before>/**
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Eclipse Public License (EPL).
 * Please see the license.txt included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */
/*
 * Created on Apr 8, 2006
 */
package com.python.pydev.refactoring.wizards.rename;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.python.pydev.core.docutils.PyStringUtils;
import org.python.pydev.editor.refactoring.IPyRefactoringRequest;
import org.python.pydev.editor.refactoring.MultiModuleMoveRefactoringRequest;
import org.python.pydev.plugin.preferences.PydevPrefs;

import com.python.pydev.refactoring.wizards.TextInputWizardPage;

public class PyRenameRefactoringWizard extends RefactoringWizard {

    private static final String UPDATE_REFERENCES = "UPDATE_REFERENCES";
    private static final String SIMPLE_RESOURCE_RENAME = "SIMPLE_RESOURCE_RENAME";
    private final String fInputPageDescription;
    private IPyRefactoringRequest fRequest;
    private TextInputWizardPage inputPage;
    private String fInitialSetting;

    public PyRenameRefactoringWizard(Refactoring refactoring, String defaultPageTitle, String inputPageDescription,
            IPyRefactoringRequest request) {
        super(refactoring, DIALOG_BASED_USER_INTERFACE);
        setDefaultPageTitle(defaultPageTitle);
        fInputPageDescription = inputPageDescription;
        this.fRequest = request;
        this.fInitialSetting = request.getInitialName();
        Assert.isNotNull(this.fInitialSetting);
    }

    /* non java-doc
     * @see RefactoringWizard#addUserInputPages
     */
    @Override
    protected void addUserInputPages() {
        inputPage = createInputPage(fInputPageDescription, fInitialSetting);
        addPage(inputPage);
    }

    protected TextInputWizardPage createInputPage(String message, final String initialSetting) {
        return new TextInputWizardPage(message, true, initialSetting) {
            private Text textField;
            private IFile targetFile;

            @Override
            protected RefactoringStatus validateTextField(String text) {
                RefactoringStatus status = new RefactoringStatus();
                boolean acceptPoint = fRequest.isModuleRenameRefactoringRequest();
                if (PyStringUtils.isValidIdentifier(text, acceptPoint)) {
                    fRequest.setInputName(text);
                } else {
                    status.addFatalError("The name: " + text + " is not a valid identifier.");
                }
                return status;
            }

            @Override
            protected Text createTextInputField(Composite parent, int style) {
                Text ret = super.createTextInputField(parent, style);
                this.textField = ret;
                setTextToFullName();
                return ret;
            }

            private void setTextToResourceName() {
                if (targetFile != null) {
                    String curr = targetFile.getName();
                    textField.setText(curr);
                    int i = curr.lastIndexOf('.');

                    if (i >= 0) {
                        textField.setSelection(0, i);
                    } else {
                        textField.selectAll();
                    }
                }
            }

            private void setTextToFullName() {
                textField.setText(initialSetting);

                String text = initialSetting;
                int i = text.lastIndexOf('.');
                if (i >= 0) {
                    textField.setSelection(i + 1, text.length());
                } else {
                    textField.selectAll();
                }
            }

            @Override
            protected void textModified(String text) {
                if (targetFile != null && fRequest.getSimpleResourceRename()) {
                    if (!isEmptyInputValid() && text.equals("")) { //$NON-NLS-1$
                        setPageComplete(false);
                        setErrorMessage(null);
                        restoreMessage();
                        return;
                    }
                    if ((!isInitialInputValid()) && text.equals(targetFile.getName())) {
                        setPageComplete(false);
                        setErrorMessage(null);
                        restoreMessage();
                        return;
                    }

                    setPageComplete(validateTextField(text));
                }
                if (fRequest instanceof MultiModuleMoveRefactoringRequest) {
                    RefactoringStatus status;
                    if (text.length() == 0) {
                        //Accept empty for move!
                        status = new RefactoringStatus();
                        status.addInfo("Empty text: move to source foder");
                    } else {
                        status = validateTextField(text);
                    }

                    if (!status.hasFatalError()) {
                        fRequest.setInputName(text);
                    }
                    setPageComplete(status);
                } else {
                    super.textModified(text);
                }
            }

            public void createControl(Composite parent) {
                Composite superComposite = new Composite(parent, SWT.NONE);
                setControl(superComposite);
                initializeDialogUnits(superComposite);

                superComposite.setLayout(new GridLayout());
                Composite composite = new Composite(superComposite, SWT.NONE);<commit_msg>Update PyRenameRefactoringWizard.java<commit_after>/**
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Eclipse Public License (EPL).
 * Please see the license.txt included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */
/*
 * Created on Apr 8, 2006
 */
package com.python.pydev.refactoring.wizards.rename;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.python.pydev.core.docutils.PyStringUtils;
import org.python.pydev.editor.refactoring.IPyRefactoringRequest;
import org.python.pydev.editor.refactoring.MultiModuleMoveRefactoringRequest;
import org.python.pydev.plugin.preferences.PydevPrefs;

import com.python.pydev.refactoring.wizards.TextInputWizardPage;

public class PyRenameRefactoringWizard extends RefactoringWizard {

    private static final String UPDATE_REFERENCES = "UPDATE_REFERENCES";
    private static final String SIMPLE_RESOURCE_RENAME = "SIMPLE_RESOURCE_RENAME";
    private final String fInputPageDescription;
    private IPyRefactoringRequest fRequest;
    private TextInputWizardPage inputPage;
    private String fInitialSetting;

    public PyRenameRefactoringWizard(Refactoring refactoring, String defaultPageTitle, String inputPageDescription,
            IPyRefactoringRequest request) {
        super(refactoring, DIALOG_BASED_USER_INTERFACE);
        setDefaultPageTitle(defaultPageTitle);
        fInputPageDescription = inputPageDescription;
        this.fRequest = request;
        this.fInitialSetting = request.getInitialName();
        Assert.isNotNull(this.fInitialSetting);
    }

    /* non java-doc
     * @see RefactoringWizard#addUserInputPages
     */
    @Override
    protected void addUserInputPages() {
        inputPage = createInputPage(fInputPageDescription, fInitialSetting);
        addPage(inputPage);
    }

    protected TextInputWizardPage createInputPage(String message, final String initialSetting) {
        return new TextInputWizardPage(message, true, initialSetting) {
            private Text textField;
            private IFile targetFile;

            @Override
            protected RefactoringStatus validateTextField(String text) {
                RefactoringStatus status = new RefactoringStatus();
                boolean acceptPoint = fRequest.isModuleRenameRefactoringRequest();
                if (PyStringUtils.isValidIdentifier(text, acceptPoint)) {
                    fRequest.setInputName(text);
                } else {
                    status.addFatalError("The name: " + text + " is not a valid identifier.");
                }
                return status;
            }

            @Override
            protected Text createTextInputField(Composite parent, int style) {
                Text ret = super.createTextInputField(parent, style);
                this.textField = ret;
                setTextToFullName();
                return ret;
            }

            private void setTextToResourceName() {
                if (targetFile != null) {
                    String curr = targetFile.getName();
                    textField.setText(curr);
                    int i = curr.lastIndexOf('.');

                    if (i >= 0) {
                        textField.setSelection(0, i);
                    } else {
                        textField.selectAll();
                    }
                }
            }

            private void setTextToFullName() {
                textField.setText(initialSetting);

                String text = initialSetting;
                int i = text.lastIndexOf('.');
                if (i >= 0) {
                    textField.setSelection(i + 1, text.length());
                } else {
                    textField.selectAll();
                }
            }

            @Override
            protected void textModified(String text) {
                if (targetFile != null && fRequest.getSimpleResourceRename()) {
                    if (!isEmptyInputValid() && text.equals("")) { //$NON-NLS-1$
                        setPageComplete(false);
                        setErrorMessage(null);
                        restoreMessage();
                        return;
                    }
                    if ((!isInitialInputValid()) && text.equals(targetFile.getName())) {
                        setPageComplete(false);
                        setErrorMessage(null);
                        restoreMessage();
                        return;
                    }

                    setPageComplete(validateTextField(text));
                }
                if (fRequest instanceof MultiModuleMoveRefactoringRequest) {
                    RefactoringStatus status;
                    if (text.length() == 0) {
                        //Accept empty for move!
                        status = new RefactoringStatus();
                        status.addInfo("Empty text: move to source folder");
                    } else {
                        status = validateTextField(text);
                    }

                    if (!status.hasFatalError()) {
                        fRequest.setInputName(text);
                    }
                    setPageComplete(status);
                } else {
                    super.textModified(text);
                }
            }

            public void createControl(Composite parent) {
                Composite superComposite = new Composite(parent, SWT.NONE);
                setControl(superComposite);
                initializeDialogUnits(superComposite);

                superComposite.setLayout(new GridLayout());
                Composite composite = new Composite(superComposite, SWT.NONE);
{'fraction_non_alphanumeric': 0.08250411345294993, 'fraction_numerical': 0.002977356420904176, 'mean_word_length': 1.9042093287827075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4447597', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3073, 'n_words': 648}