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.vmd.palette;
043:
044: import java.awt.Image;
045: import java.beans.BeanInfo;
046: import java.io.IOException;
047: import java.util.List;
048: import org.netbeans.modules.vmd.api.io.DataObjectContext;
049: import org.netbeans.modules.vmd.api.io.ProjectUtils;
050: import org.netbeans.modules.vmd.api.model.ComponentProducer;
051: import org.netbeans.modules.vmd.api.model.DescriptorRegistry;
052: import org.netbeans.modules.vmd.api.model.DesignDocument;
053: import org.netbeans.modules.vmd.api.model.common.ActiveDocumentSupport;
054: import org.openide.loaders.DataNode;
055: import org.openide.nodes.Children;
056: import org.openide.util.Lookup;
057: import org.openide.util.Utilities;
058: import org.openide.util.lookup.AbstractLookup;
059: import org.openide.util.lookup.InstanceContent;
060: import org.openide.util.lookup.Lookups;
061:
062: /**
063: *
064: * @author Anton Chechel
065: */
066: public class PaletteItemDataNode extends DataNode {
067:
068: private static Image errorBadge;
069: private PaletteItemDataObject obj;
070: private Lookup lookup;
071: private boolean isValid = true;
072: private boolean needCheck = true;
073: static {
074: errorBadge = Utilities
075: .loadImage("org/netbeans/modules/vmd/palette/resources/error-badge.gif"); // NOI18N
076: }
077:
078: public PaletteItemDataNode(PaletteItemDataObject obj) {
079: this (obj, new InstanceContent());
080: }
081:
082: private PaletteItemDataNode(PaletteItemDataObject obj,
083: InstanceContent ic) {
084: super (obj, Children.LEAF, new AbstractLookup(ic));
085: ic.add(obj);
086: ic.add(this );
087: this .obj = obj;
088: lookup = Lookups.singleton(this );
089: }
090:
091: @Override
092: public String getDisplayName() {
093: return obj.getDisplayName();
094: }
095:
096: @Override
097: public String getShortDescription() {
098: return obj.getToolTip();
099: }
100:
101: @Override
102: public Image getIcon(int type) {
103: if (needCheck) {
104: PaletteMap.getInstance().checkValidity(getProjectType(),
105: lookup);
106: }
107:
108: if (type == BeanInfo.ICON_COLOR_16x16
109: || type == BeanInfo.ICON_MONO_16x16) {
110: String iconPath = obj.getIcon();
111: Image icon = null;
112: if (iconPath != null) {
113: icon = Utilities.loadImage(iconPath);
114: }
115: if (icon == null) {
116: icon = super .getIcon(type);
117: }
118: if (!isValid) {
119: icon = Utilities.mergeImages(icon, errorBadge,
120: errorBadge.getWidth(null), errorBadge
121: .getHeight(null));
122: }
123: return icon;
124: }
125:
126: String iconPath = obj.getBigIcon();
127: Image icon = null;
128: if (iconPath != null) {
129: icon = Utilities.loadImage(iconPath);
130: }
131: if (icon == null) {
132: icon = super .getIcon(type);
133: }
134: if (!isValid) {
135: icon = Utilities.mergeImages(icon, errorBadge, errorBadge
136: .getWidth(null), errorBadge.getHeight(null));
137: }
138: return icon;
139: }
140:
141: @Override
142: public Image getOpenedIcon(int type) {
143: return getIcon(type);
144: }
145:
146: String getProjectType() {
147: return obj.getProjectType();
148: }
149:
150: String getProducerID() {
151: return obj.getProducerID();
152: }
153:
154: void setValid(boolean isValid) {
155: this .isValid = isValid;
156: }
157:
158: boolean isValid() {
159: return isValid;
160: }
161:
162: void setNeedCheck(boolean needCheck) {
163: this .needCheck = needCheck;
164: }
165:
166: @Override
167: public boolean canRename() {
168: return false;
169: }
170:
171: @Override
172: public boolean canDestroy() {
173: boolean canDestroy = false;
174:
175: DesignDocument document = ActiveDocumentSupport.getDefault()
176: .getActiveDocument();
177: if (document != null) {
178: DataObjectContext context = ProjectUtils
179: .getDataObjectContextForDocument(document);
180:
181: if (context != null) {
182: final ComponentProducer[] producer = new ComponentProducer[1];
183: final DescriptorRegistry registry = DescriptorRegistry
184: .getDescriptorRegistry(
185: context.getProjectType(), context
186: .getProjectID());
187: registry.readAccess(new Runnable() {
188:
189: public void run() {
190: List<ComponentProducer> producers = registry
191: .getComponentProducers();
192: for (ComponentProducer p : producers) {
193: if (p.getProducerID().equals(
194: obj.getProducerID())) {
195: producer[0] = p;
196: break;
197: }
198: }
199: }
200: });
201:
202: if (producer[0] != null) {
203: canDestroy = PaletteKit.CUSTOM_CATEGORY_NAME
204: .equals(producer[0].getPaletteDescriptor()
205: .getCategoryID());
206: }
207: }
208: }
209:
210: return canDestroy;
211: }
212:
213: @Override
214: public void destroy() throws IOException {
215: super .destroy();
216: DesignDocument document = ActiveDocumentSupport.getDefault()
217: .getActiveDocument();
218:
219: if (document != null) {
220: DataObjectContext context = ProjectUtils
221: .getDataObjectContextForDocument(document);
222: if (context != null) {
223: final ComponentProducer[] producer = new ComponentProducer[1];
224: final DescriptorRegistry registry = DescriptorRegistry
225: .getDescriptorRegistry(
226: context.getProjectType(), context
227: .getProjectID());
228: registry.readAccess(new Runnable() {
229:
230: public void run() {
231: List<ComponentProducer> producers = registry
232: .getComponentProducers();
233: for (ComponentProducer p : producers) {
234: if (p.getProducerID().equals(
235: obj.getProducerID())) {
236: producer[0] = p;
237: break;
238: }
239: }
240: }
241: });
242:
243: if (producer[0] != null) {
244: registry.removeComponentDescriptor(producer[0]
245: .getMainComponentTypeID());
246: }
247: }
248: }
249: }
250:
251: @Override
252: public boolean canCopy() {
253: return false;
254: }
255:
256: @Override
257: public boolean canCut() {
258: return false;
259: }
260: }
|