001 /* 002 * Copyright 2002-2006 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * 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, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.convert.support; 017 018 import org.springframework.binding.convert.ConversionContext; 019 import org.springframework.binding.convert.support.AbstractConverter; 020 import org.springframework.binding.value.support.ListListModel; 021 import org.springframework.core.ReflectiveVisitorHelper; 022 023 import javax.swing.*; 024 import java.util.ArrayList; 025 import java.util.Arrays; 026 import java.util.Collection; 027 import java.util.List; 028 029 /** 030 * @author Mathias Broekelmann 031 * 032 */ 033 public class ListModelConverter extends AbstractConverter { 034 035 private static final Class[] TARGET_CLASSES = new Class[] { ListModel.class }; 036 037 private static final Class[] SOURCE_CLASSES = new Class[] { Collection.class, List.class, Object[].class, 038 ListModel.class, Object.class }; 039 040 private final ReflectiveVisitorHelper visitorHelper = new ReflectiveVisitorHelper(); 041 042 protected Object doConvert(Object sourceValue, Class targetClass, ConversionContext context) throws Exception { 043 return visitorHelper.invokeVisit(this, sourceValue); 044 } 045 046 public Class[] getSourceClasses() { 047 return SOURCE_CLASSES; 048 } 049 050 public Class[] getTargetClasses() { 051 return TARGET_CLASSES; 052 } 053 054 ListModel visit(ListModel listModel) { 055 return listModel; 056 } 057 058 ListModel visit(List list) { 059 return new ListListModel(list); 060 } 061 062 ListModel visit(Collection collection) { 063 return visit(new ArrayList(collection)); 064 } 065 066 ListModel visit(Object[] array) { 067 return visit(Arrays.asList(array)); 068 } 069 070 ListModel visit(Object object) { 071 if(object instanceof Object[]) { 072 return visit((Object[])object); 073 } 074 return visit(new Object[] { object }); 075 } 076 }