Search in sources :

Example 1 with Factory

use of com.intellij.openapi.util.Factory in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method previewRefactoring.

protected void previewRefactoring(@NotNull UsageInfo[] usages) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!PREVIEW_IN_TESTS)
            throw new RuntimeException("Unexpected preview in tests: " + StringUtil.join(usages, info -> info.toString(), ", "));
        ensureElementsWritable(usages, createUsageViewDescriptor(usages));
        execute(usages);
        return;
    }
    final UsageViewDescriptor viewDescriptor = createUsageViewDescriptor(usages);
    final PsiElement[] elements = viewDescriptor.getElements();
    final PsiElement2UsageTargetAdapter[] targets = PsiElement2UsageTargetAdapter.convert(elements);
    Factory<UsageSearcher> factory = new Factory<UsageSearcher>() {

        @Override
        public UsageSearcher create() {
            return new UsageInfoSearcherAdapter() {

                @Override
                public void generate(@NotNull final Processor<Usage> processor) {
                    ApplicationManager.getApplication().runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            for (int i = 0; i < elements.length; i++) {
                                elements[i] = targets[i].getElement();
                            }
                            refreshElements(elements);
                        }
                    });
                    processUsages(processor, myProject);
                }

                @NotNull
                @Override
                protected UsageInfo[] findUsages() {
                    return BaseRefactoringProcessor.this.findUsages();
                }
            };
        }
    };
    showUsageView(viewDescriptor, factory, usages);
}
Also used : Language(com.intellij.lang.Language) LocalHistoryAction(com.intellij.history.LocalHistoryAction) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) RefactoringListenerManager(com.intellij.refactoring.listeners.RefactoringListenerManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Messages(com.intellij.openapi.ui.Messages) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) RefactoringTransaction(com.intellij.refactoring.listeners.impl.RefactoringTransaction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonProjectFileWritingAccessProvider(com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable) ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) LocalHistory(com.intellij.history.LocalHistory) NotNull(org.jetbrains.annotations.NotNull) Factory(com.intellij.openapi.util.Factory) Ref(com.intellij.openapi.util.Ref) java.util(java.util) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) StatusBarUtil(com.intellij.openapi.wm.impl.status.StatusBarUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DataManager(com.intellij.ide.DataManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ThrowableRunnable(com.intellij.util.ThrowableRunnable) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.usages(com.intellij.usages) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) GuiUtils(com.intellij.ui.GuiUtils) UsageViewUtil(com.intellij.usageView.UsageViewUtil) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) TestOnly(org.jetbrains.annotations.TestOnly) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UndoManager(com.intellij.openapi.command.undo.UndoManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) TransactionGuard(com.intellij.openapi.application.TransactionGuard) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) Processor(com.intellij.util.Processor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Factory(com.intellij.openapi.util.Factory) NotNull(org.jetbrains.annotations.NotNull) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with Factory

use of com.intellij.openapi.util.Factory in project intellij-community by JetBrains.

the class InjectionsSettingsUI method createActions.

private void createActions(ToolbarDecorator decorator) {
    final Consumer<BaseInjection> consumer = injection -> addInjection(injection);
    final Factory<BaseInjection> producer = (NullableFactory<BaseInjection>) () -> {
        final InjInfo info = getSelectedInjection();
        return info == null ? null : info.injection;
    };
    for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
        ContainerUtil.addAll(myAddActions, support.createAddActions(myProject, consumer));
        final AnAction action = support.createEditAction(myProject, producer);
        myEditActions.put(support.getId(), action == null ? AbstractLanguageInjectionSupport.createDefaultEditAction(myProject, producer) : action);
        mySupports.put(support.getId(), support);
    }
    Collections.sort(myAddActions, (o1, o2) -> Comparing.compare(o1.getTemplatePresentation().getText(), o2.getTemplatePresentation().getText()));
    decorator.disableUpDownActions();
    decorator.setAddActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            return !myAddActions.isEmpty();
        }
    });
    decorator.setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performAdd(button);
        }
    });
    decorator.setRemoveActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            boolean enabled = false;
            for (InjInfo info : getSelectedInjections()) {
                if (!info.bundled) {
                    enabled = true;
                    break;
                }
            }
            return enabled;
        }
    });
    decorator.setRemoveAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performRemove();
        }
    });
    decorator.setEditActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            AnAction edit = getEditAction();
            if (edit != null)
                edit.update(e);
            return edit != null && edit.getTemplatePresentation().isEnabled();
        }
    });
    decorator.setEditAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performEditAction();
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Duplicate", "Duplicate", PlatformIcons.COPY_ICON) {

        @Override
        public boolean isEnabled() {
            return getEditAction() != null;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            final InjInfo injection = getSelectedInjection();
            if (injection != null) {
                addInjection(injection.injection.copy());
            //performEditAction(e);
            }
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Enable Selected Injections", "Enable Selected Injections", PlatformIcons.SELECT_ALL_ICON) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performSelectedInjectionsEnabled(true);
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Disable Selected Injections", "Disable Selected Injections", PlatformIcons.UNSELECT_ALL_ICON) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performSelectedInjectionsEnabled(false);
        }
    });
    new DumbAwareAction("Toggle") {

        @Override
        public void update(@NotNull AnActionEvent e) {
            SpeedSearchSupply supply = SpeedSearchSupply.getSupply(myInjectionsTable);
            e.getPresentation().setEnabled(supply == null || !supply.isPopupActive());
        }

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performToggleAction();
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)), myInjectionsTable);
    if (myInfos.length > 1) {
        AnActionButton shareAction = new DumbAwareActionButton("Move to IDE Scope", null, PlatformIcons.IMPORT_ICON) {

            {
                addCustomUpdater(new AnActionButtonUpdater() {

                    @Override
                    public boolean isEnabled(AnActionEvent e) {
                        CfgInfo cfg = getTargetCfgInfo(getSelectedInjections());
                        e.getPresentation().setText(cfg == getDefaultCfgInfo() ? "Move to IDE Scope" : "Move to Project Scope");
                        return cfg != null;
                    }
                });
            }

            @Override
            public void actionPerformed(@NotNull final AnActionEvent e) {
                final List<InjInfo> injections = getSelectedInjections();
                final CfgInfo cfg = getTargetCfgInfo(injections);
                if (cfg == null)
                    return;
                for (InjInfo info : injections) {
                    if (info.cfgInfo == cfg)
                        continue;
                    if (info.bundled)
                        continue;
                    info.cfgInfo.injectionInfos.remove(info);
                    cfg.addInjection(info.injection);
                }
                final int[] selectedRows = myInjectionsTable.getSelectedRows();
                myInjectionsTable.getListTableModel().setItems(getInjInfoList(myInfos));
                TableUtil.selectRows(myInjectionsTable, selectedRows);
            }

            @Nullable
            private CfgInfo getTargetCfgInfo(final List<InjInfo> injections) {
                CfgInfo cfg = null;
                for (InjInfo info : injections) {
                    if (info.bundled) {
                        continue;
                    }
                    if (cfg == null)
                        cfg = info.cfgInfo;
                    else if (cfg != info.cfgInfo)
                        return info.cfgInfo;
                }
                if (cfg == null)
                    return null;
                for (CfgInfo info : myInfos) {
                    if (info != cfg)
                        return info;
                }
                throw new AssertionError();
            }
        };
        shareAction.setShortcut(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_DOWN_MASK)));
        decorator.addExtraAction(shareAction);
    }
    decorator.addExtraAction(new DumbAwareActionButton("Import", "Import", AllIcons.Actions.Install) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            doImportAction(e.getDataContext());
            updateCountLabel();
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Export", "Export", AllIcons.Actions.Export) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            final List<BaseInjection> injections = getInjectionList(getSelectedInjections());
            final VirtualFileWrapper wrapper = FileChooserFactory.getInstance().createSaveFileDialog(new FileSaverDescriptor("Export Selected Injections to File...", "", "xml"), myProject).save(null, null);
            if (wrapper == null)
                return;
            final Configuration configuration = new Configuration();
            configuration.setInjections(injections);
            try {
                JdomKt.write(configuration.getState(), wrapper.getFile().toPath());
            } catch (IOException ex) {
                final String msg = ex.getLocalizedMessage();
                Messages.showErrorDialog(myProject, msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
            }
        }

        @Override
        public boolean isEnabled() {
            return !getSelectedInjections().isEmpty();
        }
    });
}
Also used : Language(com.intellij.lang.Language) AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) ColumnInfo(com.intellij.util.ui.ColumnInfo) THashSet(gnu.trove.THashSet) TableCellRenderer(javax.swing.table.TableCellRenderer) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) Nls(org.jetbrains.annotations.Nls) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper) FileTypes(com.intellij.openapi.fileTypes.FileTypes) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) Messages(com.intellij.openapi.ui.Messages) NullableFactory(com.intellij.openapi.util.NullableFactory) TableView(com.intellij.ui.table.TableView) FileChooserFactory(com.intellij.openapi.fileChooser.FileChooserFactory) Configurable(com.intellij.openapi.options.Configurable) ReferenceInjector(com.intellij.psi.injection.ReferenceInjector) KeyEvent(java.awt.event.KeyEvent) com.intellij.ui(com.intellij.ui) SpeedSearchSupply(com.intellij.ui.speedSearch.SpeedSearchSupply) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SplitterProportionsData(com.intellij.openapi.ui.SplitterProportionsData) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) Factory(com.intellij.openapi.util.Factory) InputEvent(java.awt.event.InputEvent) java.util(java.util) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) ContainerUtil(com.intellij.util.containers.ContainerUtil) Comparing(com.intellij.openapi.util.Comparing) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Project(com.intellij.openapi.project.Project) ListTableModel(com.intellij.util.ui.ListTableModel) DataManager(com.intellij.ide.DataManager) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) SplitterProportionsDataImpl(com.intellij.ide.ui.SplitterProportionsDataImpl) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) Convertor(com.intellij.util.containers.Convertor) IOException(java.io.IOException) FileType(com.intellij.openapi.fileTypes.FileType) MouseEvent(java.awt.event.MouseEvent) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) FileChooser(com.intellij.openapi.fileChooser.FileChooser) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) NullableFactory(com.intellij.openapi.util.NullableFactory) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) SpeedSearchSupply(com.intellij.ui.speedSearch.SpeedSearchSupply) List(java.util.List) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) IOException(java.io.IOException) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper)

Aggregations

DataManager (com.intellij.ide.DataManager)2 Language (com.intellij.lang.Language)2 Project (com.intellij.openapi.project.Project)2 Messages (com.intellij.openapi.ui.Messages)2 Factory (com.intellij.openapi.util.Factory)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 THashSet (gnu.trove.THashSet)2 java.util (java.util)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 LocalHistory (com.intellij.history.LocalHistory)1 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 AllIcons (com.intellij.icons.AllIcons)1 SplitterProportionsDataImpl (com.intellij.ide.ui.SplitterProportionsDataImpl)1 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 TransactionGuard (com.intellij.openapi.application.TransactionGuard)1