001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.list.structure.filter.advanced;
016:
017: import java.util.Comparator;
018: import java.util.Map;
019: import org.araneaframework.backend.list.memorybased.Expression;
020: import org.araneaframework.uilib.form.Control;
021: import org.araneaframework.uilib.form.FormElement;
022: import org.araneaframework.uilib.form.constraint.RangeConstraint;
023: import org.araneaframework.uilib.list.structure.filter.FilterContext;
024: import org.araneaframework.uilib.list.util.ExpressionUtil;
025: import org.araneaframework.uilib.list.util.FilterFormUtil;
026: import org.araneaframework.uilib.list.util.FormUtil;
027: import org.araneaframework.uilib.util.Event;
028:
029: public abstract class RangeInRangeFilter extends BaseRangeInRangeFilter {
030:
031: private static final long serialVersionUID = 1L;
032:
033: private Comparator comparator;
034:
035: public static RangeInRangeFilter getFieldRangeInValueRangeInstance(
036: FilterContext ctx, String lowFieldId, String highFieldId,
037: String lowValueId, String highValueId) {
038: RangeInRangeFilter filter;
039: if (ctx.isStrict()) {
040: filter = new FieldRangeInValueRangeStrict();
041: } else {
042: filter = new FieldRangeInValueRangeNonStrict();
043: }
044: return init(ctx, filter, lowFieldId, highFieldId, lowValueId,
045: highValueId);
046: }
047:
048: public static RangeInRangeFilter getValueRangeInFieldRangeInstance(
049: FilterContext ctx, String lowFieldId, String highFieldId,
050: String lowValueId, String highValueId) {
051: RangeInRangeFilter filter;
052: if (ctx.isStrict()) {
053: filter = new ValueRangeInFieldRangeStrict();
054: } else {
055: filter = new ValueRangeInFieldRangeNonStrict();
056: }
057: return init(ctx, filter, lowFieldId, highFieldId, lowValueId,
058: highValueId);
059: }
060:
061: public static RangeInRangeFilter getOverlapInstance(
062: FilterContext ctx, String lowFieldId, String highFieldId,
063: String lowValueId, String highValueId) {
064: RangeInRangeFilter filter;
065: if (ctx.isStrict()) {
066: filter = new OverlapStrict();
067: } else {
068: filter = new OverlapNonStrict();
069: }
070: return init(ctx, filter, lowFieldId, highFieldId, lowValueId,
071: highValueId);
072: }
073:
074: private static RangeInRangeFilter init(final FilterContext ctx,
075: final RangeInRangeFilter filter, final String lowFieldId,
076: final String highFieldId, final String lowValueId,
077: final String highValueId) {
078: filter.setLowFieldId(lowFieldId);
079: filter.setHighFieldId(highFieldId);
080: filter.setLowValueId(lowValueId);
081: filter.setHighValueId(highValueId);
082:
083: ctx.addInitEvent(new Event() {
084: public void run() {
085: Comparator low = ctx.getFieldComparator(lowFieldId);
086: Comparator high = ctx.getFieldComparator(highFieldId);
087: if (low == null ? high == null : low.equals(high)) {
088: filter.setComparator(low);
089: } else {
090: throw new IllegalArgumentException(
091: "Low field and high field comparator types must be the same.");
092: }
093: }
094: });
095:
096: return filter;
097: }
098:
099: public static void addToForm(FilterContext ctx, String lowId,
100: String highId, FormElement lowElement,
101: FormElement highElement) {
102: ctx.getForm().addElement(lowId, lowElement);
103: ctx.getForm().addElement(highId, highElement);
104: FormUtil.addConstraint(ctx.getForm(), new RangeConstraint(
105: lowElement, highElement, true));
106: }
107:
108: public static void addToForm(FilterContext ctx, String lowId,
109: String highId, Control lowControl, Control highControl) {
110: addToForm(ctx, lowId, highId, FilterFormUtil.createElement(ctx,
111: lowId, lowControl), FilterFormUtil.createElement(ctx,
112: highId, highControl));
113: }
114:
115: public static void addToForm(FilterContext ctx, String lowId,
116: String highId) {
117: addToForm(ctx, lowId, highId, FilterFormUtil.createElement(ctx,
118: lowId), FilterFormUtil.createElement(ctx, highId));
119: }
120:
121: public Comparator getComparator() {
122: return comparator;
123: }
124:
125: public void setComparator(Comparator comparator) {
126: this .comparator = comparator;
127: }
128:
129: static class FieldRangeInValueRangeStrict extends
130: RangeInRangeFilter {
131: public Expression buildExpression(Map filterInfo) {
132: if (!isActive(filterInfo)) {
133: return null;
134: }
135: return ExpressionUtil.and(ExpressionUtil.gt(
136: buildLowVariableExpression(),
137: buildLowValueExpression(filterInfo),
138: getComparator()), ExpressionUtil.lt(
139: buildHighVariableExpression(),
140: buildHighValueExpression(filterInfo),
141: getComparator()));
142: }
143: }
144:
145: static class FieldRangeInValueRangeNonStrict extends
146: RangeInRangeFilter {
147: public Expression buildExpression(Map filterInfo) {
148: if (!isActive(filterInfo)) {
149: return null;
150: }
151: return ExpressionUtil.and(ExpressionUtil.ge(
152: buildLowVariableExpression(),
153: buildLowValueExpression(filterInfo),
154: getComparator()), ExpressionUtil.le(
155: buildHighVariableExpression(),
156: buildHighValueExpression(filterInfo),
157: getComparator()));
158: }
159: }
160:
161: static class ValueRangeInFieldRangeStrict extends
162: RangeInRangeFilter {
163: public Expression buildExpression(Map filterInfo) {
164: if (!isActive(filterInfo)) {
165: return null;
166: }
167: return ExpressionUtil.and(ExpressionUtil.lt(
168: buildLowVariableExpression(),
169: buildLowValueExpression(filterInfo),
170: getComparator()), ExpressionUtil.gt(
171: buildHighVariableExpression(),
172: buildHighValueExpression(filterInfo),
173: getComparator()));
174: }
175: }
176:
177: static class ValueRangeInFieldRangeNonStrict extends
178: RangeInRangeFilter {
179: public Expression buildExpression(Map filterInfo) {
180: if (!isActive(filterInfo)) {
181: return null;
182: }
183: return ExpressionUtil.and(ExpressionUtil.le(
184: buildLowVariableExpression(),
185: buildLowValueExpression(filterInfo),
186: getComparator()), ExpressionUtil.ge(
187: buildHighVariableExpression(),
188: buildHighValueExpression(filterInfo),
189: getComparator()));
190: }
191: }
192:
193: static class OverlapStrict extends RangeInRangeFilter {
194: public Expression buildExpression(Map filterInfo) {
195: if (!isActive(filterInfo)) {
196: return null;
197: }
198: return ExpressionUtil.and(ExpressionUtil.lt(
199: buildLowVariableExpression(),
200: buildHighValueExpression(filterInfo),
201: getComparator()), ExpressionUtil.gt(
202: buildHighVariableExpression(),
203: buildLowValueExpression(filterInfo),
204: getComparator()));
205: }
206: }
207:
208: static class OverlapNonStrict extends RangeInRangeFilter {
209: public Expression buildExpression(Map filterInfo) {
210: if (!isActive(filterInfo)) {
211: return null;
212: }
213: return ExpressionUtil.and(ExpressionUtil.le(
214: buildLowVariableExpression(),
215: buildHighValueExpression(filterInfo),
216: getComparator()), ExpressionUtil.ge(
217: buildHighVariableExpression(),
218: buildLowValueExpression(filterInfo),
219: getComparator()));
220: }
221: }
222: }
|