1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 import java.awt.Component ; 24 import java.awt.Font ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.text.MessageFormat ; 31 import java.util.*; 32 import javax.swing.ButtonGroup ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JRadioButton ; 36 import org.netbeans.core.UIExceptions; 37 import org.openide.explorer.propertysheet.ExPropertyEditor; 38 import org.openide.explorer.propertysheet.PropertyEnv; 39 import org.openide.util.NbBundle; 40 import org.openide.util.Lookup; 41 import org.openide.util.Utilities; 42 43 48 public final class ObjectEditor extends PropertyEditorSupport 49 implements ExPropertyEditor { 50 53 private static final String PROP_SUPERCLASS = "superClass"; 58 private static final String PROP_NULL = "nullValue"; 62 private static final String PROP_LOOKUP = "lookup"; 64 65 private ObjectPanel customEditor; 66 67 68 private Lookup.Template<Object > template; 69 70 71 private String nullValue; 72 73 74 private Lookup lookup; 75 76 77 public ObjectEditor() { 78 } 79 80 84 public synchronized void attachEnv(PropertyEnv env) { 85 Object obj = env.getFeatureDescriptor ().getValue (PROP_SUPERCLASS); 86 if (obj instanceof Class ) { 87 @SuppressWarnings ("unchecked") Class <Object > clz = (Class <Object >)obj; 88 template = new Lookup.Template<Object > (clz); 89 } else { 90 template = null; 91 } 92 93 obj = env.getFeatureDescriptor ().getValue (PROP_NULL); 94 if (Boolean.TRUE.equals (obj)) { 95 nullValue = NbBundle.getMessage (ObjectEditor.class, "CTL_NullValue"); 96 } else { 97 if (obj instanceof String ) { 98 nullValue = (String )obj; 99 } else { 100 nullValue = null; 101 } 102 } 103 104 obj = env.getFeatureDescriptor ().getValue (PROP_LOOKUP); 105 lookup = obj instanceof Lookup ? (Lookup)obj : null; 106 if (getTags()==null || getTags().length <= 1) { 108 env.getFeatureDescriptor().setValue("canEditAsText",Boolean.FALSE); } 110 } 111 112 115 protected Lookup lookup () { 116 Lookup l = lookup; 117 return l == null ? Lookup.getDefault () : l; 118 } 119 120 122 protected Lookup.Template<Object > template () { 123 if (template == null) { 124 template = new Lookup.Template<Object > (Object .class); 125 } 126 127 return template; 128 } 129 130 public String getAsText() { 131 Object value = getValue (); 132 if (value == null) { 133 return nullValue == null ? 134 NbBundle.getMessage (ObjectEditor.class, "CTL_NullValue") 135 : 136 nullValue; 137 } 138 139 Lookup.Template<Object > t = new Lookup.Template<Object > ( 140 template ().getType (), 141 template ().getId (), 142 value ); 144 Lookup.Item item = lookup ().lookupItem (t); 145 146 if (item == null) { 147 return NbBundle.getMessage (ObjectEditor.class, "CTL_NullItem"); 148 } 149 150 return item.getDisplayName(); 151 } 152 153 156 public void setAsText(java.lang.String str) throws java.lang.IllegalArgumentException { 157 if (nullValue != null && nullValue.equals (str)) { 158 setValue (null); 159 return; 160 } 161 162 163 Collection allItems = lookup ().lookup (template ()).allItems (); 164 165 Iterator it = allItems.iterator (); 166 while (it.hasNext ()) { 167 Lookup.Item item = (Lookup.Item)it.next (); 168 169 if (item.getDisplayName().equals (str)) { 170 setValue (item.getInstance ()); 171 firePropertyChange(); 172 return; 173 } 174 } 175 IllegalArgumentException iae = new IllegalArgumentException (str); 176 String msg = MessageFormat.format( 177 NbBundle.getMessage( 178 ObjectEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), new Object [] {str}); 180 UIExceptions.annotateUser(iae, str, msg, null, new Date()); 181 throw iae; 182 } 183 184 187 public java.lang.String [] getTags() { 188 Collection<? extends Lookup.Item<Object >> allItems = lookup ().lookup (template ()).allItems (); 189 if (allItems.size() <= 1) { 190 return null; 191 } 192 193 ArrayList<String > list = new ArrayList<String > (allItems.size () + 1); 194 if (nullValue != null) { 195 list.add (nullValue); 196 } 197 198 for (Lookup.Item<Object > item: allItems) { 199 list.add (item.getDisplayName()); 200 } 201 202 String [] retValue = new String [list.size()]; 203 list.toArray(retValue); 204 return retValue; 205 } 206 207 209 public boolean supportsCustomEditor() { 210 return getTags()!= null && getTags().length > 1; 212 } 213 214 public synchronized Component getCustomEditor () { 215 if (!supportsCustomEditor()) { 216 return null; 217 } 218 if (customEditor != null) { 219 return customEditor; 220 } 221 Lookup.Result<Object > contents = lookup().lookup(template()); 222 ObjectPanel panel = new ObjectPanel(contents); 223 return customEditor = panel; 224 } 225 226 private class ObjectPanel extends JPanel implements ActionListener { 227 public ObjectPanel(Lookup.Result<Object > res) { 228 getAccessibleContext().setAccessibleName( 229 NbBundle.getMessage(ObjectEditor.class, 230 "ACSN_ObjectTree")); getAccessibleContext().setAccessibleDescription( 232 NbBundle.getMessage(ObjectEditor.class, "ACSD_ObjectTree")); 234 setLayout (new GridBagLayout ()); 235 GridBagConstraints gbc = new GridBagConstraints (); 236 int row = 0; 237 ButtonGroup bg = new ButtonGroup (); 238 Font bold; 239 Font plain; 240 if (Utilities.isMac()) { 241 bold = new Font (getFont().getName(), Font.BOLD, getFont().getSize()); 243 plain = new Font (getFont().getName(), Font.PLAIN, getFont().getSize()); 246 } else { 247 bold = getFont().deriveFont(Font.BOLD); 248 plain = getFont().deriveFont(Font.PLAIN); 249 } 250 251 Collection<? extends Lookup.Item<Object >> c = res.allItems(); 252 Lookup.Item[] items = new Lookup.Item[c.size()]; 253 items = (Lookup.Item[]) c.toArray(items); 254 255 int BASE_LEFT_INSET=7; 256 for (int i=0; i < items.length; i++) { 257 JRadioButton rb = new ItemRadioButton(items[i], bold); 258 if (items[i].getInstance().equals(getValue())) { 259 rb.setSelected(true); 260 } 261 rb.addActionListener(this); 262 bg.add(rb); 263 String description = getDescription(items[i]); 264 265 gbc.gridx=0; 266 gbc.gridy=row; 267 gbc.insets = new Insets (i==0 ? 7 : 0, BASE_LEFT_INSET, 268 description != null ? 1 : i==items.length-1 ? 7: 4, BASE_LEFT_INSET); 269 gbc.fill=gbc.HORIZONTAL; 270 add(rb, gbc); 271 row++; 272 if (description != null) { 273 JLabel lbl = new JLabel (description); 274 lbl.setLabelFor(rb); 275 lbl.setFont(plain); 276 int left = rb.getIcon() != null ? rb.getIcon().getIconWidth() : 20; 277 gbc.insets = new Insets (0, BASE_LEFT_INSET + 278 left, 4, BASE_LEFT_INSET + left); 279 gbc.gridx=0; 280 gbc.gridy=row; 281 add(lbl, gbc); 282 row++; 283 } 284 } 285 } 286 287 private String getDescription (Lookup.Item item) { 288 String id = item.getId (); 289 String result = null; 290 try { 291 result = Introspector.getBeanInfo(item.getInstance().getClass()).getBeanDescriptor().getShortDescription(); 292 } catch (IntrospectionException ie) { 293 } 295 String toCheck = item.getInstance().getClass().getName(); 296 toCheck = toCheck.lastIndexOf('.')!=-1 ? 297 toCheck.substring(toCheck.lastIndexOf('.')+1) : toCheck; if (toCheck.equals(result)) { 299 result = null; 300 } 301 return result; 302 } 303 304 public void actionPerformed(ActionEvent ae) { 305 Lookup.Item item = ((ItemRadioButton) ae.getSource()).item; 306 Object o = item.getInstance(); 307 setValue (item.getInstance()); 308 ObjectEditor.this.firePropertyChange(); 309 } 310 } 311 312 private static class ItemRadioButton extends JRadioButton { 313 Lookup.Item item; 314 public ItemRadioButton(Lookup.Item item, Font font) { 315 this.item = item; 316 setName(item.getId()); 317 setText(item.getDisplayName()); 318 setFont(font); 319 getAccessibleContext().setAccessibleName(getName()); 320 getAccessibleContext().setAccessibleDescription( 321 getText()); 322 } 323 } 324 } 325 326 | Popular Tags |