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: import java.util.Arrays;
021: import java.util.List;
022: import java.util.ArrayList;
023:
024: public class JListComponentHandler extends
025: AbstractComponentHandler<JList, Object, Object> implements
026: ComponentHandler<JList, Object, Object> {
027:
028: protected Class getSetterParameterType() {
029: return int[].class;
030: }
031:
032: public boolean isPopulateHandleable(JList component,
033: Object modelPropertyValue) {
034: return modelPropertyValue != null;
035: }
036:
037: public Object preProcessPopulate(JList component,
038: Object modelPropertyValue) {
039:
040: List<Integer> integerList = new ArrayList<Integer>();
041:
042: if (modelPropertyValue.getClass().isArray()) {
043:
044: // iterate objects and locate in component model
045: List objectList = Arrays
046: .asList((Object[]) modelPropertyValue);
047:
048: ListModel dm = component.getModel();
049:
050: // allow user to configure custom comparator
051: for (int i = 0, c = dm.getSize(); i < c; i++) {
052: if (objectList.contains(dm.getElementAt(i))) {
053: integerList.add(i);
054: }
055: }
056:
057: } else {
058: ListModel dm = component.getModel();
059:
060: // allow user to configure custom comparator
061: for (int i = 0, c = dm.getSize(); i < c; i++) {
062: if (modelPropertyValue.equals(dm.getElementAt(i))) {
063: integerList.add(i);
064: break;
065: }
066: }
067: }
068:
069: int[] retVal;
070:
071: if (integerList.size() == 0) {
072: retVal = new int[] { -1 };
073: } else {
074: retVal = new int[integerList.size()];
075: int index = 0;
076: for (Integer val : integerList) {
077: retVal[index] = val;
078: }
079: }
080:
081: return retVal;
082: }
083:
084: public Object preProcessUpdate(JList component,
085: Object formFieldValue) {
086: // if a single selection return a single object
087: if (component.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) {
088: Object[] selectedValues = (Object[]) formFieldValue;
089: if (selectedValues.length == 0) {
090: return null;
091: } else {
092: return selectedValues[0];
093: }
094:
095: } else {
096: //otherwise return an array
097: return formFieldValue;
098: }
099: }
100:
101: protected String getSetterName() {
102: return "setSelectedIndices";
103: }
104:
105: protected String getGetterName() {
106: return "getSelectedValues";
107: }
108: }
|