fix NPE in UndoRedoAction update
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / UndoRedoAction.java
blob4ebd989e3d52cf124ad8b78f8999d3cc954b022f
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) {
60 final Boolean isModalContext = PlatformDataKeys.IS_MODAL_CONTEXT.getData(dataContext);
61 if (isModalContext != null && isModalContext) {
62 presentation.setEnabled(false);
63 return;
67 UndoManager undoManager = getUndoManager(editor, dataContext);
68 boolean available = isAvailable(editor, undoManager);
69 presentation.setEnabled(available);
70 String actionName = available ? formatAction(editor, undoManager) : null;
71 if (actionName == null) actionName = "";
72 String shortActionName = StringUtil.first(actionName, 30, true);
73 if (actionName.length() == 0) actionName = ActionsBundle.message(getActionDescriptionEmptyMessageKey());
75 presentation.setText(ActionsBundle.message(getActionMessageKey(), shortActionName).trim());
76 presentation.setDescription(ActionsBundle.message(getActionDescriptionMessageKey(), actionName).trim());
79 private static UndoManager getUndoManager(FileEditor editor, DataContext dataContext) {
80 Project project = getProject(editor, dataContext);
81 return project != null ? UndoManager.getInstance(project) : UndoManager.getGlobalInstance();
84 private static Project getProject(FileEditor editor, DataContext dataContext) {
85 Project project;
86 if (editor instanceof TextEditor) {
87 project = ((TextEditor)editor).getEditor().getProject();
89 else {
90 project = PlatformDataKeys.PROJECT.getData(dataContext);
92 return project;
95 protected abstract boolean isAvailable(FileEditor editor, UndoManager undoManager);
97 protected abstract void perform(FileEditor editor, UndoManager undoManager);
99 protected abstract String getActionMessageKey();
101 protected abstract String getActionDescriptionMessageKey();
103 protected abstract String getActionDescriptionEmptyMessageKey();
105 @Nullable
106 protected abstract String formatAction(FileEditor editor, UndoManager undoManager);