Search in sources :

Example 6 with RefactoringRule

use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.

the class AutoRefactorFix method createCleanUp.

/**
 * Create the clean up.
 *
 * @param compilationUnit compilation unit
 * @param enabled         enabled
 * @param fOptions        options
 * @return Clean up fix
 */
public static ICleanUpFix createCleanUp(final CompilationUnit compilationUnit, final boolean enabled, final CleanUpOptions fOptions) {
    boolean hasChanges = false;
    final ICompilationUnit iCompilationUnit = (ICompilationUnit) compilationUnit.getJavaElement();
    // $NON-NLS-1$
    final CleanUpChange cleanUpChange = new CleanUpChange("AutoRefactor", iCompilationUnit);
    TextEdit allEdits = null;
    if (enabled) {
        final IJavaProject javaProject = PrepareApplyRefactoringsJob.getIJavaProject(iCompilationUnit);
        final JavaProjectOptions options = new JavaProjectOptionsImpl(javaProject.getOptions(true));
        final Environment environment = AutoRefactorPlugin.getEnvironment();
        final List<RefactoringRule> refactoringRules = getConfiguredRefactoringRules(fOptions);
        final SubMonitor loopMonitor = SubMonitor.convert(null, 1);
        final Queue<RefactoringUnit> refactoringUnits = new ConcurrentLinkedQueue<>();
        refactoringUnits.add(new RefactoringUnit(iCompilationUnit, options));
        final ApplyRefactoringsJob applyRefactoringsJob = new ApplyRefactoringsJob(refactoringUnits, refactoringRules, environment);
        final AggregateASTVisitor visitor = new AggregateASTVisitor(refactoringRules);
        try {
            List<TextEdit> textEdits = applyRefactoringsJob.applyRefactoring(iCompilationUnit, visitor, options, loopMonitor, false);
            for (TextEdit textEdit : textEdits) {
                if (hasChanges) {
                    allEdits = TextEditUtil.merge(allEdits, textEdit);
                } else {
                    hasChanges = true;
                    allEdits = textEdit;
                }
            }
        } catch (Exception e) {
            if (!hasChanges) {
                return null;
            }
        }
    }
    if (!hasChanges) {
        return null;
    }
    cleanUpChange.setEdit(allEdits);
    AutoRefactorFix autoRefactorFix = new AutoRefactorFix();
    autoRefactorFix.cleanUpChange = cleanUpChange;
    return autoRefactorFix;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProjectOptions(org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactoringUnit(org.autorefactor.jdt.internal.corext.dom.RefactoringUnit) CoreException(org.eclipse.core.runtime.CoreException) JavaProjectOptionsImpl(org.autorefactor.jdt.internal.corext.dom.JavaProjectOptionsImpl) ApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob) PrepareApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.PrepareApplyRefactoringsJob) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextEdit(org.eclipse.text.edits.TextEdit) CleanUpChange(org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.CleanUpChange) Environment(org.autorefactor.environment.Environment) AggregateASTVisitor(org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 7 with RefactoringRule

use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.

the class AutoRefactorFix method getConfiguredRefactoringRules.

/**
 * Returns the cleanup rules which have been enabled from the Eclipse
 * preferences.
 *
 * @param options the options
 * @return the cleanup rules which have been enabled from the Eclipse
 *         preferences
 */
public static List<RefactoringRule> getConfiguredRefactoringRules(final CleanUpOptions options) {
    final List<RefactoringRule> refactorings = AllCleanUpRules.getAllCleanUpRules();
    for (final Iterator<RefactoringRule> iter = refactorings.iterator(); iter.hasNext(); ) {
        final RefactoringRule refactoring = iter.next();
        final String cleanupPropertyName = AutoRefactorTabPage.getCleanupPropertyName(refactoring);
        if (!AutoRefactorPlugin.getDefault().getPreferenceStore().contains(cleanupPropertyName) || !CleanUpOptions.TRUE.equals(AutoRefactorPlugin.getDefault().getPreferenceStore().getString(cleanupPropertyName))) {
            iter.remove();
        }
    }
    return refactorings;
}
Also used : RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule)

Example 8 with RefactoringRule

use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.

the class AutoRefactorTabPage method createContents.

/**
 * Create the contents.
 *
 * @param parent parent
 * @return Composite
 */
@Override
public Composite createContents(Composite parent) {
    final List<RefactoringRule> allRefactoringRules = AllCleanUpRules.getAllCleanUpRules();
    Collections.sort(allRefactoringRules, Comparator.comparing(RefactoringRule::getName));
    final Group ruleGroup = createControls(parent, allRefactoringRules);
    initialize();
    invalidateToggleRules(ruleGroup);
    return fieldEditorParent;
}
Also used : RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) Group(org.eclipse.swt.widgets.Group)

Example 9 with RefactoringRule

use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.

the class AutoRefactorTabPage method createControls.

