sd: keep a non-owning pointer to the OverridingShell
[LibreOffice.git] / odk / examples / java / Inspector / HideableTreeModel.java
blobb70d6304662935d0be3e895cdb07eade0209e631
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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) {
35 super();
36 setRoot(_root);
40 public Object getRoot() {
41 return this.root;
45 private void setRoot(Object r) {
46 this.root = r;
50 private Object[] getPathToRoot(Object node) {
51 return getPathToRoot(node, 0);
55 private Object[] getPathToRoot(Object node, int i) {
56 Object anode[];
57 if(node == null) {
58 if(i == 0) {
59 return null;
61 anode = new Object[i];
62 } else {
63 i++;
64 if(node == getRoot()) {
65 anode = new Object[i];
66 } else {
67 anode = getPathToRoot(getParent(node), i);
69 anode[anode.length - i] = node;
71 return anode;
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) {
96 if(index < 0) {
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) {
119 if(node != null) {
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();
150 return true;
155 private Object getParent(Object node) {
156 if(node != getRoot() && (node instanceof TreeNode)) {
157 return ((TreeNode)node).getParent();
159 return null;
163 private boolean isNodeVisible(Object node) {
164 if(node != getRoot()) {
165 if(node instanceof HideableMutableTreeNode) {
166 return ((HideableMutableTreeNode)node).isVisible();
169 return true;
173 public boolean setNodeVisible(Object node, boolean v) {
174 // can't hide root
175 if(node != getRoot()) {
176 if(node instanceof HideableMutableTreeNode) {
177 HideableMutableTreeNode n = (HideableMutableTreeNode)node;
178 if(v != n.isVisible()) {
179 TreeNode parent = n.getParent();
180 if(v) {
181 // need to get index after showing...
182 n.setVisible(v);
183 int index = getIndexOfChild(parent, n);
184 nodeInserted(parent, n, index);
185 } else {
186 // need to get index before hiding...
187 int index = getIndexOfChild(parent, n);
188 n.setVisible(v);
189 nodeRemoved(parent, n, index);
192 return true;
195 return false;
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)) {
211 j++;
213 if(j == index) {
214 return pc;
218 return null;
222 public int getChildCount(Object parent) {
223 int count = 0;
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)) {
229 count++;
233 return count;
237 public int getIndexOfChild(Object parent, Object child) {
238 int index = -1;
239 if(parent instanceof TreeNode && child instanceof TreeNode) {
240 TreeNode p = (TreeNode)parent;
241 TreeNode c = (TreeNode)child;
242 if(isNodeVisible(c)) {
243 index = 0;
244 for(int i = 0; i < p.getChildCount(); i++) {
245 TreeNode pc = p.getChildAt(i);
246 if(pc.equals(c)) {
247 return index;
249 if(isNodeVisible(pc)) {
250 index++;
255 return index;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */