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
.util
.xml
.ui
.actions
;
19 import com
.intellij
.openapi
.actionSystem
.*;
20 import com
.intellij
.openapi
.application
.ApplicationBundle
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.ui
.popup
.JBPopupFactory
;
23 import com
.intellij
.openapi
.ui
.popup
.ListPopup
;
24 import com
.intellij
.util
.ReflectionUtil
;
25 import com
.intellij
.util
.xml
.*;
26 import com
.intellij
.util
.xml
.reflect
.DomCollectionChildDescription
;
27 import com
.intellij
.util
.xml
.ui
.DomCollectionControl
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
33 import java
.awt
.event
.KeyEvent
;
34 import java
.lang
.reflect
.Type
;
35 import java
.util
.ArrayList
;
36 import java
.util
.List
;
39 * User: Sergey.Vasiliev
41 public abstract class AddDomElementAction
extends AnAction
{
43 private final static ShortcutSet shortcutSet
= new CustomShortcutSet(KeyStroke
.getKeyStroke(KeyEvent
.VK_INSERT
, 0));
45 public AddDomElementAction() {
46 super(ApplicationBundle
.message("action.add"), null, DomCollectionControl
.ADD_ICON
);
49 public void update(AnActionEvent e
) {
51 e
.getPresentation().setEnabled(false);
55 final AnAction
[] actions
= getChildren(e
);
56 for (final AnAction action
: actions
) {
57 e
.getPresentation().setEnabled(true);
59 if (e
.getPresentation().isEnabled()) {
63 if (actions
.length
== 1) {
64 e
.getPresentation().setText(actions
[0].getTemplatePresentation().getText());
66 final String actionText
= getActionText(e
);
67 if (!actionText
.endsWith("...")) {
68 e
.getPresentation().setText(actionText
+ (actions
.length
> 1 ?
"..." : ""));
71 e
.getPresentation().setIcon(DomCollectionControl
.ADD_ICON
);
76 public void actionPerformed(AnActionEvent e
) {
77 final AnAction
[] actions
= getChildren(e
);
78 if (actions
.length
> 1) {
79 final DefaultActionGroup group
= new DefaultActionGroup();
80 for (final AnAction action
: actions
) {
84 final DataContext dataContext
= e
.getDataContext();
85 final ListPopup groupPopup
=
86 JBPopupFactory
.getInstance().createActionGroupPopup(null,//J2EEBundle.message("label.menu.title.add.activation.config.property"),
87 group
, dataContext
, JBPopupFactory
.ActionSelectionAid
.NUMBERING
, true);
89 showPopup(groupPopup
, e
);
91 else if (actions
.length
== 1) {
92 actions
[0].actionPerformed(e
);
96 protected String
getActionText(final AnActionEvent e
) {
97 return e
.getPresentation().getText();
100 protected boolean isEnabled(final AnActionEvent e
) {
104 protected void showPopup(final ListPopup groupPopup
, final AnActionEvent e
) {
105 final Component component
= e
.getInputEvent().getComponent();
107 if (component
instanceof ActionButtonComponent
) {
108 groupPopup
.showUnderneathOf(component
);
110 groupPopup
.showInBestPositionFor(e
.getDataContext());
115 public AnAction
[] getChildren(final AnActionEvent e
) {
116 Project project
= PlatformDataKeys
.PROJECT
.getData(e
.getDataContext());
117 if (project
== null) return AnAction
.EMPTY_ARRAY
;
119 DomCollectionChildDescription
[] descriptions
= getDomCollectionChildDescriptions(e
);
120 final List
<AnAction
> actions
= new ArrayList
<AnAction
>();
121 for (DomCollectionChildDescription description
: descriptions
) {
122 final TypeChooser chooser
= DomManager
.getDomManager(project
).getTypeChooserManager().getTypeChooser(description
.getType());
123 for (Type type
: chooser
.getChooserTypes()) {
125 final Class
<?
> rawType
= ReflectionUtil
.getRawType(type
);
126 String name
= TypeNameManager
.getTypeName(rawType
);
128 if (!showAsPopup() || descriptions
.length
== 1) {
129 // if (descriptions.length > 1) {
130 icon
= ElementPresentationManager
.getIconForClass(rawType
);
133 actions
.add(createAddingAction(e
, ApplicationBundle
.message("action.add") + " " + name
, icon
, type
, description
));
136 if (actions
.size() > 1 && showAsPopup()) {
137 ActionGroup group
= new ActionGroup() {
138 public AnAction
[] getChildren(@Nullable AnActionEvent e
) {
139 return actions
.toArray(AnAction
.EMPTY_ARRAY
);
142 return new AnAction
[]{new ShowPopupAction(group
)};
145 if (actions
.size() > 1) {
146 actions
.add(Separator
.getInstance());
147 } else if (actions
.size() == 1) {
151 return actions
.toArray(AnAction
.EMPTY_ARRAY
);
154 protected abstract AnAction
createAddingAction(final AnActionEvent e
,
158 final DomCollectionChildDescription description
);
161 protected abstract DomCollectionChildDescription
[] getDomCollectionChildDescriptions(final AnActionEvent e
);
164 protected abstract DomElement
getParentDomElement(final AnActionEvent e
);
166 protected abstract JComponent
getComponent(AnActionEvent e
);
168 protected boolean showAsPopup() {
172 protected class ShowPopupAction
extends AnAction
{
174 protected final ActionGroup myGroup
;
176 protected ShowPopupAction(ActionGroup group
) {
177 super(ApplicationBundle
.message("action.add"), null, DomCollectionControl
.ADD_ICON
);
179 setShortcutSet(shortcutSet
);
182 public void actionPerformed(AnActionEvent e
) {
183 final ListPopup groupPopup
=
184 JBPopupFactory
.getInstance().createActionGroupPopup(null,//J2EEBundle.message("label.menu.title.add.activation.config.property"),
185 myGroup
, e
.getDataContext(), JBPopupFactory
.ActionSelectionAid
.NUMBERING
, true);
187 showPopup(groupPopup
, e
);