001: /*
002: * $Id: JavaBeanPanelModel.java 6115 2006-01-30 04:50:47Z dfs $
003: *
004: * Copyright 2004-2006 Daniel F. Savarese
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.savarese.org/software/ApacheLicense-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: package org.savarese.unicorn.ui;
020:
021: import java.beans.*;
022: import java.lang.reflect.*;
023: import java.util.HashMap;
024: import javax.management.*;
025:
026: import org.savarese.unicorn.util.Beans;
027:
028: /**
029: * An {@link ObjectPanelModel} implementation for JavaBeans.
030: */
031: public class JavaBeanPanelModel extends
032: AbstractObjectPanelModel<Object> {
033:
034: private static final Object[] NO_ARGS = new Object[0];
035:
036: private MBeanInfo __info;
037: private HashMap<String, PropertyDescriptor> __propMap;
038:
039: private void __initBeanInfo(Object bean) throws Exception {
040: BeanInfo info = Introspector.getBeanInfo(bean.getClass(),
041: Object.class);
042: PropertyDescriptor[] properties = info.getPropertyDescriptors();
043:
044: __propMap.clear();
045:
046: if (properties != null) {
047: for (PropertyDescriptor prop : properties)
048: __propMap.put(prop.getName(), prop);
049: }
050:
051: __info = Beans.beanInfoToMBeanInfo(info);
052: }
053:
054: /**
055: * Instantiates a JavaBeanPanelModel wrapping the provided bean.
056: *
057: * @param bean The JavaBean to wrap.
058: */
059: public JavaBeanPanelModel(Object bean) {
060: __propMap = new HashMap<String, PropertyDescriptor>();
061: setObject(bean);
062: }
063:
064: public void setAttribute(String attribute, Object value)
065: throws IllegalAccessException, InvocationTargetException {
066: PropertyDescriptor prop = __propMap.get(attribute);
067:
068: if (prop == null)
069: return;
070:
071: Method method = prop.getWriteMethod();
072:
073: if (method != null)
074: method.invoke(getObject(), value);
075: }
076:
077: public Object getAttribute(String attribute)
078: throws IllegalAccessException, InvocationTargetException {
079: PropertyDescriptor prop = __propMap.get(attribute);
080:
081: if (prop == null)
082: return null;
083:
084: Method method = prop.getReadMethod();
085:
086: if (method == null)
087: return null;
088:
089: return method.invoke(getObject(), NO_ARGS);
090: }
091:
092: public void setObject(Object bean) {
093: if (bean != null) {
094: try {
095: __initBeanInfo(bean);
096: } catch (Exception e) {
097: __info = null;
098: }
099: } else {
100: __info = null;
101: }
102:
103: super .setObject(bean);
104: }
105:
106: public Object invoke(String operation, Object[] params,
107: Class[] types, String[] signature)
108: throws NoSuchMethodException, InvocationTargetException,
109: ClassNotFoundException, IllegalAccessException {
110: Method method = getObject().getClass().getMethod(operation,
111: types);
112:
113: return method.invoke(getObject(), params);
114: }
115:
116: public MBeanInfo getObjectInfo() {
117: return __info;
118: }
119:
120: public boolean isValid() {
121: return (__info != null);
122: }
123: }
|