KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > WidgetState


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel;
17
18 import org.apache.commons.lang.enums.ValuedEnum;
19
20 /**
21  * The state of a widget. States are ordered from the most featured ("active")
22  * to the most constrained ("invisible"), so that state combinations can be
23  * computed: a widget's combined state is the strictest between the widget's own
24  * state and its parent state.
25  *
26  * @version $Id: WidgetState.java 155211 2005-02-24 17:05:51Z sylvain $
27  */

28 public class WidgetState extends ValuedEnum {
29
30     private static final int ACTIVE_VALUE = 4;
31     
32     private static final int DISABLED_VALUE = 3;
33
34     private static final int OUTPUT_VALUE = 2;
35
36     private static final int INVISIBLE_VALUE = 1;
37
38     /**
39      * Active state. This is the default state, where widgets read their values
40      * from the request and display them.
41      */

42     public static final WidgetState ACTIVE = new WidgetState("active", ACTIVE_VALUE);
43
44     /**
45      * Disabled state, value is displayed but user input is ignored. The widget should be
46      * rendered in a manner that indicates that this widget could be active, but is currently not.
47      */

48     public static final WidgetState DISABLED = new WidgetState("disabled", DISABLED_VALUE);
49     
50     /**
51      * Output state, value is displayed but user input is ignored. The widget should be rendered
52      * as plain text, giving no indication that it could be input.
53      */

54     public static final WidgetState OUTPUT = new WidgetState("output", OUTPUT_VALUE);
55
56     /**
57      * Invisible state. Values are not displayed and user input is ignored.
58      */

59     public static final WidgetState INVISIBLE = new WidgetState("invisible", INVISIBLE_VALUE);
60
61     /**
62      * Private constructor
63      */

64     private WidgetState(String JavaDoc name, int value) {
65         super(name, value);
66     }
67
68     /**
69      * Get a state given its name. Valid names are "active", "disabled",
70      * "invisible".
71      *
72      * @param name the state name
73      * @return the state, or <code>null</code> if <code>name</code> doesn't
74      * denote a known state name
75      */

76     public static WidgetState stateForName(String JavaDoc name) {
77         return (WidgetState) getEnum(WidgetState.class, name);
78     }
79
80     /**
81      * Determine the strictest of two states. "invisible" is stricter than
82      * "disabled" which is stricter than "active"
83      *
84      * @param one a state
85      * @param two another state
86      * @return the strictes of <code>one</code> and <code>two</code>
87      */

88     public static WidgetState strictest(WidgetState one, WidgetState two) {
89         return (one.getValue() < two.getValue()) ? one : two;
90     }
91
92     /**
93      * Test if the current state is stricter than another one.
94      *
95      * @param other a state
96      * @return <code>true</code> if <code>this</code> is stricter
97      * than <code>other</code>
98      */

99     public boolean stricterThan(WidgetState other) {
100         return this.getValue() < other.getValue();
101     }
102
103     /**
104      * Does this state accept user inputs?
105      *
106      * @return <code>true</code> if this state accepts user inputs.
107      */

108     public boolean isAcceptingInputs() {
109         return this.getValue() == ACTIVE_VALUE;
110     }
111
112     /**
113      * Does this state display widget values?
114      *
115      * @return <code>true</code> if this state displays widget values.
116      */

117     public boolean isDisplayingValues() {
118         return this.getValue() > INVISIBLE_VALUE;
119     }
120
121     /**
122      * Does this state validate widget values?
123      *
124      * @return <code>true</code> if this state validates widget values.
125      */

126     public boolean isValidatingValues() {
127         return this.getValue() == ACTIVE_VALUE;
128     }
129
130 // Potential features provided by ValuedEnum that don't seem to be needed now
131
//
132
// public static WidgetState stateForValue(int stateValue) {
133
// return (WidgetState) getEnum(WidgetState.class, stateValue);
134
// }
135
//
136
// public static Map getEnumMap() {
137
// return getEnumMap(WidgetState.class);
138
// }
139
//
140
// public static List getStateList() {
141
// return getEnumList(WidgetState.class);
142
// }
143
//
144
// public static Iterator iterator() {
145
// return iterator(WidgetState.class);
146
// }
147

148 }
149
Popular Tags