1 11 12 package org.eclipse.core.databinding; 13 14 import org.eclipse.core.databinding.conversion.IConverter; 15 import org.eclipse.core.databinding.observable.list.IObservableList; 16 import org.eclipse.core.databinding.validation.ValidationStatus; 17 import org.eclipse.core.internal.databinding.BindingMessages; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 46 public class UpdateListStrategy extends UpdateStrategy { 47 48 53 public static int POLICY_NEVER = notInlined(1); 54 55 60 public static int POLICY_ON_REQUEST = notInlined(2); 61 62 68 public static int POLICY_UPDATE = notInlined(8); 69 70 79 private static int notInlined(int i) { 80 return i; 81 } 82 83 protected IConverter converter; 84 85 private int updatePolicy; 86 87 protected boolean provideDefaults; 88 89 95 public UpdateListStrategy() { 96 this(true, POLICY_UPDATE); 97 } 98 99 108 public UpdateListStrategy(int updatePolicy) { 109 this(true, updatePolicy); 110 } 111 112 126 public UpdateListStrategy(boolean provideDefaults, int updatePolicy) { 127 this.provideDefaults = provideDefaults; 128 this.updatePolicy = updatePolicy; 129 } 130 131 143 public Object convert(Object element) { 144 return converter == null ? element : converter.convert(element); 145 } 146 147 152 protected void fillDefaults(IObservableList source, 153 IObservableList destination) { 154 Object sourceType = source.getElementType(); 155 Object destinationType = destination.getElementType(); 156 if (provideDefaults && sourceType != null && destinationType != null) { 157 if (converter == null) { 158 setConverter(createConverter(sourceType, destinationType)); 159 } 160 } 161 if (converter != null) { 162 if (sourceType != null) { 163 checkAssignable(converter.getFromType(), sourceType, 164 "converter does not convert from type " + sourceType); } 166 if (destinationType != null) { 167 checkAssignable(converter.getToType(), destinationType, 168 "converter does not convert to type " + destinationType); } 170 } 171 } 172 173 176 public int getUpdatePolicy() { 177 return updatePolicy; 178 } 179 180 187 public UpdateListStrategy setConverter(IConverter converter) { 188 this.converter = converter; 189 return this; 190 } 191 192 201 protected IStatus doAdd(IObservableList observableList, Object element, 202 int index) { 203 try { 204 observableList.add(index, element); 205 } catch (Exception ex) { 206 return ValidationStatus.error(BindingMessages 207 .getString("ValueBinding_ErrorWhileSettingValue"), ex); 209 } 210 return Status.OK_STATUS; 211 } 212 213 221 protected IStatus doRemove(IObservableList observableList, int index) { 222 try { 223 observableList.remove(index); 224 } catch (Exception ex) { 225 return ValidationStatus.error(BindingMessages 226 .getString("ValueBinding_ErrorWhileSettingValue"), ex); 228 } 229 return Status.OK_STATUS; 230 } 231 } 232 | Popular Tags |