08c443ef12c33f45a5386b9c24c5600fe72a0ea6
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / UndoRedoAction.java
blob08c443ef12c33f45a5386b9c24c5600fe72a0ea6
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.actions;
18 import com.intellij.idea.ActionsBundle;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.actionSystem.PlatformDataKeys;
22 import com.intellij.openapi.actionSystem.Presentation;
23 import com.intellij.openapi.command.undo.UndoManager;
24 import com.intellij.openapi.fileEditor.FileEditor;
25 import com.intellij.openapi.fileEditor.TextEditor;
26 import com.intellij.openapi.project.DumbAwareAction;
27 import com.intellij.openapi.project.DumbService;
28 import com.intellij.openapi.project.Project;
29 import com.intellij.openapi.util.text.StringUtil;
30 import org.jetbrains.annotations.Nullable;
32 public abstract class UndoRedoAction extends DumbAwareAction {
33 public UndoRedoAction() {
34 setEnabledInModalContext(true);
37 public void actionPerformed(AnActionEvent e) {
38 DataContext dataContext = e.getDataContext();
39 FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(dataContext);
40 UndoManager undoManager = getUndoManager(editor, dataContext);
41 Project project = getProject(editor, dataContext);
43 if (editor == null && project != null) {
44 if (DumbService.getInstance(project).isDumb()) {
45 DumbService.getInstance(project).showDumbModeNotification("Global Undo and Redo are not available while indices are being built");
46 return;
50 perform(editor, undoManager);
53 public void update(AnActionEvent event) {
54 Presentation presentation = event.getPresentation();
55 DataContext dataContext = event.getDataContext();
56 FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(dataContext);
58 // do not allow global undo in dialogs
59 if (editor == null && PlatformDataKeys.IS_MODAL_CONTEXT.getData(dataContext)) {
60 presentation.setEnabled(false);
61 return;
64 UndoManager undoManager = getUndoManager(editor, dataContext);
65 boolean available = isAvailable(editor, undoManager);
66 presentation.setEnabled(available);
67 String actionName = available ? formatAction(editor, undoManager) : null;
68 if (actionName == null) actionName = "";
69 String shortActionName = StringUtil.first(actionName, 30, true);
70 if (actionName.length() == 0) actionName = ActionsBundle.message(getActionDescriptionEmptyMessageKey());
72 presentation.setText(ActionsBundle.message(getActionMessageKey(), shortActionName).trim());
73 presentation.setDescription(ActionsBundle.message(getActionDescriptionMessageKey(), actionName).trim());
76 private static UndoManager getUndoManager(FileEditor editor, DataContext dataContext) {
77 Project project = getProject(editor, dataContext);
78 return project != null ? UndoManager.getInstance(project) : UndoManager.getGlobalInstance();
81 private static Project getProject(FileEditor editor, DataContext dataContext) {
82 Project project;
83 if (editor instanceof TextEditor) {
84 project = ((TextEditor)editor).getEditor().getProject();
86 else {
87 project = PlatformDataKeys.PROJECT.getData(dataContext);
89 return project;
92 protected abstract boolean isAvailable(FileEditor editor, UndoManager undoManager);
94 protected abstract void perform(FileEditor editor, UndoManager undoManager);
96 protected abstract String getActionMessageKey();
98 protected abstract String getActionDescriptionMessageKey();
100 protected abstract String getActionDescriptionEmptyMessageKey();
102 @Nullable
103 protected abstract String formatAction(FileEditor editor, UndoManager undoManager);