1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Image ; 23 import java.beans.PropertyEditorSupport ; 24 import java.beans.FeatureDescriptor ; 25 import org.netbeans.core.UIExceptions; 26 27 import org.openide.explorer.propertysheet.PropertyEnv; 28 import org.openide.explorer.propertysheet.ExPropertyEditor; 29 import org.openide.nodes.Node; 30 import org.openide.util.NbBundle; 31 32 33 40 public class ListImageEditor extends PropertyEditorSupport implements ExPropertyEditor { 41 42 public static final String PROP_IMAGES = "images"; public static final String PROP_VALUES = "values"; public static final String PROP_DESCRIPTIONS = "descriptions"; 46 private boolean canWrite = true; 47 48 private Image [] images = null; 49 private Integer [] values = null; 50 private String [] descriptions = null; 51 52 53 public ListImageEditor () { 54 super (); 55 } 56 57 public void attachEnv (PropertyEnv env) { 58 FeatureDescriptor d = env.getFeatureDescriptor (); 59 if (d instanceof Node.Property) { 60 canWrite = ((Node.Property)d).canWrite (); 61 } 62 63 Object o; 64 Image imgs [] = null; 65 Integer vals [] = null; 66 String descs [] = null; 67 68 o = d.getValue (PROP_IMAGES); 69 if (o instanceof Image []) { 70 imgs = (Image [])o; 71 } 72 o = d.getValue (PROP_VALUES); 73 if (o instanceof Integer []) { 74 vals = (Integer [])o; 75 } 76 o = d.getValue (PROP_DESCRIPTIONS); 77 if (o instanceof String []) { 78 descs = (String [])o; 79 } 80 81 if (imgs != null && vals != null) { 82 int length = length = imgs.length; 83 84 if(vals.length < length) { 85 length = vals.length; 86 } 87 88 if (descs != null && descs.length < length) { 89 length = descs.length; 90 } 91 92 images = new Image [length]; 93 values = new Integer [length]; 94 descriptions = new String [length]; 95 96 for (int i = 0; i < length; i++) { 97 images [i] = imgs [i]; 98 values [i] = vals [i]; 99 descriptions [i] = descs == null ? vals [i].toString () : descs [i]; 100 } 101 } 102 } 103 104 public boolean isEditable () { 105 return canWrite; 106 } 107 108 public String getAsText () { 109 int i = findIndex (values, getValue ()); 110 return (String ) findObject (descriptions, i); 111 } 112 113 public void setAsText (String str) throws java.lang.IllegalArgumentException { 114 int i = findIndex (descriptions, str); 115 if (i == -1) { 116 IllegalArgumentException iae = new IllegalArgumentException ( 117 "negative: " + str); String msg = NbBundle.getMessage(ListImageEditor.class, 119 "CTL_NegativeSize"); UIExceptions.annotateUser(iae, iae.getMessage(), msg, null, 121 new java.util.Date ()); 122 throw iae; 123 } 124 setValue (findObject (values, i)); 125 } 126 127 public String [] getTags () { 128 return descriptions; 129 } 130 131 public boolean isPaintable () { 132 return true; 133 } 134 135 public void paintValue (java.awt.Graphics g, java.awt.Rectangle rectangle) { 136 Image img = (Image ) findObject (images, findIndex (values, getValue ())); 137 138 if (img != null) { 139 g.drawImage (img, 140 rectangle.x + (rectangle.width - img.getWidth (null))/ 2, 141 rectangle.y + (rectangle.height - img.getHeight (null))/ 2, 142 img.getWidth (null), 143 img.getHeight (null), 144 null); 145 } 146 } 147 148 public String getJavaInitializationString () { 149 return "new Integer(" + getValue () + ")"; } 151 152 private Object findObject (Object [] objs, int i) { 153 if (objs == null || i < 0 || i >= objs.length) 154 return null; 155 156 return objs[i]; 157 } 158 159 private int findIndex (Object [] objs, Object obj) { 160 if (objs != null) { 161 for ( int i = 0; i < objs.length; i++) { 162 if (objs[i].equals (obj)) 163 return i; 164 } 165 } 166 return -1; 167 } 168 } 169 | Popular Tags |