KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 /**
17  * <p>
18  * A location element referring to a specific location within one or more popup
19  * menus in the application. If the <code>id</code> is <code>null</code>,
20  * then this location element refers to all popup menus. If it is not
21  * <code>null</code>, then it refers to all popup menus with that id. Popups
22  * menus are also known as context menus. They are typically opened by clicking
23  * the right mouse button.
24  * </p>
25  * <p>
26  * Clients may instantiate this class, but must not extend.
27  * </p>
28  * <p>
29  * <strong>PROVISIONAL</strong>. This class or interface has been added as
30  * part of a work in progress. There is a guarantee neither that this API will
31  * work nor that it will remain the same. Please do not use this API without
32  * consulting with the Platform/UI team.
33  * </p>
34  * <p>
35  * This class will eventually exist in <code>org.eclipse.jface.menus</code>.
36  * </p>
37  *
38  * @since 3.2
39  */

40 public final class SPopup extends LeafLocationElement {
41
42     /**
43      * The identifier of the popup in which the menu element should appear. If
44      * this value is <code>null</code>, then it applies to all popup menus.
45      */

46     private final String JavaDoc id;
47
48     /**
49      * Constructs a new instance of <code>SPopup</code>.
50      *
51      * @param id
52      * The identifier of the popup in which the menu should appear;
53      * may be <code>null</code> if this applies to all context
54      * menus.
55      * @param path
56      * The path to the final location. If this value is
57      * <code>null</code>, it means that it should be inserted at
58      * the top-level of the context menu.
59      */

60     public SPopup(final String JavaDoc id, final String JavaDoc path) {
61         super(path);
62
63         this.id = id;
64     }
65
66     public final LocationElement createChild(final String JavaDoc id) {
67         final String JavaDoc parentPath = getPath();
68         final String JavaDoc path;
69         if (parentPath == null) {
70             path = id;
71         } else {
72             path = parentPath + PATH_SEPARATOR + id;
73         }
74         return new SPopup(getId(), path);
75     }
76
77     /**
78      * Returns the identifier of the popup. If the identifier is
79      * <code>null</code>, then this location element refers to all context
80      * menus.
81      *
82      * @return The identifier of the popup; may be <code>null</code>.
83      */

84     public final String JavaDoc getId() {
85         return id;
86     }
87
88     public final ILocationElementTokenizer getTokenizer() {
89         return new ILocationElementTokenizer() {
90             String JavaDoc remainingPath = getPath();
91
92             String JavaDoc parsedPath = null;
93
94             public final LocationElementToken nextToken() {
95                 final SLocation location = new SLocation(new SPopup(getId(),
96                         parsedPath));
97                 final int separator = remainingPath
98                         .indexOf(LeafLocationElement.PATH_SEPARATOR);
99                 final String JavaDoc id;
100                 if (separator == -1) {
101                     id = remainingPath;
102                     remainingPath = null;
103                 } else {
104                     id = remainingPath.substring(0, separator);
105                     remainingPath = remainingPath.substring(separator + 1);
106                 }
107                 parsedPath = parsedPath + id;
108                 return new LocationElementToken(location, id);
109             }
110
111             public final boolean hasMoreTokens() {
112                 return remainingPath != null;
113             }
114         };
115     }
116
117     public final String JavaDoc toString() {
118         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
119         buffer.append("SPopup("); //$NON-NLS-1$
120
buffer.append(id);
121         buffer.append(',');
122         buffer.append(getPath());
123         buffer.append(')');
124         return buffer.toString();
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.ui.internal.menus.LeafLocationElement#equals(java.lang.Object)
129      */

130     public boolean equals(Object JavaDoc obj) {
131         if (this==obj) {
132             return true;
133         }
134         if (obj instanceof SPopup) {
135             SPopup popup = (SPopup) obj;
136             return Util.equals(id, popup.id) && super.equals(obj);
137         }
138         return false;
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.ui.internal.menus.LeafLocationElement#hashCode()
143      */

144     public int hashCode() {
145         return Util.hashCode(id) + super.hashCode();
146     }
147 }
148
Popular Tags