ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / util / scopeChooser / PackageSetChooserCombo.java
blob129610b1506fc957c88fbcae3fc50ef34d73dda4
1 /*
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.util.scopeChooser;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import com.intellij.packageDependencies.DefaultScopesProvider;
22 import com.intellij.packageDependencies.DependencyValidationManager;
23 import com.intellij.psi.search.scope.packageSet.NamedScope;
24 import com.intellij.psi.search.scope.packageSet.PackageSet;
25 import com.intellij.ui.ComboboxWithBrowseButton;
26 import org.jetbrains.annotations.Nullable;
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.util.Map;
34 public class PackageSetChooserCombo extends ComboboxWithBrowseButton {
35 private final Project myProject;
36 private static final Logger LOG = Logger.getInstance("#" + PackageSetChooserCombo.class.getName());
38 public PackageSetChooserCombo(final Project project, String preselect) {
39 this(project, preselect, true);
42 public PackageSetChooserCombo(final Project project, String preselect, final boolean enableBrowseButton) {
43 final JComboBox combo = getComboBox();
44 combo.setBorder(null);
45 myProject = project;
46 if (enableBrowseButton) {
48 addActionListener(new ActionListener() {
49 public void actionPerformed(ActionEvent e) {
50 final NamedScope scope = (NamedScope)combo.getSelectedItem();
51 if (scope instanceof NamedScope.UnnamedScope) {
52 final Map<String,PackageSet> unnamedScoopes = DependencyValidationManager.getInstance(myProject).getUnnamedScopes();
53 final EditUnnamedScopesDialog dlg = new EditUnnamedScopesDialog(scope);
54 dlg.show();
55 if (dlg.isOK()) {
56 final PackageSet packageSet = scope.getValue();
57 LOG.assertTrue(packageSet != null);
58 unnamedScoopes.remove(packageSet.getText());
59 final PackageSet editedScope = dlg.getScope();
60 if (editedScope != null) {
61 unnamedScoopes.put(editedScope.getText(), editedScope);
63 rebuild();
64 if (editedScope != null) {
65 selectScope(editedScope.getText());
68 } else {
69 final ScopeChooserConfigurable configurable = ScopeChooserConfigurable.getInstance(myProject);
70 final EditScopesDialog dlg = EditScopesDialog.editConfigurable(myProject, new Runnable() {
71 public void run() {
72 configurable.selectNodeInTree(scope.getName());
74 }, true);
75 if (dlg.isOK()){
76 rebuild();
77 final NamedScope namedScope = dlg.getSelectedScope();
78 if (namedScope != null) {
79 selectScope(namedScope.getName());
84 });
85 } else {
86 getButton().setVisible(false);
89 combo.setRenderer(new DefaultListCellRenderer() {
90 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
91 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
92 setText(value == null ? "" : ((NamedScope)value).getName());
93 return this;
95 });
97 rebuild();
99 selectScope(preselect);
102 private void selectScope(String preselect) {
103 final JComboBox combo = getComboBox();
104 if (preselect != null) {
105 DefaultComboBoxModel model = (DefaultComboBoxModel)combo.getModel();
106 for (int i = 0; i < model.getSize(); i++) {
107 NamedScope descriptor = (NamedScope)model.getElementAt(i);
108 if (preselect.equals(descriptor.getName())) {
109 combo.setSelectedIndex(i);
110 break;
116 private void rebuild() {
117 getComboBox().setModel(createModel());
120 private DefaultComboBoxModel createModel() {
121 DependencyValidationManager manager = DependencyValidationManager.getInstance(myProject);
122 final DefaultComboBoxModel model = new DefaultComboBoxModel(manager.getScopes());
123 final Map<String, PackageSet> unnamedScopes = manager.getUnnamedScopes();
124 for (PackageSet unnamedScope : unnamedScopes.values()) {
125 model.addElement(new NamedScope.UnnamedScope(unnamedScope));
127 model.removeElement(DefaultScopesProvider.getInstance(myProject).getProblemsScope());
128 return model;
131 @Nullable
132 public NamedScope getSelectedScope() {
133 JComboBox combo = getComboBox();
134 int idx = combo.getSelectedIndex();
135 if (idx < 0) return null;
136 return (NamedScope)combo.getSelectedItem();
139 private class EditUnnamedScopesDialog extends DialogWrapper {
140 private PackageSet myScope;
141 private final ScopeEditorPanel myPanel;
143 public EditUnnamedScopesDialog(final NamedScope scope) {
144 super(PackageSetChooserCombo.this, false);
145 myScope = scope.getValue();
146 myPanel = new ScopeEditorPanel(myProject, DependencyValidationManager.getInstance(myProject));
147 init();
148 myPanel.reset(myScope, null);
151 @Nullable
152 protected JComponent createCenterPanel() {
153 return myPanel.getPanel();
156 protected void doOKAction() {
157 myScope = myPanel.getCurrentScope();
158 super.doOKAction();
161 public PackageSet getScope() {
162 return myScope;