| /* |
| * |
| * Copyright (c) 2011 - 2016 - Loetz GmbH & Co KG, 69115 Heidelberg, Germany |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Initial contribution: |
| * Loetz GmbH & Co. KG |
| * |
| */ |
| package org.eclipse.osbp.abstractstatemachine; |
| |
| import java.beans.BeanInfo; |
| import java.beans.IntrospectionException; |
| import java.beans.Introspector; |
| import java.beans.PropertyChangeListener; |
| import java.beans.PropertyChangeSupport; |
| import java.beans.PropertyDescriptor; |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| import java.math.BigDecimal; |
| import java.util.Date; |
| import java.util.Locale; |
| |
| import org.eclipse.osbp.dsl.common.datatypes.IDto; |
| import org.eclipse.osbp.ecview.core.common.context.IViewContext; |
| import org.eclipse.osbp.ui.api.statemachine.IStateMachine; |
| import org.eclipse.osbp.ui.api.statemachine.IStateMachineParticipant; |
| import org.joda.time.DateTime; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public abstract class AbstractStateMachineParticipant implements |
| IStateMachineParticipant { |
| |
| /** The LOGGER. */ |
| protected static final Logger LOGGER = LoggerFactory |
| .getLogger(AbstractStateMachineParticipant.class); |
| protected PropertyChangeSupport pcs = new PropertyChangeSupport(this); |
| protected IStateMachine statemachine; |
| protected IViewContext viewContext; |
| protected Locale locale; |
| |
| public void addPropertyChangeListener(PropertyChangeListener listener) { |
| pcs.addPropertyChangeListener(listener); |
| } |
| |
| public void addPropertyChangeListener(String propertyName, |
| PropertyChangeListener listener) { |
| pcs.addPropertyChangeListener(propertyName, listener); |
| } |
| |
| public void removePropertyChangeListener(PropertyChangeListener listener) { |
| pcs.removePropertyChangeListener(listener); |
| } |
| |
| public void removePropertyChangeListener(String propertyName, |
| PropertyChangeListener listener) { |
| pcs.removePropertyChangeListener(propertyName, listener); |
| } |
| |
| @Override |
| public IStateMachine getStatemachine() { |
| return statemachine; |
| } |
| |
| @Override |
| public void setStatemachine(IStateMachine statemachine) { |
| this.statemachine = statemachine; |
| } |
| |
| @Override |
| public void setViewContext(IViewContext viewContext) { |
| this.viewContext = viewContext; |
| } |
| |
| @Override |
| public void setLocale(Locale locale) { |
| this.locale = locale; |
| } |
| |
| @Override |
| public Method getter(Object listener, String id) { |
| if (id == null) { |
| return null; |
| } |
| Class<?> clz = listener.getClass(); |
| try { |
| BeanInfo info = Introspector.getBeanInfo(clz); |
| for (PropertyDescriptor desc : info.getPropertyDescriptors()) { |
| if (desc.getName().equalsIgnoreCase(id)) { |
| return desc.getReadMethod(); |
| } |
| } |
| } catch (IntrospectionException e) { |
| LOGGER.error("{}", e); |
| } |
| return null; |
| } |
| |
| @Override |
| public Method setter(Object listener, String id, |
| Class<?> parameter) { |
| if (id == null) { |
| return null; |
| } |
| Class<?> clz = listener.getClass(); |
| try { |
| BeanInfo info = Introspector.getBeanInfo(clz); |
| for (PropertyDescriptor desc : info.getPropertyDescriptors()) { |
| if (desc.getName().equalsIgnoreCase(id)) { |
| if (parameter.isAssignableFrom(desc.getPropertyType())) { |
| return desc.getWriteMethod(); |
| } |
| } |
| } |
| } catch (IntrospectionException e) { |
| LOGGER.error("{}", e); |
| } |
| return null; |
| } |
| |
| @Override |
| public Class<?> getReturnType(Object obj, String id) { |
| if (id == null) { |
| return null; |
| } |
| Class<?> clz = obj.getClass(); |
| |
| try { |
| BeanInfo info = Introspector.getBeanInfo(clz); |
| for (PropertyDescriptor desc : info.getPropertyDescriptors()) { |
| if (desc.getName().equalsIgnoreCase(id)) { |
| Method method = desc.getReadMethod(); |
| return method.getReturnType(); |
| } |
| } |
| } catch (IntrospectionException e) { |
| LOGGER.error("{}", e); |
| } |
| return null; |
| } |
| |
| @Override |
| public void set(Object obj, String id, Object content) { |
| try { |
| if (content instanceof String) { |
| Method method = setter(obj, id, String.class); |
| if (method != null) { |
| method.invoke(obj, (String) content); |
| } |
| } else if (content instanceof Double) { |
| Method method = setter(obj, id, Double.class); |
| if (method != null) { |
| method.invoke(obj, (Double) content); |
| } |
| } else if (content instanceof Integer) { |
| Method method = setter(obj, id, Integer.class); |
| if (method != null) { |
| method.invoke(obj, (Integer) content); |
| } |
| } else if (content instanceof Long) { |
| Method method = setter(obj, id, Long.class); |
| if (method != null) { |
| method.invoke(obj, (Long) content); |
| } |
| } else if (content instanceof Boolean) { |
| Method method = setter(obj, id, Boolean.class); |
| if (method != null) { |
| method.invoke(obj, (Boolean) content); |
| } |
| } else if (content instanceof BigDecimal) { |
| Method method = setter(obj, id, BigDecimal.class); |
| if (method != null) { |
| method.invoke(obj, (BigDecimal) content); |
| } |
| } else if (content instanceof IDto) { |
| Method method = setter(obj, id, IDto.class); |
| if (method != null) { |
| method.invoke(obj, (IDto) content); |
| } |
| } else if (content instanceof Date) { |
| Method method = setter(obj, id, Date.class); |
| if (method != null) { |
| method.invoke(obj, (Date) content); |
| } |
| } else if (content instanceof DateTime) { |
| Method method = setter(obj, id, DateTime.class); |
| if (method != null) { |
| method.invoke(obj, (DateTime) content); |
| } |
| } |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| @Override |
| public Object get(Object obj, String id) { |
| Method method = getter(obj, id); |
| if (method == null) { |
| return null; |
| } |
| try { |
| return method.invoke(obj); |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| return null; |
| } |
| } |
| |
| @Override |
| public void clear(Object obj, String id) { |
| Method method = setter(obj, id, String.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, ""); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, Double.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, 0.0); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, BigDecimal.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, 0.0); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, Integer.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, 0); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, Long.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, 0L); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, IDto.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, (Object[])null); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, Date.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, (Date)null); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| method = setter(obj, id, DateTime.class); |
| if (method != null) { |
| try { |
| method.invoke(obj, (DateTime)null); |
| return; |
| } catch (IllegalAccessException | IllegalArgumentException |
| | InvocationTargetException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| } |
| |
| abstract public void init(); |
| } |