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-2007 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.compapp.projects.jbi.ui.customizer;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.util.Iterator;
048: import java.util.Properties;
049: import org.netbeans.api.project.ant.AntArtifact;
050: import org.netbeans.spi.project.ant.AntArtifactProvider;
051: import org.netbeans.api.project.libraries.Library;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.ProjectInformation;
054: import org.netbeans.modules.compapp.projects.jbi.api.JbiProjectConstants;
055: import org.openide.util.Exceptions;
056: import org.openide.util.Utilities;
057: import javax.swing.Icon;
058: import javax.swing.ImageIcon;
059: import org.openide.filesystems.FileObject;
060:
061: /**
062: * Represents classpath items of various types. Can be used in the model of classpath editing
063: * controls.
064: *
065: * @author phrebejk
066: */
067: public class VisualClassPathItem {
068: // Types of the classpath elements
069:
070: /**
071: * DOCUMENT ME!
072: */
073: public static final int TYPE_JAR = 0;
074:
075: /**
076: * DOCUMENT ME!
077: */
078: public static final int TYPE_LIBRARY = 1; // XXX Not used yet
079:
080: /**
081: * DOCUMENT ME!
082: */
083: public static final int TYPE_ARTIFACT = 2;
084:
085: /**
086: * DOCUMENT ME!
087: */
088: public static final int TYPE_CLASSPATH = 3; // XXX Not used yet
089:
090: private static String RESOURCE_ICON_JAR = "org/netbeans/modules/compapp/projects/jbi/ui/resources/jar.gif"; // NOI18N
091: private static String RESOURCE_ICON_LIBRARY = "org/netbeans/modules/compapp/projects/jbi/ui/resources/libraries.gif"; // NOI18N
092: private static String RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/compapp/projects/jbi/ui/resources/projectDependencies.gif"; // NOI18N
093: private static String RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/compapp/projects/jbi/ui/resources/j2seProject.gif"; // NOI18N
094: private static Icon ICON_JAR = new ImageIcon(Utilities
095: .loadImage(RESOURCE_ICON_JAR));
096: private static Icon ICON_LIBRARY = new ImageIcon(Utilities
097: .loadImage(RESOURCE_ICON_LIBRARY));
098: private static Icon ICON_ARTIFACT = new ImageIcon(Utilities
099: .loadImage(RESOURCE_ICON_ARTIFACT));
100: private static Icon ICON_CLASSPATH = new ImageIcon(Utilities
101: .loadImage(RESOURCE_ICON_CLASSPATH));
102:
103: private int type;
104: private Object cpElement;
105: private String raw;
106: private String eval;
107: private String shortName;
108: private String projectName;
109: private boolean inDeployment;
110:
111: // ASA inforamtion
112: private String asaType; // target component ID, e.x., sun-bpel-engine
113: private String asaDescription;
114: private String asaTarget;
115: private Icon projIcon;
116:
117: /**
118: * Creates a new VisualClassPathItem object.
119: *
120: * @param cpElement DOCUMENT ME!
121: * @param type DOCUMENT ME!
122: * @param raw DOCUMENT ME!
123: * @param eval DOCUMENT ME!
124: * @param inDeployment DOCUMENT ME!
125: */
126: public VisualClassPathItem(Object cpElement, int type, String raw,
127: String eval, boolean inDeployment) {
128: this .cpElement = cpElement;
129: this .type = type;
130: this .raw = raw;
131: this .eval = eval;
132: this .inDeployment = inDeployment;
133: this .shortName = (eval != null) ? eval : raw;
134: this .projectName = ""; // NOI18N
135: this .asaType = ""; // NOI18N
136:
137: if (cpElement instanceof AntArtifact) {
138: shortName = new File(shortName).getName();
139:
140: AntArtifact aa = (AntArtifact) cpElement;
141:
142: ProjectInformation info = aa.getProject().getLookup()
143: .lookup(ProjectInformation.class);
144:
145: if (info != null) {
146: projectName = info.getDisplayName(); // e.x., SynchronousSample // #121834
147: // TMP FIX
148: // eval doesn't always give us the desired name
149: // JavaEE project: WebApplication.war (good)
150: // SU project: SEDeployment.jar (bad)
151: if (shortName.equals("SEDeployment.jar")) { // NOI18N
152: shortName = projectName + ".jar"; // NOI18N
153: }
154: projIcon = info.getIcon();
155: }
156:
157: // extract the JBI component type info
158: String aType = aa.getType(); // e.x., CAPS.asa:sun-bpel-engine
159: int idx = aType.indexOf(':');
160: if (idx > 0) {
161: asaType = aType.substring(idx + 1);
162: } else {
163: if (isJavaEEProjectAntArtifact(aa)) {
164: asaType = JbiProjectConstants.JAVA_EE_SE_COMPONENT_NAME;
165: }
166: }
167:
168: // Get service unit description from base project
169: // todo: add lookup in base project
170: FileObject projDir = aa.getProject().getProjectDirectory();
171: FileObject projPropFile = projDir
172: .getFileObject("nbproject/project.properties"); // NOI18N
173: if (projPropFile != null) {
174: InputStream is = null;
175: try {
176: Properties p = new Properties();
177: is = projPropFile.getInputStream();
178: p.load(is);
179: asaDescription = p
180: .getProperty("jbi.service-unit.description"); // NOI18N
181: if (asaDescription == null) {
182: asaDescription = p
183: .getProperty("com.sun.jbi.ui.devtool.jbi.description.application-sub-assembly"); // NOI18N
184: }
185: } catch (IOException ex) {
186: Exceptions.printStackTrace(ex);
187: } finally {
188: if (is != null) {
189: try {
190: is.close();
191: } catch (Exception e) {
192: }
193: }
194: }
195: }
196: }
197: }
198:
199: /**
200: * DOCUMENT ME!
201: *
202: * @param artifact DOCUMENT ME!
203: * @param pathInWar DOCUMENT ME!
204: *
205: * @return DOCUMENT ME!
206: */
207: public static VisualClassPathItem create(AntArtifact artifact,
208: String pathInWar) {
209: return new VisualClassPathItem(artifact,
210: VisualClassPathItem.TYPE_ARTIFACT, artifact
211: .getArtifactLocations()[0].toString(),
212: pathInWar, false);
213: }
214:
215: public static boolean isJavaEEProjectAntArtifact(AntArtifact aa) {
216: Project project = aa.getProject();
217: if (project != null) {
218: AntArtifactProvider prov = project.getLookup().lookup(
219: AntArtifactProvider.class);
220: if (prov != null) {
221: AntArtifact[] artifacts = prov.getBuildArtifacts();
222: Iterator<String> artifactTypeItr = null;
223: String artifactType = null;
224: if (artifacts != null) {
225: for (int i = 0; i < artifacts.length; i++) {
226: artifactTypeItr = JbiProjectConstants.JAVA_EE_AA_TYPES
227: .iterator();
228: while (artifactTypeItr.hasNext()) {
229: artifactType = artifactTypeItr.next();
230: if (artifacts[i].getType().startsWith(
231: artifactType)) {
232: return true;
233: }
234: }
235: }
236: }
237: }
238: }
239: return false;
240: }
241:
242: /**
243: * DOCUMENT ME!
244: *
245: * @return DOCUMENT ME!
246: */
247: public Object getObject() {
248: return cpElement;
249: }
250:
251: public AntArtifact getAntArtifact() {
252: return (AntArtifact) getObject();
253: }
254:
255: /**
256: * DOCUMENT ME!
257: *
258: * @return DOCUMENT ME!
259: */
260: public int getType() {
261: return type;
262: }
263:
264: /**
265: * DOCUMENT ME!
266: *
267: * @return DOCUMENT ME!
268: */
269: public String getRaw() {
270: return raw;
271: }
272:
273: /**
274: * DOCUMENT ME!
275: *
276: * @return DOCUMENT ME!
277: */
278: public String getProjectName() {
279: return projectName;
280: }
281:
282: /**
283: * DOCUMENT ME!
284: *
285: * @return DOCUMENT ME!
286: */
287: public String getAsaType() {
288: return asaType;
289: }
290:
291: /**
292: * DOCUMENT ME!
293: *
294: * @return DOCUMENT ME!
295: */
296: public String getShortName() {
297: return shortName;
298: }
299:
300: /**
301: * DOCUMENT ME!
302: *
303: * @return DOCUMENT ME!
304: */
305: public boolean isInDeployment() {
306: return inDeployment;
307: }
308:
309: /**
310: * DOCUMENT ME!
311: *
312: * @param inDeployment DOCUMENT ME!
313: */
314: public void setInDeployment(boolean inDeployment) {
315: this .inDeployment = inDeployment;
316: }
317:
318: /**
319: * DOCUMENT ME!
320: *
321: * @return DOCUMENT ME!
322: */
323: public String getEvaluated() {
324: return (eval == null) ? getRaw() : eval;
325: }
326:
327: /**
328: * DOCUMENT ME!
329: *
330: * @return DOCUMENT ME!
331: */
332: public boolean canDelete() {
333: return getType() != TYPE_CLASSPATH;
334: }
335:
336: /**
337: * DOCUMENT ME!
338: *
339: * @return DOCUMENT ME!
340: */
341: public Icon getIcon() {
342: switch (getType()) {
343: case TYPE_JAR:
344: return ICON_JAR;
345:
346: case TYPE_LIBRARY:
347: return ICON_LIBRARY;
348:
349: case TYPE_ARTIFACT:
350: return ICON_ARTIFACT;
351:
352: case TYPE_CLASSPATH:
353: return ICON_CLASSPATH;
354:
355: default:
356: return null;
357: }
358: }
359:
360: public Icon getProjectIcon() {
361: return this .projIcon;
362: }
363:
364: /**
365: * DOCUMENT ME!
366: *
367: * @return DOCUMENT ME!
368: */
369: public int hashCode() {
370: int hash = getType();
371:
372: switch (getType()) {
373: case TYPE_ARTIFACT:
374: if (getObject() != null) {
375: AntArtifact aa = (AntArtifact) getObject();
376:
377: hash += aa.getType().hashCode();
378: hash += aa.getScriptLocation().hashCode();
379: hash += aa.getArtifactLocations()[0].hashCode();
380: } else {
381: hash += getRaw().hashCode();
382: }
383: break;
384: default:
385: if (getObject() != null) {
386: hash += getObject().hashCode();
387: } else {
388: hash += getRaw().hashCode();
389: }
390: break;
391: }
392:
393: return hash;
394: }
395:
396: /**
397: * DOCUMENT ME!
398: *
399: * @param object DOCUMENT ME!
400: *
401: * @return DOCUMENT ME!
402: */
403: public boolean equals(Object object) {
404: if (!(object instanceof VisualClassPathItem)) {
405: return false;
406: }
407:
408: VisualClassPathItem vcpi = (VisualClassPathItem) object;
409:
410: if (getType() != vcpi.getType()) {
411: return false;
412: }
413:
414: switch (getType()) {
415: case TYPE_ARTIFACT:
416:
417: AntArtifact aa1 = (AntArtifact) getObject();
418: AntArtifact aa2 = (AntArtifact) vcpi.getObject();
419:
420: if ((aa1 == null) && (aa2 == null)) {
421: return true;
422: }
423:
424: if ((aa1 == null) && (aa2 != null)) {
425: return false;
426: }
427:
428: if ((aa1 != null) && (aa2 == null)) {
429: return false;
430: }
431:
432: if (!(aa1.getType().equals(aa2.getType()))) {
433: return false;
434: }
435:
436: if (!aa1.getScriptLocation()
437: .equals(aa2.getScriptLocation())) {
438: return false;
439: }
440:
441: if (!aa1.getArtifactLocations()[0].equals(aa2
442: .getArtifactLocations()[0])) {
443: return false;
444: }
445:
446: return true;
447:
448: default:
449: return getObject().equals(vcpi.getObject());
450: }
451: }
452:
453: /**
454: * DOCUMENT ME!
455: *
456: * @return DOCUMENT ME!
457: */
458: public String toString() {
459: switch (getType()) {
460: case TYPE_JAR:
461: return getEvaluated();
462:
463: case TYPE_LIBRARY:
464: return ((Library) this .getObject()).getDisplayName();
465:
466: case TYPE_ARTIFACT:
467: return shortName; // getEvaluated();
468:
469: case TYPE_CLASSPATH:
470: return getEvaluated();
471:
472: default:
473: assert true : "Unknown item type"; // NOI18N
474:
475: return getEvaluated();
476: }
477: }
478:
479: /**
480: * DOCUMENT ME!
481: *
482: * @return DOCUMENT ME!
483: */
484: public String getAsaDescription() {
485: return asaDescription;
486: }
487:
488: /**
489: * DOCUMENT ME!
490: *
491: * @param asaDescription DOCUMENT ME!
492: */
493: public void setAsaDescription(String asaDescription) {
494: this .asaDescription = asaDescription;
495: }
496:
497: /**
498: * DOCUMENT ME!
499: *
500: * @return DOCUMENT ME!
501: */
502: public String getAsaTarget() {
503: return asaTarget;
504: }
505:
506: /**
507: * DOCUMENT ME!
508: *
509: * @param asaTarget DOCUMENT ME!
510: */
511: public void setAsaTarget(String asaTarget) {
512: this.asaTarget = asaTarget;
513: }
514: }
|