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
.openapi
.roots
.ui
.configuration
.projectRoot
;
18 import com
.intellij
.facet
.Facet
;
19 import com
.intellij
.facet
.FacetManager
;
20 import com
.intellij
.facet
.FacetType
;
21 import com
.intellij
.facet
.FacetTypeRegistry
;
22 import com
.intellij
.facet
.impl
.autodetecting
.FacetAutodetectingManager
;
23 import com
.intellij
.facet
.impl
.autodetecting
.FacetAutodetectingManagerImpl
;
24 import com
.intellij
.facet
.impl
.ui
.facetType
.FacetTypeEditor
;
25 import com
.intellij
.facet
.ui
.FacetEditor
;
26 import com
.intellij
.facet
.ui
.MultipleFacetSettingsEditor
;
27 import com
.intellij
.ide
.DataManager
;
28 import com
.intellij
.openapi
.actionSystem
.*;
29 import com
.intellij
.openapi
.components
.State
;
30 import com
.intellij
.openapi
.components
.Storage
;
31 import com
.intellij
.openapi
.module
.Module
;
32 import com
.intellij
.openapi
.module
.ModuleManager
;
33 import com
.intellij
.openapi
.options
.ConfigurationException
;
34 import com
.intellij
.openapi
.options
.ShowSettingsUtil
;
35 import com
.intellij
.openapi
.project
.DumbAware
;
36 import com
.intellij
.openapi
.project
.Project
;
37 import com
.intellij
.openapi
.project
.ProjectBundle
;
38 import com
.intellij
.openapi
.roots
.ui
.configuration
.ProjectStructureConfigurable
;
39 import com
.intellij
.openapi
.ui
.DetailsComponent
;
40 import com
.intellij
.openapi
.ui
.NamedConfigurable
;
41 import com
.intellij
.openapi
.util
.IconLoader
;
42 import org
.jetbrains
.annotations
.NotNull
;
43 import org
.jetbrains
.annotations
.Nullable
;
52 name
= "FacetStructureConfigurable.UI",
56 file
= "$WORKSPACE_FILE$"
60 public class FacetStructureConfigurable
extends BaseStructureConfigurable
{
61 private static final Icon ICON
= IconLoader
.getIcon("/modules/modules.png");//todo[nik] use facets icon
62 private final ModuleManager myModuleManager
;
63 private final Map
<FacetType
<?
, ?
>, FacetTypeEditor
> myFacetTypeEditors
= new HashMap
<FacetType
<?
,?
>, FacetTypeEditor
>();
64 private MultipleFacetSettingsEditor myCurrentMultipleSettingsEditor
;
66 public FacetStructureConfigurable(final Project project
, ModuleManager moduleManager
) {
68 myModuleManager
= moduleManager
;
71 public static FacetStructureConfigurable
getInstance(final @NotNull Project project
) {
72 return ShowSettingsUtil
.getInstance().findProjectConfigurable(project
, FacetStructureConfigurable
.class);
75 public static boolean isEnabled() {
76 return FacetTypeRegistry
.getInstance().getFacetTypes().length
> 0;
79 protected void loadTree() {
80 myTree
.setRootVisible(false);
81 myTree
.setShowsRootHandles(true);
82 for (FacetType
<?
,?
> facetType
: FacetTypeRegistry
.getInstance().getFacetTypes()) {
83 FacetTypeConfigurable facetTypeConfigurable
= new FacetTypeConfigurable(this, facetType
);
84 MyNode facetTypeNode
= new MyNode(facetTypeConfigurable
);
85 addNode(facetTypeNode
, myRoot
);
87 for (Module module
: myModuleManager
.getModules()) {
88 Collection
<?
extends Facet
> facets
= FacetManager
.getInstance(module
).getFacetsByType(facetType
.getId());
89 for (Facet facet
: facets
) {
90 FacetEditorFacadeImpl editorFacade
= ModuleStructureConfigurable
.getInstance(myProject
).getFacetEditorFacade();
91 FacetConfigurable facetConfigurable
= editorFacade
.getOrCreateConfigurable(facet
);
92 addNode(new FacetConfigurableNode(facetConfigurable
), facetTypeNode
);
99 public FacetTypeEditor
getFacetTypeEditor(@NotNull FacetType
<?
, ?
> facetType
) {
100 return myFacetTypeEditors
.get(facetType
);
103 public FacetTypeEditor
getOrCreateFacetTypeEditor(@NotNull FacetType
<?
, ?
> facetType
) {
104 FacetTypeEditor editor
= myFacetTypeEditors
.get(facetType
);
105 if (editor
== null) {
106 editor
= new FacetTypeEditor(myProject
, myContext
, facetType
);
108 myFacetTypeEditors
.put(facetType
, editor
);
113 public void reset() {
115 myFacetTypeEditors
.clear();
119 public void apply() throws ConfigurationException
{
121 for (FacetTypeEditor editor
: myFacetTypeEditors
.values()) {
124 if (!myProject
.isDefault()) {
125 ((FacetAutodetectingManagerImpl
)FacetAutodetectingManager
.getInstance(myProject
)).redetectFacets();
129 public boolean isModified() {
130 return super.isModified() || isEditorsModified();
133 private boolean isEditorsModified() {
134 for (FacetTypeEditor editor
: myFacetTypeEditors
.values()) {
135 if (editor
.isModified()) {
142 public void disposeUIResources() {
143 super.disposeUIResources();
145 for (FacetTypeEditor editor
: myFacetTypeEditors
.values()) {
146 editor
.disposeUIResources();
148 myFacetTypeEditors
.clear();
152 protected ArrayList
<AnAction
> createActions(final boolean fromPopup
) {
153 ArrayList
<AnAction
> actions
= new ArrayList
<AnAction
>();
155 actions
.add(new MyNavigateAction());
157 actions
.add(new MyRemoveAction());
158 actions
.add(Separator
.getInstance());
159 addCollapseExpandActions(actions
);
163 protected List
<Facet
> removeFacet(final Facet facet
) {
164 List
<Facet
> removed
= super.removeFacet(facet
);
165 ModuleStructureConfigurable
.getInstance(myProject
).removeFacetNodes(removed
);
169 protected boolean updateMultiSelection(final List
<NamedConfigurable
> selectedConfigurables
) {
170 return updateMultiSelection(selectedConfigurables
, getDetailsComponent());
173 public boolean updateMultiSelection(final List
<NamedConfigurable
> selectedConfigurables
, final DetailsComponent detailsComponent
) {
174 FacetType selectedFacetType
= null;
175 List
<FacetEditor
> facetEditors
= new ArrayList
<FacetEditor
>();
176 for (NamedConfigurable selectedConfigurable
: selectedConfigurables
) {
177 if (selectedConfigurable
instanceof FacetConfigurable
) {
178 FacetConfigurable facetConfigurable
= (FacetConfigurable
)selectedConfigurable
;
179 FacetType facetType
= facetConfigurable
.getEditableObject().getType();
180 if (selectedFacetType
!= null && selectedFacetType
!= facetType
) {
183 selectedFacetType
= facetType
;
184 facetEditors
.add(facetConfigurable
.getEditor());
187 if (facetEditors
.size() <= 1 || selectedFacetType
== null) {
191 FacetEditor
[] selectedEditors
= facetEditors
.toArray(new FacetEditor
[facetEditors
.size()]);
192 MultipleFacetSettingsEditor editor
= selectedFacetType
.createMultipleConfigurationsEditor(myProject
, selectedEditors
);
193 if (editor
== null) {
197 setSelectedNode(null);
198 myCurrentMultipleSettingsEditor
= editor
;
199 detailsComponent
.setText(ProjectBundle
.message("multiple.facets.banner.0.1.facets", selectedEditors
.length
,
200 selectedFacetType
.getPresentableName()));
201 detailsComponent
.setContent(editor
.createComponent());
205 protected void updateSelection(@Nullable final NamedConfigurable configurable
) {
206 disposeMultipleSettingsEditor();
207 if (configurable
instanceof FacetTypeConfigurable
) {
208 ((FacetTypeConfigurable
)configurable
).updateComponent();
210 super.updateSelection(configurable
);
213 public void disposeMultipleSettingsEditor() {
214 if (myCurrentMultipleSettingsEditor
!= null) {
215 myCurrentMultipleSettingsEditor
.disposeUIResources();
216 myCurrentMultipleSettingsEditor
= null;
221 protected AbstractAddGroup
createAddAction() {
225 protected void processRemovedItems() {
228 protected boolean wasObjectStored(final Object editableObject
) {
232 public String
getDisplayName() {
233 return ProjectBundle
.message("project.facets.display.name");
236 public Icon
getIcon() {
240 public String
getHelpTopic() {
241 final Object component
= DataManager
.getInstance().getDataContext().getData(DataKeys
.CONTEXT_COMPONENT
.getName());
242 if (myTree
.equals(component
)) {
243 final NamedConfigurable selectedConfugurable
= getSelectedConfugurable();
244 if (selectedConfugurable
instanceof FacetTypeConfigurable
) {
245 final FacetType facetType
= ((FacetTypeConfigurable
)selectedConfugurable
).getEditableObject();
246 final String topic
= facetType
.getHelpTopic();
252 if (myCurrentMultipleSettingsEditor
!= null) {
253 final String topic
= myCurrentMultipleSettingsEditor
.getHelpTopic();
258 String topic
= super.getHelpTopic();
262 return "reference.settingsdialog.project.structure.facet";
265 public String
getId() {
266 return "project.facets";
269 public Runnable
enableSearch(final String option
) {
273 public void dispose() {
276 private class FacetConfigurableNode
extends MyNode
{
277 public FacetConfigurableNode(final FacetConfigurable facetConfigurable
) {
278 super(facetConfigurable
);
282 public String
getDisplayName() {
283 FacetConfigurable facetConfigurable
= (FacetConfigurable
)getConfigurable();
284 String moduleName
= myContext
.getRealName(facetConfigurable
.getEditableObject().getModule());
285 return facetConfigurable
.getDisplayName() + " (" + moduleName
+ ")";
289 private class MyNavigateAction
extends AnAction
implements DumbAware
{
290 private MyNavigateAction() {
291 super(ProjectBundle
.message("action.name.facet.navigate"));
292 registerCustomShortcutSet(CommonShortcuts
.getEditSource(), myTree
);
295 public void update(final AnActionEvent e
) {
296 NamedConfigurable selected
= getSelectedConfugurable();
297 e
.getPresentation().setEnabled(selected
instanceof FacetConfigurable
);
300 public void actionPerformed(final AnActionEvent e
) {
301 NamedConfigurable selected
= getSelectedConfugurable();
302 if (selected
instanceof FacetConfigurable
) {
303 ProjectStructureConfigurable
.getInstance(myProject
).select(((FacetConfigurable
)selected
).getEditableObject(), true);