Example usage for com.intellij.openapi.editor.event EditorFactoryAdapter EditorFactoryAdapter

List of usage examples for com.intellij.openapi.editor.event EditorFactoryAdapter EditorFactoryAdapter

Introduction

In this page you can find the example usage for com.intellij.openapi.editor.event EditorFactoryAdapter EditorFactoryAdapter.

Prototype

EditorFactoryAdapter

Source Link

Usage

From source file:com.intellij.codeInsight.intention.impl.IntentionHintComponent.java

License:Apache License

private IntentionHintComponent(@NotNull Project project, @NotNull PsiFile file, @NotNull final Editor editor,
        @NotNull ShowIntentionsPass.IntentionsInfo intentions) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    myFile = file;/* w ww  . j  ava2s .co  m*/
    myEditor = editor;

    setLayout(new BorderLayout());
    setOpaque(false);

    boolean showRefactoringsBulb = false;
    for (HighlightInfo.IntentionActionDescriptor descriptor : intentions.inspectionFixesToShow) {
        if (descriptor.getAction() instanceof BaseRefactoringIntentionAction) {
            showRefactoringsBulb = true;
            break;
        }
    }
    boolean showFix = false;
    if (!showRefactoringsBulb) {
        showFix = false;
        for (final HighlightInfo.IntentionActionDescriptor pairs : intentions.errorFixesToShow) {
            IntentionAction fix = pairs.getAction();
            if (IntentionManagerSettings.getInstance().isShowLightBulb(fix)) {
                showFix = true;
                break;
            }
        }
    }

    Icon smartTagIcon = showRefactoringsBulb ? AllIcons.Actions.RefactoringBulb
            : showFix ? AllIcons.Actions.QuickfixBulb : AllIcons.Actions.IntentionBulb;

    myHighlightedIcon = new RowIcon(2);
    myHighlightedIcon.setIcon(smartTagIcon, 0);
    myHighlightedIcon.setIcon(AllIcons.General.ArrowDown, 1);

    myInactiveIcon = new RowIcon(2);
    myInactiveIcon.setIcon(smartTagIcon, 0);
    myInactiveIcon.setIcon(ourInactiveArrowIcon, 1);

    myIconLabel = new JLabel(myInactiveIcon);
    myIconLabel.setOpaque(false);

    add(myIconLabel, BorderLayout.CENTER);

    setBorder(editor.isOneLineMode() ? INACTIVE_BORDER_SMALL : INACTIVE_BORDER);

    myIconLabel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (!e.isPopupTrigger() && e.getButton() == MouseEvent.BUTTON1) {
                showPopup();
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            onMouseEnter(editor.isOneLineMode());
        }

        @Override
        public void mouseExited(MouseEvent e) {
            onMouseExit(editor.isOneLineMode());
        }
    });

    myComponentHint = new MyComponentHint(this);
    IntentionListStep step = new IntentionListStep(this, intentions, myEditor, myFile, project);
    recreateMyPopup(step);
    // dispose myself when editor closed
    EditorFactory.getInstance().addEditorFactoryListener(new EditorFactoryAdapter() {
        @Override
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            if (event.getEditor() == myEditor) {
                hide();
            }
        }
    }, this);
}

From source file:com.intellij.codeInsight.intention.impl.QuickEditHandler.java

License:Apache License

