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 org
.jetbrains
.idea
.svn
.dialogs
.browserCache
;
18 import org
.jetbrains
.idea
.svn
.dialogs
.RepositoryBrowserComponent
;
19 import org
.jetbrains
.idea
.svn
.dialogs
.RepositoryTreeNode
;
22 import javax
.swing
.tree
.DefaultTreeModel
;
23 import javax
.swing
.tree
.TreeNode
;
24 import javax
.swing
.tree
.TreePath
;
25 import java
.util
.Enumeration
;
27 public class KeepingSelectionExpander
implements Expander
{
28 private final JTree myTree
;
29 private TreePath mySelectionPath
;
31 public KeepingSelectionExpander(final RepositoryBrowserComponent browser
) {
32 myTree
= browser
.getRepositoryTree();
35 public void onBeforeRefresh(final RepositoryTreeNode node
) {
36 mySelectionPath
= myTree
.getSelectionPath();
39 public void onAfterRefresh(final RepositoryTreeNode node
) {
40 if (mySelectionPath
== null) {
44 setSelection(mySelectionPath
.getPath(), 1, (TreeNode
) myTree
.getModel().getRoot());
47 private boolean setSelection(final Object
[] oldSelectionPath
, final int idx
, final TreeNode node
) {
48 if (idx
>= oldSelectionPath
.length
) {
51 if ((oldSelectionPath
[idx
] != null) && (oldSelectionPath
[idx
].toString() != null)) {
52 final Enumeration children
= node
.children();
53 while (children
.hasMoreElements()) {
54 final TreeNode child
= (TreeNode
) children
.nextElement();
55 if (oldSelectionPath
[idx
].toString().equals(child
.toString())) {
56 final boolean childStatus
= setSelection(oldSelectionPath
, idx
+ 1, child
);
58 myTree
.setSelectionPath(new TreePath(((DefaultTreeModel
) myTree
.getModel()).getPathToRoot(child
)));
64 myTree
.setSelectionPath(new TreePath(((DefaultTreeModel
) myTree
.getModel()).getPathToRoot(node
)));