Example usage for com.intellij.openapi.command.undo UndoManager isUndoInProgress

List of usage examples for com.intellij.openapi.command.undo UndoManager isUndoInProgress

Introduction

In this page you can find the example usage for com.intellij.openapi.command.undo UndoManager isUndoInProgress.

Prototype

public abstract boolean isUndoInProgress();

Source Link

Usage

From source file:com.intellij.codeInsight.completion.XmlTagInsertHandler.java

License:Apache License

public static void insertIncompleteTag(char completionChar, final Editor editor, XmlTag tag) {
    XmlElementDescriptor descriptor = tag.getDescriptor();
    final Project project = editor.getProject();
    TemplateManager templateManager = TemplateManager.getInstance(project);
    Template template = templateManager.createTemplate("", "");

    template.setToIndent(true);/*from  w w w. j  a v a  2s.  c  om*/

    // temp code
    PsiFile containingFile = tag.getContainingFile();
    boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
    template.setToReformat(!htmlCode);

    StringBuilder indirectRequiredAttrs = addRequiredAttributes(descriptor, tag, template, containingFile);
    final boolean chooseAttributeName = addTail(completionChar, descriptor, htmlCode, tag, template,
            indirectRequiredAttrs);

    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
        private RangeMarker myAttrValueMarker;

        @Override
        public void waitingForInput(Template template) {
            int offset = editor.getCaretModel().getOffset();
            myAttrValueMarker = editor.getDocument().createRangeMarker(offset + 1, offset + 4);
        }

        @Override
        public void templateFinished(final Template template, boolean brokenOff) {
            final int offset = editor.getCaretModel().getOffset();

            if (chooseAttributeName && offset > 0) {
                char c = editor.getDocument().getCharsSequence().charAt(offset - 1);
                if (c == '/' || (c == ' ' && brokenOff)) {
                    new WriteCommandAction.Simple(project) {
                        @Override
                        protected void run() throws Throwable {
                            editor.getDocument().replaceString(offset, offset + 3, ">");
                        }
                    }.execute();
                }
            }
        }

        @Override
        public void templateCancelled(final Template template) {
            if (myAttrValueMarker == null) {
                return;
            }

            final UndoManager manager = UndoManager.getInstance(project);
            if (manager.isUndoInProgress() || manager.isRedoInProgress()) {
                return;
            }

            if (chooseAttributeName && myAttrValueMarker.isValid()) {
                final int startOffset = myAttrValueMarker.getStartOffset();
                final int endOffset = myAttrValueMarker.getEndOffset();
                new WriteCommandAction.Simple(project) {
                    @Override
                    protected void run() throws Throwable {
                        editor.getDocument().replaceString(startOffset, endOffset, ">");
                    }
                }.execute();
            }
        }
    });
}

From source file:com.intellij.codeInsight.daemon.impl.PostHighlightingPass.java

License:Apache License

public static void invokeOnTheFlyImportOptimizer(@NotNull final Runnable runnable, @NotNull final PsiFile file,
        @NotNull final Editor editor) {
    final long stamp = editor.getDocument().getModificationStamp();
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override/*ww w .ja v  a2 s .c o  m*/
        public void run() {
            if (file.getProject().isDisposed() || editor.isDisposed()
                    || editor.getDocument().getModificationStamp() != stamp) {
                return;
            }
            //no need to optimize imports on the fly during undo/redo
            final UndoManager undoManager = UndoManager.getInstance(editor.getProject());
            if (undoManager.isUndoInProgress() || undoManager.isRedoInProgress()) {
                return;
            }
            PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments();
            String beforeText = file.getText();
            final long oldStamp = editor.getDocument().getModificationStamp();
            CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
                @Override
                public void run() {
                    ApplicationManager.getApplication().runWriteAction(runnable);
                }
            });
            if (oldStamp != editor.getDocument().getModificationStamp()) {
                String afterText = file.getText();
                if (Comparing.strEqual(beforeText, afterText)) {
                    LOG.error(LogMessageEx.createEvent("Import optimizer  hasn't optimized any imports",
                            file.getViewProvider().getVirtualFile().getPath(),
                            AttachmentFactory.createAttachment(file.getViewProvider().getVirtualFile())));
                }
            }
        }
    });
}

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

