]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaString.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaString.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.accessor.java;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.accessor.Accessor;
16 import org.simantics.databoard.accessor.StringAccessor;
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;
18 import org.simantics.databoard.accessor.error.AccessorException;
19 import org.simantics.databoard.accessor.error.ReferenceException;
20 import org.simantics.databoard.accessor.event.Event;
21 import org.simantics.databoard.accessor.event.ValueAssigned;
22 import org.simantics.databoard.accessor.impl.AccessorParams;
23 import org.simantics.databoard.accessor.impl.ListenerEntry;
24 import org.simantics.databoard.accessor.interestset.StringInterestSet;
25 import org.simantics.databoard.accessor.reference.ChildReference;
26 import org.simantics.databoard.binding.ArrayBinding;
27 import org.simantics.databoard.binding.Binding;
28 import org.simantics.databoard.binding.RecordBinding;
29 import org.simantics.databoard.binding.StringBinding;
30 import org.simantics.databoard.binding.VariantBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.StringType;
33
34 public class JavaString extends JavaObject implements StringAccessor {
35         
36         public JavaString(Accessor parent, StringBinding binding, Object object, AccessorParams params) {
37                 super(parent, binding, object, params);
38         }
39         
40         public StringBinding getBinding() {
41                 return (StringBinding) binding;
42         }
43
44         @Override
45         public StringType type() {
46                 return getBinding().type();
47         }
48
49         @Override
50         public String getValue() throws AccessorException {
51                 readLock();
52                 try {
53                         return getBinding().getValue(object);
54                 } catch (BindingException e) {
55                         throw new AccessorException(e);
56                 } finally {
57                         readUnlock();
58                 }
59         }       
60         
61         @Override
62         public void setValue(String newValue) throws AccessorException {
63                 if (newValue.equals( getValue() )) return;
64                 writeLock();
65                 try {
66                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
67                                 JavaObject jo = (JavaObject) parent;
68                                 ArrayBinding ab = (ArrayBinding) jo.binding;
69                                 Object nv = getBinding().create(newValue);
70                                 ab.set(jo.object, (Integer)keyInParent, nv);
71                                 this.object = nv;
72                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
73                                 JavaObject jo = (JavaObject) parent;
74                                 RecordBinding rb = (RecordBinding) jo.binding;
75                                 Object nv = getBinding().create(newValue);
76                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);
77                                 this.object = nv;
78                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
79                                 JavaObject jo = (JavaObject) parent;
80                                 VariantBinding vb = (VariantBinding) jo.binding;
81                                 Object nv = getBinding().create(newValue);
82                                 vb.setContent(jo.object, getBinding(), nv);
83                                 this.object = nv;
84                         } else {                        
85                                 // Set value
86                                 getBinding().setValue(object, newValue);
87                         }
88
89                         
90                         // Notify
91                         ListenerEntry le = listeners;
92                         while (le!=null) {
93                                 StringInterestSet is = le.getInterestSet();
94                                 if (is.inNotifications()) {
95                                         Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? newValue : null );
96                                         emitEvent(le, e);
97                                 }
98                                 le = le.next;
99                         }
100                         
101                 } catch (BindingException e) {
102                         throw new AccessorException(e);
103                 } finally {
104                         writeUnlock();
105                 }
106         }
107
108         @Override
109         public void setValue(Binding binding, Object newValue) throws AccessorException {
110                 try {
111                         String nv = ((StringBinding)binding).getValue(newValue);
112                         setValue(nv);
113                 } catch (BindingException e) {
114                         throw new AccessorException(e);
115                 }
116         }
117
118         @SuppressWarnings("unchecked")
119         @Override
120         public <T extends Accessor> T getComponent(ChildReference reference)
121         throws AccessorConstructionException {
122                 if (reference==null) return (T) this;           
123                 throw new ReferenceException(reference.getClass()+" is not a subreference of StringType");      
124         }
125
126         @Override
127         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
128                 Event rollback = makeRollback ? new ValueAssigned( Bindings.STRING, getValue() ) : null;                
129                 if (e instanceof ValueAssigned) {
130                         ValueAssigned va = (ValueAssigned) e;
131                         if (va.newValue == null) throw new AccessorException("String value expected, got null");
132                         setValue(va.newValue.getBinding(), va.newValue.getValue());
133                         return rollback;
134                 } else {
135                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to String");                       
136                 }
137         }
138
139 }
140
141