1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.modifiers;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
20 * A trivial EnumerationModifier base implementation
22 * @author Tuukka Lehtonen
24 public abstract class AbstractEnumerationModifier<T> implements EnumerationModifier {
26 private final Enumeration<T> enumeration;
27 private final EnumeratedValue<T> value;
28 private final List<String> values;
30 public AbstractEnumerationModifier(Enumeration<T> enumeration, EnumeratedValue<T> value) {
31 assert enumeration != null;
32 assert enumeration.size() > 0;
34 this.enumeration = enumeration;
37 this.values = new ArrayList<String>(enumeration.size());
38 for (EnumeratedValue<T> v : enumeration.values())
39 values.add(v.getName());
43 public final List<String> getValues() {
48 public final String getValue() {
50 return value.getName();
51 return enumeration.values().get(0).getName();
55 public final String isValid(String label) {
56 if (!values.contains(label))
57 return "Value '" + label + "' is not among the enumerated values " + values;
62 public final void modify(String label) {
63 int index = values.indexOf(label);
65 throw new IllegalArgumentException("Cannot modify enumeration with value '" + label + "', not among the enumerated values " + values);
66 modifyWithValue(value, enumeration.values().get(index));
69 protected void modifyWithValue(EnumeratedValue<T> oldEnumValue, EnumeratedValue<T> enumValue) {
70 T oldObject = oldEnumValue != null ? oldEnumValue.getObject() : null;
71 T newObject = enumValue != null ? enumValue.getObject() : null;
72 modifyWithObject(oldObject, newObject);
76 * At least implement this if you don't override
77 * {@link #modifyWithValue(EnumeratedValue, EnumeratedValue)}.
79 * @param oldEnumObject
82 protected void modifyWithObject(T oldEnumObject, T enumObject) {