update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / tree / actions / AddElementInCollectionAction.java
blobf8d9c21d8897d841b9d1d6ebd11868b1d7e20769
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.util.xml.tree.actions;
19 import com.intellij.openapi.actionSystem.AnAction;
20 import com.intellij.openapi.actionSystem.AnActionEvent;
21 import com.intellij.openapi.actionSystem.DefaultActionGroup;
22 import com.intellij.openapi.application.ApplicationBundle;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.ui.popup.ListPopup;
25 import com.intellij.psi.xml.XmlFile;
26 import com.intellij.ui.treeStructure.SimpleNode;
27 import com.intellij.util.ReflectionUtil;
28 import com.intellij.util.xml.DomElement;
29 import com.intellij.util.xml.MergedObject;
30 import com.intellij.util.xml.TypeNameManager;
31 import com.intellij.util.xml.DomUtil;
32 import com.intellij.util.xml.reflect.DomCollectionChildDescription;
33 import com.intellij.util.xml.tree.BaseDomElementNode;
34 import com.intellij.util.xml.tree.DomElementsGroupNode;
35 import com.intellij.util.xml.tree.DomModelTreeView;
36 import com.intellij.util.xml.ui.actions.AddDomElementAction;
37 import com.intellij.util.xml.ui.actions.DefaultAddAction;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
41 import javax.swing.*;
42 import java.lang.reflect.Type;
43 import java.util.List;
45 /**
46 * User: Sergey.Vasiliev
48 public class AddElementInCollectionAction extends AddDomElementAction {
49 private DomModelTreeView myTreeView;
51 public AddElementInCollectionAction() {
54 public AddElementInCollectionAction(final DomModelTreeView treeView) {
55 myTreeView = treeView;
58 protected DomModelTreeView getTreeView(AnActionEvent e) {
59 if (myTreeView != null) return myTreeView;
61 return (DomModelTreeView)e.getDataContext().getData(DomModelTreeView.DOM_MODEL_TREE_VIEW_KEY);
64 protected boolean isEnabled(final AnActionEvent e) {
65 final DomModelTreeView treeView = getTreeView(e);
67 final boolean enabled = treeView != null;
68 e.getPresentation().setEnabled(enabled);
70 return enabled;
74 protected void showPopup(final ListPopup groupPopup, final AnActionEvent e) {
75 if (myTreeView == null) {
76 if (e.getPlace().equals(DomModelTreeView.DOM_MODEL_TREE_VIEW_POPUP)) {
77 groupPopup.showInCenterOf(getTreeView(e).getTree());
79 else {
80 groupPopup.showInBestPositionFor(e.getDataContext());
83 else {
84 super.showPopup(groupPopup, e);
88 @NotNull
89 protected DomCollectionChildDescription[] getDomCollectionChildDescriptions(final AnActionEvent e) {
90 final DomModelTreeView view = getTreeView(e);
92 SimpleNode node = view.getTree().getSelectedNode();
93 if (node instanceof BaseDomElementNode) {
94 List<DomCollectionChildDescription> consolidated = ((BaseDomElementNode)node).getConsolidatedChildrenDescriptions();
95 if (consolidated.size() > 0) {
96 return consolidated.toArray(DomCollectionChildDescription.EMPTY_ARRAY);
100 final DomElementsGroupNode groupNode = getDomElementsGroupNode(view);
102 return groupNode == null
103 ? DomCollectionChildDescription.EMPTY_ARRAY
104 : new DomCollectionChildDescription[]{groupNode.getChildDescription()};
107 protected DomElement getParentDomElement(final AnActionEvent e) {
108 final DomModelTreeView view = getTreeView(e);
109 SimpleNode node = view.getTree().getSelectedNode();
110 if (node instanceof BaseDomElementNode) {
111 if (((BaseDomElementNode)node).getConsolidatedChildrenDescriptions().size() > 0) {
112 return ((BaseDomElementNode)node).getDomElement();
115 final DomElementsGroupNode groupNode = getDomElementsGroupNode(view);
117 return groupNode == null ? null : groupNode.getDomElement();
120 protected JComponent getComponent(AnActionEvent e) {
121 return getTreeView(e);
124 protected boolean showAsPopup() {
125 return true;
128 protected String getActionText(final AnActionEvent e) {
129 String text = ApplicationBundle.message("action.add");
130 if (e.getPresentation().isEnabled()) {
131 final DomElementsGroupNode selectedNode = getDomElementsGroupNode(getTreeView(e));
132 if (selectedNode != null) {
133 final Type type = selectedNode.getChildDescription().getType();
134 text += " " + TypeNameManager.getTypeName(ReflectionUtil.getRawType(type));
137 return text;
140 @Nullable
141 private static DomElementsGroupNode getDomElementsGroupNode(final DomModelTreeView treeView) {
142 SimpleNode simpleNode = treeView.getTree().getSelectedNode();
143 while (simpleNode != null) {
144 if (simpleNode instanceof DomElementsGroupNode) return (DomElementsGroupNode)simpleNode;
146 simpleNode = simpleNode.getParent();
148 return null;
152 protected AnAction createAddingAction(final AnActionEvent e,
153 final String name,
154 final Icon icon,
155 final Type type,
156 final DomCollectionChildDescription description) {
158 final DomElement parentDomElement = getParentDomElement(e);
160 if (parentDomElement instanceof MergedObject) {
161 final List<DomElement> implementations = (List<DomElement>)((MergedObject)parentDomElement).getImplementations();
162 final DefaultActionGroup actionGroup = new DefaultActionGroup(name, true);
164 for (DomElement implementation : implementations) {
165 final XmlFile xmlFile = DomUtil.getFile(implementation);
166 actionGroup.add(new MyDefaultAddAction(implementation, xmlFile.getName(), xmlFile.getIcon(0), e, type, description));
168 return actionGroup;
171 return new MyDefaultAddAction(parentDomElement, name, icon, e, type, description);
174 private class MyDefaultAddAction extends DefaultAddAction {
175 // we need this properties, don't remove it (shared dataContext assertion)
176 private final DomElement myParent;
177 private final DomModelTreeView myView;
178 private final Type myType;
179 private final DomCollectionChildDescription myDescription;
181 public MyDefaultAddAction(final DomElement parent,
182 final String name,
183 final Icon icon,
184 final AnActionEvent e,
185 final Type type,
186 final DomCollectionChildDescription description) {
187 super(name, name, icon);
188 myType = type;
189 myDescription = description;
190 myParent = parent;
191 myView = getTreeView(e);
194 protected Type getElementType() {
195 return myType;
198 protected DomCollectionChildDescription getDomCollectionChildDescription() {
199 return myDescription;
202 protected DomElement getParentDomElement() {
203 return myParent;
206 protected void afterAddition(final DomElement newElement) {
207 final DomElement copy = newElement.createStableCopy();
209 ApplicationManager.getApplication().invokeLater(new Runnable() {
210 public void run() {
211 myView.setSelectedDomElement(copy);