KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.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 JavaDoc P_MIN_OCCURS = "minOccurs"; //$NON-NLS-1$
22
public static final String JavaDoc P_MAX_OCCURS = "maxOccurs"; //$NON-NLS-1$
23
protected Vector descriptors;
24
25     class MinValidator implements ICellEditorValidator {
26         public String JavaDoc isValid(Object JavaDoc value) {
27             String JavaDoc svalue = value.toString();
28             try {
29                 int ivalue = Integer.parseInt(svalue);
30                 return isMinOccursValid(ivalue);
31             } catch (NumberFormatException JavaDoc e) {
32                 return PDEUIMessages.GrammarPropertySource_minOccursFormat; //$NON-NLS-1$
33
}
34         }
35     }
36     class MaxValidator implements ICellEditorValidator {
37         public String JavaDoc isValid(Object JavaDoc value) {
38             String JavaDoc svalue = value.toString();
39             if (svalue.equals("unbounded")) //$NON-NLS-1$
40
return isMaxOccursValid(Integer.MAX_VALUE);
41             try {
42                 int ivalue = Integer.parseInt(svalue);
43                 return isMaxOccursValid(ivalue);
44             } catch (NumberFormatException JavaDoc e) {
45                 return PDEUIMessages.GrammarPropertySource_maxOccursFormat; //$NON-NLS-1$
46
}
47         }
48     }
49     
50     protected String JavaDoc isMinOccursValid(int ivalue) {
51         if (ivalue < 0)
52             return PDEUIMessages.GrammarPropertySource_minOccursValue; //$NON-NLS-1$
53
return null;
54     }
55     
56     protected String JavaDoc isMaxOccursValid(int ivalue) {
57         if (ivalue < 0)
58             return PDEUIMessages.GrammarPropertySource_maxOccursValue; //$NON-NLS-1$
59
return null;
60     }
61
62     public GrammarPropertySource(ISchemaRepeatable obj) {
63         super(obj);
64     }
65     public Object JavaDoc getEditableValue() {
66         return null;
67     }
68     protected String JavaDoc getMaxOccurs(ISchemaRepeatable obj) {
69         if (obj.getMaxOccurs() == Integer.MAX_VALUE)
70             return "unbounded"; //$NON-NLS-1$
71
return Integer.toString(obj.getMaxOccurs());
72     }
73     protected String JavaDoc 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"); //$NON-NLS-1$
86
desc.setValidator(new MinValidator());
87         result.addElement(desc);
88         desc = createTextPropertyDescriptor(P_MAX_OCCURS, "maxOccurs"); //$NON-NLS-1$
89
desc.setValidator(new MaxValidator());
90         result.addElement(desc);
91         return result;
92     }
93     public Object JavaDoc getPropertyValue(Object JavaDoc 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 JavaDoc property) {
102         return false;
103     }
104     public int parseValue(Object JavaDoc value) {
105         String JavaDoc svalue = (String JavaDoc) value;
106         if (svalue.equals("unbounded")) //$NON-NLS-1$
107
return Integer.MAX_VALUE;
108         try {
109             return Integer.parseInt(svalue.toString());
110
111         } catch (NumberFormatException JavaDoc e) {
112             PDEPlugin.logException(e);
113         }
114         return 1;
115     }
116     public void resetPropertyValue(Object JavaDoc property) {
117     }
118     public void setPropertyValue(Object JavaDoc name, Object JavaDoc 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