]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/PropertyHandlerImpl.java.keep
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / PropertyHandlerImpl.java.keep
1 /*******************************************************************************
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.g2d.element.handler;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.utils.datastructures.hints.IHintContext.Key;
21 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
22 import org.simantics.utils.datastructures.valueinterface.InvalidItemException;
23 import org.simantics.utils.datastructures.valueinterface.SubscriptionException;
24 import org.simantics.utils.datastructures.valueinterface.ValueAccess;
25 import org.simantics.utils.datastructures.valueinterface.ValueMap;
26 import org.simantics.utils.datastructures.valueinterface.impl.SimpleValueMap;
27 import org.simantics.utils.datastructures.valueinterface.impl.ValueMonitor;
28
29 /**
30  * 
31  * @Author Toni Kalajainen
32  */
33 public class PropertyHandlerImpl implements PropertyHandler {
34
35         List<String> keys = new ArrayList<String>();
36         Set<String> keys2 = new HashSet<String>();
37         ValueMap<String> initialValues;
38         
39         private static final Key KEY_VM = new KeyOf(ValueMonitor.class);
40         
41         public PropertyHandlerImpl(String ... keys)
42         {
43                 for (String k : keys) {
44                         this.keys.add(k);
45                         this.keys2.add(k);
46                 }
47         }
48         
49         public PropertyHandlerImpl(Collection<String> keys, ValueMap<String> initialValues)
50         {
51                 for (String k : keys) {
52                         this.keys.add(k);
53                         this.keys2.add(k);
54                 }
55                 if (initialValues!=null)
56                         this.initialValues = new SimpleValueMap<String>(initialValues);
57         }
58         
59         @Override
60         public void enumerateProperties(Collection<String> collection) {
61                 collection.addAll(keys);
62         }
63
64         @Override
65         public Object getValue(IElement e, String key) {
66                 ValueMonitor<String> vm = e.getHint(KEY_VM);
67                 if (vm==null) return null;
68                 return vm.get(key);
69         }
70
71         @Override
72         public void setValueSource(IElement e, ValueAccess<String> access) {
73                 if (access==null) {
74                         ValueMonitor<String> vm = e.getHint(KEY_VM);
75                         if (vm!=null) {                 
76                                 e.removeHint(KEY_VM);
77                                 vm.dispose();
78                         }
79                         return;
80                 }
81                 try {
82                         ValueMonitor<String> vm = new ValueMonitor<String>(access, keys2, initialValues);
83                         e.setHint(KEY_VM, vm);
84                         vm.dispose();
85                 } catch (SubscriptionException e1) {
86                         e1.printStackTrace();
87                 } catch (InvalidItemException e1) {
88                         e1.printStackTrace();
89                 }
90         }
91
92 }