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.beaninfo.editors;
043:
044: import java.awt.Image;
045: import java.beans.PropertyEditorSupport;
046: import java.beans.FeatureDescriptor;
047: import org.netbeans.core.UIExceptions;
048:
049: import org.openide.explorer.propertysheet.PropertyEnv;
050: import org.openide.explorer.propertysheet.ExPropertyEditor;
051: import org.openide.nodes.Node;
052: import org.openide.util.NbBundle;
053:
054: /** Editor for property of enumerated integers, each integer should
055: * have associated image displayed as a property value. It's possible
056: * to associate descriptions for each value which is then shown in combobox
057: * when property is edited.
058: *
059: * @author Vitezslav Stejskal
060: */
061: public class ListImageEditor extends PropertyEditorSupport implements
062: ExPropertyEditor {
063:
064: public static final String PROP_IMAGES = "images"; //NOI18N
065: public static final String PROP_VALUES = "values"; //NOI18N
066: public static final String PROP_DESCRIPTIONS = "descriptions"; //NOI18N
067:
068: private boolean canWrite = true;
069:
070: private Image[] images = null;
071: private Integer[] values = null;
072: private String[] descriptions = null;
073:
074: /** Creates new ListEditor */
075: public ListImageEditor() {
076: super ();
077: }
078:
079: public void attachEnv(PropertyEnv env) {
080: FeatureDescriptor d = env.getFeatureDescriptor();
081: if (d instanceof Node.Property) {
082: canWrite = ((Node.Property) d).canWrite();
083: }
084:
085: Object o;
086: Image imgs[] = null;
087: Integer vals[] = null;
088: String descs[] = null;
089:
090: o = d.getValue(PROP_IMAGES);
091: if (o instanceof Image[]) {
092: imgs = (Image[]) o;
093: }
094: o = d.getValue(PROP_VALUES);
095: if (o instanceof Integer[]) {
096: vals = (Integer[]) o;
097: }
098: o = d.getValue(PROP_DESCRIPTIONS);
099: if (o instanceof String[]) {
100: descs = (String[]) o;
101: }
102:
103: if (imgs != null && vals != null) {
104: int length = length = imgs.length;
105:
106: if (vals.length < length) {
107: length = vals.length;
108: }
109:
110: if (descs != null && descs.length < length) {
111: length = descs.length;
112: }
113:
114: images = new Image[length];
115: values = new Integer[length];
116: descriptions = new String[length];
117:
118: for (int i = 0; i < length; i++) {
119: images[i] = imgs[i];
120: values[i] = vals[i];
121: descriptions[i] = descs == null ? vals[i].toString()
122: : descs[i];
123: }
124: }
125: }
126:
127: public boolean isEditable() {
128: return canWrite;
129: }
130:
131: public String getAsText() {
132: int i = findIndex(values, getValue());
133: return (String) findObject(descriptions, i);
134: }
135:
136: public void setAsText(String str)
137: throws java.lang.IllegalArgumentException {
138: int i = findIndex(descriptions, str);
139: if (i == -1) {
140: IllegalArgumentException iae = new IllegalArgumentException(
141: "negative: " + str); //NOI18N
142: String msg = NbBundle.getMessage(ListImageEditor.class,
143: "CTL_NegativeSize"); //NOI18N
144: UIExceptions.annotateUser(iae, iae.getMessage(), msg, null,
145: new java.util.Date());
146: throw iae;
147: }
148: setValue(findObject(values, i));
149: }
150:
151: public String[] getTags() {
152: return descriptions;
153: }
154:
155: public boolean isPaintable() {
156: return true;
157: }
158:
159: public void paintValue(java.awt.Graphics g,
160: java.awt.Rectangle rectangle) {
161: Image img = (Image) findObject(images, findIndex(values,
162: getValue()));
163:
164: if (img != null) {
165: g.drawImage(img, rectangle.x
166: + (rectangle.width - img.getWidth(null)) / 2,
167: rectangle.y
168: + (rectangle.height - img.getHeight(null))
169: / 2, img.getWidth(null), img
170: .getHeight(null), null);
171: }
172: }
173:
174: public String getJavaInitializationString() {
175: return "new Integer(" + getValue() + ")"; // NOI18N
176: }
177:
178: private Object findObject(Object[] objs, int i) {
179: if (objs == null || i < 0 || i >= objs.length)
180: return null;
181:
182: return objs[i];
183: }
184:
185: private int findIndex(Object[] objs, Object obj) {
186: if (objs != null) {
187: for (int i = 0; i < objs.length; i++) {
188: if (objs[i].equals(obj))
189: return i;
190: }
191: }
192: return -1;
193: }
194: }
|