'Show project structure' actions moved to idea-ui module
[idea/community.git] / platform / platform-impl / src / com / intellij / ide / actions / ShowSettingsUtilImpl.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 package com.intellij.ide.actions;
17
18 import com.intellij.ide.util.PropertiesComponent;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.options.Configurable;
22 import com.intellij.openapi.options.ConfigurableGroup;
23 import com.intellij.openapi.options.ShowSettingsUtil;
24 import com.intellij.openapi.options.ex.*;
25 import com.intellij.openapi.options.newEditor.OptionsEditorDialog;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.ProjectManager;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import java.awt.*;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37  * @author max
38  */
39 public class ShowSettingsUtilImpl extends ShowSettingsUtil {
40   private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.ShowSettingsUtilImpl");
41   @NonNls
42   private static final String PREFER_CLASSIC_OPTIONS_EDITOR = "PREFER_CLASSIC_OPTIONS_EDITOR";
43
44   public void showSettingsDialog(Project project, ConfigurableGroup[] group) {
45     _showSettingsDialog(project, group, null);
46   }
47
48   private void _showSettingsDialog(final Project project, ConfigurableGroup[] group, Configurable toSelect) {
49     group = filterEmptyGroups(group);
50
51     if ("false".equalsIgnoreCase(System.getProperty("new.options.editor"))) {
52       if (Boolean.toString(true).equals(PropertiesComponent.getInstance().getValue(PREFER_CLASSIC_OPTIONS_EDITOR))) {
53         showExplorerOptions(project, group);
54       }
55       else {
56         showControlPanelOptions(project, group, toSelect);
57       }
58     } else {
59       new OptionsEditorDialog(project, group, toSelect).show();
60     }
61   }
62
63   public void showSettingsDialog(@Nullable final Project project, final Class configurableClass) {
64     assert Configurable.class.isAssignableFrom(configurableClass) : "Not a configurable: " + configurableClass.getName();
65
66     Project actualProject = project != null ? project  : ProjectManager.getInstance().getDefaultProject();
67     Configurable config = (Configurable)actualProject.getComponent(configurableClass);
68     if (config == null) {
69       config = (Configurable)ApplicationManager.getApplication().getComponent(configurableClass);
70     }
71
72     assert config != null : "Cannot find configurable: " + configurableClass.getName();
73
74     showSettingsDialog(actualProject, config);
75   }
76
77   public void showSettingsDialog(@Nullable final Project project, final @NotNull String nameToSelect) {
78     ConfigurableGroup[] group;
79     if (project == null) {
80       group = new ConfigurableGroup[] {new IdeConfigurablesGroup()};
81     } else {
82       group = new ConfigurableGroup[] {new ProjectConfigurablesGroup(project, false), new IdeConfigurablesGroup()};
83     }
84
85     Project actualProject = project != null ? project  : ProjectManager.getInstance().getDefaultProject();
86
87     group = filterEmptyGroups(group);
88
89     OptionsEditorDialog dialog = new OptionsEditorDialog(actualProject, group, nameToSelect);
90     dialog.show();
91
92   }
93
94   public void showSettingsDialog(@NotNull final Project project, final Configurable toSelect) {
95     _showSettingsDialog(project, new ConfigurableGroup[]{
96       new ProjectConfigurablesGroup(project, false),
97       new IdeConfigurablesGroup()
98     }, toSelect);
99   }
100
101   private static ConfigurableGroup[] filterEmptyGroups(final ConfigurableGroup[] group) {
102     List<ConfigurableGroup> groups = new ArrayList<ConfigurableGroup>();
103     for (ConfigurableGroup g : group) {
104       if (g.getConfigurables().length > 0) {
105         groups.add(g);
106       }
107     }
108     return groups.toArray(new ConfigurableGroup[groups.size()]);
109   }
110
111   public static void showControlPanelOptions(Project project, ConfigurableGroup[] groups, Configurable preselectedConfigurable) {
112     PropertiesComponent.getInstance().setValue(PREFER_CLASSIC_OPTIONS_EDITOR, Boolean.toString(false));
113
114     ControlPanelSettingsEditor editor = new ControlPanelSettingsEditor(project, groups, preselectedConfigurable);
115     editor.show();
116   }
117
118   public static void showExplorerOptions(Project project, ConfigurableGroup[] group) {
119     PropertiesComponent.getInstance().setValue(PREFER_CLASSIC_OPTIONS_EDITOR, Boolean.toString(true));
120     ExplorerSettingsEditor editor = new ExplorerSettingsEditor(project, group);
121     editor.show();
122   }
123
124   public boolean editConfigurable(Project project, Configurable configurable) {
125     final SingleConfigurableEditor configurableEditor = new SingleConfigurableEditor(project, configurable, createDimensionKey(configurable));
126     configurableEditor.show();
127     return configurableEditor.isOK();
128   }
129
130   public <T extends Configurable> T findApplicationConfigurable(final Class<T> confClass) {
131     return selectConfigurable(confClass, ApplicationManager.getApplication().getExtensions(Configurable.APPLICATION_CONFIGURABLES));
132   }
133
134   public <T extends Configurable> T findProjectConfigurable(final Project project, final Class<T> confClass) {
135     return selectConfigurable(confClass, project.getExtensions(Configurable.PROJECT_CONFIGURABLES));
136   }
137
138   private static <T extends Configurable> T selectConfigurable(final Class<T> confClass, final Configurable... configurables) {
139     for (Configurable configurable : configurables) {
140       if (confClass.isAssignableFrom(configurable.getClass())) return (T)configurable;
141     }
142
143     throw new IllegalStateException("Can't find configurable of class " + confClass.getName());
144   }
145
146   public boolean editConfigurable(Project project, String dimensionServiceKey, Configurable configurable) {
147     final SingleConfigurableEditor configurableEditor = new SingleConfigurableEditor(project, configurable, dimensionServiceKey);
148     configurableEditor.show();
149     return configurableEditor.isOK();
150   }
151
152   @Override
153   public boolean editProjectConfigurable(@NotNull Project project,
154                                          Class<? extends Configurable> configurableClass,
155                                          @NonNls String dimensionServiceKey) {
156     final Configurable configurable = findProjectConfigurable(project, configurableClass);
157     if (configurable == null) {
158       LOG.error("Cannot find project configurable for " + configurableClass);
159       return false;
160     }
161     if (dimensionServiceKey == null) {
162       dimensionServiceKey = createDimensionKey(configurable);
163     }
164     return editConfigurable(project, dimensionServiceKey, configurable);
165   }
166
167   public boolean editConfigurable(Component parent, Configurable configurable) {
168     final SingleConfigurableEditor configurableEditor = new SingleConfigurableEditor(parent, configurable, createDimensionKey(configurable));
169     configurableEditor.show();
170     return configurableEditor.isOK();
171   }
172
173   public boolean editConfigurable(final Component parent, final Configurable configurable, final Runnable advancedInitialization) {
174     SingleConfigurableEditor editor = new SingleConfigurableEditor(parent, configurable, createDimensionKey(configurable));
175     advancedInitialization.run();
176     editor.show();
177     return editor.isOK();
178   }
179
180   private static String createDimensionKey(Configurable configurable) {
181     String displayName = configurable.getDisplayName();
182     displayName = displayName.replaceAll("\n", "_").replaceAll(" ", "_");
183     return "#" + displayName;
184   }
185
186   public boolean editConfigurable(Component parent, String dimensionServiceKey,Configurable configurable) {
187     final SingleConfigurableEditor configurableEditor = new SingleConfigurableEditor(parent, configurable, dimensionServiceKey);
188     configurableEditor.show();
189     return configurableEditor.isOK();
190   }
191
192   public boolean editConfigurable(Project project, Configurable configurable, Runnable advancedInitialization) {
193     SingleConfigurableEditor editor = new SingleConfigurableEditor(project, configurable, createDimensionKey(configurable));
194     advancedInitialization.run();
195     editor.show();
196     return editor.isOK();
197   }
198 }