001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.tmap.nodes;
021:
022: import java.awt.Image;
023: import java.util.concurrent.atomic.AtomicReference;
024: import javax.swing.Icon;
025: import javax.swing.ImageIcon;
026: import org.netbeans.modules.xslt.tmap.model.api.Invoke;
027: import org.netbeans.modules.xslt.tmap.model.api.Operation;
028: import org.netbeans.modules.xslt.tmap.model.api.Param;
029: import org.netbeans.modules.xslt.tmap.model.api.Service;
030: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
031: import org.netbeans.modules.xslt.tmap.model.api.Transform;
032: import org.netbeans.modules.xslt.tmap.model.api.TransformMap;
033: import org.openide.util.NbBundle;
034: import org.openide.util.Utilities;
035:
036: /**
037: *
038: * @author Vitaly Bychkov
039: * @version 1.0
040: */
041: public enum NodeType {
042: UNKNOWN_TYPE, // Special element which means that the value isn't known.
043: TRANSFORMMAP(TransformMap.class), SERVICE(Service.class), OPERATION(
044: Operation.class), INVOKE(Invoke.class), TRANSFORM(
045: Transform.class), PARAM(Param.class);
046:
047: private AtomicReference<String> myDisplayName = new AtomicReference<String>();
048: private AtomicReference<Image> myDefaultImage = new AtomicReference<Image>();
049: private Class<? extends TMapComponent> myComponentType;
050: private static final String IMAGE_FOLDER_PATH = "org/netbeans/modules/xslt/tmap/resources/images/"; // NOI18N
051: public static final Image UNKNOWN_IMAGE = getImageImpl(
052: UNKNOWN_TYPE, null);
053:
054: private NodeType() {
055: }
056:
057: private NodeType(Class<? extends TMapComponent> tMapComponentType) {
058: myComponentType = tMapComponentType;
059: }
060:
061: public static NodeType getNodeType(TMapComponent entity) {
062: if (entity == null) {
063: return null;
064: }
065: return getNodeType(entity.getComponentType());
066: }
067:
068: public static NodeType getNodeType(
069: Class<? extends TMapComponent> tMapComponentType) {
070: NodeType type = UNKNOWN_TYPE;
071: NodeType[] types = values();
072: for (NodeType typeEl : types) {
073: if (tMapComponentType == typeEl.getComponentType()) {
074: type = typeEl;
075: break;
076: }
077: }
078: return type;
079: }
080:
081: public Class<? extends TMapComponent> getComponentType() {
082: return myComponentType;
083: }
084:
085: public String getDisplayName() {
086: if (myDisplayName.get() == null) {
087: try {
088: myDisplayName.compareAndSet(null, NbBundle.getMessage(
089: NodeType.class, this .toString()));
090: } catch (Exception ex) {
091: myDisplayName.compareAndSet(null, name());
092: }
093: }
094: return myDisplayName.get();
095: }
096:
097: public String getDisplayName(Object modificator) {
098: String displayName = null;
099: try {
100: String key = this .toString() + "_" + modificator.toString();
101: displayName = NbBundle.getMessage(NodeType.class, key);
102: } catch (Exception ex) {
103: // do nothing
104: }
105: // can return null intentionally!
106: return displayName;
107: }
108:
109: public Icon getIcon() {
110: return new ImageIcon(getImage());
111: }
112:
113: public Image getImage() {
114: if (myDefaultImage.get() == null) {
115: Image image = getImageImpl(this , null);
116: if (image == null) {
117: image = UNKNOWN_IMAGE;
118: }
119: //
120: myDefaultImage.compareAndSet(null, image);
121: }
122: return myDefaultImage.get();
123: }
124:
125: private static Image getImageImpl(Object name, Object modificator) {
126: String fileName = null;
127: if (modificator == null) {
128: fileName = IMAGE_FOLDER_PATH + name + ".png"; // NOI18N
129: } else {
130: fileName = IMAGE_FOLDER_PATH + name + "_" + modificator
131: + ".png"; // NOI18N
132: }
133: return Utilities.loadImage(fileName);
134: }
135: }
|