private Group createControls(final Composite parent, final List<RefactoringRule> allRefactoringRules) {
    fieldEditorParent = new Composite(parent, SWT.FILL);
    fieldEditorParent.setLayout(new FillLayout());
    ScrolledComposite scrolledComposite = new ScrolledComposite(fieldEditorParent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    initFields(allRefactoringRules);
    Composite composite = new Composite(scrolledComposite, SWT.FILL);
    final Group ruleGroup = new Group(composite, SWT.FILL);
    // $NON-NLS-1$
    ruleGroup.setText("Rules by default");
    // All rule checkbox
    toggleAllRules = new Button(ruleGroup, SWT.CHECK | SWT.LEFT);
    toggleAllRules.setFont(ruleGroup.getFont());
    // $NON-NLS-1$
    toggleAllRules.setText("Toggle all the rules");
    toggleAllRules.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            boolean isSelected = AutoRefactorTabPage.this.toggleAllRules.getSelection();
            for (BooleanFieldEditor rule : AutoRefactorTabPage.this.rules) {
                ((Button) rule.getDescriptionControl(ruleGroup)).setSelection(isSelected);
                rule.getPreferenceStore().setValue(rule.getPreferenceName(), isSelected);
            }
            AutoRefactorPlugin.getDefault().getPreferenceStore().needsSaving();
        }
    });
    // Add a space
    Composite spacer = new Composite(ruleGroup, SWT.NULL);
    spacer.setLayoutData(new GridData(0, 5));
    rules = new ArrayList<>(allRefactoringRules.size());
    for (RefactoringRule refactoringRule : allRefactoringRules) {
        final BooleanFieldEditor booleanFieldEditor = new BooleanFieldEditor(getPropertyName(refactoringRule), refactoringRule.getName(), SWT.WRAP, ruleGroup);
        booleanFieldEditor.getDescriptionControl(ruleGroup).setToolTipText(refactoringRule.getDescription());
        ((Button) booleanFieldEditor.getDescriptionControl(ruleGroup)).addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                final String isPreferenceEnabled = booleanFieldEditor.getBooleanValue() ? CleanUpOptions.TRUE : CleanUpOptions.FALSE;
                booleanFieldEditor.getPreferenceStore().setValue(booleanFieldEditor.getPreferenceName(), isPreferenceEnabled);
                booleanFieldEditor.getPreferenceStore().needsSaving();
                invalidateToggleRules(ruleGroup);
            }
        });
        rules.add(booleanFieldEditor);
    }
    fields.addAll(rules);
    scrolledComposite.setContent(composite);
    ruleGroup.pack();
    composite.pack();
    return ruleGroup;
}
Also used : Group(org.eclipse.swt.widgets.Group) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) BooleanFieldEditor(org.eclipse.jface.preference.BooleanFieldEditor) FillLayout(org.eclipse.swt.layout.FillLayout) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite)

Example 10 with RefactoringRule

use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.

the class RefactoringRulesTest method testRefactoring0.

private void testRefactoring0() throws Exception {
    // $NON-NLS-1$
    String sampleName = testName + "Sample.java";
    // $NON-NLS-1$
    File sampleIn = new File(SAMPLES_BASE_DIR, "samples_in/" + sampleName);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue(testName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
    // $NON-NLS-1$
    File sampleOut = new File(SAMPLES_BASE_DIR, "samples_out/" + sampleName);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue(testName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
    // $NON-NLS-1$
    String refactoringClassname = testName + "CleanUp";
    RefactoringRule refactoring = getRefactoringClass(refactoringClassname);
    assertNotNull(// $NON-NLS-1$ //$NON-NLS-2$
    testName + ": refactoring class " + refactoringClassname + " should exist.\n" + "Make sure you added it to the method getAllCleanUpRules() " + "of the " + // $NON-NLS-1$ //$NON-NLS-2$
    AllCleanUpRules.class + ".", // $NON-NLS-1$
    refactoring);
    String sampleInSource = readAll(sampleIn);
    String sampleOutSource = readAll(sampleOut);
    given(sampleInSource, sampleOutSource);
    IDocument doc = when(sampleName, refactoring, sampleInSource);
    then(sampleOutSource, doc);
}
Also used : RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

RefactoringRule (org.autorefactor.jdt.internal.corext.dom.RefactoringRule)11 Group (org.eclipse.swt.widgets.Group)4 GridData (org.eclipse.swt.layout.GridData)3 PrepareApplyRefactoringsJob (org.autorefactor.jdt.internal.corext.dom.PrepareApplyRefactoringsJob)2 BooleanFieldEditor (org.eclipse.jface.preference.BooleanFieldEditor)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Button (org.eclipse.swt.widgets.Button)2 Composite (org.eclipse.swt.widgets.Composite)2 File (java.io.File)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Environment (org.autorefactor.environment.Environment)1 ApplyRefactoringsJob (org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob)1 JavaProjectOptions (org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions)1 JavaProjectOptionsImpl (org.autorefactor.jdt.internal.corext.dom.JavaProjectOptionsImpl)1 JavaRefactoringRule (org.autorefactor.jdt.internal.corext.dom.JavaRefactoringRule)1 RefactoringUnit (org.autorefactor.jdt.internal.corext.dom.RefactoringUnit)1 AggregateASTVisitor (org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor)1 PreferenceConstants (org.autorefactor.preferences.PreferenceConstants)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1