001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.mobility.project.ui.customizer;
043:
044: import java.io.File;
045: import java.net.URI;
046: import java.awt.*;
047: import javax.swing.Icon;
048: import javax.swing.ImageIcon;
049: import org.netbeans.api.project.ant.AntArtifact;
050: import org.netbeans.api.project.libraries.Library;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.util.Utilities;
053:
054: /** Represents classpath items of various types. Can be used in the model
055: * of classpath editing controls.
056: *
057: * @author phrebejk
058: */
059: public final class VisualClassPathItem {
060:
061: // Types of the classpath elements
062: public static final int TYPE_JAR = 0;
063: public static final int TYPE_LIBRARY = 1;
064: public static final int TYPE_ARTIFACT = 2;
065: public static final int TYPE_CLASSPATH = 3;
066: public static final int TYPE_FOLDER = 4;
067:
068: final private static String RESOURCE_ICON_JAR = "org/netbeans/modules/project/support/customizer/resources/jar.gif"; //NOI18N
069: final private static String RESOURCE_ICON_FOLDER = "org/netbeans/modules/project/support/customizer/resources/folder.gif"; //NOI18N
070: final private static String RESOURCE_ICON_LIBRARY = "org/netbeans/modules/project/support/customizer/resources/libraries.gif"; //NOI18N
071: final private static String RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/project/support/customizer/resources/projectDependencies.gif"; //NOI18N
072: final private static String RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/project/support/customizer/resources/project.gif"; //NOI18N
073: final private static String RESOURCE_ICON_BROKEN = "org/netbeans/modules/project/support/customizer/resources/brokenProjectBadge.gif"; //NOI18N
074:
075: private static Icon ICON_JAR;
076: private static Icon ICON_LIBRARY;
077: private static Icon ICON_ARTIFACT;
078: private static Icon ICON_CLASSPATH;
079: private static Icon ICON_FOLDER;
080: private static Icon ICON_BROKEN_JAR;
081: private static Icon ICON_BROKEN_LIBRARY;
082: private static Icon ICON_BROKEN_ARTIFACT;
083: private static Icon ICON_BROKEN_CLASSPATH;
084: private static Icon ICON_BROKEN_FOLDER;
085:
086: static {
087: Image i;
088: Image broken = Utilities.loadImage(RESOURCE_ICON_BROKEN);
089:
090: i = Utilities.loadImage(RESOURCE_ICON_JAR);
091: ICON_JAR = new ImageIcon(i);
092: ICON_BROKEN_JAR = new ImageIcon(Utilities.mergeImages(i,
093: broken, 8, 0));
094:
095: i = Utilities.loadImage(RESOURCE_ICON_LIBRARY);
096: ICON_LIBRARY = new ImageIcon(i);
097: ICON_BROKEN_LIBRARY = new ImageIcon(Utilities.mergeImages(i,
098: broken, 8, 0));
099:
100: i = Utilities.loadImage(RESOURCE_ICON_ARTIFACT);
101: ICON_ARTIFACT = new ImageIcon(i);
102: ICON_BROKEN_ARTIFACT = new ImageIcon(Utilities.mergeImages(i,
103: broken, 8, 0));
104:
105: i = Utilities.loadImage(RESOURCE_ICON_CLASSPATH);
106: ICON_CLASSPATH = new ImageIcon(i);
107: ICON_BROKEN_CLASSPATH = new ImageIcon(Utilities.mergeImages(i,
108: broken, 8, 0));
109:
110: i = Utilities.loadImage(RESOURCE_ICON_FOLDER);
111: ICON_FOLDER = new ImageIcon(i);
112: ICON_BROKEN_FOLDER = new ImageIcon(Utilities.mergeImages(i,
113: broken, 8, 0));
114: }
115:
116: private int type;
117: private Object cpElement;
118: private String display;
119: private String rawText;
120: private URI uri;
121: private boolean extra = false;
122:
123: public static VisualClassPathItem create(final Library library) {
124: if (library == null) {
125: throw new IllegalArgumentException(
126: "library must not be null"); // NOI18N
127: }
128: final String libraryName = library.getName();
129: return new VisualClassPathItem(library, TYPE_LIBRARY, "${libs."
130: + libraryName + ".classpath}", library.getDisplayName()); //NOI18N
131:
132: }
133:
134: public static VisualClassPathItem create(
135: final AntArtifact artifact, final URI artifactURI) {
136: if (artifactURI == null) {
137: throw new IllegalArgumentException(
138: "artifactURI must not be null"); // NOI18N
139: }
140: if (artifact == null) {
141: throw new IllegalArgumentException(
142: "artifact must not be null"); // NOI18N
143: }
144: String location;
145: try {
146: location = FileUtil.normalizeFile(
147: new File(artifact.getScriptLocation()
148: .getParentFile().toURI().resolve(
149: artifactURI))).getPath();
150: } catch (Exception e) {
151: location = artifactURI.getPath();
152: }
153: return new VisualClassPathItem(artifact, artifactURI,
154: TYPE_ARTIFACT, null, location);
155: }
156:
157: public static VisualClassPathItem create(final File file) {
158: if (file == null) {
159: throw new IllegalArgumentException("file must not be null"); // NOI18N
160: }
161: return new VisualClassPathItem(file,
162: isJar(file.getName()) ? TYPE_JAR : TYPE_FOLDER, null,
163: file.getPath());
164: }
165:
166: private static boolean isJar(String s) {
167: if (s == null)
168: return false;
169: s = s.toLowerCase();
170: return s.endsWith(".jar") || s.endsWith(".zip"); //NOI18N
171: }
172:
173: public void setExtra(boolean extra) {
174: this .extra = extra;
175: }
176:
177: public boolean isExtra() {
178: return extra;
179: }
180:
181: public VisualClassPathItem(AntArtifact cpElement, URI uri,
182: int type, String rawText, String display) {
183: this (cpElement, type, rawText, display);
184: this .uri = uri;
185: }
186:
187: public VisualClassPathItem(Object cpElement, int type,
188: String rawText, String display) {
189: this .cpElement = cpElement;
190: this .type = type;
191: this .display = display;
192: this .rawText = rawText;
193: }
194:
195: public Object getElement() {
196: return cpElement;
197: }
198:
199: public URI getURI() {
200: return uri;
201: }
202:
203: public int getType() {
204: return type;
205: }
206:
207: public String getDisplayName() {
208: return display;
209: }
210:
211: public String getRawText() {
212: return rawText;
213: }
214:
215: public boolean canDelete() {
216: return getType() != TYPE_CLASSPATH;
217: }
218:
219: public Icon getIcon() {
220:
221: switch (getType()) {
222: case TYPE_JAR:
223: return getElement() != null ? ICON_JAR : ICON_BROKEN_JAR;
224: case TYPE_FOLDER:
225: return getElement() != null ? ICON_FOLDER
226: : ICON_BROKEN_FOLDER;
227: case TYPE_LIBRARY:
228: return getElement() != null ? ICON_LIBRARY
229: : ICON_BROKEN_LIBRARY;
230: case TYPE_ARTIFACT:
231: return getElement() != null ? ICON_ARTIFACT
232: : ICON_BROKEN_ARTIFACT;
233: case TYPE_CLASSPATH:
234: return getElement() != null ? ICON_CLASSPATH
235: : ICON_BROKEN_CLASSPATH;
236: default:
237: return null;
238: }
239:
240: }
241:
242: public String toString() {
243: return getDisplayName();
244: }
245:
246: public int hashCode() {
247:
248: int hash = getType();
249:
250: switch (getType()) {
251: case TYPE_ARTIFACT:
252: final AntArtifact aa = (AntArtifact) getElement();
253: if (aa == null) {
254: if (rawText != null)
255: hash += rawText.hashCode();
256: break;
257: }
258:
259: hash += aa.getType().hashCode();
260: hash += aa.getScriptLocation().hashCode();
261: hash += uri.hashCode();
262: break;
263: default:
264: final Object element = getElement();
265: if (element != null)
266: hash += element.hashCode();
267: break;
268: }
269:
270: return hash;
271: }
272:
273: public boolean equals(final Object object) {
274: //fix for 98455 - obvious case is missing
275: if (this == object)
276: return true;
277:
278: if (!(object instanceof VisualClassPathItem)) {
279: return false;
280: }
281: final VisualClassPathItem vcpi = (VisualClassPathItem) object;
282:
283: if (getType() != vcpi.getType()) {
284: return false;
285: }
286:
287: switch (getType()) {
288: case TYPE_ARTIFACT:
289: final AntArtifact aa1 = (AntArtifact) getElement();
290: final AntArtifact aa2 = (AntArtifact) vcpi.getElement();
291: if (aa1 == null || aa2 == null) {
292: if (getRawText() != null && vcpi.getRawText() != null)
293: if (getRawText().equals(vcpi.getRawText()))
294: return true;
295: return false;
296: }
297:
298: if (aa1.getType() != aa2.getType()) {
299: return false;
300: }
301:
302: if (!aa1.getScriptLocation()
303: .equals(aa2.getScriptLocation())) {
304: return false;
305: }
306:
307: if (!getURI().equals(vcpi.getURI())) {
308: return false;
309: }
310:
311: return true;
312: default:
313: final Object element = getElement();
314: final Object cElement = vcpi.getElement();
315: //Special case when the library is missing but we want compare values
316: if (cElement == null && element == null
317: && vcpi.getRawText().equals(getRawText()))
318: return true;
319: //And another special case when library was removed from manager
320: if (element == null && cElement instanceof Library
321: && toString().equals(vcpi.toString())
322: && vcpi.getRawText().equals(getRawText()))
323: return true;
324:
325: if (element == null)
326: return false;
327:
328: return element.equals(vcpi.getElement());
329: }
330:
331: }
332:
333: }
|