1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import org.eclipse.pde.internal.core.schema.*; 14 import org.eclipse.ui.views.properties.*; 15 import java.util.*; 16 import org.eclipse.pde.internal.core.ischema.*; 17 import org.eclipse.jface.viewers.*; 18 import org.eclipse.pde.internal.ui.*; 19 20 public class GrammarPropertySource extends SchemaObjectPropertySource { 21 public static final String P_MIN_OCCURS = "minOccurs"; public static final String P_MAX_OCCURS = "maxOccurs"; protected Vector descriptors; 24 25 class MinValidator implements ICellEditorValidator { 26 public String isValid(Object value) { 27 String svalue = value.toString(); 28 try { 29 int ivalue = Integer.parseInt(svalue); 30 return isMinOccursValid(ivalue); 31 } catch (NumberFormatException e) { 32 return PDEUIMessages.GrammarPropertySource_minOccursFormat; } 34 } 35 } 36 class MaxValidator implements ICellEditorValidator { 37 public String isValid(Object value) { 38 String svalue = value.toString(); 39 if (svalue.equals("unbounded")) return isMaxOccursValid(Integer.MAX_VALUE); 41 try { 42 int ivalue = Integer.parseInt(svalue); 43 return isMaxOccursValid(ivalue); 44 } catch (NumberFormatException e) { 45 return PDEUIMessages.GrammarPropertySource_maxOccursFormat; } 47 } 48 } 49 50 protected String isMinOccursValid(int ivalue) { 51 if (ivalue < 0) 52 return PDEUIMessages.GrammarPropertySource_minOccursValue; return null; 54 } 55 56 protected String isMaxOccursValid(int ivalue) { 57 if (ivalue < 0) 58 return PDEUIMessages.GrammarPropertySource_maxOccursValue; return null; 60 } 61 62 public GrammarPropertySource(ISchemaRepeatable obj) { 63 super(obj); 64 } 65 public Object getEditableValue() { 66 return null; 67 } 68 protected String getMaxOccurs(ISchemaRepeatable obj) { 69 if (obj.getMaxOccurs() == Integer.MAX_VALUE) 70 return "unbounded"; return Integer.toString(obj.getMaxOccurs()); 72 } 73 protected String getMinOccurs(ISchemaRepeatable obj) { 74 return Integer.toString(obj.getMinOccurs()); 75 } 76 public IPropertyDescriptor[] getPropertyDescriptors() { 77 if (descriptors == null) { 78 descriptors = getPropertyDescriptorsVector(); 79 } 80 return toDescriptorArray(descriptors); 81 } 82 protected Vector getPropertyDescriptorsVector() { 83 Vector result = new Vector(); 84 PropertyDescriptor desc = 85 createTextPropertyDescriptor(P_MIN_OCCURS, "minOccurs"); desc.setValidator(new MinValidator()); 87 result.addElement(desc); 88 desc = createTextPropertyDescriptor(P_MAX_OCCURS, "maxOccurs"); desc.setValidator(new MaxValidator()); 90 result.addElement(desc); 91 return result; 92 } 93 public Object getPropertyValue(Object name) { 94 ISchemaRepeatable obj = (ISchemaRepeatable) getSourceObject(); 95 if (name.equals(P_MIN_OCCURS)) 96 return getMinOccurs(obj); 97 if (name.equals(P_MAX_OCCURS)) 98 return getMaxOccurs(obj); 99 return null; 100 } 101 public boolean isPropertySet(Object property) { 102 return false; 103 } 104 public int parseValue(Object value) { 105 String svalue = (String ) value; 106 if (svalue.equals("unbounded")) return Integer.MAX_VALUE; 108 try { 109 return Integer.parseInt(svalue.toString()); 110 111 } catch (NumberFormatException e) { 112 PDEPlugin.logException(e); 113 } 114 return 1; 115 } 116 public void resetPropertyValue(Object property) { 117 } 118 public void setPropertyValue(Object name, Object value) { 119 ISchemaRepeatable obj = (ISchemaRepeatable) getSourceObject(); 120 121 if (name.equals(P_MIN_OCCURS)) { 122 int ivalue = parseValue(value); 123 if (obj instanceof RepeatableSchemaObject) { 124 ((RepeatableSchemaObject) obj).setMinOccurs(ivalue); 125 } else if (obj instanceof SchemaElementReference) { 126 ((SchemaElementReference) obj).setMinOccurs(ivalue); 127 } 128 } else if (name.equals(P_MAX_OCCURS)) { 129 int ivalue = parseValue(value); 130 if (obj instanceof RepeatableSchemaObject) { 131 ((RepeatableSchemaObject) obj).setMaxOccurs(ivalue); 132 } else if (obj instanceof SchemaElementReference) { 133 ((SchemaElementReference) obj).setMaxOccurs(ivalue); 134 } 135 } 136 } 137 } 138 | Popular Tags |