IDEA-127739 Navigation Tab
[idea/community.git] / platform / platform-impl / src / com / intellij / ide / actions / SplitAction.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.ide.actions;
17
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.CommonDataKeys;
21 import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
22 import com.intellij.openapi.fileEditor.impl.EditorWindow;
23 import com.intellij.openapi.project.DumbAware;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.vfs.VirtualFile;
26
27 /**
28  * @author Vladimir Kondratyev
29  * @author Konstantin Bulenkov
30  */
31 public abstract class SplitAction extends AnAction implements DumbAware {
32   private final int myOrientation;
33   private final boolean myCloseSource;
34
35   protected SplitAction(final int orientation) {
36     this(orientation, false);
37   }
38
39   protected SplitAction(final int orientation, boolean closeSource) {
40     myOrientation = orientation;
41     myCloseSource = closeSource;
42   }
43
44   public void actionPerformed(final AnActionEvent event) {
45     final Project project = event.getData(CommonDataKeys.PROJECT);
46     final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
47     final EditorWindow window = event.getData(EditorWindow.DATA_KEY);
48     final VirtualFile file = event.getData(CommonDataKeys.VIRTUAL_FILE);
49
50     fileEditorManager.createSplitter(myOrientation, window);
51     
52     if (myCloseSource && window != null && file != null) {
53       window.closeFile(file, false, false);
54     }
55   }
56
57   public void update(final AnActionEvent event) {
58     final Project project = event.getData(CommonDataKeys.PROJECT);
59     final EditorWindow window = event.getData(EditorWindow.DATA_KEY);
60     final int minimum = myCloseSource ? 2 : 1;
61     final boolean enabled = project != null
62                             && window != null
63                             && window.getTabCount() >= minimum
64                             && !window.getOwner().isPreview();
65     event.getPresentation().setEnabled(enabled);
66   }
67 }