Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / HideableTreeModel.java
blobdae9d0cdff0acb740315d653ac4f4ddb0e1bdd1b
1 /*
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) {
34 super();
35 setRoot(_root);
39 public Object getRoot() {
40 return this.root;
44 private void setRoot(Object r) {
45 this.root = r;
49 private Object[] getPathToRoot(Object node) {
50 return getPathToRoot(node, 0);
54 private Object[] getPathToRoot(Object node, int i) {
55 Object anode[];
56 if(node == null) {
57 if(i == 0) {
58 return null;
60 anode = new Object[i];
61 } else {
62 i++;
63 if(node == getRoot()) {
64 anode = new Object[i];
65 } else {
66 anode = getPathToRoot(getParent(node), i);
68 anode[anode.length - i] = node;
70 return anode;
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) {
95 if(index < 0) {
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) {
118 if(node != null) {
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();
149 return true;
154 private Object getParent(Object node) {
155 if(node != getRoot() && (node instanceof TreeNode)) {
156 return ((TreeNode)node).getParent();
158 return null;
162 private boolean isNodeVisible(Object node) {
163 if(node != getRoot()) {
164 if(node instanceof HideableMutableTreeNode) {
165 return ((HideableMutableTreeNode)node).isVisible();
168 return true;
172 public boolean setNodeVisible(Object node, boolean v) {
173 // can't hide root
174 if(node != getRoot()) {
175 if(node instanceof HideableMutableTreeNode) {
176 HideableMutableTreeNode n = (HideableMutableTreeNode)node;
177 if(v != n.isVisible()) {
178 TreeNode parent = n.getParent();
179 if(v) {
180 // need to get index after showing...
181 n.setVisible(v);
182 int index = getIndexOfChild(parent, n);
183 nodeInserted(parent, n, index);
184 } else {
185 // need to get index before hiding...
186 int index = getIndexOfChild(parent, n);
187 n.setVisible(v);
188 nodeRemoved(parent, n, index);
191 return true;
194 return false;
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)) {
210 j++;
212 if(j == index) {
213 return pc;
217 return null;
221 public int getChildCount(Object parent) {
222 int count = 0;
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)) {
228 count++;
232 return count;
236 public int getIndexOfChild(Object parent, Object child) {
237 int index = -1;
238 if(parent instanceof TreeNode && child instanceof TreeNode) {
239 TreeNode p = (TreeNode)parent;
240 TreeNode c = (TreeNode)child;
241 if(isNodeVisible(c)) {
242 index = 0;
243 for(int i = 0; i < p.getChildCount(); i++) {
244 TreeNode pc = p.getChildAt(i);
245 if(pc.equals(c)) {
246 return index;
248 if(isNodeVisible(pc)) {
249 index++;
254 return index;