ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / util / scopeChooser / ScopeConfigurable.java
blob4d495a3775968ec1eb65c8dfc30f856fc407533e
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.
17 package com.intellij.ide.util.scopeChooser;
19 import com.intellij.ide.IdeBundle;
20 import com.intellij.openapi.options.ConfigurationException;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.NamedConfigurable;
23 import com.intellij.openapi.util.Comparing;
24 import com.intellij.packageDependencies.DependencyValidationManager;
25 import com.intellij.psi.search.scope.packageSet.NamedScope;
26 import com.intellij.psi.search.scope.packageSet.NamedScopeManager;
27 import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
28 import com.intellij.psi.search.scope.packageSet.PackageSet;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
37 /**
38 * User: anna
39 * Date: 01-Jul-2006
41 public class ScopeConfigurable extends NamedConfigurable<NamedScope> {
42 private NamedScope myScope;
43 private ScopeEditorPanel myPanel;
44 private String myPackageSet;
45 private final JCheckBox mySharedCheckbox;
46 private boolean myShareScope = false;
47 private final Project myProject;
48 private Icon myIcon;
50 public ScopeConfigurable(final NamedScope scope, final boolean shareScope, final Project project, final Runnable updateTree) {
51 super(true, updateTree);
52 myScope = scope;
53 myShareScope = shareScope;
54 myProject = project;
55 final PackageSet packageSet = scope.getValue();
56 myPackageSet = packageSet != null ? packageSet.getText() : null;
57 mySharedCheckbox = new JCheckBox(IdeBundle.message("share.scope.checkbox.title"), shareScope);
58 myPanel = new ScopeEditorPanel(project){
59 public NamedScopesHolder getHolder() {
60 return ScopeConfigurable.this.getHolder();
63 myIcon = getHolder(myShareScope).getIcon();
64 mySharedCheckbox.addActionListener(new ActionListener() {
65 public void actionPerformed(final ActionEvent e) {
66 myIcon = getHolder().getIcon();
68 });
71 public void setDisplayName(final String name) {
72 if (Comparing.strEqual(myScope.getName(), name)){
73 return;
75 final PackageSet packageSet = myScope.getValue();
76 myScope = new NamedScope(name, packageSet != null ? packageSet.createCopy() : null);
79 public NamedScope getEditableObject() {
80 return new NamedScope(myScope.getName(), myPanel.getCurrentScope());
83 public String getBannerSlogan() {
84 return IdeBundle.message("scope.banner.text", myScope.getName());
87 public String getDisplayName() {
88 return myScope.getName();
91 public Icon getIcon() {
92 return myIcon;
95 public NamedScopesHolder getHolder() {
96 return getHolder(mySharedCheckbox.isSelected());
99 private NamedScopesHolder getHolder(boolean local) {
100 return (NamedScopesHolder)(local
101 ? DependencyValidationManager.getInstance(myProject)
102 : NamedScopeManager.getInstance(myProject));
105 @Nullable
106 @NonNls
107 public String getHelpTopic() {
108 return "project.scopes";
111 public JComponent createOptionsPanel() {
112 final JPanel wholePanel = new JPanel(new BorderLayout());
113 wholePanel.add(myPanel.getPanel(), BorderLayout.CENTER);
114 wholePanel.add(mySharedCheckbox, BorderLayout.SOUTH);
115 return wholePanel;
118 public boolean isModified() {
119 if (mySharedCheckbox.isSelected() != myShareScope) return true;
120 final PackageSet currentScope = myPanel.getCurrentScope();
121 return !Comparing.strEqual(myPackageSet, currentScope != null ? currentScope.getText() : null);
124 public void apply() throws ConfigurationException {
125 try {
126 myPanel.apply();
127 final PackageSet packageSet = myPanel.getCurrentScope();
128 myScope = new NamedScope(myScope.getName(), packageSet);
129 myPackageSet = packageSet != null ? packageSet.getText() : null;
130 myShareScope = mySharedCheckbox.isSelected();
132 catch (ConfigurationException e) {
133 //was canceled - didn't change anything
137 public void reset() {
138 mySharedCheckbox.setSelected(myShareScope);
139 myPanel.reset(myScope.getValue(), null);
142 public void disposeUIResources() {
143 if (myPanel != null){
144 myPanel.cancelCurrentProgress();
145 myPanel.clearCaches();
146 myPanel = null;
150 public void cancelCurrentProgress(){
151 if (myPanel != null) { //not disposed
152 myPanel.cancelCurrentProgress();
156 public NamedScope getScope() {
157 return myScope;
160 public void restoreCanceledProgress() {
161 if (myPanel != null) {
162 myPanel.restoreCanceledProgress();