1 19 20 26 27 package org.netbeans.modules.xml.schema.ui.nodes.schema.properties; 28 29 import java.lang.reflect.InvocationTargetException ; 30 import org.openide.ErrorManager; 31 import org.openide.util.NbBundle; 32 import org.netbeans.modules.xml.schema.model.SchemaComponent; 33 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction; 34 import org.netbeans.modules.xml.schema.model.TotalDigits; 35 import org.netbeans.modules.xml.schema.ui.basic.editors.StringEditor; 36 import org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype.SchemaComponentCreator; 37 import org.netbeans.modules.xml.xam.ui.XAMUtils; 38 39 43 public class AdvancedFacetProperty extends BaseSchemaProperty { 44 45 private Class <? extends SchemaComponent> facetType; 46 47 private SimpleTypeRestriction parent; 48 49 50 public AdvancedFacetProperty(SimpleTypeRestriction parent, 51 SchemaComponent facet, 52 Class <? extends SchemaComponent> facetType, 53 Class valueType, 54 String property, 55 String propDispName, 56 String propDesc, 57 Class propEditorClass) 58 throws NoSuchMethodException { 59 super(facet, 60 valueType, 61 facetType.getMethod(BaseSchemaProperty. 62 firstLetterToUpperCase(property, "get"), new Class [0]), 63 facetType.getMethod(BaseSchemaProperty. 64 firstLetterToUpperCase(property, "set"), new Class []{valueType}), 65 property,propDispName,propDesc, 66 valueType.equals(int.class)?StringEditor.class:propEditorClass); 67 super.setName(propDispName); 68 this.parent = parent; 69 this.facetType = facetType; 70 } 71 72 public void setValue(Object val) throws IllegalAccessException , 73 IllegalArgumentException , InvocationTargetException { 74 if(getComponent()==null && val == null) return; 75 if(val==null) { 76 parent.getModel().removeChildComponent(getComponent()); 77 setComponent(null); 78 return; 79 } 80 if (super.getValueType().equals(int.class)) { 81 int lowerLimit = TotalDigits.class.isAssignableFrom(facetType)?1:0; 82 int newVal = lowerLimit-1; 83 try { 84 val = Integer.valueOf((String )val); 85 newVal = ((Integer )val).intValue(); 86 } catch (Exception e) { 87 } 89 if(newVal<lowerLimit) { 90 String msg = NbBundle.getMessage(AdvancedFacetProperty.class, 91 "MSG_Neg_Int_Value", val); IllegalArgumentException iae = new IllegalArgumentException (msg); 93 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, 94 msg, msg, null, new java.util.Date ()); 95 throw iae; 96 } 97 } 98 if(getComponent()==null) { 99 SchemaComponent sc = createFacet(); 100 setComponent(sc); 101 try { 102 super.setValue(val); 103 parent.getModel().addChildComponent(parent,getComponent(),-1); 104 } catch (IllegalAccessException iae) { 105 setComponent(null); 106 throw iae; 107 } catch (IllegalArgumentException iae) { 108 setComponent(null); 109 throw iae; 110 } catch (InvocationTargetException ite) { 111 setComponent(null); 112 throw ite; 113 } catch (Exception e) { 114 setComponent(null); 115 assert false; 116 } 117 } else { 118 super.setValue(val); 119 } 120 } 121 122 public Object getValue() throws IllegalAccessException , 123 IllegalArgumentException , InvocationTargetException { 124 if(getComponent()==null) return null; 125 if (super.getValueType().equals(int.class)) { 126 return super.getValue().toString(); 127 } 128 return super.getValue(); 129 } 130 131 135 public boolean canWrite() { 136 if(getComponent()==null) { 137 return XAMUtils.isWritable(parent.getModel()); 138 } 139 return super.canWrite(); 140 } 141 142 143 private SchemaComponent createFacet() { 144 return SchemaComponentCreator.createComponent( 145 parent.getModel().getFactory(), facetType); 146 } 147 } 148 | Popular Tags |