]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariantBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / mutable / MutableVariantBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.mutable;
13
14 import java.util.Set;
15
16 import org.simantics.databoard.adapter.AdapterFactory;
17 import org.simantics.databoard.binding.Binding;
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.binding.reflection.ClassBindingFactory;
20
21 /**
22  * MutableVariantBinding binds VariantType to {@link MutableVariant} Class. 
23  */
24 public class MutableVariantBinding extends ImmutableVariantBinding {
25                 
26         public MutableVariantBinding(ClassBindingFactory bindingFactory, AdapterFactory adapterFactory) {
27                 super(bindingFactory, adapterFactory);
28         }
29         
30         @Override
31         public Object create(Binding binding, Object value) throws BindingException {
32                 return new MutableVariant(binding, value);
33         }
34         
35         @Override
36         public boolean isImmutable() {
37                 return false;
38         }
39
40         @Override
41         public boolean isInstance(Object obj) {
42                 if (obj==null) return false;
43                 if (obj instanceof MutableVariant == false) return false;
44                 return true;
45         }
46         
47         @Override
48         public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
49                 if (obj==null) throw new BindingException("null value is not MutableVariant");
50                 if (obj instanceof MutableVariant == false) throw new BindingException("wrong class, MutableVariant expected");
51                 MutableVariant var = (MutableVariant) obj;
52                 if (var.binding==null) throw new BindingException("Binding is expected");               
53                 var.binding.assertInstaceIsValid(var.value, validInstances);
54         }
55         
56         @Override
57         public void setContent(Object variant, Binding binding, Object value)
58                         throws BindingException {
59                 assert(isValid(binding, value));
60                 MutableVariant var = (MutableVariant) variant;
61                 var.binding = binding;
62                 var.value = value;
63         }
64
65 }