KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > OrderNode


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.menus;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.commands.common.IIdentifiable;
19
20 /**
21  * <p>
22  * A decorator on top of a layout node which allows it to hold ordering
23  * information -- including nodes that are ordering relative to it. An order
24  * node tracks whether it should appear in the beginning, middle or end block.
25  * It also holds layout nodes that are intended to appear before or after it.
26  * </p>
27  * <p>
28  * This class is only intended to be used from within the
29  * <code>org.eclipse.jface</code> plug-in.
30  * </p>
31  * <p>
32  * <strong>PROVISIONAL</strong>. This class or interface has been added as
33  * part of a work in progress. There is a guarantee neither that this API will
34  * work nor that it will remain the same. Please do not use this API without
35  * consulting with the Platform/UI team.
36  * </p>
37  * <p>
38  * This class will eventually exist in <code>org.eclipse.jface.menus</code>.
39  * </p>
40  *
41  * @since 3.2
42  */

43 final class OrderNode implements IIdentifiable {
44
45     /**
46      * The nodes that should appear after this ordering node, in the order they
47      * should appear. This value is <code>null</code> if there are no such
48      * nodes.
49      */

50     private List JavaDoc after;
51
52     /**
53      * The nodes that should appear before this ordering node, in the order they
54      * should appear. This value is <code>null</code> if there are no such
55      * nodes.
56      */

57     private List JavaDoc before;
58
59     /**
60      * The layout node associated with this ordering node; never
61      * <code>null</code>.
62      */

63     private final LayoutNode layoutNode;
64
65     /**
66      * Constructs a new instance of <code>OrderNode</code>.
67      *
68      * @param layoutNode
69      * The layout node which is to be ordered; must not be
70      * <code>null</code>.
71      */

72     OrderNode(final LayoutNode layoutNode) {
73         if (layoutNode == null) {
74             throw new NullPointerException JavaDoc("The layout node cannot be null"); //$NON-NLS-1$
75
}
76
77         this.layoutNode = layoutNode;
78     }
79
80     /**
81      * Add a node to the list of nodes that appear after this one. This is an
82      * alphabetical sorted insert by id.
83      *
84      * @param orderNode
85      * The node to appear after this one; never <code>null</code>.
86      */

87     public final void addAfterNode(final OrderNode orderNode) {
88         if (after == null) {
89             after = new ArrayList JavaDoc(1);
90             after.add(orderNode);
91             return;
92         }
93
94         LayoutNode.sortedInsert(after, orderNode);
95     }
96
97     /**
98      * Add a node to the list of nodes that appear before this one. This is an
99      * alphabetical sorted insert by id.
100      *
101      * @param orderNode
102      * The node to appear before this one; never <code>null</code>.
103      */

104     public final void addBeforeNode(final OrderNode orderNode) {
105         if (before == null) {
106             before = new ArrayList JavaDoc(1);
107             before.add(orderNode);
108             return;
109         }
110
111         LayoutNode.sortedInsert(before, orderNode);
112     }
113
114     /**
115      * @param sortedChildren
116      */

117     public final void addTo(final ArrayList JavaDoc sortedChildren) {
118         Iterator JavaDoc itr;
119
120         if (before != null) {
121             itr = before.iterator();
122             while (itr.hasNext()) {
123                 final OrderNode node = (OrderNode) itr.next();
124                 node.addTo(sortedChildren);
125             }
126         }
127
128         sortedChildren.add(getLayoutNode());
129
130         if (after != null) {
131             itr = after.iterator();
132             while (itr.hasNext()) {
133                 final OrderNode node = (OrderNode) itr.next();
134                 node.addTo(sortedChildren);
135             }
136         }
137     }
138
139     /**
140      * Returns the nodes that should appear after this node, in the order they
141      * should appear.
142      *
143      * @return The nodes that should appear after this node; never
144      * <code>null</code>.
145      */

146     public final List JavaDoc getAfterNodes() {
147         if (after == null) {
148             return Collections.EMPTY_LIST;
149         }
150
151         return after;
152     }
153
154     /**
155      * Returns the nodes that should appear before this node, in the order they
156      * should appear.
157      *
158      * @return The nodes that should appear before this node; never
159      * <code>null</code>.
160      */

161     public final List JavaDoc getBeforeNodes() {
162         if (before == null) {
163             return Collections.EMPTY_LIST;
164         }
165
166         return before;
167     }
168
169     public final String JavaDoc getId() {
170         return layoutNode.getId();
171     }
172
173     /**
174      * Returns the main layout node for this order node.
175      *
176      * @return The layout node; never <code>null</code>.
177      */

178     public final Object JavaDoc getLayoutNode() {
179         return layoutNode;
180     }
181     
182     public final String JavaDoc toString() {
183         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
184         buffer.append("OrderNode("); //$NON-NLS-1$
185
buffer.append(layoutNode);
186         buffer.append(')');
187         return buffer.toString();
188     }
189 }
Popular Tags