/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * 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 * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g2d.element.handler; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.simantics.g2d.element.IElement; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; import org.simantics.utils.datastructures.valueinterface.InvalidItemException; import org.simantics.utils.datastructures.valueinterface.SubscriptionException; import org.simantics.utils.datastructures.valueinterface.ValueAccess; import org.simantics.utils.datastructures.valueinterface.ValueMap; import org.simantics.utils.datastructures.valueinterface.impl.SimpleValueMap; import org.simantics.utils.datastructures.valueinterface.impl.ValueMonitor; /** * * @Author Toni Kalajainen */ public class PropertyHandlerImpl implements PropertyHandler { List keys = new ArrayList(); Set keys2 = new HashSet(); ValueMap initialValues; private static final Key KEY_VM = new KeyOf(ValueMonitor.class); public PropertyHandlerImpl(String ... keys) { for (String k : keys) { this.keys.add(k); this.keys2.add(k); } } public PropertyHandlerImpl(Collection keys, ValueMap initialValues) { for (String k : keys) { this.keys.add(k); this.keys2.add(k); } if (initialValues!=null) this.initialValues = new SimpleValueMap(initialValues); } @Override public void enumerateProperties(Collection collection) { collection.addAll(keys); } @Override public Object getValue(IElement e, String key) { ValueMonitor vm = e.getHint(KEY_VM); if (vm==null) return null; return vm.get(key); } @Override public void setValueSource(IElement e, ValueAccess access) { if (access==null) { ValueMonitor vm = e.getHint(KEY_VM); if (vm!=null) { e.removeHint(KEY_VM); vm.dispose(); } return; } try { ValueMonitor vm = new ValueMonitor(access, keys2, initialValues); e.setHint(KEY_VM, vm); vm.dispose(); } catch (SubscriptionException e1) { e1.printStackTrace(); } catch (InvalidItemException e1) { e1.printStackTrace(); } } }