KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > AttributePropertySource


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc P_USE = "use"; //$NON-NLS-1$
24
public static final String JavaDoc P_KIND = "kind"; //$NON-NLS-1$
25
public static final String JavaDoc P_VALUE = "value"; //$NON-NLS-1$
26
public static final String JavaDoc P_BASED_ON = "basedOn"; //$NON-NLS-1$
27
public static final String JavaDoc P_TYPE = "type"; //$NON-NLS-1$
28
public static final String JavaDoc P_TRANSLATABLE = "translatable"; //$NON-NLS-1$
29
public static final String JavaDoc P_DEPRECATED = "deprecated"; //$NON-NLS-1$
30

31     public static final String JavaDoc P_RESTRICTION = "restriction"; //$NON-NLS-1$
32
public static final String JavaDoc P_NAME = "name"; //$NON-NLS-1$
33
private Vector descriptors;
34     
35     private static final String JavaDoc[] typeTable = { "string", "boolean" }; //$NON-NLS-1$ //$NON-NLS-2$
36
private static final String JavaDoc[] booleanTable = { "false", "true" }; //$NON-NLS-1$ //$NON-NLS-2$
37

38     class ValueValidator implements ICellEditorValidator {
39         public String JavaDoc isValid(Object JavaDoc value) {
40             String JavaDoc svalue = value.toString();
41             ISchemaAttribute att = (ISchemaAttribute) getSourceObject();
42             ISchemaSimpleType type = att.getType();
43             if (type.getName().equals("boolean")) { //$NON-NLS-1$
44
if (!svalue.equals("true") && !svalue.equals("false")) //$NON-NLS-1$ //$NON-NLS-2$
45
return PDEUIMessages.AttributePropertySource_assertBoolean; //$NON-NLS-1$
46
} else if (type.getName().equals("string") //$NON-NLS-1$
47
&& type.getRestriction() != null) {
48                 ISchemaRestriction restriction = type.getRestriction();
49                 if (restriction.isValueValid(svalue) == false) {
50                     return NLS.bind(PDEUIMessages.AttributePropertySource_invalidRestriction, svalue); //$NON-NLS-1$
51
}
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 JavaDoc doClone() {
63         ISchemaAttribute att = (ISchemaAttribute) getSourceObject();
64         SchemaElement element = (SchemaElement) att.getParent();
65         String JavaDoc 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 JavaDoc getEditableValue() {
72         return null;
73     }
74
75     private int getIndexOf(String JavaDoc value, String JavaDoc[] 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); //$NON-NLS-1$
98
cdesc.setLabelProvider(new ComboProvider(P_TRANSLATABLE, booleanTable));
99         descriptors.addElement(cdesc);
100
101         cdesc = createComboBoxPropertyDescriptor(P_DEPRECATED, PDEUIMessages.AttributePropertySource_deprecated, booleanTable); //$NON-NLS-1$
102
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 JavaDoc getPropertyValue(Object JavaDoc name) {
121         ISchemaAttribute att = (ISchemaAttribute) getSourceObject();
122         if (name.equals(P_DEPRECATED))
123             return att.isDeprecated() ? new Integer JavaDoc(1) : new Integer JavaDoc(0);
124             
125         if (name.equals(P_TRANSLATABLE))
126             return att.isTranslatable() ? new Integer JavaDoc(1) : new Integer JavaDoc(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 JavaDoc(att.getUse());
139             return ISchemaAttribute.useTable[att.getUse()];
140         }
141         if (name.equals(P_KIND)) {
142             if (isSchemaObject)
143                 return new Integer JavaDoc(att.getKind());
144             return ISchemaAttribute.kindTable[att.getKind()];
145         }
146         if (name.equals(P_TYPE)) {
147             if (isSchemaObject)
148                 return new Integer JavaDoc(getIndexOf(att.getType().getName(), typeTable));
149             return att.getType().getName();
150         }
151         return ""; //$NON-NLS-1$
152
}
153
154     public boolean isCloneable() {
155         ISchemaAttribute att = (ISchemaAttribute) getSourceObject();
156         if (att.getParent().getName().equals("extension")) //$NON-NLS-1$
157
return false;
158         return true;
159     }
160
161     public boolean isPropertySet(Object JavaDoc property) {
162         return false;
163     }
164
165     public void resetPropertyValue(Object JavaDoc property) {
166     }
167
168     public void setPropertyValue(Object JavaDoc name, Object JavaDoc value) {
169         SchemaAttribute att = (SchemaAttribute) getSourceObject();
170         if (value instanceof Integer JavaDoc) {
171             int index = ((Integer JavaDoc) 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 JavaDoc) {
195             String JavaDoc svalue = (String JavaDoc) 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