KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.menus;
13
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.commands.common.NotDefinedException;
20
21 /**
22  * <p>
23  * The layout of various menu elements. A layout includes all menus, groups,
24  * items and widgets -- regardless of whether they are currently visible.
25  * </p>
26  * <p>
27  * There are three basic types of menu elements: part, popup and bar. Each of
28  * these fundamental types has a map of subelements -- indexed by some string.
29  * </p>
30  * <p>
31  * Clients must not instantiate, and must not extend.
32  * </p>
33  * <p>
34  * <strong>PROVISIONAL</strong>. This class or interface has been added as
35  * part of a work in progress. There is a guarantee neither that this API will
36  * work nor that it will remain the same. Please do not use this API without
37  * consulting with the Platform/UI team.
38  * </p>
39  * <p>
40  * This class will eventually exist in <code>org.eclipse.jface.menus</code>.
41  * </p>
42  *
43  * @since 3.2
44  */

45 public final class SMenuLayout extends SPartMenuLayout {
46
47     /**
48      * Generates a menu layout based on a collection of menu elements. This
49      * layout will take into account ordering constraints.
50      *
51      * @param menuElements
52      * The menu elements to be sorted into a menu layout; must not
53      * <code>null</code>.
54      * @return The menu layout corresponding to the menu elements; never
55      * <code>null</code>.
56      */

57     static final SMenuLayout computeLayout(final Collection JavaDoc menuElements) {
58         final SMenuLayout layout = new SMenuLayout();
59
60         final Iterator JavaDoc elementItr = menuElements.iterator();
61         while (elementItr.hasNext()) {
62             final MenuElement element = (MenuElement) elementItr.next();
63             try {
64                 final SLocation[] locations = element.getLocations();
65                 for (int i = 0; i < locations.length; i++) {
66                     final SLocation location = locations[i];
67                     final LocationElement locationElement = location.getPath();
68                     if (locationElement instanceof SBar) {
69                         final SBar bar = (SBar) locationElement;
70                         layout.addBar(element, location, bar);
71
72                     } else if (locationElement instanceof SPopup) {
73                         final SPopup popup = (SPopup) locationElement;
74                         layout.addPopup(element, location, popup);
75
76                     } else if (locationElement instanceof SPart) {
77                         final SPart part = (SPart) locationElement;
78                         layout.addPart(element, location, part);
79
80                     }
81                 }
82             } catch (final NotDefinedException e) {
83                 // This menu element is not defined. Just skip it.
84
}
85         }
86
87         return layout;
88     }
89
90     /**
91      * The menu layouts for the various parts, indexed by identifier and type ({@link String}).
92      * TODO Figure out the real structure for this item.
93      */

94     private final Map JavaDoc partsById;
95
96     /**
97      * Constructs a new instance of <code>SMenuLayout</code>.
98      */

99     SMenuLayout() {
100         this.partsById = new HashMap JavaDoc();
101     }
102
103     /**
104      * Adds an entry into a part-level menu layout. A part can contain a menu
105      * bar or a tool bar.
106      *
107      * @param element
108      * The element to insert into the node; must not be
109      * <code>null</code>.
110      * @param location
111      * The location at which the element is to be inserted; may be
112      * <code>null</code>.
113      * @param part
114      * The part indicator; must not be <code>null</code>.
115      * @throws NotDefinedException
116      * If the given menu element is not defined.
117      */

118     private final void addPart(final MenuElement element,
119             final SLocation location, final SPart part)
120             throws NotDefinedException {
121         final String JavaDoc partIdOrType = part.getPart();
122         SPartMenuLayout layout = (SPartMenuLayout) partsById.get(partIdOrType);
123         if (layout == null) {
124             layout = new SPartMenuLayout();
125             partsById.put(partIdOrType, layout);
126         }
127         final LeafLocationElement locationElement = part.getLocation();
128         if (locationElement instanceof SBar) {
129             final SBar bar = (SBar) locationElement;
130             layout.addBar(element, location, bar);
131
132         } else if (locationElement instanceof SPopup) {
133             final SPopup popup = (SPopup) locationElement;
134             layout.addPopup(element, location, popup);
135
136         }
137     }
138
139     /**
140      * Prints out some debugging information about the entire menu layout. This
141      * information is quite large.
142      */

143     public final String JavaDoc toString() {
144         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
145         Iterator JavaDoc entryItr;
146
147         // Print out the bars.
148
buffer.append(" ***** TOP-LEVEL BARS ***** \n"); //$NON-NLS-1$
149
entryItr = getBarsByType().entrySet().iterator();
150         while (entryItr.hasNext()) {
151             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryItr.next();
152             final String JavaDoc type = (String JavaDoc) entry.getKey();
153             buffer.append(type);
154             buffer.append('\n');
155             LayoutNode node = (LayoutNode) entry.getValue();
156             printNode(node, buffer, 2);
157         }
158
159         // Print out the parts.
160
buffer.append(" ***** PART-SPECIFIC BARS ***** \n"); //$NON-NLS-1$
161
entryItr = partsById.entrySet().iterator();
162         while (entryItr.hasNext()) {
163             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryItr.next();
164             final String JavaDoc partId = (String JavaDoc) entry.getKey();
165             buffer.append(partId);
166             buffer.append('\n');
167             final SPartMenuLayout layout = (SPartMenuLayout) entry.getValue();
168             buffer.append(layout.toString());
169         }
170
171         // Print out the context menus.
172
buffer.append(" ***** CONTEXT MENUS ***** \n"); //$NON-NLS-1$
173
entryItr = getPopupsById().entrySet().iterator();
174         while (entryItr.hasNext()) {
175             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryItr.next();
176             final String JavaDoc id = (String JavaDoc) entry.getKey();
177             buffer.append(id);
178             buffer.append('\n');
179             LayoutNode node = (LayoutNode) entry.getValue();
180             printNode(node, buffer, 2);
181         }
182
183         return buffer.toString();
184     }
185 }
186
Popular Tags