<commit_before>package com.redhat.ceylon.eclipse.imp.preferences;

import static com.redhat.ceylon.compiler.typechecker.TypeChecker.LANGUAGE_MODULE_VERSION;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;
import org.osgi.service.prefs.BackingStoreException;

import com.redhat.ceylon.eclipse.imp.builder.CeylonNature;
import com.redhat.ceylon.eclipse.imp.wizard.ExportModuleWizard;
import com.redhat.ceylon.eclipse.ui.CeylonPlugin;
import com.redhat.ceylon.eclipse.ui.ICeylonResources;

public class CeylonPreferencesPage extends PropertyPage {

    private String repositoryPath;
    private boolean useEmbeddedRepo;
    private boolean enableJdtClassesDir;
    
    private Button useEmbedded;
    private Button enableJdtClasses;

    //TODO: fix copy/paste!
    public boolean isRepoValid() {
        if (useEmbeddedRepo) {
            return true;
        }
        else if (repositoryPath==null) {
            return false;
        }
        else {
            String carPath = repositoryPath + "/ceylon/language/" + LANGUAGE_MODULE_VERSION + 
                    "/ceylon.language-" + LANGUAGE_MODULE_VERSION + ".car";
            return new File(carPath).exists();
        }        
    }
    
    @Override
    public boolean performOk() {
        if (!isRepoValid()) return false;
        store();
        return super.performOk();
    }
    
    @Override
    protected void performApply() {
        if (isRepoValid()) {
            store();
            super.performApply();
        }
    }
    
    @Override
    public boolean okToLeave() {
        if (!isRepoValid()) return false;
        return super.okToLeave();
    }
    
    @Override
    protected void performDefaults() {
        useEmbeddedRepo=true;
        useEmbedded.setSelection(true);
        enableJdtClassesDir=false;
        enableJdtClasses.setSelection(false);
        selectFolder.setEnabled(false);
        folder.setEnabled(false);
        super.performDefaults();
    }
    
    private void store() {
        final IProject project = getSelectedProject();
		IEclipsePreferences node = new ProjectScope(project)
                .getNode(CeylonPlugin.PLUGIN_ID);
        if (!useEmbeddedRepo && repositoryPath!=null && !repositoryPath.isEmpty()) {
            node.put("repo", repositoryPath);
            /*getCreatedElement().getProject()
                    .setPersistentProperty(new QualifiedName(CeylonPlugin.PLUGIN_ID, "repo"), 
                            repositoryPath);*/
            ExportModuleWizard.persistDefaultRepositoryPath(repositoryPath);
        }
        else if (useEmbeddedRepo) {
            node.remove("repo");
        }
        if (enableJdtClassesDir) {
        	node.putBoolean("jdtClasses", true);;
        }
        else {
        	node.remove("jdtClasses");
            IWorkspaceRunnable r= new IWorkspaceRunnable() {
                public void run(IProgressMonitor monitor) throws CoreException {
                	project.getFolder("JDTClasses").delete(true, null);
                }
            };
            try {
                project.getWorkspace().run(r, project, IWorkspace.AVOID_UPDATE, null);
            } 
            catch (CoreException e) {
                e.printStackTrace();
            }
        }
        try {
            node.flush();
        } 
        catch (BackingStoreException e) {
            e.printStackTrace();
        }
    	new CeylonNature().addToProject(project);
        /*Job buildJob = new Job("Rebuilding Ceylon project " + getSelectedProject().getName()) {
            @Override
            public IStatus run(IProgressMonitor monitor) {
                try {
                    getSelectedProject().build(FULL_BUILD, monitor);
                } 
                catch (CoreException e) {
                    return new Status(IStatus.ERROR, CeylonPlugin.getInstance().getID(), 
                            "Job '" + this.getName() + "' failed", e);
                }
                return Status.OK_STATUS;
            }

        };
        buildJob.schedule();*/
    }

    private IProject getSelectedProject() {
        return (IProject) getElement().getAdapter(IProject.class);
    }
    
    private Button selectFolder;
    private Text folder;
    
