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
.ui
;
19 import javax
.swing
.event
.TreeModelEvent
;
20 import javax
.swing
.event
.TreeModelListener
;
21 import javax
.swing
.event
.TreeSelectionEvent
;
22 import javax
.swing
.event
.TreeSelectionListener
;
23 import javax
.swing
.tree
.TreeModel
;
24 import javax
.swing
.tree
.TreeNode
;
25 import javax
.swing
.tree
.TreePath
;
26 import java
.beans
.PropertyChangeEvent
;
27 import java
.beans
.PropertyChangeListener
;
28 import java
.util
.ArrayList
;
29 import java
.util
.Collection
;
31 public class SelectionSaver
implements TreeSelectionListener
, TreeModelListener
, PropertyChangeListener
{
33 private final JTree myTree
;
34 private Collection
<TreeNode
> myCurrentSelection
= new ArrayList
<TreeNode
>();
37 private SelectionSaver(JTree tree
) {
41 public static void installOn(JTree tree
){
42 new SelectionSaver(tree
).install();
45 private void install(){
46 myTree
.getModel().addTreeModelListener(this);
47 myTree
.getSelectionModel().addTreeSelectionListener(this);
48 myTree
.addPropertyChangeListener(this);
51 public void propertyChange(PropertyChangeEvent evt
) {
52 if (evt
.getPropertyName().equals(JTree
.TREE_MODEL_PROPERTY
)){
53 ((TreeModel
)evt
.getOldValue()).removeTreeModelListener(this);
54 ((TreeModel
)evt
.getNewValue()).addTreeModelListener(this);
58 public void treeNodesRemoved(TreeModelEvent treeModelEvent
) {
59 TreePath pathToDelete
= treeModelEvent
.getTreePath();
60 Object
[] children
= treeModelEvent
.getChildren();
62 for (int i
= 0; i
< children
.length
; i
++) {
63 Object nodeToDelete
= children
[i
];
64 if (myCurrentSelection
.contains(nodeToDelete
)){
65 int deletedRow
= myTree
.getRowForPath(pathToDelete
.pathByAddingChild(nodeToDelete
));
66 if (deletedRow
== 0) return;
67 TreePath treePath
= new TreePath(myTree
.getPathForRow(deletedRow
- 1));
68 myTree
.getSelectionModel().addSelectionPath((TreePath
)treePath
.getLastPathComponent());
74 public void valueChanged(TreeSelectionEvent e
) {
75 myCurrentSelection
= new ArrayList
<TreeNode
>();
76 TreePath
[] selection
= myTree
.getSelectionModel().getSelectionPaths();
77 if (selection
== null) return;
78 for (int i
= 0; i
< selection
.length
; i
++) {
79 TreePath treePath
= selection
[i
];
80 myCurrentSelection
.add((TreeNode
)treePath
.getLastPathComponent());
84 public void treeNodesChanged(TreeModelEvent e
) {
88 public void treeNodesInserted(TreeModelEvent e
) {
92 public void treeStructureChanged(TreeModelEvent e
) {