KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > form > EnclosingControl


1 package org.apache.slide.projector.processor.form;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.slide.projector.ConfigurationException;
8 import org.apache.slide.projector.Context;
9 import org.apache.slide.projector.ProcessException;
10 import org.apache.slide.projector.Result;
11 import org.apache.slide.projector.descriptor.ArrayValueDescriptor;
12 import org.apache.slide.projector.descriptor.BooleanValueDescriptor;
13 import org.apache.slide.projector.descriptor.NumberValueDescriptor;
14 import org.apache.slide.projector.descriptor.ParameterDescriptor;
15 import org.apache.slide.projector.descriptor.StringValueDescriptor;
16 import org.apache.slide.projector.descriptor.ValueDescriptor;
17 import org.apache.slide.projector.i18n.ErrorMessage;
18 import org.apache.slide.projector.value.ArrayValue;
19 import org.apache.slide.projector.value.NumberValue;
20 import org.apache.slide.projector.value.StreamableValue;
21 import org.apache.slide.projector.value.StringValue;
22 import org.apache.slide.projector.value.Value;
23
24 /**
25  * @version $Revision: 1.3 $
26  */

27
28 public class EnclosingControl extends Control {
29     protected final static int MAX_OPTIONS = 100;
30
31     private final static String JavaDoc OPTIONS = "options";
32     private final static String JavaDoc ENTRY_POSTFIX = " entry";
33     private final static String JavaDoc SELECTED_ENTRY_POSTFIX = " selected entry";
34
35     protected Template optionalEntryFragment, optionalSelectedEntryFragment, optionalValidEntryFragment, optionalValidSelectedEntryFragment, optionalInvalidEntryFragment, optionalInvalidSelectedEntryFragment;
36     protected Template requiredEntryFragment, requiredSelectedEntryFragment, requiredValidEntryFragment, requiredValidSelectedEntryFragment, requiredInvalidEntryFragment, requiredInvalidSelectedEntryFragment;
37
38     public EnclosingControl() {
39         setRequiredFragments(new String JavaDoc[] { OPTIONAL+getName(), OPTIONAL+getName()+ENTRY_POSTFIX, OPTIONAL+getName()+SELECTED_ENTRY_POSTFIX,
40                                             REQUIRED+getName(), REQUIRED+getName()+ENTRY_POSTFIX, REQUIRED+getName()+SELECTED_ENTRY_POSTFIX });
41         setOptionalFragments(new String JavaDoc[] { OPTIONAL_VALID_CONTROL+getName(), OPTIONAL_INVALID_CONTROL+getName(), OPTIONAL_VALID_CONTROL+getName()+ENTRY_POSTFIX, OPTIONAL_INVALID_CONTROL+getName()+ENTRY_POSTFIX,
42                                             OPTIONAL_VALID_CONTROL+getName()+SELECTED_ENTRY_POSTFIX, OPTIONAL_INVALID_CONTROL+getName()+SELECTED_ENTRY_POSTFIX,
43                                             REQUIRED_VALID_CONTROL+getName(), REQUIRED_INVALID_CONTROL+getName(), REQUIRED_VALID_CONTROL+getName()+ENTRY_POSTFIX, REQUIRED_INVALID_CONTROL+getName()+ENTRY_POSTFIX,
44                                             REQUIRED_VALID_CONTROL+getName()+SELECTED_ENTRY_POSTFIX, REQUIRED_INVALID_CONTROL+getName()+SELECTED_ENTRY_POSTFIX});
45     }
46
47     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
48         ParameterDescriptor parameterDescriptor = getParameterDescriptor(parameter, context);
49         String JavaDoc state = parameter.get(STATE).toString();
50         Object JavaDoc value = parameter.get(VALUE);
51         String JavaDoc[] options = getOptions(parameterDescriptor.getValueDescriptor());
52         StringBuffer JavaDoc renderedEntries = new StringBuffer JavaDoc();
53         Template template = optionalEntryFragment;
54         for (int j = 0; j < options.length; j++) {
55             if (valueMatches(options[j], value)) {
56                 if ( state.equals(OPTIONAL_CONTROL) ) {
57                   template = optionalSelectedEntryFragment;
58                 } else if ( state.equals(OPTIONAL_VALID_CONTROL) ) {
59                     template = optionalValidSelectedEntryFragment;
60                 } else if ( state.equals(OPTIONAL_INVALID_CONTROL) ) {
61                     template = optionalInvalidSelectedEntryFragment;
62                 } else if ( state.equals(REQUIRED_CONTROL) ) {
63                   template = requiredSelectedEntryFragment;
64                 } else if ( state.equals(REQUIRED_VALID_CONTROL) ) {
65                     template = requiredValidSelectedEntryFragment;
66                 } else if ( state.equals(REQUIRED_INVALID_CONTROL) ) {
67                     template = requiredInvalidSelectedEntryFragment;
68                 }
69             } else {
70                 if ( state.equals(OPTIONAL_CONTROL) ) {
71                   template = optionalEntryFragment;
72                 } else if ( state.equals(OPTIONAL_VALID_CONTROL) ) {
73                     template = optionalValidEntryFragment;
74                 } else if ( state.equals(OPTIONAL_INVALID_CONTROL) ) {
75                     template = optionalInvalidEntryFragment;
76                 } else if ( state.equals(REQUIRED_CONTROL) ) {
77                   template = requiredEntryFragment;
78                 } else if ( state.equals(REQUIRED_VALID_CONTROL) ) {
79                     template = requiredValidEntryFragment;
80                 } else if ( state.equals(REQUIRED_INVALID_CONTROL) ) {
81                     template = requiredInvalidEntryFragment;
82                 }
83             }
84             parameter.put(VALUE, new StringValue(options[j]));
85             template.evaluate(renderedEntries, parameter);
86         }
87         parameter.put(OPTIONS, new StringValue(renderedEntries.toString()));
88         return super.process(parameter, context);
89     }
90
91     public void configure(StreamableValue config) throws ConfigurationException {
92         super.configure(config);
93         try {
94             optionalEntryFragment = getRequiredFragment(OPTIONAL_CONTROL + getName() + ENTRY_POSTFIX);
95             optionalSelectedEntryFragment = getRequiredFragment(OPTIONAL_CONTROL + getName() + SELECTED_ENTRY_POSTFIX);
96             requiredEntryFragment = getRequiredFragment(REQUIRED_CONTROL + getName() + ENTRY_POSTFIX);
97             requiredSelectedEntryFragment = getRequiredFragment(REQUIRED_CONTROL + getName() + SELECTED_ENTRY_POSTFIX);
98         } catch ( ProcessException exception ) {
99             throw new ConfigurationException(new ErrorMessage("control/requiredFragmentMissing", new String JavaDoc[] { OPTIONAL+getName()}), exception);
100         }
101         optionalValidEntryFragment = getOptionalFragment(OPTIONAL_VALID_CONTROL + getName() + ENTRY_POSTFIX, optionalEntryFragment);
102         optionalInvalidEntryFragment = getOptionalFragment(OPTIONAL_INVALID_CONTROL + getName() + ENTRY_POSTFIX, optionalEntryFragment);
103         optionalValidSelectedEntryFragment = getOptionalFragment(OPTIONAL_VALID_CONTROL + getName() + SELECTED_ENTRY_POSTFIX, optionalSelectedEntryFragment);
104         optionalInvalidSelectedEntryFragment = getOptionalFragment(OPTIONAL_INVALID_CONTROL + getName() + SELECTED_ENTRY_POSTFIX, optionalSelectedEntryFragment);
105         requiredValidEntryFragment = getOptionalFragment(REQUIRED_VALID_CONTROL + getName() + ENTRY_POSTFIX, requiredEntryFragment);
106         requiredInvalidEntryFragment = getOptionalFragment(REQUIRED_INVALID_CONTROL + getName() + ENTRY_POSTFIX, requiredEntryFragment);
107         requiredValidSelectedEntryFragment = getOptionalFragment(REQUIRED_VALID_CONTROL + getName() + SELECTED_ENTRY_POSTFIX, requiredSelectedEntryFragment);
108         requiredInvalidSelectedEntryFragment = getOptionalFragment(REQUIRED_INVALID_CONTROL + getName() + SELECTED_ENTRY_POSTFIX, requiredSelectedEntryFragment);
109     }
110
111     protected String JavaDoc[] getOptions(ValueDescriptor valueDescriptor) {
112         if (valueDescriptor instanceof ArrayValueDescriptor ) {
113             valueDescriptor = ((ArrayValueDescriptor)valueDescriptor).getEntryValueDescriptor();
114         }
115         if (valueDescriptor instanceof StringValueDescriptor) {
116             return ((StringValueDescriptor)valueDescriptor).getAllowedValues();
117         } else if (valueDescriptor instanceof BooleanValueDescriptor) {
118             return new String JavaDoc[]{BooleanValueDescriptor.TRUE, BooleanValueDescriptor.FALSE};
119         } else if (valueDescriptor instanceof NumberValueDescriptor) {
120             NumberValueDescriptor numberDescriptor = (NumberValueDescriptor)valueDescriptor;
121             if (numberDescriptor.isConstrained()) {
122                 List JavaDoc allowedValues = new ArrayList JavaDoc();
123                 if (numberDescriptor.getAllowedValues() != null) {
124                     Number JavaDoc[] numbers = numberDescriptor.getAllowedValues();
125                     for (int i = 0; i < numbers.length; i++) {
126                         allowedValues.add(String.valueOf(numbers[i]));
127                     }
128                 } else {
129                     int counter = 0;
130                     for (int i = numberDescriptor.getMinimum().intValue(); i <= numberDescriptor.getMaximum().intValue(); i++) {
131                         allowedValues.add(String.valueOf(i));
132                         counter++;
133                         if (counter == MAX_OPTIONS) break;
134                     }
135                 }
136                 String JavaDoc[] options = new String JavaDoc[allowedValues.size()];
137                 return (String JavaDoc[])allowedValues.toArray(options);
138             }
139         }
140         return new String JavaDoc[0];
141     }
142
143     protected boolean valueMatches(String JavaDoc value, Object JavaDoc resource) {
144         if (resource instanceof StringValue) {
145             return value.equals(resource.toString());
146         } else if (resource instanceof NumberValue) {
147             return value.equals(String.valueOf(((NumberValue)resource).getNumber()));
148         } else if (resource instanceof ArrayValue) {
149             Value[] array = ((ArrayValue)resource).getArray();
150             for (int i = 0; i < array.length; i++) {
151                 if (array[i] instanceof StringValue) {
152                     if (array[i].toString().equals(value)) return true;
153                 }
154             }
155         }
156         return false;
157     }
158 }
Popular Tags