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
.facet
.impl
.ui
.actions
;
19 import com
.intellij
.facet
.FacetInfo
;
20 import com
.intellij
.facet
.FacetType
;
21 import com
.intellij
.facet
.FacetTypeId
;
22 import com
.intellij
.facet
.impl
.ui
.FacetEditorFacade
;
23 import com
.intellij
.openapi
.actionSystem
.AnAction
;
24 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
25 import com
.intellij
.openapi
.diagnostic
.Logger
;
26 import com
.intellij
.openapi
.module
.ModuleType
;
27 import com
.intellij
.openapi
.project
.DumbAware
;
29 import java
.util
.Collection
;
34 public class AddFacetAction
extends AnAction
implements DumbAware
{
35 private static final Logger LOG
= Logger
.getInstance("#com.intellij.facet.impl.ui.actions.AddFacetAction");
36 private final FacetEditorFacade myEditor
;
37 private final FacetType myType
;
39 public AddFacetAction(final FacetEditorFacade editor
, final FacetType type
) {
40 super(type
.getPresentableName(), null, type
.getIcon());
45 public void actionPerformed(AnActionEvent e
) {
46 FacetInfo parent
= myEditor
.getSelectedFacetInfo();
47 final Collection
<FacetInfo
> facetInfos
= myEditor
.getFacetsByType(myType
);
48 String facetName
= myType
.getDefaultFacetName();
50 while (facetExists(facetName
, facetInfos
)) {
51 facetName
= myType
.getPresentableName() + i
;
54 final FacetTypeId
<?
> underlyingFacetType
= myType
.getUnderlyingFacetType();
55 if (parent
== null && underlyingFacetType
== null || parent
!= null && parent
.getFacetType().getId() == underlyingFacetType
) {
56 myEditor
.createFacet(parent
, myType
, facetName
);
59 LOG
.assertTrue(parent
!= null);
60 final FacetInfo grandParent
= myEditor
.getParent(parent
);
61 LOG
.assertTrue(grandParent
== null && underlyingFacetType
== null ||
62 grandParent
!= null && grandParent
.getFacetType().getId() == underlyingFacetType
);
63 myEditor
.createFacet(grandParent
, myType
, facetName
);
67 private static boolean facetExists(final String facetName
, final Collection
<FacetInfo
> facetInfos
) {
68 for (FacetInfo facetInfo
: facetInfos
) {
69 if (facetInfo
.getName().equals(facetName
)) {
76 public void update(AnActionEvent e
) {
77 e
.getPresentation().setVisible(isVisible(myEditor
, myType
));
80 public static boolean isVisible(FacetEditorFacade editor
, final FacetType
<?
, ?
> type
) {
81 final ModuleType moduleType
= editor
.getSelectedModuleType();
82 if (moduleType
== null || !type
.isSuitableModuleType(moduleType
)) {
86 final FacetTypeId
<?
> underlyingTypeId
= type
.getUnderlyingFacetType();
87 final FacetInfo selectedFacet
= editor
.getSelectedFacetInfo();
88 if (selectedFacet
== null) {
89 return underlyingTypeId
== null && canAddFacet(null, type
, editor
);
92 final FacetTypeId selectedFacetType
= selectedFacet
.getFacetType().getId();
93 if (selectedFacetType
== underlyingTypeId
) {
94 return canAddFacet(selectedFacet
, type
, editor
);
97 final FacetInfo parent
= editor
.getParent(selectedFacet
);
98 if (!canAddFacet(parent
, type
, editor
)) {
101 return parent
== null && underlyingTypeId
== null || parent
!= null && parent
.getFacetType().getId() == underlyingTypeId
;
104 private static boolean canAddFacet(final FacetInfo selectedFacet
, final FacetType
<?
, ?
> type
, final FacetEditorFacade editor
) {
105 return !(type
.isOnlyOneFacetAllowed() && editor
.nodeHasFacetOfType(selectedFacet
, type
.getId()));