git4idea: Added notnull annotations for rebase operations
[fedora-idea.git] / plugins / git4idea / src / git4idea / rebase / GitInteractiveRebaseEditorHandler.java
blob1bf0d34c1917d518db7252bd13aa101b8c4cb0e0
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 git4idea.rebase;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.util.Ref;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.util.ui.UIUtil;
23 import org.jetbrains.annotations.NotNull;
25 import java.io.Closeable;
27 /**
28 * The handler for rebase editor request. The handler shows {@link git4idea.rebase.GitRebaseEditor}
29 * dialog with the specified file. If user accepts the changes, it saves file and returns 0,
30 * otherwise it just returns error code.
32 public class GitInteractiveRebaseEditorHandler implements Closeable, GitRebaseEditorHandler {
33 /**
34 * The logger
36 private final static Logger LOG = Logger.getInstance(GitInteractiveRebaseEditorHandler.class.getName());
37 /**
38 * The service object that has created this handler
40 private final GitRebaseEditorService myService;
41 /**
42 * The context project
44 private final Project myProject;
45 /**
46 * The git repository root
48 private final VirtualFile myRoot;
49 /**
50 * The handler number
52 private int myHandlerNo;
53 /**
54 * If true, the handler has been closed
56 private boolean myIsClosed;
57 /**
58 * Set to true after rebase editor was shown
60 protected boolean myRebaseEditorShown = false;
62 /**
63 * The constructor from fields that is expected to be
64 * accessed only from {@link git4idea.rebase.GitRebaseEditorService}.
66 * @param service the service object that has created this handler
67 * @param project the context project
68 * @param root the git repository root
70 public GitInteractiveRebaseEditorHandler(@NotNull final GitRebaseEditorService service,
71 @NotNull final Project project,
72 @NotNull final VirtualFile root) {
73 myService = service;
74 myProject = project;
75 myRoot = root;
76 myHandlerNo = service.registerHandler(this);
79 /**
80 * Edit commits request
82 * @param path the path to editing
83 * @return the exit code to be returned from editor
85 public int editCommits(final String path) {
86 ensureOpen();
87 final Ref<Boolean> isSuccess = new Ref<Boolean>();
88 UIUtil.invokeAndWaitIfNeeded(new Runnable() {
89 public void run() {
90 try {
91 if (myRebaseEditorShown) {
92 GitRebaseUnstructuredEditor editor = new GitRebaseUnstructuredEditor(myProject, myRoot, path);
93 editor.show();
94 if (editor.isOK()) {
95 editor.save();
96 isSuccess.set(true);
97 return;
99 else {
100 isSuccess.set(false);
103 else {
104 setRebaseEditorShown();
105 GitRebaseEditor editor = new GitRebaseEditor(myProject, myRoot, path);
106 editor.show();
107 if (editor.isOK()) {
108 editor.save();
109 isSuccess.set(true);
110 return;
112 else {
113 editor.cancel();
114 isSuccess.set(true);
118 catch (Exception e) {
119 LOG.error("Failed to edit the git rebase file: " + path, e);
121 isSuccess.set(false);
124 return (isSuccess.isNull() || !isSuccess.get().booleanValue()) ? GitRebaseEditorMain.ERROR_EXIT_CODE : 0;
128 * This method is invoked to indicate that this editor will be invoked in the rebase continuation action.
130 public void setRebaseEditorShown() {
131 myRebaseEditorShown = true;
135 * Check that handler has not yet been closed
137 private void ensureOpen() {
138 if (myIsClosed) {
139 throw new IllegalStateException("The handler was already closed");
144 * Stop using the handler
146 public void close() {
147 ensureOpen();
148 myIsClosed = true;
149 myService.unregisterHandler(myHandlerNo);
153 * @return the handler number
155 public int getHandlerNo() {
156 return myHandlerNo;