QuickEditHandler(Project project, PsiFile injectedFile, final PsiFile origFile, Editor editor,
        QuickEditAction action) {/*from   w  w w . j ava 2  s .com*/
    myProject = project;
    myEditor = editor;
    myAction = action;
    myOrigDocument = editor.getDocument();
    final Place shreds = InjectedLanguageUtil.getShreds(injectedFile);
    final FileType fileType = injectedFile.getFileType();
    final Language language = injectedFile.getLanguage();

    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final String text = InjectedLanguageManager.getInstance(project).getUnescapedText(injectedFile);
    final String newFileName = StringUtil.notNullize(language.getDisplayName(), "Injected") + " Fragment " + "("
            + origFile.getName() + ":" + shreds.get(0).getHost().getTextRange().getStartOffset() + ")" + "."
            + fileType.getDefaultExtension();
    myNewFile = factory.createFileFromText(newFileName, language, text, true, true);
    myNewVirtualFile = (LightVirtualFile) myNewFile.getVirtualFile();
    assert myNewVirtualFile != null;
    // suppress possible errors as in injected mode
    myNewFile.putUserData(InjectedLanguageUtil.FRANKENSTEIN_INJECTION,
            injectedFile.getUserData(InjectedLanguageUtil.FRANKENSTEIN_INJECTION));
    final SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(project);
    myNewFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT,
            smartPointerManager.createSmartPsiElementPointer(origFile));
    myNewDocument = PsiDocumentManager.getInstance(project).getDocument(myNewFile);
    assert myNewDocument != null;
    EditorActionManager.getInstance().setReadonlyFragmentModificationHandler(myNewDocument,
            new MyQuietHandler());
    myOrigCreationStamp = myOrigDocument.getModificationStamp(); // store creation stamp for UNDO tracking
    myOrigDocument.addDocumentListener(this, this);
    myNewDocument.addDocumentListener(this, this);
    EditorFactory editorFactory = ObjectUtils.assertNotNull(EditorFactory.getInstance());
    // not FileEditorManager listener because of RegExp checker and alike
    editorFactory.addEditorFactoryListener(new EditorFactoryAdapter() {

        int myEditorCount;

        @Override
        public void editorCreated(@NotNull EditorFactoryEvent event) {
            if (event.getEditor().getDocument() != myNewDocument)
                return;
            myEditorCount++;
            final EditorActionHandler editorEscape = EditorActionManager.getInstance()
                    .getActionHandler(IdeActions.ACTION_EDITOR_ESCAPE);
            new AnAction() {
                @Override
                public void update(AnActionEvent e) {
                    Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext());
                    e.getPresentation().setEnabled(editor != null
                            && LookupManager.getActiveLookup(editor) == null
                            && TemplateManager.getInstance(myProject).getActiveTemplate(editor) == null
                            && (editorEscape == null || !editorEscape.isEnabled(editor, e.getDataContext())));
                }

                @Override
                public void actionPerformed(AnActionEvent e) {
                    closeEditor();
                }
            }.registerCustomShortcutSet(CommonShortcuts.ESCAPE, event.getEditor().getContentComponent());
        }

        @Override
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            if (event.getEditor().getDocument() != myNewDocument)
                return;
            if (--myEditorCount > 0)
                return;
            Disposer.dispose(QuickEditHandler.this);
        }
    }, this);

    if ("JAVA".equals(shreds.get(0).getHost().getLanguage().getID())) {
        myAltFullRange = myOrigDocument.createRangeMarker(shreds.get(0).getHostRangeMarker().getStartOffset(),
                shreds.get(shreds.size() - 1).getHostRangeMarker().getEndOffset());
        myAltFullRange.setGreedyToLeft(true);
        myAltFullRange.setGreedyToRight(true);

        initGuardedBlocks(shreds);
        myInjectedFile = null;
    } else {
        initMarkers(shreds);
        myAltFullRange = null;
        myInjectedFile = injectedFile;
    }
}

From source file:com.intellij.codeInsight.lookup.impl.LookupManagerImpl.java

License:Apache License

public LookupManagerImpl(Project project, MessageBus bus) {
    myProject = project;/*from   w  w  w.  j  a  v  a2s . com*/

    bus.connect().subscribe(EditorHintListener.TOPIC, new EditorHintListener() {
        @Override
        public void hintShown(final Project project, final LightweightHint hint, final int flags) {
            if (project == myProject) {
                Lookup lookup = getActiveLookup();
                if (lookup != null && (flags & HintManager.HIDE_BY_LOOKUP_ITEM_CHANGE) != 0) {
                    lookup.addLookupListener(new LookupAdapter() {
                        @Override
                        public void currentItemChanged(LookupEvent event) {
                            hint.hide();
                        }

                        @Override
                        public void itemSelected(LookupEvent event) {
                            hint.hide();
                        }

                        @Override
                        public void lookupCanceled(LookupEvent event) {
                            hint.hide();
                        }
                    });
                }
            }
        }
    });

    bus.connect().subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
        @Override
        public void enteredDumbMode() {
            hideActiveLookup();
        }

        @Override
        public void exitDumbMode() {
            hideActiveLookup();
        }
    });

    final EditorFactoryAdapter myEditorFactoryListener = new EditorFactoryAdapter() {
        @Override
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            if (event.getEditor() == myActiveLookupEditor) {
                hideActiveLookup();
            }
        }
    };
    EditorFactory.getInstance().addEditorFactoryListener(myEditorFactoryListener, myProject);
}

From source file:com.intellij.codeInsight.template.impl.TemplateManagerImpl.java

License:Apache License

