1 19 20 package org.openide.src.nodes; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.beans.*; 24 25 import org.openide.src.*; 26 import org.openide.nodes.PropertySupport; 27 import org.openide.ErrorManager; 28 import org.openide.explorer.propertysheet.PropertyPanel; 29 import org.openide.explorer.propertysheet.editors.ModifierEditor; 30 31 37 class ElementBeanModel extends PropertySupport.Reflection { 38 private Element bean; 39 private PropertyEditor editor; 40 41 public ElementBeanModel(Element bean, String propertyName) throws NoSuchMethodException { 42 this(bean, findInfo(bean, propertyName)); 43 } 44 45 public ElementBeanModel(Element bean, PropertyDescriptor desc) throws NoSuchMethodException { 46 super(bean, desc.getPropertyType(), desc.getName()); 47 setPropertyEditorClass(desc.getPropertyEditorClass()); 48 this.bean = bean; 49 } 50 51 public PropertyEditor getPropertyEditor() { 52 if (editor == null) { 53 editor = super.getPropertyEditor(); 54 } 55 return editor; 56 } 57 58 59 private boolean isRunningAtomic = false; 60 61 public void setValue(final Object val) throws 62 IllegalAccessException , IllegalArgumentException , InvocationTargetException { 63 64 if (isRunningAtomic) { 65 super.setValue(val); 66 return; 67 } 68 69 SourceElement srcel = SourceEditSupport.findSource(bean); 70 71 if (srcel == null) { 72 super.setValue(val); 73 return; 74 } 75 76 final InvocationTargetException [] ex = new InvocationTargetException [1]; 77 final IllegalAccessException [] ex2 = new IllegalAccessException [1]; 78 try { 79 isRunningAtomic = true; 80 srcel.runAtomicAsUser(new Runnable () { 81 public void run() { 82 try { 83 setValue(val); 84 } catch (InvocationTargetException e) { 85 ex[0] = e; 86 ErrorManager.getDefault().annotate( 87 e, e.getTargetException() 88 ); 89 } catch (IllegalAccessException e) { 90 ex2[0] = e; 91 } 92 } 93 }); 94 } catch (SourceException e) { 95 ex[0] = new InvocationTargetException (e); 96 ErrorManager.getDefault().annotate(ex[0], e); 97 } finally { 98 isRunningAtomic = false; 99 } 100 if (ex[0] != null) throw ex[0]; 101 if (ex2[0] != null) throw ex2[0]; 102 103 } 104 105 111 private static PropertyDescriptor findInfo (Object bean, String name) 112 throws IllegalArgumentException { 113 try { 114 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); 115 PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors(); 116 for (int i = 0; i < descr.length; i++) { 117 if (descr[i].getName().equals(name)) { 118 return descr[i]; 119 } 120 } 121 throw new IllegalArgumentException ( 122 "No property named " + name + " in class " + bean.getClass () ); 124 } catch (IntrospectionException e) { 125 IllegalArgumentException newEx = new IllegalArgumentException (); 126 ErrorManager.getDefault().annotate (newEx, e); 127 throw newEx; 128 } 129 } 130 131 public static PropertyPanel createPropertyPanel(MemberElement element, String propertyName) { 132 try { 133 PropertyPanel panel = new PropertyPanel ( 134 new ElementBeanModel(element, propertyName), 135 PropertyPanel.PREF_CUSTOM_EDITOR 136 ); 137 138 return panel; 139 } catch (NoSuchMethodException e) { 140 IllegalStateException ise = new IllegalStateException ("Corrupted code"); ise.initCause(e); 142 throw ise; 143 } 144 } 145 146 public static PropertyPanel createModifiersPanel(MemberElement element) { 147 PropertyPanel modifPanel = createPropertyPanel(element, ElementProperties.PROP_MODIFIERS); 148 PropertyEditor propEdit = modifPanel.getProperty().getPropertyEditor(); 149 if (propEdit instanceof ModifierEditor) { 150 ((ModifierEditor) propEdit).setMask(element.getModifiersMask()); 151 } 152 return modifPanel; 153 } 154 155 } 156 | Popular Tags |