    //TODO: fix copy/paste!
    void addSelectRepo(Composite parent) {
        
        Label desc = new Label(parent, SWT.LEFT | SWT.WRAP);
        desc.setText("The Ceylon builder compiles Ceylon source contained in the project:");

        final Button enableBuilder = new Button(parent, SWT.PUSH);
        enableBuilder.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
        enableBuilder.setText("Enable Ceylon Builder");
        enableBuilder.setEnabled(!builderEnabled);
        enableBuilder.setImage(CeylonPlugin.getInstance()
                .getImageRegistry().get(ICeylonResources.ELE32));
        //enableBuilder.setSize(40, 40);

        Label sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData sgd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
        sep.setLayoutData(sgd);

        enableJdtClasses = new Button(parent, SWT.CHECK | SWT.LEFT | SWT.WRAP);<commit_msg>fix build of dependent projects after enable/disable of JDTClasses<commit_after>package com.redhat.ceylon.eclipse.imp.preferences;

import static com.redhat.ceylon.compiler.typechecker.TypeChecker.LANGUAGE_MODULE_VERSION;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;
import org.osgi.service.prefs.BackingStoreException;

import com.redhat.ceylon.eclipse.imp.builder.CeylonNature;
import com.redhat.ceylon.eclipse.imp.wizard.ExportModuleWizard;
import com.redhat.ceylon.eclipse.ui.CeylonPlugin;
import com.redhat.ceylon.eclipse.ui.ICeylonResources;

public class CeylonPreferencesPage extends PropertyPage {

    private String repositoryPath;
    private boolean useEmbeddedRepo;
    private boolean enableJdtClassesDir;
    
    private Button useEmbedded;
    private Button enableJdtClasses;

    //TODO: fix copy/paste!
    public boolean isRepoValid() {
        if (useEmbeddedRepo) {
            return true;
        }
        else if (repositoryPath==null) {
            return false;
        }
        else {
            String carPath = repositoryPath + "/ceylon/language/" + LANGUAGE_MODULE_VERSION + 
                    "/ceylon.language-" + LANGUAGE_MODULE_VERSION + ".car";
            return new File(carPath).exists();
        }        
    }
    
    @Override
    public boolean performOk() {
        if (!isRepoValid()) return false;
        store();
        return super.performOk();
    }
    
    @Override
    protected void performApply() {
        if (isRepoValid()) {
            store();
            super.performApply();
        }
    }
    
    @Override
    public boolean okToLeave() {
        if (!isRepoValid()) return false;
        return super.okToLeave();
    }
    
    @Override
    protected void performDefaults() {
        useEmbeddedRepo=true;
        useEmbedded.setSelection(true);
        enableJdtClassesDir=false;
        enableJdtClasses.setSelection(false);
        selectFolder.setEnabled(false);
        folder.setEnabled(false);
        super.performDefaults();
    }
    
    private void store() {
        final IProject project = getSelectedProject();
		IEclipsePreferences node = new ProjectScope(project)
                .getNode(CeylonPlugin.PLUGIN_ID);
        if (!useEmbeddedRepo && repositoryPath!=null && !repositoryPath.isEmpty()) {
            node.put("repo", repositoryPath);
            /*getCreatedElement().getProject()
                    .setPersistentProperty(new QualifiedName(CeylonPlugin.PLUGIN_ID, "repo"), 
                            repositoryPath);*/
            ExportModuleWizard.persistDefaultRepositoryPath(repositoryPath);
        }
        else if (useEmbeddedRepo) {
            node.remove("repo");
        }
        if (enableJdtClassesDir) {
        	node.putBoolean("jdtClasses", true);;
        }
        else {
        	node.remove("jdtClasses");
        }
        try {
            node.flush();
        } 
        catch (BackingStoreException e) {
            e.printStackTrace();
        }
    	new CeylonNature() {
    		protected void setUpClasspath(IProject project) {
    			try {
					project.getFolder("JDTClasses").delete(true, null);
				} 
    			catch (CoreException e) {
					e.printStackTrace();
				}
    			super.setUpClasspath(project);
    			try {
    				project.getWorkspace().build(IncrementalProjectBuilder.CLEAN_BUILD, null);
    			}
    			catch (CoreException e) {
    				e.printStackTrace();
    			}
    		}
    	}.addToProject(project);
    }

    private IProject getSelectedProject() {
        return (IProject) getElement().getAdapter(IProject.class);
    }
    
    private Button selectFolder;
    private Text folder;
    
    //TODO: fix copy/paste!
    void addSelectRepo(Composite parent) {
        
        Label desc = new Label(parent, SWT.LEFT | SWT.WRAP);
        desc.setText("The Ceylon builder compiles Ceylon source contained in the project:");

        final Button enableBuilder = new Button(parent, SWT.PUSH);
        enableBuilder.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
        enableBuilder.setText("Enable Ceylon Builder");
        enableBuilder.setEnabled(!builderEnabled);
        enableBuilder.setImage(CeylonPlugin.getInstance()
                .getImageRegistry().get(ICeylonResources.ELE32));
        //enableBuilder.setSize(40, 40);

        Label sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData sgd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
        sep.setLayoutData(sgd);

        enableJdtClasses = new Button(parent, SWT.CHECK | SWT.LEFT | SWT.WRAP);
{'fraction_non_alphanumeric': 0.10244621790423317, 'fraction_numerical': 0.001040943789035392, 'mean_word_length': 2.567141089108911, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3757046', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3251, 'n_words': 587}