1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import java
.util
.ArrayList
;
21 import javax
.swing
.event
.TreeModelEvent
;
22 import javax
.swing
.event
.TreeModelListener
;
23 import javax
.swing
.tree
.TreeModel
;
24 import javax
.swing
.tree
.TreeNode
;
25 import javax
.swing
.tree
.TreePath
;
28 public class HideableTreeModel
implements TreeModel
{
30 private ArrayList
<TreeModelListener
> modelListeners
= new ArrayList
<TreeModelListener
>();
31 private Object root
= null;
34 public HideableTreeModel(TreeNode _root
) {
40 public Object
getRoot() {
45 private void setRoot(Object r
) {
50 private Object
[] getPathToRoot(Object node
) {
51 return getPathToRoot(node
, 0);
55 private Object
[] getPathToRoot(Object node
, int i
) {
61 anode
= new Object
[i
];
64 if(node
== getRoot()) {
65 anode
= new Object
[i
];
67 anode
= getPathToRoot(getParent(node
), i
);
69 anode
[anode
.length
- i
] = node
;
75 public void addTreeModelListener(TreeModelListener l
) {
76 modelListeners
.add(l
);
80 public void removeTreeModelListener(TreeModelListener l
) {
81 modelListeners
.remove(l
);
88 public void valueForPathChanged(TreePath path
, Object newValue
) {
89 nodeChanged(path
.getLastPathComponent());
95 public void nodeInserted(Object node
, Object child
, int index
) {
97 index
= getIndexOfChild(node
, child
);
99 if(node
!= null && child
!= null && index
>= 0) {
100 TreePath tp
= new TreePath(getPathToRoot(node
));
101 int[] ai
= { index
};
102 Object
[] ac
= { child
};
103 fireTreeNodesInserted(new TreeModelEvent(this, tp
, ai
, ac
));
108 private void nodeRemoved(Object node
, Object child
, int index
) {
109 if(node
!= null && child
!= null && index
>= 0) {
110 TreePath tp
= new TreePath(getPathToRoot(node
));
111 int[] ai
= { index
};
112 Object
[] ac
= { child
};
113 fireTreeNodesRemoved(new TreeModelEvent(this, tp
, ai
, ac
));
118 public void nodeChanged(Object node
) {
120 TreePath tp
= new TreePath(getPathToRoot(node
));
121 fireTreeNodesChanged(new TreeModelEvent(this, tp
, null, null));
126 private void fireTreeNodesChanged(TreeModelEvent event
) {
127 for(TreeModelListener l
: modelListeners
) {
128 l
.treeNodesChanged(event
);
133 private void fireTreeNodesInserted(TreeModelEvent event
) {
134 for(TreeModelListener l
: modelListeners
) {
135 l
.treeNodesInserted(event
);
140 private void fireTreeNodesRemoved(TreeModelEvent event
) {
141 for(TreeModelListener l
: modelListeners
) {
142 l
.treeNodesRemoved(event
);
146 public boolean isLeaf(Object _oNode
) {
147 if(_oNode
instanceof TreeNode
) {
148 return ((TreeNode
) _oNode
).isLeaf();
155 private Object
getParent(Object node
) {
156 if(node
!= getRoot() && (node
instanceof TreeNode
)) {
157 return ((TreeNode
)node
).getParent();
163 private boolean isNodeVisible(Object node
) {
164 if(node
!= getRoot()) {
165 if(node
instanceof HideableMutableTreeNode
) {
166 return ((HideableMutableTreeNode
)node
).isVisible();
173 public boolean setNodeVisible(Object node
, boolean v
) {
175 if(node
!= getRoot()) {
176 if(node
instanceof HideableMutableTreeNode
) {
177 HideableMutableTreeNode n
= (HideableMutableTreeNode
)node
;
178 if(v
!= n
.isVisible()) {
179 TreeNode parent
= n
.getParent();
181 // need to get index after showing...
183 int index
= getIndexOfChild(parent
, n
);
184 nodeInserted(parent
, n
, index
);
186 // need to get index before hiding...
187 int index
= getIndexOfChild(parent
, n
);
189 nodeRemoved(parent
, n
, index
);
205 public Object
getChild(Object parent
, int index
) {
206 if(parent
instanceof TreeNode
) {
207 TreeNode p
= (TreeNode
) parent
;
208 for(int i
= 0, j
= -1; i
< p
.getChildCount(); i
++) {
209 TreeNode pc
= p
.getChildAt(i
);
210 if(isNodeVisible(pc
)) {
222 public int getChildCount(Object parent
) {
224 if(parent
instanceof TreeNode
) {
225 TreeNode p
= (TreeNode
) parent
;
226 for(int i
= 0; i
< p
.getChildCount(); i
++) {
227 TreeNode pc
= p
.getChildAt(i
);
228 if(isNodeVisible(pc
)) {
237 public int getIndexOfChild(Object parent
, Object child
) {
239 if(parent
instanceof TreeNode
&& child
instanceof TreeNode
) {
240 TreeNode p
= (TreeNode
)parent
;
241 TreeNode c
= (TreeNode
)child
;
242 if(isNodeVisible(c
)) {
244 for(int i
= 0; i
< p
.getChildCount(); i
++) {
245 TreeNode pc
= p
.getChildAt(i
);
249 if(isNodeVisible(pc
)) {
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */