001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
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: */package com.l2fprod.common.propertysheet;
018:
019: import com.l2fprod.common.beans.ExtendedPropertyDescriptor;
020:
021: import java.beans.PropertyDescriptor;
022: import java.beans.PropertyVetoException;
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025:
026: /**
027: * PropertyDescriptorAdapter.<br>
028: *
029: */
030: class PropertyDescriptorAdapter extends AbstractProperty {
031:
032: private PropertyDescriptor descriptor;
033:
034: public PropertyDescriptorAdapter() {
035: super ();
036: }
037:
038: public PropertyDescriptorAdapter(PropertyDescriptor descriptor) {
039: this ();
040: setDescriptor(descriptor);
041: }
042:
043: public void setDescriptor(PropertyDescriptor descriptor) {
044: this .descriptor = descriptor;
045: }
046:
047: public PropertyDescriptor getDescriptor() {
048: return descriptor;
049: }
050:
051: public String getName() {
052: return descriptor.getName();
053: }
054:
055: public String getDisplayName() {
056: return descriptor.getDisplayName();
057: }
058:
059: public String getShortDescription() {
060: return descriptor.getShortDescription();
061: }
062:
063: public Class getType() {
064: return descriptor.getPropertyType();
065: }
066:
067: public Object clone() {
068: PropertyDescriptorAdapter clone = new PropertyDescriptorAdapter(
069: descriptor);
070: clone.setValue(getValue());
071: return clone;
072: }
073:
074: public void readFromObject(Object object) {
075: try {
076: Method method = descriptor.getReadMethod();
077: if (method != null) {
078: setValue(method.invoke(object, null));
079: }
080: } catch (Exception e) {
081: String message = "Got exception when reading property "
082: + getName();
083: if (object == null) {
084: message += ", object was 'null'";
085: } else {
086: message += ", object was " + String.valueOf(object);
087: }
088: throw new RuntimeException(message, e);
089: }
090: }
091:
092: public void writeToObject(Object object) {
093: try {
094: Method method = descriptor.getWriteMethod();
095: if (method != null) {
096: method.invoke(object, new Object[] { getValue() });
097: }
098: } catch (Exception e) {
099: // let PropertyVetoException go to the upper level without logging
100: if (e instanceof InvocationTargetException
101: && ((InvocationTargetException) e)
102: .getTargetException() instanceof PropertyVetoException) {
103: throw new RuntimeException(
104: ((InvocationTargetException) e)
105: .getTargetException());
106: }
107:
108: String message = "Got exception when writing property "
109: + getName();
110: if (object == null) {
111: message += ", object was 'null'";
112: } else {
113: message += ", object was " + String.valueOf(object);
114: }
115: throw new RuntimeException(message, e);
116: }
117: }
118:
119: public boolean isEditable() {
120: return descriptor.getWriteMethod() != null;
121: }
122:
123: public String getCategory() {
124: if (descriptor instanceof ExtendedPropertyDescriptor) {
125: return ((ExtendedPropertyDescriptor) descriptor)
126: .getCategory();
127: } else {
128: return null;
129: }
130: }
131:
132: }
|