1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import org.eclipse.osgi.util.NLS; 14 import org.eclipse.pde.internal.core.schema.*; 15 import org.eclipse.ui.views.properties.*; 16 import java.util.*; 17 import org.eclipse.pde.internal.core.ischema.*; 18 import org.eclipse.jface.viewers.*; 19 import org.eclipse.pde.internal.ui.*; 20 21 public class AttributePropertySource extends SchemaObjectPropertySource implements 22 ICloneablePropertySource { 23 public static final String P_USE = "use"; public static final String P_KIND = "kind"; public static final String P_VALUE = "value"; public static final String P_BASED_ON = "basedOn"; public static final String P_TYPE = "type"; public static final String P_TRANSLATABLE = "translatable"; public static final String P_DEPRECATED = "deprecated"; 31 public static final String P_RESTRICTION = "restriction"; public static final String P_NAME = "name"; private Vector descriptors; 34 35 private static final String [] typeTable = { "string", "boolean" }; private static final String [] booleanTable = { "false", "true" }; 38 class ValueValidator implements ICellEditorValidator { 39 public String isValid(Object value) { 40 String svalue = value.toString(); 41 ISchemaAttribute att = (ISchemaAttribute) getSourceObject(); 42 ISchemaSimpleType type = att.getType(); 43 if (type.getName().equals("boolean")) { if (!svalue.equals("true") && !svalue.equals("false")) return PDEUIMessages.AttributePropertySource_assertBoolean; } else if (type.getName().equals("string") && type.getRestriction() != null) { 48 ISchemaRestriction restriction = type.getRestriction(); 49 if (restriction.isValueValid(svalue) == false) { 50 return NLS.bind(PDEUIMessages.AttributePropertySource_invalidRestriction, svalue); } 52 } 53 return null; 54 } 55 } 56 57 public AttributePropertySource( 58 org.eclipse.pde.internal.core.ischema.ISchemaAttribute att) { 59 super(att); 60 } 61 62 public Object doClone() { 63 ISchemaAttribute att = (ISchemaAttribute) getSourceObject(); 64 SchemaElement element = (SchemaElement) att.getParent(); 65 String value = NLS.bind(PDEUIMessages.SchemaEditor_AttributePR_attributeCopy, att.getName()); 66 SchemaAttribute att2 = new SchemaAttribute(att, value); 67 ((SchemaComplexType) element.getType()).addAttribute(att2); 68 return att2; 69 } 70 71 public Object getEditableValue() { 72 return null; 73 } 74 75 private int getIndexOf(String value, String [] table) { 76 for (int i = 0; i < table.length; i++) { 77 if (value.equals(table[i])) 78 return i; 79 } 80 return 0; 81 } 82 83 public IPropertyDescriptor[] getPropertyDescriptors() { 84 descriptors = new Vector(); 85 PropertyDescriptor cdesc = createComboBoxPropertyDescriptor(P_USE, PDEUIMessages.SchemaEditor_AttributePR_use, ISchemaAttribute.useTable); 86 cdesc.setLabelProvider(new ComboProvider(P_USE, ISchemaAttribute.useTable)); 87 descriptors.addElement(cdesc); 88 89 cdesc = createComboBoxPropertyDescriptor(P_KIND, PDEUIMessages.SchemaEditor_AttributePR_kind, ISchemaAttribute.kindTable); 90 cdesc.setLabelProvider(new ComboProvider(P_KIND, ISchemaAttribute.kindTable)); 91 descriptors.addElement(cdesc); 92 93 cdesc = createComboBoxPropertyDescriptor(P_TYPE, PDEUIMessages.SchemaEditor_AttributePR_type, typeTable); 94 cdesc.setLabelProvider(new ComboProvider(P_TYPE, typeTable)); 95 descriptors.addElement(cdesc); 96 97 cdesc = createComboBoxPropertyDescriptor(P_TRANSLATABLE, PDEUIMessages.AttributePropertySource_translatable, booleanTable); cdesc.setLabelProvider(new ComboProvider(P_TRANSLATABLE, booleanTable)); 99 descriptors.addElement(cdesc); 100 101 cdesc = createComboBoxPropertyDescriptor(P_DEPRECATED, PDEUIMessages.AttributePropertySource_deprecated, booleanTable); cdesc.setLabelProvider(new ComboProvider(P_DEPRECATED, booleanTable)); 103 descriptors.addElement(cdesc); 104 105 cdesc = new TypeRestrictionDescriptor(P_RESTRICTION, PDEUIMessages.SchemaEditor_AttributePR_restriction, !isEditable()); 106 descriptors.addElement(cdesc); 107 cdesc = createTextPropertyDescriptor(P_VALUE, PDEUIMessages.SchemaEditor_AttributePR_value); 108 cdesc.setValidator(new ValueValidator()); 109 descriptors.addElement(cdesc); 110 111 PropertyDescriptor desc = createTextPropertyDescriptor(P_BASED_ON, PDEUIMessages.SchemaEditor_AttributePR_basedOn); 112 descriptors.addElement(desc); 113 114 desc = createTextPropertyDescriptor(P_NAME, PDEUIMessages.SchemaEditor_AttributePR_name); 115 descriptors.addElement(desc); 116 117 return toDescriptorArray(descriptors); 118 } 119 120 public Object getPropertyValue(Object name) { 121 ISchemaAttribute att = (ISchemaAttribute) getSourceObject(); 122 if (name.equals(P_DEPRECATED)) 123 return att.isDeprecated() ? new Integer (1) : new Integer (0); 124 125 if (name.equals(P_TRANSLATABLE)) 126 return att.isTranslatable() ? new Integer (1) : new Integer (0); 127 128 if (name.equals(P_RESTRICTION)) 129 return att.getType().getRestriction(); 130 if (name.equals(P_VALUE)) 131 return getNonzeroValue(att.getValue()); 132 if (name.equals(P_BASED_ON)) 133 return getNonzeroValue(att.getBasedOn()); 134 if (name.equals(P_NAME)) 135 return getNonzeroValue(att.getName()); 136 if (name.equals(P_USE)) { 137 if (isSchemaObject) 138 return new Integer (att.getUse()); 139 return ISchemaAttribute.useTable[att.getUse()]; 140 } 141 if (name.equals(P_KIND)) { 142 if (isSchemaObject) 143 return new Integer (att.getKind()); 144 return ISchemaAttribute.kindTable[att.getKind()]; 145 } 146 if (name.equals(P_TYPE)) { 147 if (isSchemaObject) 148 return new Integer (getIndexOf(att.getType().getName(), typeTable)); 149 return att.getType().getName(); 150 } 151 return ""; } 153 154 public boolean isCloneable() { 155 ISchemaAttribute att = (ISchemaAttribute) getSourceObject(); 156 if (att.getParent().getName().equals("extension")) return false; 158 return true; 159 } 160 161 public boolean isPropertySet(Object property) { 162 return false; 163 } 164 165 public void resetPropertyValue(Object property) { 166 } 167 168 public void setPropertyValue(Object name, Object value) { 169 SchemaAttribute att = (SchemaAttribute) getSourceObject(); 170 if (value instanceof Integer ) { 171 int index = ((Integer ) value).intValue(); 172 if (name.equals(P_USE)) 173 att.setUse(index); 174 else if (name.equals(P_KIND)) 175 att.setKind(index); 176 else if (name.equals(P_TYPE)) { 177 att.setType(new SchemaSimpleType(att.getSchema(), typeTable[index])); 178 if (att.getValue() != null) 179 att.setValue(null); 180 } else if (name.equals(P_TRANSLATABLE)) { 181 att.setTranslatableProperty(index == 1); 182 } else if (name.equals(P_DEPRECATED)) { 183 att.setDeprecatedProperty(index == 1); 184 } 185 } else if (name.equals(P_RESTRICTION)) { 186 ISchemaRestriction restriction = (ISchemaRestriction) value; 187 if (restriction != null && restriction.getChildren().length == 0) 188 restriction = null; 189 if (att.getType() instanceof SchemaSimpleType) { 190 SchemaSimpleType type = (SchemaSimpleType) att.getType(); 191 type.setRestriction(restriction); 192 att.setType(type); 193 } 194 } else if (value instanceof String ) { 195 String svalue = (String ) value; 196 if (name.equals(P_VALUE)) 197 att.setValue(svalue); 198 else if (name.equals(P_BASED_ON)) 199 att.setBasedOn(svalue); 200 else if (name.equals(P_NAME)) 201 att.setName(svalue); 202 } 203 } 204 } 205 | Popular Tags |