Source code: com/maddyhome/idea/vim/key/BranchNode.java
1 package com.maddyhome.idea.vim.key;
2
3 /*
4 * IdeaVim - A Vim emulator plugin for IntelliJ Idea
5 * Copyright (C) 2003 Rick Maddy
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 import java.util.Iterator;
23 import javax.swing.KeyStroke;
24
25 /**
26 * This node of the key/action tree will contain one or more child nodes.
27 */
28 public class BranchNode extends ParentNode
29 {
30 /** This is a special key for an argument child node */
31 public static final String ARGUMENT = "argument";
32
33 /**
34 * Creates the branch node for the given keystroke
35 * @param key The keystroke to get to this node
36 */
37 public BranchNode(KeyStroke key)
38 {
39 this.key = key;
40 }
41
42 /**
43 * Returns the child node associated with the supplied key. The key must be the same as used in {@link #addChild}.
44 * If no such child is found but there is an argument node, the argument node is returned.
45 * @param key The key used to find the child
46 * @return The child mapped to key or an argument node or null if no such mapping found
47 */
48 public Node getChild(Object key)
49 {
50 Node res = super.getChild(key);
51 if (res == null)
52 {
53 res = (Node)children.get(ARGUMENT);
54 }
55
56 return res;
57 }
58
59 public Node getArgumentNode()
60 {
61 return (Node)children.get(ARGUMENT);
62 }
63
64 /**
65 * The key this node is associated with
66 * @return The node's keystroke
67 */
68 public KeyStroke getKey()
69 {
70 return key;
71 }
72
73 public String toString()
74 {
75 StringBuffer res = new StringBuffer();
76 res.append("BranchNode[key=");
77 res.append(key);
78 res.append("children=[");
79 int cnt = 0;
80 for (Iterator iterator = children.keySet().iterator(); iterator.hasNext();)
81 {
82 Object key = iterator.next();
83 Node node = (Node)children.get(key);
84 if (cnt > 0)
85 {
86 res.append(",");
87 }
88 res.append(key);
89 res.append("->");
90 res.append(node);
91 cnt++;
92 }
93 res.append("]");
94
95 return res.toString();
96 }
97
98 protected KeyStroke key;
99 }