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
.openapi
.actionSystem
.*;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.openapi
.fileEditor
.ex
.FileEditorManagerEx
;
21 import com
.intellij
.openapi
.fileEditor
.impl
.EditorWindow
;
22 import com
.intellij
.openapi
.project
.DumbAware
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.openapi
.wm
.ToolWindowManager
;
26 import com
.intellij
.ui
.content
.ContentManager
;
27 import com
.intellij
.util
.ArrayUtil
;
29 abstract class TabNavigationActionBase
extends AnAction
implements DumbAware
{
30 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ide.actions.TabNavigationActionBase");
32 private final int myDir
;
34 TabNavigationActionBase (final int dir
) {
35 LOG
.assertTrue (dir
== 1 || dir
== -1);
39 public void actionPerformed(AnActionEvent e
) {
40 DataContext dataContext
= e
.getDataContext();
41 Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
42 if (project
== null) {
46 ToolWindowManager windowManager
= ToolWindowManager
.getInstance(project
);
48 if (windowManager
.isEditorComponentActive()) {
49 doNavigate(dataContext
, project
);
53 ContentManager contentManager
= PlatformDataKeys
.NONEMPTY_CONTENT_MANAGER
.getData(dataContext
);
54 if (contentManager
== null) return;
55 doNavigate(contentManager
);
58 public void update(AnActionEvent event
){
59 Presentation presentation
= event
.getPresentation();
60 DataContext dataContext
= event
.getDataContext();
61 Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
62 presentation
.setEnabled(false);
63 if (project
== null) {
66 final ToolWindowManager windowManager
= ToolWindowManager
.getInstance(project
);
67 if (windowManager
.isEditorComponentActive()) {
68 final FileEditorManagerEx editorManager
= FileEditorManagerEx
.getInstanceEx(project
);
69 EditorWindow currentWindow
= EditorWindow
.DATA_KEY
.getData(dataContext
);
70 if (currentWindow
== null){
71 editorManager
.getCurrentWindow ();
73 if (currentWindow
!= null) {
74 final VirtualFile
[] files
= currentWindow
.getFiles();
75 presentation
.setEnabled(files
.length
> 1);
80 ContentManager contentManager
= PlatformDataKeys
.NONEMPTY_CONTENT_MANAGER
.getData(dataContext
);
81 presentation
.setEnabled(contentManager
!= null && contentManager
.getContentCount() > 1 && contentManager
.isSingleSelection());
84 private void doNavigate(ContentManager contentManager
) {
86 contentManager
.selectPreviousContent();
89 contentManager
.selectNextContent();
93 private void doNavigate(DataContext dataContext
, Project project
) {
94 VirtualFile selectedFile
= PlatformDataKeys
.VIRTUAL_FILE
.getData(dataContext
);
95 navigateImpl(dataContext
, project
, selectedFile
, myDir
);
98 public static void navigateImpl(final DataContext dataContext
, Project project
, VirtualFile selectedFile
, final int dir
) {
99 LOG
.assertTrue (dir
== 1 || dir
== -1);
100 final FileEditorManagerEx editorManager
= FileEditorManagerEx
.getInstanceEx(project
);
101 EditorWindow currentWindow
= EditorWindow
.DATA_KEY
.getData(dataContext
);
102 if (currentWindow
== null){
103 currentWindow
= editorManager
.getCurrentWindow ();
105 final VirtualFile
[] files
= currentWindow
.getFiles();
106 int index
= ArrayUtil
.find(files
, selectedFile
);
107 LOG
.assertTrue(index
!= -1);
108 editorManager
.openFile(files
[(index
+ files
.length
+ dir
) % files
.length
], true);