public TemplateManagerImpl(Project project) {
    myProject = project;/*from w  w w . ja va  2s  .  c o m*/
    final EditorFactoryListener myEditorFactoryListener = new EditorFactoryAdapter() {
        @Override
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            Editor editor = event.getEditor();
            if (editor.getProject() != null && editor.getProject() != myProject)
                return;
            if (myProject.isDisposed() || !myProject.isOpen())
                return;
            TemplateState state = getTemplateState(editor);
            if (state != null) {
                state.gotoEnd();
            }
            clearTemplateState(editor);
        }
    };
    EditorFactory.getInstance().addEditorFactoryListener(myEditorFactoryListener, myProject);
}

From source file:com.maddyhome.idea.vim.group.ChangeGroup.java

License:Open Source License

/**
 * Creates the group/*from  w  w w .  j av a2s .c  o m*/
 */
public ChangeGroup() {
    // We want to know when a user clicks the mouse somewhere in the editor so we can clear any
    // saved text for the current insert mode.
    final EventFacade eventFacade = EventFacade.getInstance();

    eventFacade.addEditorFactoryListener(new EditorFactoryAdapter() {
        public void editorCreated(@NotNull EditorFactoryEvent event) {
            final Editor editor = event.getEditor();
            eventFacade.addEditorMouseListener(editor, listener);
            EditorData.setChangeGroup(editor, true);
        }

        public void editorReleased(@NotNull EditorFactoryEvent event) {
            final Editor editor = event.getEditor();
            if (EditorData.getChangeGroup(editor)) {
                eventFacade.removeEditorMouseListener(editor, listener);
                EditorData.setChangeGroup(editor, false);
            }
        }

        @NotNull
        private final EditorMouseAdapter listener = new EditorMouseAdapter() {
            public void mouseClicked(@NotNull EditorMouseEvent event) {
                Editor editor = event.getEditor();
                if (!VimPlugin.isEnabled()) {
                    return;
                }

                if (CommandState.inInsertMode(editor)) {
                    clearStrokes(editor);
                }
            }
        };
    }, ApplicationManager.getApplication());
}

From source file:com.maddyhome.idea.vim.group.EditorGroup.java

License:Open Source License

public EditorGroup() {
    final Options options = Options.getInstance();
    final OptionChangeListener numbersChangeListener = new OptionChangeListener() {
        @Override//  w w w.  j a  v  a2s .c  o  m
        public void valueChange(OptionChangeEvent event) {
            for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
                updateLineNumbers(editor);
            }
        }
    };
    options.getOption(Options.NUMBER).addOptionChangeListener(numbersChangeListener);
    options.getOption(Options.RELATIVE_NUMBER).addOptionChangeListener(numbersChangeListener);

    EventFacade.getInstance().addEditorFactoryListener(new EditorFactoryAdapter() {
        @Override
        public void editorCreated(@NotNull EditorFactoryEvent event) {
            final Editor editor = event.getEditor();
            isBlockCursor = editor.getSettings().isBlockCursor();
            isAnimatedScrolling = editor.getSettings().isAnimatedScrolling();
            isRefrainFromScrolling = editor.getSettings().isRefrainFromScrolling();
            EditorData.initializeEditor(editor);
            DocumentManager.getInstance().addListeners(editor.getDocument());
            VimPlugin.getKey().registerRequiredShortcutKeys(editor);

            if (VimPlugin.isEnabled()) {
                initLineNumbers(editor);
                // Turn on insert mode if editor doesn't have any file
                if (!EditorData.isFileEditor(editor) && editor.getDocument().isWritable()
                        && !CommandState.inInsertMode(editor)) {
                    KeyHandler.getInstance().handleKey(editor, KeyStroke.getKeyStroke('i'),
                            new EditorDataContext(editor));
                }
                editor.getSettings().setBlockCursor(!CommandState.inInsertMode(editor));
                editor.getSettings().setAnimatedScrolling(ANIMATED_SCROLLING_VIM_VALUE);
                editor.getSettings().setRefrainFromScrolling(REFRAIN_FROM_SCROLLING_VIM_VALUE);
            }
        }

        @Override
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            final Editor editor = event.getEditor();
            deinitLineNumbers(editor);
            EditorData.unInitializeEditor(editor);
            VimPlugin.getKey().unregisterShortcutKeys(editor);
            editor.getSettings().setAnimatedScrolling(isAnimatedScrolling);
            editor.getSettings().setRefrainFromScrolling(isRefrainFromScrolling);
            DocumentManager.getInstance().removeListeners(editor.getDocument());
        }
    }, ApplicationManager.getApplication());
}

From source file:com.maddyhome.idea.vim.group.MarkGroup.java

