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();
}
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;
}
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;
}
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());
}
}
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;
}
Aggregations