a5bf21e817a4ae16a0c39634af5b61f3b6599f0b
[idea/community.git] / platform / lang-impl / src / com / intellij / openapi / editor / actions / IncrementalFindAction.java
1 /*
2  * Copyright 2000-2009 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
17 package com.intellij.openapi.editor.actions;
18
19 import com.intellij.execution.impl.ConsoleViewUtil;
20 import com.intellij.find.EditorSearchSession;
21 import com.intellij.find.FindManager;
22 import com.intellij.find.FindModel;
23 import com.intellij.find.FindUtil;
24 import com.intellij.ide.DataManager;
25 import com.intellij.openapi.actionSystem.CommonDataKeys;
26 import com.intellij.openapi.actionSystem.DataContext;
27 import com.intellij.openapi.editor.Editor;
28 import com.intellij.openapi.editor.actionSystem.EditorAction;
29 import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.wm.IdeFocusManager;
32
33 public class IncrementalFindAction extends EditorAction {
34   public static class Handler extends EditorActionHandler {
35
36     private final boolean myReplace;
37
38     public Handler(boolean isReplace) {
39
40       myReplace = isReplace;
41     }
42
43     @Override
44     public void execute(final Editor editor, DataContext dataContext) {
45       final Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editor.getComponent()));
46       if (!editor.isOneLineMode()) {
47         EditorSearchSession search = EditorSearchSession.get(editor);
48         if (search != null) {
49           search.getComponent().requestFocusInTheSearchFieldAndSelectContent(project);
50           FindUtil.configureFindModel(myReplace, editor, search.getFindModel(), false);
51         } else {
52           FindManager findManager = FindManager.getInstance(project);
53           FindModel model;
54           if (myReplace) {
55             model = findManager.createReplaceInFileModel();
56           } else {
57             model = new FindModel();
58             model.copyFrom(findManager.getFindInFileModel());
59           }
60           FindUtil.configureFindModel(myReplace, editor, model, true);
61           EditorSearchSession.start(editor, model, project).getComponent()
62             .requestFocusInTheSearchFieldAndSelectContent(project);
63         }
64       }
65     }
66
67     @Override
68     public boolean isEnabled(Editor editor, DataContext dataContext) {
69       if (myReplace && ConsoleViewUtil.isConsoleViewEditor(editor)) {
70         return false;
71       }
72       Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editor.getComponent()));
73       return project != null && !editor.isOneLineMode();
74     }
75   }
76
77   public IncrementalFindAction() {
78     super(new Handler(false));
79   }
80 }