License:Open Source License

/**
 * Creates the class/*from w  w  w  .  j a v  a2s . co  m*/
 */
public MarkGroup() {
    EventFacade.getInstance().addEditorFactoryListener(new EditorFactoryAdapter() {
        public void editorReleased(@NotNull EditorFactoryEvent event) {
            // Save off the last caret position of the file before it is closed
            Editor editor = event.getEditor();
            setMark(editor, '"', editor.getCaretModel().getOffset());
        }
    }, ApplicationManager.getApplication());
}

From source file:com.maddyhome.idea.vim.group.MotionGroup.java

License:Open Source License

/**
 * Create the group/* w  w w.j  a v a2  s . c  o  m*/
 */
public MotionGroup() {
    EventFacade.getInstance().addEditorFactoryListener(new EditorFactoryAdapter() {
        public void editorCreated(@NotNull EditorFactoryEvent event) {
            final Editor editor = event.getEditor();
            // This ridiculous code ensures that a lot of events are processed BEFORE we finally start listening
            // to visible area changes. The primary reason for this change is to fix the cursor position bug
            // using the gd and gD commands (Goto Declaration). This bug has been around since Idea 6.0.4?
            // Prior to this change the visible area code was moving the cursor around during file load and messing
            // with the cursor position of the Goto Declaration processing.
            ApplicationManager.getApplication().invokeLater(new Runnable() {
                public void run() {
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                        public void run() {
                            ApplicationManager.getApplication().invokeLater(new Runnable() {
                                public void run() {
                                    addEditorListener(editor);
                                    EditorData.setMotionGroup(editor, true);
                                }
                            });
                        }
                    });
                }
            });
        }

        public void editorReleased(@NotNull EditorFactoryEvent event) {
            Editor editor = event.getEditor();
            if (EditorData.getMotionGroup(editor)) {
                removeEditorListener(editor);
                EditorData.setMotionGroup(editor, false);
            }
        }
    }, ApplicationManager.getApplication());
}

From source file:com.maddyhome.idea.vim.VimPlugin.java

License:Open Source License

/**
 * Reports statistics about installed IdeaVim and enabled Vim emulation.
 *
 * See https://github.com/go-lang-plugin-org/go-lang-idea-plugin/commit/5182ab4a1d01ad37f6786268a2fe5e908575a217
 *//*from   w  w  w  . j  av  a2 s .  c  o  m*/
private void setupStatisticsReporter(@NotNull EventFacade eventFacade) {
    final Application application = ApplicationManager.getApplication();
    eventFacade.addEditorFactoryListener(new EditorFactoryAdapter() {
        @Override
        public void editorCreated(@NotNull EditorFactoryEvent event) {
            final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
            final long lastUpdate = propertiesComponent.getOrInitLong(IDEAVIM_STATISTICS_TIMESTAMP_KEY, 0);
            final boolean outOfDate = lastUpdate == 0
                    || System.currentTimeMillis() - lastUpdate > TimeUnit.DAYS.toMillis(1);
            if (outOfDate && isEnabled()) {
                application.executeOnPooledThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String buildNumber = ApplicationInfo.getInstance().getBuild().asString();
                            final String pluginId = IDEAVIM_PLUGIN_ID;
                            final String version = URLEncoder.encode(getVersion(), CharsetToolkit.UTF8);
                            final String os = URLEncoder.encode(
                                    SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION, CharsetToolkit.UTF8);
                            final String uid = UpdateChecker
                                    .getInstallationUID(PropertiesComponent.getInstance());
                            final String url = "https://plugins.jetbrains.com/plugins/list" + "?pluginId="
                                    + pluginId + "&build=" + buildNumber + "&pluginVersion=" + version + "&os="
                                    + os + "&uuid=" + uid;
                            PropertiesComponent.getInstance().setValue(IDEAVIM_STATISTICS_TIMESTAMP_KEY,
                                    String.valueOf(System.currentTimeMillis()));
                            HttpRequests.request(url).connect(new HttpRequests.RequestProcessor<Object>() {
                                @Override
                                public Object process(@NotNull HttpRequests.Request request)
                                        throws IOException {
                                    LOG.info("Sending statistics: " + url);
                                    try {
                                        JDOMUtil.load(request.getInputStream());
                                    } catch (JDOMException e) {
                                        LOG.warn(e);
                                    }
                                    return null;
                                }
                            });
                        } catch (IOException e) {
                            LOG.warn(e);
                        }
                    }
                });
            }
        }
    }, application);
}