KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
15  * <p>
16  * A location element that is used to select a specific part within the
17  * application. In the Eclipse workbench, this correspond with the concept of a
18  * part. Within JFace, this can refer to any similar ideas or can just be
19  * ignored.
20  * </p>
21  * <p>
22  * Clients may instantiate this class, but must not extend.
23  * </p>
24  * <p>
25  * <strong>PROVISIONAL</strong>. This class or interface has been added as
26  * part of a work in progress. There is a guarantee neither that this API will
27  * work nor that it will remain the same. Please do not use this API without
28  * consulting with the Platform/UI team.
29  * </p>
30  * <p>
31  * This class will eventually exist in <code>org.eclipse.jface.menus</code>.
32  * </p>
33  *
34  * @since 3.2
35  *
36  */

37 public final class SPart implements LocationElement {
38
39     /**
40      * The constant used to indicate that the <code>part</code> is a class.
41      */

42     public static final int TYPE_CLASS = 0;
43
44     /**
45      * The constant used to indicate that the <code>part</code> is an
46      * identifier.
47      */

48     public static final int TYPE_ID = 1;
49
50     /**
51      * The location within the part to place the menu element.
52      */

53     private final LeafLocationElement location;
54
55     /**
56      * The identifier of the part to which this instance refers, or the class
57      * (i.e., class, super class, interface or super interface) of the part.
58      * This value must not be <code>null</code>.
59      */

60     private final String JavaDoc part;
61
62     /**
63      * The type of information carried in <code>part</code>.
64      */

65     private final int type;
66
67     /**
68      * Constructs a new instance of <code>SPart</code>.
69      *
70      * @param part
71      * The part to which this refers. Either its identifier or its
72      * class. Must not be <code>null</code>.
73      * @param type
74      * The type of data carried in <code>part</code>.
75      * @param location
76      * The location within this part that the menu element should be
77      * placed. Must not be <code>null</code>.
78      */

79     public SPart(final String JavaDoc part, final int type,
80             final LeafLocationElement location) {
81         if (part == null) {
82             throw new NullPointerException JavaDoc("A part needs a class or id"); //$NON-NLS-1$
83
}
84         if ((type < TYPE_CLASS) || (type > TYPE_ID)) {
85             throw new IllegalArgumentException JavaDoc(
86                     "The part must be either a class or an identifier"); //$NON-NLS-1$
87
}
88         if (location == null) {
89             throw new NullPointerException JavaDoc(
90                     "A part needs a location for the menu element to appear"); //$NON-NLS-1$
91
}
92
93         this.part = part;
94         this.type = type;
95         this.location = location;
96     }
97
98     public final LocationElement createChild(final String JavaDoc id) {
99         final LeafLocationElement childPath = (LeafLocationElement) getLocation()
100                 .createChild(id);
101         return new SPart(getPart(), getType(), childPath);
102     }
103
104     /**
105      * Returns the location within this part that the menu element should be
106      * placed. Must not be <code>null</code>.
107      *
108      * @return The location in which the menu element should be placed; never
109      * <code>null</code>.
110      */

111     public final LeafLocationElement getLocation() {
112         return location;
113     }
114
115     /**
116      * Returns either the class name to which this part refers or the identifier
117      * of the part. The type is indicated by a call to {@link SPart#getType()}
118      *
119      * @return The class name or identifier; never <code>null</code>.
120      */

121     public final String JavaDoc getPart() {
122         return part;
123     }
124
125     /**
126      * Returns the type of this part. This indicates whether this part refers to
127      * an identifier or to a class.
128      *
129      * @return Either <code>TYPE_CLASS</code> or <code>TYPE_ID</code>.
130      */

131     public final int getType() {
132         return type;
133     }
134
135     public final String JavaDoc toString() {
136         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137         buffer.append("SPart("); //$NON-NLS-1$
138
switch (type) {
139         case TYPE_CLASS:
140             buffer.append("class"); //$NON-NLS-1$
141
break;
142         case TYPE_ID:
143             buffer.append("id"); //$NON-NLS-1$
144
break;
145         default:
146             buffer.append("unknown"); //$NON-NLS-1$
147
}
148         buffer.append(',');
149         buffer.append(part);
150         buffer.append(',');
151         buffer.append(location);
152         buffer.append(')');
153         return buffer.toString();
154     }
155 }
156
Popular Tags