KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal.menus;
13
14 import org.eclipse.jface.util.Util;
15 import org.eclipse.ui.internal.misc.Policy;
16
17 /**
18  * <p>
19  * A location element referring to a specific path within within the menu bar,
20  * tool bar or status line.
21  * </p>
22  * <p>
23  * Clients may instantiate this class, but must not extend.
24  * </p>
25  * <p>
26  * <strong>PROVISIONAL</strong>. This class or interface has been added as
27  * part of a work in progress. There is a guarantee neither that this API will
28  * work nor that it will remain the same. Please do not use this API without
29  * consulting with the Platform/UI team.
30  * </p>
31  * <p>
32  * This class will eventually exist in <code>org.eclipse.jface.menus</code>.
33  * </p>
34  *
35  * @since 3.2
36  */

37 public final class SBar extends LeafLocationElement {
38
39     /**
40      * The constant used for a menu bar.
41      */

42     public static final String JavaDoc TYPE_MENU = "menu"; //$NON-NLS-1$
43

44     /**
45      * The constant used for the tool bar.
46      */

47     public static final String JavaDoc TYPE_TRIM = "trim"; //$NON-NLS-1$
48

49     /**
50      * The type of bar this is making reference to.
51      */

52     private final String JavaDoc type;
53
54     /**
55      * Constructs a new instance of <code>SBar</code>. This bar will refer to
56      * the menu type. It will contribute the item to top-most node in the menu
57      * hierarchy.
58      */

59     public SBar() {
60         this(TYPE_MENU, null);
61     }
62
63     /**
64      * Constructs a new instance of <code>SBar</code>. This bar will refer to
65      * the menu type.
66      *
67      * @param path
68      * The path to the final location. If this value is
69      * <code>null</code>, it means that it should be inserted at
70      * the top-level of the bar.
71      * @see #TYPE_MENU
72      */

73     public SBar(final String JavaDoc path) {
74         this(TYPE_MENU, path);
75     }
76
77     /**
78      * Constructs a new instance of <code>SBar</code>.
79      *
80      * @param type
81      * The type of bar this is making reference to.
82      * @param path
83      * The path to the final location. If this value is
84      * <code>null</code>, it means that it should be inserted at
85      * the top-level of the bar.
86      * @see #TYPE_MENU
87      * @see #TYPE_STATUS
88      * @see #TYPE_TRIM
89      */

90     public SBar(final String JavaDoc type, final String JavaDoc path) {
91         super(path);
92         this.type = type;
93     }
94
95     public final LocationElement createChild(final String JavaDoc id) {
96         final String JavaDoc parentPath = getPath();
97         final String JavaDoc path;
98         if (parentPath == null) {
99             path = id;
100         } else {
101             path = parentPath + PATH_SEPARATOR + id;
102         }
103         return new SBar(getType(), path);
104     }
105
106     public final ILocationElementTokenizer getTokenizer() {
107         if (Policy.EXPERIMENTAL_MENU && getPath() != null
108                 && getPath().indexOf(LeafLocationElement.BREAKPOINT_PATH) > -1) {
109             System.err.println("getTokenizer: " + getPath()); //$NON-NLS-1$
110
}
111         return new ILocationElementTokenizer() {
112             String JavaDoc remainingPath = getPath();
113
114             String JavaDoc parsedPath = null;
115
116             public final LocationElementToken nextToken() {
117                 final SLocation location = new SLocation(new SBar(getType(),
118                         parsedPath));
119                 final int separator = remainingPath
120                         .indexOf(LeafLocationElement.PATH_SEPARATOR);
121                 final String JavaDoc id;
122                 if (separator == -1) {
123                     id = remainingPath;
124                     remainingPath = null;
125                 } else {
126                     id = remainingPath.substring(0, separator);
127                     remainingPath = remainingPath.substring(separator + 1);
128                     if (remainingPath.length()==0) {
129                         remainingPath = null;
130                     }
131                 }
132                 if (parsedPath==null) {
133                     parsedPath = id;
134                 } else {
135                     parsedPath = parsedPath + '/' + id;
136                 }
137                 return new LocationElementToken(location, id);
138             }
139
140             public final boolean hasMoreTokens() {
141                 return remainingPath != null;
142             }
143         };
144     }
145
146     /**
147      * Returns the type for this bar.
148      *
149      * @return The type for this bar.
150      */

151     public final String JavaDoc getType() {
152         return type;
153     }
154
155     public final String JavaDoc toString() {
156         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
157         buffer.append("SBar("); //$NON-NLS-1$
158
buffer.append(type);
159         buffer.append(',');
160         buffer.append(getPath());
161         buffer.append(')');
162         return buffer.toString();
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.ui.internal.menus.LeafLocationElement#equals(java.lang.Object)
167      */

168     public boolean equals(Object JavaDoc obj) {
169         if (this==obj) {
170             return true;
171         }
172         if (obj instanceof SBar) {
173             SBar bar = (SBar) obj;
174             return Util.equals(type, bar.type) && super.equals(obj);
175         }
176         return false;
177     }
178
179     /* (non-Javadoc)
180      * @see org.eclipse.ui.internal.menus.LeafLocationElement#hashCode()
181      */

182     public int hashCode() {
183         return Util.hashCode(type) + super.hashCode();
184     }
185 }
186
Popular Tags