License:Apache License

@Override
public void documentChanged(DocumentEvent e) {
    UndoManager undoManager = UndoManager.getInstance(myProject);
    boolean undoOrRedo = undoManager.isUndoInProgress() || undoManager.isRedoInProgress();
    if (undoOrRedo) {
        // allow undo/redo up until 'creation stamp' back in time
        // and check it after action is completed
        if (e.getDocument() == myOrigDocument) {
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(new Runnable() {
                @Override//from  w  ww  .j a v  a  2 s.c om
                public void run() {
                    if (myOrigCreationStamp > myOrigDocument.getModificationStamp()) {
                        closeEditor();
                    }
                }
            });
        }
    } else if (e.getDocument() == myNewDocument) {
        commitToOriginal();
    } else if (e.getDocument() == myOrigDocument) {
        if (myCommittingToOriginal || myAltFullRange != null && myAltFullRange.isValid())
            return;
        closeEditor();
    }
}

From source file:com.intellij.uiDesigner.designSurface.GuiEditor.java

License:Apache License

/**
 * @param file file to be edited//from   w ww  .j a v  a2s .  c  om
 * @throws java.lang.IllegalArgumentException if the <code>file</code>
 *                                            is <code>null</code> or <code>file</code> is not valid PsiFile
 */
public GuiEditor(@NotNull UIFormEditor editor, @NotNull Project project, @NotNull Module module,
        @NotNull VirtualFile file) {
    myEditor = editor;
    LOG.assertTrue(file.isValid());

    myProject = project;
    myModule = module;
    myFile = file;

    myCutCopyPasteSupport = new CutCopyPasteSupport(this);

    setLayout(new BorderLayout());

    myContentSplitter.setDividerWidth(0);
    myContentSplitter.setDividerMouseZoneSize(Registry.intValue("ide.splitter.mouseZone"));
    add(myContentSplitter, BorderLayout.CENTER);

    myValidCard = new JPanel(new BorderLayout());
    myInvalidCard = createInvalidCard();

    myCardPanel.add(myValidCard, CARD_VALID);
    myCardPanel.add(myInvalidCard, CARD_INVALID);

    JPanel contentPanel = new JPanel(new LightFillLayout());
    JLabel toolbar = new JLabel();
    toolbar.setVisible(false);
    contentPanel.add(toolbar);
    contentPanel.add(myCardPanel);

    myContentSplitter.setInnerComponent(contentPanel);

    myListenerList = new EventListenerList();

    myDecorationLayer = new PassiveDecorationLayer(this);
    myDragLayer = new DragLayer(this);

    myLayeredPane = new MyLayeredPane();
    myInplaceEditingLayer = new InplaceEditingLayer(this);
    myLayeredPane.add(myInplaceEditingLayer, LAYER_INPLACE_EDITING);
    myActiveDecorationLayer = new ActiveDecorationLayer(this);
    myLayeredPane.add(myActiveDecorationLayer, LAYER_ACTIVE_DECORATION);
    myGlassLayer = new GlassLayer(this);
    myLayeredPane.add(myGlassLayer, LAYER_GLASS);
    myLayeredPane.add(myDecorationLayer, LAYER_PASSIVE_DECORATION);
    myLayeredPane.add(myDragLayer, LAYER_DND);

    myGlassLayer.addFocusListener(new FocusListener() {
        @Override
        public void focusGained(FocusEvent e) {
            myDecorationLayer.repaint();
            //fireSelectedComponentChanged(); // EA-36478
        }

        @Override
        public void focusLost(FocusEvent e) {
            myDecorationLayer.repaint();
        }
    });

    // Ctrl+W / Ctrl+Shift+W support
    mySelectionState = new SelectionState(this);

    // DeleteProvider
    myDeleteProvider = new MyDeleteProvider();

    // We need to synchronize GUI editor with the document
    final Alarm alarm = new Alarm();
    myDocumentListener = new DocumentAdapter() {
        @Override
        public void documentChanged(final DocumentEvent e) {
            if (!myInsideChange) {
                UndoManager undoManager = UndoManager.getInstance(getProject());
                alarm.cancelAllRequests();
                alarm.addRequest(
                        new MySynchronizeRequest(
                                undoManager.isUndoInProgress() || undoManager.isRedoInProgress()),
                        100/*any arbitrary delay*/, ModalityState.stateForComponent(GuiEditor.this));
            }
        }
    };

    // Prepare document
    myDocument = FileDocumentManager.getInstance().getDocument(file);
    myDocument.addDocumentListener(myDocumentListener);

    // Read form from file
    readFromFile(false);

    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBackground(GridCaptionPanel.getGutterColor());

    myHorzCaptionPanel = new GridCaptionPanel(this, false);
    myVertCaptionPanel = new GridCaptionPanel(this, true);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    gbc.fill = GridBagConstraints.BOTH;
    panel.add(myVertCaptionPanel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    panel.add(myHorzCaptionPanel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;

    myScrollPane = ScrollPaneFactory.createScrollPane(myLayeredPane);
    myScrollPane.setBackground(new JBColor(new NotNullProducer<Color>() {
        @NotNull
        @Override
        public Color produce() {
            return EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground();
        }
    }));
    panel.add(myScrollPane, gbc);
    myHorzCaptionPanel.attachToScrollPane(myScrollPane);
    myVertCaptionPanel.attachToScrollPane(myScrollPane);

    myValidCard.add(panel, BorderLayout.CENTER);

    final CancelCurrentOperationAction cancelCurrentOperationAction = new CancelCurrentOperationAction();
    cancelCurrentOperationAction.registerCustomShortcutSet(CommonShortcuts.ESCAPE, this);

    myProcessor = new MainProcessor(this);

    // PSI listener to restart error highlighter
    myPsiTreeChangeListener = new MyPsiTreeChangeListener();
    PsiManager.getInstance(getProject()).addPsiTreeChangeListener(myPsiTreeChangeListener);

    myQuickFixManager = new QuickFixManagerImpl(this, myGlassLayer, myScrollPane.getViewport());

    myDropTargetListener = new DesignDropTargetListener(this);
    if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
        new DropTarget(getGlassLayer(), DnDConstants.ACTION_COPY_OR_MOVE, myDropTargetListener);
    }

    myActiveDecorationLayer.installSelectionWatcher();

    ActionManager.getInstance().getAction("GuiDesigner.IncreaseIndent").registerCustomShortcutSet(
            new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)), myGlassLayer);
    ActionManager.getInstance().getAction("GuiDesigner.DecreaseIndent").registerCustomShortcutSet(
            new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK)), myGlassLayer);

    UIUtil.invokeLaterIfNeeded(new Runnable() {
        @Override
        public void run() {
            DesignerToolWindowManager.getInstance(myProject).bind(GuiEditor.this);
            PaletteToolWindowManager.getInstance(myProject).bind(GuiEditor.this);
        }
    });
}

From source file:com.intellij.uiDesigner.designSurface.GuiEditor.java

License:Apache License

public boolean isUndoRedoInProgress() {
    UndoManager undoManager = UndoManager.getInstance(getProject());
    return undoManager.isUndoInProgress() || undoManager.isRedoInProgress();
}

From source file:org.jetbrains.idea.svn.SvnFileSystemListener.java

License:Apache License

private static boolean isUndoOrRedo(@NotNull final Project project) {
    final UndoManager undoManager = UndoManager.getInstance(project);
    return undoManager.isUndoInProgress() || undoManager.isRedoInProgress();
}