navigation to domElement after addition
[fedora-idea.git] / dom / impl / src / com / intellij / util / xml / tree / actions / AddElementInCollectionAction.java
blob68e40eeb42f9569382210569157408241d83c9a7
1 /*
2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
3 */
5 package com.intellij.util.xml.tree.actions;
7 import com.intellij.openapi.actionSystem.AnActionEvent;
8 import com.intellij.openapi.application.ApplicationBundle;
9 import com.intellij.openapi.application.ApplicationManager;
10 import com.intellij.openapi.ui.popup.ListPopup;
11 import com.intellij.ui.treeStructure.SimpleNode;
12 import com.intellij.util.xml.DomElement;
13 import com.intellij.util.xml.DomReflectionUtil;
14 import com.intellij.util.xml.ElementPresentationManager;
15 import com.intellij.util.xml.reflect.DomCollectionChildDescription;
16 import com.intellij.util.xml.tree.BaseDomElementNode;
17 import com.intellij.util.xml.tree.DomElementsGroupNode;
18 import com.intellij.util.xml.tree.DomModelTreeView;
19 import com.intellij.util.xml.ui.actions.AddDomElementAction;
20 import com.intellij.util.xml.ui.actions.DefaultAddAction;
21 import org.jetbrains.annotations.NotNull;
22 import org.jetbrains.annotations.Nullable;
24 import javax.swing.*;
25 import java.lang.reflect.Type;
26 import java.util.List;
28 /**
29 * User: Sergey.Vasiliev
31 public class AddElementInCollectionAction extends AddDomElementAction {
32 private DomModelTreeView myTreeView;
34 public AddElementInCollectionAction() {
37 public AddElementInCollectionAction(final DomModelTreeView treeView) {
38 myTreeView = treeView;
41 protected DomModelTreeView getTreeView(AnActionEvent e) {
42 if (myTreeView != null) return myTreeView;
44 return (DomModelTreeView)e.getDataContext().getData(DomModelTreeView.DOM_MODEL_TREE_VIEW_KEY);
47 protected boolean isEnabled(final AnActionEvent e) {
48 final DomModelTreeView treeView = getTreeView(e);
50 final boolean enabled = treeView != null;
51 e.getPresentation().setEnabled(enabled);
53 return enabled;
57 protected void showPopup(final ListPopup groupPopup, final AnActionEvent e) {
58 if (myTreeView == null) {
59 if (e.getPlace().equals(DomModelTreeView.DOM_MODEL_TREE_VIEW_POPUP)) {
60 groupPopup.showInCenterOf(getTreeView(e).getTree());
62 else {
63 groupPopup.showInBestPositionFor(e.getDataContext());
66 else {
67 super.showPopup(groupPopup, e);
71 @NotNull
72 protected DomCollectionChildDescription[] getDomCollectionChildDescriptions(final AnActionEvent e) {
73 final DomModelTreeView view = getTreeView(e);
75 SimpleNode node = view.getTree().getSelectedNode();
76 if (node instanceof BaseDomElementNode) {
77 List<DomCollectionChildDescription> consolidated = ((BaseDomElementNode)node).getConsolidatedChildrenDescriptions();
78 if (consolidated.size() > 0) {
79 return consolidated.toArray(DomCollectionChildDescription.EMPTY_ARRAY);
83 final DomElementsGroupNode groupNode = getDomElementsGroupNode(view);
85 return groupNode == null
86 ? DomCollectionChildDescription.EMPTY_ARRAY
87 : new DomCollectionChildDescription[]{groupNode.getChildDescription()};
90 protected DomElement getParentDomElement(final AnActionEvent e) {
91 final DomModelTreeView view = getTreeView(e);
92 SimpleNode node = view.getTree().getSelectedNode();
93 if (node instanceof BaseDomElementNode) {
94 if (((BaseDomElementNode)node).getConsolidatedChildrenDescriptions().size() > 0) {
95 return ((BaseDomElementNode)node).getDomElement();
98 final DomElementsGroupNode groupNode = getDomElementsGroupNode(view);
100 return groupNode == null ? null : groupNode.getDomElement();
103 protected JComponent getComponent(AnActionEvent e) {
104 return getTreeView(e);
107 protected boolean showAsPopup() {
108 return true;
111 protected String getActionText(final AnActionEvent e) {
112 String text = ApplicationBundle.message("action.add");
113 if (e.getPresentation().isEnabled()) {
114 final DomElementsGroupNode selectedNode = getDomElementsGroupNode(getTreeView(e));
115 if (selectedNode != null) {
116 final Type type = selectedNode.getChildDescription().getType();
117 text += " " + ElementPresentationManager.getTypeName(DomReflectionUtil.getRawType(type));
120 return text;
123 @Nullable
124 private static DomElementsGroupNode getDomElementsGroupNode(final DomModelTreeView treeView) {
125 SimpleNode simpleNode = treeView.getTree().getSelectedNode();
126 while (simpleNode != null) {
127 if (simpleNode instanceof DomElementsGroupNode) return (DomElementsGroupNode)simpleNode;
129 simpleNode = simpleNode.getParent();
131 return null;
135 protected DefaultAddAction createAddingAction(final AnActionEvent e,
136 final String name,
137 final Icon icon,
138 final Class s,
139 final DomCollectionChildDescription description) {
141 return new DefaultAddAction(name, name, icon) {
142 // we need this properties, don't remove it (shared dataContext assertion)
143 private DomElement myParent = AddElementInCollectionAction.this.getParentDomElement(e);
144 private DomModelTreeView myView = getTreeView(e);
146 protected Class getElementClass() {
147 return s;
150 protected DomCollectionChildDescription getDomCollectionChildDescription() {
151 return description;
154 protected DomElement getParentDomElement() {
155 return myParent;
158 protected void afterAddition(final DomElement newElement) {
159 final DomElement copy = newElement.createStableCopy();
161 ApplicationManager.getApplication().invokeLater(new Runnable() {
162 public void run() {
163 myView.setSelectedDomElement(copy);