Search in sources :

Example 1 with RefactoringRule

use of org.autorefactor.refactoring.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);
    fields = new ArrayList<FieldEditor>(1 + allRefactoringRules.size());
    fields.add(new BooleanFieldEditor(DEBUG_MODE_ON.getName(), DEBUG_MODE_ON.getDescription(), fieldEditorParent));
    final Group ruleGroup = new Group(fieldEditorParent, SWT.FILL);
    ruleGroup.setText("Rules by default");
    // All rule checkbox
    toggleAllRules = new Button(ruleGroup, SWT.CHECK | SWT.LEFT);
    toggleAllRules.setFont(ruleGroup.getFont());
    toggleAllRules.setText("Toggle all the rules");
    toggleAllRules.addSelectionListener(new SelectionAdapter() {

        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<BooleanFieldEditor>(allRefactoringRules.size());
    for (final RefactoringRule refactoringRule : allRefactoringRules) {
        final BooleanFieldEditor booleanFieldEditor = new BooleanFieldEditor(refactoringRule.getClass().getCanonicalName(), refactoringRule.getName(), SWT.WRAP, ruleGroup);
        booleanFieldEditor.getDescriptionControl(ruleGroup).setToolTipText(refactoringRule.getDescription());
        ((Button) booleanFieldEditor.getDescriptionControl(ruleGroup)).addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(final SelectionEvent e) {
                invalidateToggleRules(ruleGroup);
            }
        });
        rules.add(booleanFieldEditor);
    }
    fields.addAll(rules);
    return ruleGroup;
}
Also used : BooleanFieldEditor(org.eclipse.jface.preference.BooleanFieldEditor) FieldEditor(org.eclipse.jface.preference.FieldEditor) Group(org.eclipse.swt.widgets.Group) RefactoringRule(org.autorefactor.refactoring.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 2 with RefactoringRule

use of org.autorefactor.refactoring.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 : AllRefactoringRules.getAllRefactoringRules()) {
        store.setDefault(refactoringRule.getClass().getCanonicalName(), refactoringRule.isByDefault());
    }
}
Also used : PreferenceConstants(org.autorefactor.preferences.PreferenceConstants) RefactoringRule(org.autorefactor.refactoring.RefactoringRule) NotImplementedException(org.autorefactor.util.NotImplementedException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 3 with RefactoringRule

use of org.autorefactor.refactoring.RefactoringRule in project AutoRefactor by JnRouvignac.

the class ChooseRefactoringWizardPage method createRefactoringsTable.

private void createRefactoringsTable(Composite parent) {
    tableViewer = newCheckList(parent, BORDER | H_SCROLL | CHECK | NO_FOCUS | HIDE_SELECTION);
    createColumns(tableViewer);
    tableViewer.setContentProvider(new ArrayContentProvider());
    final List<RefactoringRule> refactorings = AllRefactoringRules.getAllRefactoringRules();
    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) {
            return ((RefactoringRule) refactoring).getDescription();
        }

        @Override
        public Point getToolTipShift(Object object) {
            return new Point(10, 20);
        }
    });
    final Table table = tableViewer.getTable();
    table.setLinesVisible(true);
    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) 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) RefactoringRule(org.autorefactor.refactoring.RefactoringRule) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData)

Example 4 with RefactoringRule

use of org.autorefactor.refactoring.RefactoringRule in project AutoRefactor by JnRouvignac.

the class WorkspacePreferencePage method createContents.

@Override
protected Control createContents(Composite parent) {
    final List<RefactoringRule> allRefactoringRules = AllRefactoringRules.getAllRefactoringRules();
    Collections.sort(allRefactoringRules, new Comparator<RefactoringRule>() {

        @Override
        public int compare(final RefactoringRule o1, final RefactoringRule o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    final Group ruleGroup = createControls(parent, allRefactoringRules);
    initialize();
    invalidateToggleRules(ruleGroup);
    checkState();
    return fieldEditorParent;
}
Also used : RefactoringRule(org.autorefactor.refactoring.RefactoringRule) Group(org.eclipse.swt.widgets.Group)

Example 5 with RefactoringRule

use of org.autorefactor.refactoring.RefactoringRule in project AutoRefactor by JnRouvignac.

the class RefactoringRulesTest method testRefactoring0.

private void testRefactoring0() throws Exception {
    final String sampleName = testName + "Sample.java";
    final File sampleIn = new File(SAMPLES_BASE_DIR, "samples_in/" + sampleName);
    assertTrue(testName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
    final File sampleOut = new File(SAMPLES_BASE_DIR, "samples_out/" + sampleName);
    assertTrue(testName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
    final String refactoringClassname = testName + "Refactoring";
    final RefactoringRule refactoring = getRefactoringClass(refactoringClassname);
    assertNotNull(testName + ": refactoring class " + refactoringClassname + " should exist.\n" + "Make sure you added it to the method getAllRefactoringRules() " + "of the " + AllRefactoringRules.class + ".", refactoring);
    final String sampleInSource = readAll(sampleIn);
    final String sampleOutSource = readAll(sampleOut);
    final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
    final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
    cu.getBuffer().setContents(sampleInSource);
    cu.save(null, true);
    final IDocument doc = new Document(sampleInSource);
    new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, new AggregateASTVisitor(Arrays.asList(refactoring)), newJavaProjectOptions(Release.javaSE("1.7.0"), 4), new NullProgressMonitor());
    final String actual = normalizeJavaSourceCode(doc.get().replaceAll("samples_in", "samples_out"));
    final String expected = normalizeJavaSourceCode(sampleOutSource);
    assertEquals(testName + ": wrong output;", expected, actual);
}
Also used : ApplyRefactoringsJob(org.autorefactor.refactoring.ApplyRefactoringsJob) RefactoringRule(org.autorefactor.refactoring.RefactoringRule) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

RefactoringRule (org.autorefactor.refactoring.RefactoringRule)6 GridData (org.eclipse.swt.layout.GridData)2 Group (org.eclipse.swt.widgets.Group)2 File (java.io.File)1 PreferenceConstants (org.autorefactor.preferences.PreferenceConstants)1 ApplyRefactoringsJob (org.autorefactor.refactoring.ApplyRefactoringsJob)1 PrepareApplyRefactoringsJob (org.autorefactor.refactoring.PrepareApplyRefactoringsJob)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 BooleanFieldEditor (org.eclipse.jface.preference.BooleanFieldEditor)1 FieldEditor (org.eclipse.jface.preference.FieldEditor)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 ArrayContentProvider (org.eclipse.jface.viewers.ArrayContentProvider)1 CheckboxTableViewer (org.eclipse.jface.viewers.CheckboxTableViewer)1 ICheckStateProvider (org.eclipse.jface.viewers.ICheckStateProvider)1 StructuredViewer (org.eclipse.jface.viewers.StructuredViewer)1