KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > LibraryStandin


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.debug.ui.jres;
12
13 import java.net.URL JavaDoc;
14 import java.text.MessageFormat JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
22 import org.eclipse.jdt.launching.LibraryLocation;
23
24
25 /**
26  * Wrapper for an original library location, to support editing.
27  *
28  */

29 public final class LibraryStandin {
30     private IPath fSystemLibrary;
31     private IPath fSystemLibrarySource;
32     private IPath fPackageRootPath;
33     private URL JavaDoc fJavadocLocation;
34     
35     /**
36      * Creates a new library standin on the given library location.
37      */

38     public LibraryStandin(LibraryLocation libraryLocation) {
39         fSystemLibrary= libraryLocation.getSystemLibraryPath();
40         setSystemLibrarySourcePath(libraryLocation.getSystemLibrarySourcePath());
41         setPackageRootPath(libraryLocation.getPackageRootPath());
42         setJavadocLocation(libraryLocation.getJavadocLocation());
43     }
44         
45     /**
46      * Returns the JRE library jar location.
47      *
48      * @return The JRE library jar location.
49      */

50     public IPath getSystemLibraryPath() {
51         return fSystemLibrary;
52     }
53     
54     /**
55      * Returns the JRE library source zip location.
56      *
57      * @return The JRE library source zip location.
58      */

59     public IPath getSystemLibrarySourcePath() {
60         return fSystemLibrarySource;
61     }
62     
63     /**
64      * Sets the source location for this library.
65      *
66      * @param path path source archive or Path.EMPTY if none
67      */

68     void setSystemLibrarySourcePath(IPath path) {
69         fSystemLibrarySource = path;
70     }
71     
72     /**
73      * Returns the path to the default package in the sources zip file
74      *
75      * @return The path to the default package in the sources zip file.
76      */

77     public IPath getPackageRootPath() {
78         return fPackageRootPath;
79     }
80     
81     /**
82      * Sets the root source location within source archive.
83      *
84      * @param path path to root source location or Path.EMPTY if none
85      */

86     void setPackageRootPath(IPath path) {
87         fPackageRootPath = path;
88     }
89     
90     /* (non-Javadoc)
91      * @see java.lang.Object#equals(java.lang.Object)
92      */

93     public boolean equals(Object JavaDoc obj) {
94         if (obj instanceof LibraryStandin) {
95             LibraryStandin lib = (LibraryStandin)obj;
96             return getSystemLibraryPath().equals(lib.getSystemLibraryPath())
97                 && equals(getSystemLibrarySourcePath(), lib.getSystemLibrarySourcePath())
98                 && equals(getPackageRootPath(), lib.getPackageRootPath())
99                 && equalsOrNull(getJavadocLocation(), lib.getJavadocLocation());
100         }
101         return false;
102     }
103
104     /* (non-Javadoc)
105      * @see java.lang.Object#hashCode()
106      */

107     public int hashCode() {
108         return getSystemLibraryPath().hashCode();
109     }
110     
111     /**
112      * Returns whether the given paths are equal - either may be <code>null</code>.
113      * @param path1 path to be compared
114      * @param path2 path to be compared
115      * @return whether the given paths are equal
116      */

117     protected boolean equals(IPath path1, IPath path2) {
118         return equalsOrNull(path1, path2);
119     }
120     
121     /**
122      * Returns whether the given objects are equal - either may be <code>null</code>.
123      * @param o1 object to be compared
124      * @param o2 object to be compared
125      * @return whether the given objects are equal or both null
126      * @since 3.1
127      */

128     private boolean equalsOrNull(Object JavaDoc o1, Object JavaDoc o2) {
129         if (o1 == null) {
130             return o2 == null;
131         }
132         if (o2 == null) {
133             return false;
134         }
135         return o1.equals(o2);
136     }
137
138     /**
139      * Returns the Javadoc location associated with this Library location.
140      *
141      * @return a url pointing to the Javadoc location associated with
142      * this Library location, or <code>null</code> if none
143      * @since 3.1
144      */

145     public URL JavaDoc getJavadocLocation() {
146         return fJavadocLocation;
147     }
148     
149     /**
150      * Sets the javadoc location of this library.
151      *
152      * @param url The location of the javadoc for <code>library</code> or <code>null</code>
153      * if none
154      */

155     void setJavadocLocation(URL JavaDoc url) {
156         fJavadocLocation = url;
157     }
158
159     /**
160      * Returns an equivalent library location.
161      *
162      * @return library location
163      */

164     LibraryLocation toLibraryLocation() {
165         return new LibraryLocation(getSystemLibraryPath(), getSystemLibrarySourcePath(), getPackageRootPath(), getJavadocLocation());
166     }
167     
168     /**
169      * Returns a status for this library describing any error states
170      *
171      * @return
172      */

173     IStatus validate() {
174         if (!getSystemLibraryPath().toFile().exists()) {
175             return new Status(IStatus.ERROR, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR,
176                     MessageFormat.format(JREMessages.LibraryStandin_0, new String JavaDoc[]{getSystemLibraryPath().toOSString()}), null);
177         }
178         IPath path = getSystemLibrarySourcePath();
179         if (!path.isEmpty()) {
180             if (!path.toFile().exists()) {
181                 // check for workspace resource
182
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
183                 if (!resource.exists()) {
184                     return new Status(IStatus.ERROR, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR,
185                             MessageFormat.format(JREMessages.LibraryStandin_1, new String JavaDoc[]{path.toOSString()}), null);
186                 }
187             }
188         }
189         return Status.OK_STATUS;
190     }
191     
192 }
193
Popular Tags