update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / sourceItems / SourceItemsTree.java
blob9f84bf28112602d89936bebfdbf293eea630e166
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.
16 package com.intellij.openapi.roots.ui.configuration.artifacts.sourceItems;
18 import com.intellij.ide.CommonActionsManager;
19 import com.intellij.ide.DefaultTreeExpander;
20 import com.intellij.ide.dnd.DnDAction;
21 import com.intellij.ide.dnd.DnDDragStartBean;
22 import com.intellij.ide.dnd.DnDManager;
23 import com.intellij.ide.dnd.DnDSource;
24 import com.intellij.ide.dnd.aware.DnDAwareTree;
25 import com.intellij.openapi.Disposable;
26 import com.intellij.openapi.actionSystem.ActionGroup;
27 import com.intellij.openapi.actionSystem.ActionManager;
28 import com.intellij.openapi.actionSystem.ActionPlaces;
29 import com.intellij.openapi.actionSystem.DefaultActionGroup;
30 import com.intellij.openapi.project.ProjectBundle;
31 import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorImpl;
32 import com.intellij.openapi.roots.ui.configuration.artifacts.SimpleDnDAwareTree;
33 import com.intellij.openapi.roots.ui.configuration.artifacts.SourceItemsDraggingObject;
34 import com.intellij.openapi.util.Disposer;
35 import com.intellij.openapi.util.Pair;
36 import com.intellij.packaging.ui.ArtifactEditorContext;
37 import com.intellij.packaging.ui.PackagingSourceItem;
38 import com.intellij.ui.PopupHandler;
39 import com.intellij.ui.treeStructure.SimpleTreeBuilder;
40 import com.intellij.ui.treeStructure.SimpleTreeStructure;
41 import com.intellij.ui.treeStructure.Tree;
42 import com.intellij.ui.treeStructure.WeightBasedComparator;
43 import com.intellij.util.ui.tree.TreeUtil;
45 import javax.swing.tree.DefaultMutableTreeNode;
46 import java.awt.*;
47 import java.util.ArrayList;
48 import java.util.List;
50 /**
51 * @author nik
53 public class SourceItemsTree implements DnDSource, Disposable{
54 private SimpleDnDAwareTree myTree;
55 private final ArtifactEditorImpl myArtifactsEditor;
56 private SimpleTreeBuilder myBuilder;
58 public SourceItemsTree(ArtifactEditorContext editorContext, ArtifactEditorImpl artifactsEditor) {
59 myArtifactsEditor = artifactsEditor;
60 myTree = new SimpleDnDAwareTree();
61 myBuilder = new SimpleTreeBuilder(myTree, myTree.getBuilderModel(), new SourceItemsTreeStructure(editorContext, artifactsEditor), new WeightBasedComparator(true));
62 myTree.setRootVisible(false);
63 myTree.setShowsRootHandles(true);
64 Disposer.register(this, myBuilder);
65 DnDManager.getInstance().registerSource(this, myTree);
66 PopupHandler.installPopupHandler(myTree, createPopupGroup(), ActionPlaces.UNKNOWN, ActionManager.getInstance());
69 private ActionGroup createPopupGroup() {
70 final DefaultActionGroup group = new DefaultActionGroup();
71 group.add(new PutSourceItemIntoDefaultLocationAction(this, myArtifactsEditor));
72 group.add(new PutSourceItemIntoParentAndLinkViaManifestAction(this, myArtifactsEditor));
74 DefaultTreeExpander expander = new DefaultTreeExpander(myTree);
75 final CommonActionsManager commonActionsManager = CommonActionsManager.getInstance();
76 group.addAction(commonActionsManager.createExpandAllAction(expander, myTree));
77 group.addAction(commonActionsManager.createCollapseAllAction(expander, myTree));
78 return group;
81 public void rebuildTree() {
82 myBuilder.updateFromRoot(true);
85 public void initTree() {
86 myBuilder.initRootNode();
89 public Tree getTree() {
90 return myTree;
93 public void dispose() {
94 DnDManager.getInstance().unregisterSource(this, myTree);
97 private DefaultMutableTreeNode[] getSelectedNodes() {
98 return myTree.getSelectedNodes(DefaultMutableTreeNode.class, null);
101 public boolean canStartDragging(DnDAction action, Point dragOrigin) {
102 return !getSelectedItems().isEmpty();
105 public DnDDragStartBean startDragging(DnDAction action, Point dragOrigin) {
106 List<PackagingSourceItem> items = getSelectedItems();
107 return new DnDDragStartBean(new SourceItemsDraggingObject(items.toArray(new PackagingSourceItem[items.size()])));
110 public List<PackagingSourceItem> getSelectedItems() {
111 final DefaultMutableTreeNode[] nodes = getSelectedNodes();
112 List<PackagingSourceItem> items = new ArrayList<PackagingSourceItem>();
113 for (DefaultMutableTreeNode node : nodes) {
114 final Object userObject = node.getUserObject();
115 if (userObject instanceof SourceItemNode) {
116 final PackagingSourceItem sourceItem = ((SourceItemNode)userObject).getSourceItem();
117 if (sourceItem != null && sourceItem.isProvideElements()) {
118 items.add(sourceItem);
122 return items;
125 public Pair<Image, Point> createDraggedImage(DnDAction action, Point dragOrigin) {
126 final DefaultMutableTreeNode[] nodes = getSelectedNodes();
127 if (nodes.length == 1) {
128 return DnDAwareTree.getDragImage(myTree, TreeUtil.getPathFromRoot(nodes[0]), dragOrigin);
130 return DnDAwareTree.getDragImage(myTree, ProjectBundle.message("drag.n.drop.text.0.packaging.elements", nodes.length), dragOrigin);
133 public void dragDropEnd() {
136 public void dropActionChanged(int gestureModifiers) {
139 private static class SourceItemsTreeStructure extends SimpleTreeStructure {
140 private final ArtifactEditorContext myEditorContext;
141 private final ArtifactEditorImpl myArtifactsEditor;
142 private SourceItemsTreeRoot myRoot;
144 public SourceItemsTreeStructure(ArtifactEditorContext editorContext, ArtifactEditorImpl artifactsEditor) {
145 myEditorContext = editorContext;
146 myArtifactsEditor = artifactsEditor;
149 @Override
150 public Object getRootElement() {
151 if (myRoot == null) {
152 myRoot = new SourceItemsTreeRoot(myEditorContext, myArtifactsEditor);
154 return myRoot;