2 * Copyright 2000-2014 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 com.intellij.ui;
18 import com.intellij.ui.speedSearch.SpeedSearchSupply;
19 import com.intellij.ui.treeStructure.Tree;
20 import com.intellij.util.EventDispatcher;
21 import com.intellij.util.ui.tree.TreeUtil;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
26 import javax.swing.tree.TreeModel;
27 import javax.swing.tree.TreeNode;
28 import javax.swing.tree.TreePath;
30 import java.awt.event.KeyAdapter;
31 import java.awt.event.KeyEvent;
32 import java.awt.event.MouseEvent;
33 import java.lang.reflect.Array;
34 import java.util.ArrayList;
35 import java.util.Enumeration;
40 class CheckboxTreeHelper {
41 static final CheckboxTreeBase.CheckPolicy DEFAULT_POLICY = new CheckboxTreeBase.CheckPolicy(true, true, false, true);
42 private final CheckboxTreeBase.CheckPolicy myCheckPolicy;
43 private final EventDispatcher<CheckboxTreeListener> myEventDispatcher;
45 CheckboxTreeHelper(CheckboxTreeBase.CheckPolicy checkPolicy, EventDispatcher<CheckboxTreeListener> dispatcher) {
46 myCheckPolicy = checkPolicy;
47 myEventDispatcher = dispatcher;
50 public void initTree(@NotNull final Tree tree, JComponent mainComponent, CheckboxTreeBase.CheckboxTreeCellRendererBase cellRenderer) {
51 tree.setCellRenderer(cellRenderer);
52 tree.setRootVisible(false);
53 tree.setShowsRootHandles(true);
54 tree.setLineStyleAngled();
55 TreeUtil.installActions(tree);
57 setupKeyListener(tree, mainComponent);
58 setupMouseListener(tree, mainComponent, cellRenderer);
61 public void setNodeState(Tree tree, CheckedTreeNode node, boolean checked) {
62 changeNodeState(node, checked);
63 adjustParentsAndChildren(node, checked);
66 // notify model listeners about model change
67 final TreeModel model = tree.getModel();
68 model.valueForPathChanged(new TreePath(node.getPath()), node.getUserObject());
71 private void toggleNode(Tree tree, CheckedTreeNode node) {
72 setNodeState(tree, node, !node.isChecked());
75 private void adjustParentsAndChildren(final CheckedTreeNode node, final boolean checked) {
77 if (myCheckPolicy.uncheckParentWithUncheckedChild) {
78 TreeNode parent = node.getParent();
79 while (parent != null) {
80 if (parent instanceof CheckedTreeNode) {
81 changeNodeState((CheckedTreeNode)parent, false);
83 parent = parent.getParent();
86 if (myCheckPolicy.uncheckChildrenWithUncheckedParent) {
87 uncheckChildren(node);
91 if (myCheckPolicy.checkChildrenWithCheckedParent) {
95 if (myCheckPolicy.checkParentWithCheckedChild) {
96 TreeNode parent = node.getParent();
97 while (parent != null) {
98 if (parent instanceof CheckedTreeNode) {
99 changeNodeState((CheckedTreeNode)parent, true);
101 parent = parent.getParent();
107 private void changeNodeState(final CheckedTreeNode node, final boolean checked) {
108 if (node.isChecked() != checked) {
109 myEventDispatcher.getMulticaster().beforeNodeStateChanged(node);
110 node.setChecked(checked);
111 myEventDispatcher.getMulticaster().nodeStateChanged(node);
115 private void uncheckChildren(final CheckedTreeNode node) {
116 final Enumeration children = node.children();
117 while (children.hasMoreElements()) {
118 final Object o = children.nextElement();
119 if (!(o instanceof CheckedTreeNode)) continue;
120 CheckedTreeNode child = (CheckedTreeNode)o;
121 changeNodeState(child, false);
122 uncheckChildren(child);
126 private void checkChildren(final CheckedTreeNode node) {
127 final Enumeration children = node.children();
128 while (children.hasMoreElements()) {
129 final Object o = children.nextElement();
130 if (!(o instanceof CheckedTreeNode)) continue;
131 CheckedTreeNode child = (CheckedTreeNode)o;
132 changeNodeState(child, true);
133 checkChildren(child);
137 private void setupKeyListener(final Tree tree, final JComponent mainComponent) {
138 mainComponent.addKeyListener(new KeyAdapter() {
139 public void keyPressed(@NotNull KeyEvent e) {
140 if (isToggleEvent(e, mainComponent)) {
141 TreePath treePath = tree.getLeadSelectionPath();
142 if (treePath == null) return;
143 final Object o = treePath.getLastPathComponent();
144 if (!(o instanceof CheckedTreeNode)) return;
145 CheckedTreeNode firstNode = (CheckedTreeNode)o;
146 if (!firstNode.isEnabled()) return;
147 toggleNode(tree, firstNode);
148 boolean checked = firstNode.isChecked();
150 TreePath[] selectionPaths = tree.getSelectionPaths();
151 for (int i = 0; selectionPaths != null && i < selectionPaths.length; i++) {
152 final TreePath selectionPath = selectionPaths[i];
153 final Object o1 = selectionPath.getLastPathComponent();
154 if (!(o1 instanceof CheckedTreeNode)) continue;
155 CheckedTreeNode node = (CheckedTreeNode)o1;
156 setNodeState(tree, node, checked);
165 private static boolean isToggleEvent(KeyEvent e, JComponent mainComponent) {
166 return e.getKeyCode() == KeyEvent.VK_SPACE && SpeedSearchSupply.getSupply(mainComponent) == null;
169 private void setupMouseListener(final Tree tree, JComponent mainComponent, final CheckboxTreeBase.CheckboxTreeCellRendererBase cellRenderer) {
170 new ClickListener() {
172 public boolean onClick(@NotNull MouseEvent e, int clickCount) {
173 int row = tree.getRowForLocation(e.getX(), e.getY());
174 if (row < 0) return false;
175 final Object o = tree.getPathForRow(row).getLastPathComponent();
176 if (!(o instanceof CheckedTreeNode)) return false;
177 Rectangle rowBounds = tree.getRowBounds(row);
178 cellRenderer.setBounds(rowBounds);
179 Rectangle checkBounds = cellRenderer.myCheckbox.getBounds();
180 checkBounds.setLocation(rowBounds.getLocation());
182 if (checkBounds.height == 0) checkBounds.height = checkBounds.width = rowBounds.height;
184 final CheckedTreeNode node = (CheckedTreeNode)o;
185 if (checkBounds.contains(e.getPoint())) {
186 if (node.isEnabled()) {
187 toggleNode(tree, node);
188 tree.setSelectionRow(row);
192 else if (clickCount > 1) {
193 myEventDispatcher.getMulticaster().mouseDoubleClicked(node);
199 }.installOn(mainComponent);
202 @SuppressWarnings("unchecked")
203 public static <T> T[] getCheckedNodes(final Class<T> nodeType, @Nullable final Tree.NodeFilter<T> filter, final TreeModel model) {
204 final ArrayList<T> nodes = new ArrayList<T>();
205 final Object root = model.getRoot();
206 if (!(root instanceof CheckedTreeNode)) {
207 throw new IllegalStateException(
208 "The root must be instance of the " + CheckedTreeNode.class.getName() + ": " + root.getClass().getName());
211 @SuppressWarnings("unchecked")
212 public void collect(CheckedTreeNode node) {
214 Object userObject = node.getUserObject();
215 if (node.isChecked() && userObject != null && nodeType.isAssignableFrom(userObject.getClass())) {
216 final T value = (T)userObject;
217 if (filter != null && !filter.accept(value)) return;
222 for (int i = 0; i < node.getChildCount(); i++) {
223 final TreeNode child = node.getChildAt(i);
224 if (child instanceof CheckedTreeNode) {
225 collect((CheckedTreeNode)child);
230 }.collect((CheckedTreeNode)root);
231 T[] result = (T[])Array.newInstance(nodeType, nodes.size());
232 nodes.toArray(result);