001: package com.silvermindsoftware.hitch.handlers.component;
002:
003: /**
004: * Copyright 2007 Brandon Goodin
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import javax.swing.*;
020:
021: public class JSpinnerComponentHandler extends
022: AbstractComponentHandler<JSpinner, Object, Object> implements
023: ComponentHandler<JSpinner, Object, Object> {
024:
025: private String valueProperty;
026: private String[] compareProperties;
027:
028: protected Class getSetterParameterType() {
029: return Object.class;
030: }
031:
032: protected String getGetterName() {
033: return "getValue";
034: }
035:
036: protected String getSetterName() {
037: return "setValue";
038: }
039:
040: public boolean isPopulateHandleable(JSpinner component,
041: Object modelPropertyValue) {
042: return modelPropertyValue != null;
043: }
044:
045: public Object preProcessPopulate(JSpinner spinner,
046: Object modelPropertyValue) {
047:
048: // if compareProperties have values then use them to select the proper Index
049: if (compareProperties != null && compareProperties.length > 0) {
050:
051: Object selectedItem = modelPropertyValue;
052: //iterate model
053:
054: // check if SpinnerModel is a SpinnerListModel and iterate and compare
055: if (spinner.getModel() instanceof SpinnerListModel) {
056:
057: SpinnerListModel spinnerListModel = (SpinnerListModel) spinner
058: .getModel();
059:
060: for (Object item : spinnerListModel.getList()) {
061: boolean isEqual = true;
062: for (String property : compareProperties) {
063:
064: // get value on current item
065: Object itemValue = getPropertyValue(item,
066: property);
067:
068: // if model object is prmitive 1st class compare
069: if (isBaseType(modelPropertyValue)) {
070: if (itemValue == null) {
071: if (modelPropertyValue != null) {
072: isEqual = false;
073: break;
074: }
075: } else if (!itemValue
076: .equals(modelPropertyValue)) {
077: isEqual = false;
078: break;
079: }
080: } else {
081: // otherwise find the property
082:
083: Object nestedPropertyValue = getPropertyValue(
084: modelPropertyValue, property);
085:
086: // compare
087: if (itemValue == null) {
088: if (nestedPropertyValue != null) {
089: isEqual = false;
090: break;
091: }
092: } else if (!itemValue
093: .equals(nestedPropertyValue)) {
094: isEqual = false;
095: break;
096: }
097:
098: }
099:
100: }
101:
102: if (isEqual) {
103: selectedItem = item;
104: break;
105: }
106:
107: }
108: }
109:
110: return selectedItem;
111:
112: } else {
113: // otherwise just set the object
114: return modelPropertyValue;
115: }
116: }
117:
118: /**
119: * If the selected item in the JSpinner is not the desired value to be set on the
120: * model object property, you can use the valueProperty to specify a property on the JSpinner
121: * selected item that will be used as the value that is set on the model object property.
122: *
123: * @param component
124: * @param componentValue
125: * @return
126: */
127: public Object preProcessUpdate(JSpinner component,
128: Object componentValue) {
129: return getPropertyValue(componentValue, valueProperty);
130: }
131:
132: public void setCompareProperties(String compareProperties) {
133: this .compareProperties = compareProperties.split(",");
134: }
135:
136: public void setValueProperty(String valueProperty) {
137: this.valueProperty = valueProperty;
138: }
139:
140: }
|