Search in sources :

Example 1 with RefactoringRule

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

the class ChooseRefactoringWizardPage method createRefactoringsTable.

private void createRefactoringsTable(Composite parent) {
    tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.H_SCROLL | SWT.CHECK | SWT.NO_FOCUS | SWT.HIDE_SELECTION);
    createColumns(tableViewer);
    tableViewer.setContentProvider(new ArrayContentProvider());
    final List<RefactoringRule> refactorings = AllCleanUpRules.getAllCleanUpRules();
    tableViewer.setInput(refactorings);
    tableViewer.setCheckStateProvider(new CheckStateProvider(refactorings));
    tableViewer.setComparator(new ViewerComparator() {

        @Override
        public int compare(Viewer viewer, Object o1, Object o2) {
            return ((RefactoringRule) o1).getName().compareTo(((RefactoringRule) o2).getName());
        }
    });
    tableViewer.addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object refactoring) {
            final String filter = filterText.getText().toLowerCase();
            final RefactoringRule rule = (RefactoringRule) refactoring;
            final String description = rule.getDescription().toLowerCase();
            final String name = rule.getName().toLowerCase();
            return description.contains(filter) || name.contains(filter);
        }
    });
    ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.NO_RECREATE);
    tableViewer.setLabelProvider(new StyledCellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            final String filter = filterText.getText().toLowerCase();
            final String name = ((RefactoringRule) cell.getElement()).getName();
            cell.setText(name);
            cell.setStyleRanges(getStyleRanges(name, filter));
        }

        private StyleRange[] getStyleRanges(String text, String filter) {
            final int matchIndex = text.toLowerCase().indexOf(filter);
            final int matchLength = filter.length();
            if (matchIndex != -1 && matchLength != 0) {
                final StyledString styledString = new StyledString(text, defaultStyler);
                styledString.setStyle(matchIndex, matchLength, underlineStyler);
                return styledString.getStyleRanges();
            }
            return null;
        }

        @Override
        public String getToolTipText(Object refactoring) {
            RefactoringRule refactoringRule = (RefactoringRule) refactoring;
            // $NON-NLS-1$
            return refactoringRule.getDescription() + "\n\nWhy to do so:\n" + refactoringRule.getReason();
        }

        @Override
        public Point getToolTipShift(Object object) {
            return new Point(10, 20);
        }
    });
    final Table table = tableViewer.getTable();
    table.setLinesVisible(false);
    table.addListener(SWT.EraseItem, event -> {
        if ((event.detail & SWT.SELECTED) != 0) {
            event.detail &= ~SWT.SELECTED;
        }
    });
    table.addListener(SWT.MouseDown, event -> {
        Point pt = new Point(event.x, event.y);
        TableItem item = table.getItem(pt);
        if (item == null) {
            return;
        }
        int index = table.indexOf(item);
        Object element = tableViewer.getElementAt(index);
        tableViewer.setChecked(element, !tableViewer.getChecked(element));
    });
    table.addListener(SWT.MouseDoubleClick, event -> {
        Point pt = new Point(event.x, event.y);
        TableItem item = table.getItem(pt);
        if (item == null) {
            return;
        }
        int index = table.indexOf(item);
        Object element = tableViewer.getElementAt(index);
        tableViewer.setCheckedElements(new Object[] { element });
        ChooseRefactoringWizardPage.this.getWizard().performFinish();
        ((WizardDialog) ChooseRefactoringWizardPage.this.getWizard().getContainer()).close();
    });
    tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
    packColumns(table);
    table.setFocus();
}
Also used : StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) Table(org.eclipse.swt.widgets.Table) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) TableItem(org.eclipse.swt.widgets.TableItem) TableViewer(org.eclipse.jface.viewers.TableViewer) CheckboxTableViewer(org.eclipse.jface.viewers.CheckboxTableViewer) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer) Viewer(org.eclipse.jface.viewers.Viewer) ICheckStateProvider(org.eclipse.jface.viewers.ICheckStateProvider) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) ViewerCell(org.eclipse.jface.viewers.ViewerCell) Point(org.eclipse.swt.graphics.Point) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 2 with RefactoringRule

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

the class WorkspacePreferencePage method createContents.

@Override
protected Control 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);
    checkState();
    return fieldEditorParent;
}
Also used : RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) Group(org.eclipse.swt.widgets.Group)

Example 3 with RefactoringRule

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

the class WorkspacePreferencePage method createControls.

private Group createControls(final Composite parent, final List<RefactoringRule> allRefactoringRules) {
    fieldEditorParent = new Composite(parent, SWT.FILL);
    initFields(allRefactoringRules);
    final Group ruleGroup = new Group(fieldEditorParent, 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 = WorkspacePreferencePage.this.toggleAllRules.getSelection();
            for (BooleanFieldEditor rule : WorkspacePreferencePage.this.rules) {
                ((Button) rule.getDescriptionControl(ruleGroup)).setSelection(isSelected);
            }
        }
    });
    // 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) {
                invalidateToggleRules(ruleGroup);
            }
        });
        rules.add(booleanFieldEditor);
    }
    fields.addAll(rules);
    return ruleGroup;
}
Also used : Group(org.eclipse.swt.widgets.Group) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) Composite(org.eclipse.swt.widgets.Composite) Button(org.eclipse.swt.widgets.Button) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData) BooleanFieldEditor(org.eclipse.jface.preference.BooleanFieldEditor)

Example 4 with RefactoringRule

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

the class PreferenceInitializer method initializeDefaultPreferences.

@Override
public void initializeDefaultPreferences() {
    // TODO initialize preferences from the JDT preferences like:
    // code style/cleanup/formatting
    final IPreferenceStore store = AutoRefactorPlugin.getDefault().getPreferenceStore();
    for (PreferenceConstants preference : PreferenceConstants.values()) {
        final String name = preference.getName();
        final Object defaultValue = preference.getDefaultValue();
        if (defaultValue instanceof Boolean) {
            store.setDefault(name, (Boolean) defaultValue);
        } else if (defaultValue instanceof Integer) {
            store.setDefault(name, (Integer) defaultValue);
        } else if (defaultValue instanceof Long) {
            store.setDefault(name, (Long) defaultValue);
        } else if (defaultValue instanceof Double) {
            store.setDefault(name, (Double) defaultValue);
        } else if (defaultValue instanceof Float) {
            store.setDefault(name, (Float) defaultValue);
        } else if (defaultValue instanceof String) {
            store.setDefault(name, (String) defaultValue);
        } else {
            throw new NotImplementedException(null, defaultValue);
        }
    }
    for (RefactoringRule refactoringRule : AllCleanUpRules.getAllCleanUpRules()) {
        store.setDefault(refactoringRule.getClass().getCanonicalName(), refactoringRule.isByDefault());
    }
}
Also used : PreferenceConstants(org.autorefactor.preferences.PreferenceConstants) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) NotImplementedException(org.autorefactor.util.NotImplementedException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 5 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)

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