ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInspection / ex / VisibleTreeState.java
blobbe4a595bc4f2383cc8609362aa23c35733612d9e
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.codeInspection.ex;
19 import com.intellij.profile.codeInspection.ui.InspectionConfigTreeNode;
20 import com.intellij.psi.search.scope.packageSet.NamedScope;
21 import com.intellij.ui.treeStructure.Tree;
22 import com.intellij.util.ui.tree.TreeUtil;
23 import com.intellij.util.xmlb.annotations.AbstractCollection;
24 import com.intellij.util.xmlb.annotations.Tag;
26 import javax.swing.tree.DefaultMutableTreeNode;
27 import javax.swing.tree.TreeNode;
28 import javax.swing.tree.TreePath;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.List;
32 import java.util.TreeSet;
34 /**
35 * User: anna
36 * Date: Dec 18, 2004
38 @Tag("profile-state")
39 public class VisibleTreeState{
40 @Tag("expanded-state")
41 @AbstractCollection(surroundWithTag = false, elementTag = "expanded", elementValueAttribute = "path", elementTypes = {State.class})
42 public TreeSet<State> myExpandedNodes = new TreeSet<State>();
44 @Tag("selected-state")
45 @AbstractCollection(surroundWithTag = false, elementTag = "selected", elementValueAttribute = "path", elementTypes = {State.class})
46 public TreeSet<State> mySelectedNodes = new TreeSet<State>();
48 public VisibleTreeState(VisibleTreeState src) {
49 myExpandedNodes.addAll(src.myExpandedNodes);
50 mySelectedNodes.addAll(src.mySelectedNodes);
53 public VisibleTreeState() {
56 public void expandNode(String nodeTitle) {
57 myExpandedNodes.add(new State(nodeTitle));
60 public void collapseNode(String nodeTitle) {
61 myExpandedNodes.remove(new State(nodeTitle));
64 public void restoreVisibleState(Tree tree) {
65 ArrayList<TreePath> pathsToExpand = new ArrayList<TreePath>();
66 ArrayList<TreePath> toSelect = new ArrayList<TreePath>();
67 traverseNodes((DefaultMutableTreeNode)tree.getModel().getRoot(), pathsToExpand, toSelect);
68 TreeUtil.restoreExpandedPaths(tree, pathsToExpand);
69 if (toSelect.isEmpty()) {
70 TreeUtil.selectFirstNode(tree);
72 else {
73 for (final TreePath aToSelect : toSelect) {
74 TreeUtil.selectPath(tree, aToSelect);
79 private void traverseNodes(final DefaultMutableTreeNode root, List<TreePath> pathsToExpand, List<TreePath> toSelect) {
80 final Descriptor descriptor = ((InspectionConfigTreeNode)root).getDesriptor();
81 final TreeNode[] rootPath = root.getPath();
82 if (descriptor != null) {
83 final String shortName = descriptor.getKey().toString();
84 if (mySelectedNodes.contains(new State(descriptor))) {
85 toSelect.add(new TreePath(rootPath));
87 if (myExpandedNodes.contains(new State(descriptor))) {
88 pathsToExpand.add(new TreePath(rootPath));
91 else {
92 final String str = ((InspectionConfigTreeNode)root).getGroupName();
93 if (mySelectedNodes.contains(new State(str))) {
94 toSelect.add(new TreePath(rootPath));
96 if (myExpandedNodes.contains(new State(str))) {
97 pathsToExpand.add(new TreePath(rootPath));
100 for (int i = 0; i < root.getChildCount(); i++) {
101 traverseNodes((DefaultMutableTreeNode)root.getChildAt(i), pathsToExpand, toSelect);
105 public void saveVisibleState(Tree tree) {
106 myExpandedNodes.clear();
107 final DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)tree.getModel().getRoot();
108 Enumeration<TreePath> expanded = tree.getExpandedDescendants(new TreePath(rootNode.getPath()));
109 if (expanded != null) {
110 while (expanded.hasMoreElements()) {
111 final TreePath treePath = expanded.nextElement();
112 final InspectionConfigTreeNode node = (InspectionConfigTreeNode)treePath.getLastPathComponent();
113 final Descriptor descriptor = node.getDesriptor();
114 myExpandedNodes.add(getState(node, descriptor));
118 setSelectionPaths(tree.getSelectionPaths());
121 private static State getState(InspectionConfigTreeNode node, Descriptor descriptor) {
122 final State expandedNode;
123 if (descriptor != null) {
124 expandedNode = new State(descriptor);
126 else {
127 expandedNode = new State(node.getGroupName());
129 return expandedNode;
132 public void setSelectionPaths(final TreePath[] selectionPaths) {
133 mySelectedNodes.clear();
134 if (selectionPaths != null) {
135 for (TreePath selectionPath : selectionPaths) {
136 final InspectionConfigTreeNode node = (InspectionConfigTreeNode)selectionPath.getLastPathComponent();
137 final Descriptor descriptor = node.getDesriptor();
138 mySelectedNodes.add(getState(node, descriptor));
144 public static class State implements Comparable{
145 @Tag("id")
146 public String myKey;
147 Descriptor myDescriptor;
149 public State(String key) {
150 myKey = key;
153 public State(Descriptor descriptor) {
154 myKey = descriptor.toString();
155 myDescriptor = descriptor;
158 //readExternal
159 public State(){
162 @Override
163 public boolean equals(Object o) {
164 if (this == o) return true;
165 if (o == null || getClass() != o.getClass()) return false;
167 State state = (State)o;
169 if (myKey != null ? !myKey.equals(state.myKey) : state.myKey != null) return false;
171 return true;
174 @Override
175 public int hashCode() {
176 int result = myKey != null ? myKey.hashCode() : 0;
177 result = 31 * result + (myDescriptor != null ? myDescriptor.hashCode() : 0);
178 return result;
181 public int compareTo(Object o) {
182 if (!(o instanceof State)) return -1;
183 final State other = (State)o;
184 if (myKey.equals(other.myKey)) {
185 if (myDescriptor != null && other.myDescriptor != null) {
186 final NamedScope scope1 = myDescriptor.getScope();
187 final NamedScope scope2 = other.myDescriptor.getScope();
188 if (scope1 != null && scope2 != null) {
189 return scope1.getName().compareTo(scope2.getName());
193 return myKey.compareTo(other.myKey);