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
;
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
{
36 private final static Logger LOG
= Logger
.getInstance(GitInteractiveRebaseEditorHandler
.class.getName());
38 * The service object that has created this handler
40 private final GitRebaseEditorService myService
;
44 private final Project myProject
;
46 * The git repository root
48 private final VirtualFile myRoot
;
52 private int myHandlerNo
;
54 * If true, the handler has been closed
56 private boolean myIsClosed
;
58 * Set to true after rebase editor was shown
60 protected boolean myRebaseEditorShown
= false;
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
) {
76 myHandlerNo
= service
.registerHandler(this);
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
) {
87 final Ref
<Boolean
> isSuccess
= new Ref
<Boolean
>();
88 UIUtil
.invokeAndWaitIfNeeded(new Runnable() {
91 if (myRebaseEditorShown
) {
92 GitRebaseUnstructuredEditor editor
= new GitRebaseUnstructuredEditor(myProject
, myRoot
, path
);
100 isSuccess
.set(false);
104 setRebaseEditorShown();
105 GitRebaseEditor editor
= new GitRebaseEditor(myProject
, myRoot
, path
);
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() {
139 throw new IllegalStateException("The handler was already closed");
144 * Stop using the handler
146 public void close() {
149 myService
.unregisterHandler(myHandlerNo
);
153 * @return the handler number
155 public int getHandlerNo() {