]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/modifiers/AbstractEnumerationModifier.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / modifiers / AbstractEnumerationModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.browsing.ui.common.modifiers;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
18
19 /**
20  * A trivial EnumerationModifier base implementation
21  * 
22  * @author Tuukka Lehtonen
23  */
24 public abstract class AbstractEnumerationModifier<T> implements EnumerationModifier {
25
26     private final Enumeration<T>     enumeration;
27     private final EnumeratedValue<T> value;
28     private final List<String>       values;
29
30     public AbstractEnumerationModifier(Enumeration<T> enumeration, EnumeratedValue<T> value) {
31         assert enumeration != null;
32         assert enumeration.size() > 0;
33
34         this.enumeration = enumeration;
35         this.value = value;
36
37         this.values = new ArrayList<String>(enumeration.size());
38         for (EnumeratedValue<T> v : enumeration.values())
39             values.add(v.getName());
40     }
41
42     @Override
43     public final List<String> getValues() {
44         return values;
45     }
46
47     @Override
48     public final String getValue() {
49         if (value != null)
50             return value.getName();
51         return enumeration.values().get(0).getName();
52     }
53
54     @Override
55     public final String isValid(String label) {
56         if (!values.contains(label))
57             return "Value '" + label + "' is not among the enumerated values " + values;
58         return null;
59     }
60
61     @Override
62     public final void modify(String label) {
63         int index = values.indexOf(label);
64         if (index == -1)
65             throw new IllegalArgumentException("Cannot modify enumeration with value '" + label + "', not among the enumerated values " + values);
66         modifyWithValue(value, enumeration.values().get(index));
67     }
68
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);
73     }
74
75     /**
76      * At least implement this if you don't override
77      * {@link #modifyWithValue(EnumeratedValue, EnumeratedValue)}.
78      * 
79      * @param oldEnumObject
80      * @param enumObject
81      */
82     protected void modifyWithObject(T oldEnumObject, T enumObject) {
83     }
84
85 };