001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. 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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.integration.beans; 021 022import java.beans.PropertyEditor; 023import java.beans.PropertyEditorSupport; 024 025/** 026 * An abstract bi-directional {@link PropertyEditor}. 027 * 028 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 029 */ 030public abstract class AbstractPropertyEditor extends PropertyEditorSupport { 031 032 private String text; 033 034 private Object value; 035 036 private boolean trimText = true; 037 038 protected void setTrimText(boolean trimText) { 039 this.trimText = trimText; 040 } 041 042 @Override 043 public String getAsText() { 044 return text; 045 } 046 047 @Override 048 public Object getValue() { 049 return value; 050 } 051 052 @Override 053 public void setAsText(String text) throws IllegalArgumentException { 054 this.text = text; 055 if (text == null) { 056 value = defaultValue(); 057 } else { 058 value = toValue(trimText ? text.trim() : text); 059 } 060 } 061 062 @Override 063 public void setValue(Object value) { 064 this.value = value; 065 if (value == null) { 066 text = defaultText(); 067 } else { 068 text = toText(value); 069 } 070 } 071 072 protected String defaultText() { 073 return null; 074 } 075 076 protected Object defaultValue() { 077 return null; 078 } 079 080 protected abstract String toText(Object value); 081 082 protected abstract Object toValue(String text) throws IllegalArgumentException; 083 084}