2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import java
.util
.ArrayList
;
20 import javax
.swing
.event
.TreeModelEvent
;
21 import javax
.swing
.event
.TreeModelListener
;
22 import javax
.swing
.tree
.TreeModel
;
23 import javax
.swing
.tree
.TreeNode
;
24 import javax
.swing
.tree
.TreePath
;
27 public class HideableTreeModel
implements TreeModel
{
29 private ArrayList
<TreeModelListener
> modelListeners
= new ArrayList
<TreeModelListener
>();
30 private Object root
= null;
33 public HideableTreeModel(TreeNode _root
) {
39 public Object
getRoot() {
44 private void setRoot(Object r
) {
49 private Object
[] getPathToRoot(Object node
) {
50 return getPathToRoot(node
, 0);
54 private Object
[] getPathToRoot(Object node
, int i
) {
60 anode
= new Object
[i
];
63 if(node
== getRoot()) {
64 anode
= new Object
[i
];
66 anode
= getPathToRoot(getParent(node
), i
);
68 anode
[anode
.length
- i
] = node
;
74 public void addTreeModelListener(TreeModelListener l
) {
75 modelListeners
.add(l
);
79 public void removeTreeModelListener(TreeModelListener l
) {
80 modelListeners
.remove(l
);
87 public void valueForPathChanged(TreePath path
, Object newValue
) {
88 nodeChanged(path
.getLastPathComponent());
94 public void nodeInserted(Object node
, Object child
, int index
) {
96 index
= getIndexOfChild(node
, child
);
98 if(node
!= null && child
!= null && index
>= 0) {
99 TreePath tp
= new TreePath(getPathToRoot(node
));
100 int[] ai
= { index
};
101 Object
[] ac
= { child
};
102 fireTreeNodesInserted(new TreeModelEvent(this, tp
, ai
, ac
));
107 private void nodeRemoved(Object node
, Object child
, int index
) {
108 if(node
!= null && child
!= null && index
>= 0) {
109 TreePath tp
= new TreePath(getPathToRoot(node
));
110 int[] ai
= { index
};
111 Object
[] ac
= { child
};
112 fireTreeNodesRemoved(new TreeModelEvent(this, tp
, ai
, ac
));
117 public void nodeChanged(Object node
) {
119 TreePath tp
= new TreePath(getPathToRoot(node
));
120 fireTreeNodesChanged(new TreeModelEvent(this, tp
, null, null));
125 private void fireTreeNodesChanged(TreeModelEvent event
) {
126 for(TreeModelListener l
: modelListeners
) {
127 l
.treeNodesChanged(event
);
132 private void fireTreeNodesInserted(TreeModelEvent event
) {
133 for(TreeModelListener l
: modelListeners
) {
134 l
.treeNodesInserted(event
);
139 private void fireTreeNodesRemoved(TreeModelEvent event
) {
140 for(TreeModelListener l
: modelListeners
) {
141 l
.treeNodesRemoved(event
);
145 public boolean isLeaf(Object _oNode
) {
146 if(_oNode
instanceof TreeNode
) {
147 return ((TreeNode
) _oNode
).isLeaf();
154 private Object
getParent(Object node
) {
155 if(node
!= getRoot() && (node
instanceof TreeNode
)) {
156 return ((TreeNode
)node
).getParent();
162 private boolean isNodeVisible(Object node
) {
163 if(node
!= getRoot()) {
164 if(node
instanceof HideableMutableTreeNode
) {
165 return ((HideableMutableTreeNode
)node
).isVisible();
172 public boolean setNodeVisible(Object node
, boolean v
) {
174 if(node
!= getRoot()) {
175 if(node
instanceof HideableMutableTreeNode
) {
176 HideableMutableTreeNode n
= (HideableMutableTreeNode
)node
;
177 if(v
!= n
.isVisible()) {
178 TreeNode parent
= n
.getParent();
180 // need to get index after showing...
182 int index
= getIndexOfChild(parent
, n
);
183 nodeInserted(parent
, n
, index
);
185 // need to get index before hiding...
186 int index
= getIndexOfChild(parent
, n
);
188 nodeRemoved(parent
, n
, index
);
204 public Object
getChild(Object parent
, int index
) {
205 if(parent
instanceof TreeNode
) {
206 TreeNode p
= (TreeNode
) parent
;
207 for(int i
= 0, j
= -1; i
< p
.getChildCount(); i
++) {
208 TreeNode pc
= p
.getChildAt(i
);
209 if(isNodeVisible(pc
)) {
221 public int getChildCount(Object parent
) {
223 if(parent
instanceof TreeNode
) {
224 TreeNode p
= (TreeNode
) parent
;
225 for(int i
= 0; i
< p
.getChildCount(); i
++) {
226 TreeNode pc
= p
.getChildAt(i
);
227 if(isNodeVisible(pc
)) {
236 public int getIndexOfChild(Object parent
, Object child
) {
238 if(parent
instanceof TreeNode
&& child
instanceof TreeNode
) {
239 TreeNode p
= (TreeNode
)parent
;
240 TreeNode c
= (TreeNode
)child
;
241 if(isNodeVisible(c
)) {
243 for(int i
= 0; i
< p
.getChildCount(); i
++) {
244 TreeNode pc
= p
.getChildAt(i
);
248 if(isNodeVisible(pc
)) {