2 * Copyright 2006 Sascha Weinreuter
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 org
.intellij
.plugins
.xpathView
.search
;
18 import com
.intellij
.ide
.util
.scopeChooser
.ScopeChooserCombo
;
19 import com
.intellij
.openapi
.Disposable
;
20 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptorFactory
;
21 import com
.intellij
.openapi
.module
.Module
;
22 import com
.intellij
.openapi
.module
.ModuleManager
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.ui
.ComboBox
;
25 import com
.intellij
.openapi
.ui
.TextFieldWithBrowseButton
;
26 import com
.intellij
.openapi
.util
.Disposer
;
27 import com
.intellij
.ui
.ComboboxWithBrowseButton
;
28 import com
.intellij
.ui
.DocumentAdapter
;
29 import org
.jetbrains
.annotations
.NotNull
;
30 import org
.jetbrains
.annotations
.Nullable
;
33 import javax
.swing
.event
.DocumentEvent
;
35 import java
.awt
.event
.ItemEvent
;
36 import java
.awt
.event
.ItemListener
;
37 import java
.util
.Arrays
;
38 import java
.util
.Vector
;
40 public class ScopePanel
extends JPanel
implements Disposable
{
42 @SuppressWarnings({ "FieldCanBeLocal", "UnusedDeclaration" })
43 private JPanel myRoot
;
45 private JRadioButton myWholeProjectScope
;
47 private JRadioButton myModuleScope
;
48 private ComboBox myModuleSelection
;
50 private JRadioButton myDirectoryScope
;
51 private TextFieldWithBrowseButton myDirectory
;
52 private JCheckBox myRecursive
;
54 private JRadioButton myCustomScope
;
55 private ComboboxWithBrowseButton myCustomScopeSelection
;
57 private final Project myProject
;
59 public ScopePanel(@NotNull Project project
) {
63 public void initComponent(@Nullable Module currentModule
, final SearchScope scope
) {
64 final ItemListener stateListener
= new ItemListener() {
65 public void itemStateChanged(ItemEvent e
) {
66 myModuleSelection
.setEnabled(myModuleScope
.isSelected());
67 myDirectory
.setEnabled(myDirectoryScope
.isSelected());
68 myRecursive
.setEnabled(myDirectoryScope
.isSelected());
69 myCustomScopeSelection
.setEnabled(myCustomScope
.isSelected());
71 if (e
.getStateChange() == ItemEvent
.SELECTED
) {
72 firePropertyChange("scope", null, getSelectedScope());
76 final ItemListener scopeListener
= new ItemListener() {
77 public void itemStateChanged(ItemEvent e
) {
78 if (e
.getStateChange() == ItemEvent
.SELECTED
) {
79 firePropertyChange("scope", null, getSelectedScope());
84 myWholeProjectScope
.addItemListener(stateListener
);
85 myWholeProjectScope
.setSelected(scope
.getScopeType() == SearchScope
.ScopeType
.PROJECT
);
86 myModuleScope
.addItemListener(stateListener
);
87 myModuleScope
.setSelected(scope
.getScopeType() == SearchScope
.ScopeType
.MODULE
);
88 myDirectoryScope
.addItemListener(stateListener
);
89 myDirectoryScope
.setSelected(scope
.getScopeType() == SearchScope
.ScopeType
.DIRECTORY
);
90 myCustomScope
.addItemListener(stateListener
);
91 myCustomScope
.setSelected(scope
.getScopeType() == SearchScope
.ScopeType
.CUSTOM
);
93 myModuleSelection
.setModel(createModel(ModuleManager
.getInstance(myProject
).getModules()));
94 myModuleSelection
.setRenderer(new DefaultListCellRenderer() {
95 public Component
getListCellRendererComponent(JList list
, Object value
, int index
, boolean isSelected
, boolean cellHasFocus
) {
96 final JLabel l
= (JLabel
)super.getListCellRendererComponent(list
, value
, index
, isSelected
, cellHasFocus
);
98 final Module m
= ((Module
)value
);
99 l
.setIcon(m
.getModuleType().getNodeIcon(true));
100 l
.setText(m
.getName());
107 if (scope
.getModuleName() != null) {
108 if ((m
= ModuleManager
.getInstance(myProject
).findModuleByName(scope
.getModuleName())) == null) {
115 myModuleSelection
.setSelectedItem(m
);
118 myModuleSelection
.addItemListener(scopeListener
);
120 ((ScopeChooserCombo
)myCustomScopeSelection
).init(myProject
, true, true, scope
.getScopeName());
121 myCustomScopeSelection
.getComboBox().addItemListener(scopeListener
);
123 myDirectory
.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
124 protected void textChanged(DocumentEvent e
) {
125 firePropertyChange("scope", null, getSelectedScope());
128 myDirectory
.setText(scope
.getPath());
129 myDirectory
.addBrowseFolderListener("Select Path", "Select Path", myProject
, FileChooserDescriptorFactory
.createSingleFolderDescriptor());
131 myRecursive
.setSelected(scope
.isRecursive());
134 @SuppressWarnings({ "unchecked" })
135 private static ComboBoxModel
createModel(Object
[] elements
) {
136 return new DefaultComboBoxModel(new Vector(Arrays
.asList(elements
)));
139 private void createUIComponents() {
141 myCustomScopeSelection
= new ScopeChooserCombo();
145 private String
getDirectoryName() {
146 final String s
= myDirectory
.getText();
147 return s
!= null && s
.length() > 0 ? s
: null;
151 private String
getModuleName() {
152 final Module module
= ((Module
)myModuleSelection
.getSelectedItem());
153 return module
!= null ? module
.getName() : null;
157 private SearchScope
.ScopeType
getScopeType() {
158 if (myWholeProjectScope
.isSelected()) return SearchScope
.ScopeType
.PROJECT
;
159 if (myModuleScope
.isSelected()) return SearchScope
.ScopeType
.MODULE
;
160 if (myDirectoryScope
.isSelected()) return SearchScope
.ScopeType
.DIRECTORY
;
161 if (myCustomScope
.isSelected()) return SearchScope
.ScopeType
.CUSTOM
;
163 assert false : "Unknown Scope";
167 public SearchScope
getSearchScope() {
168 final SearchScope scope
= getSelectedScope();
169 scope
.setCustomScope(((ScopeChooserCombo
)myCustomScopeSelection
).getSelectedScope());
173 SearchScope
getSelectedScope() {
174 return new SearchScope(getScopeType(),
175 getDirectoryName(), myRecursive
.isSelected(),
177 ((ScopeChooserCombo
)myCustomScopeSelection
).getSelectedScopeName());
180 public void dispose() {
181 Disposer
.dispose(myCustomScopeSelection
);