]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/PropertyTabUtil.java
Reflection-based handling of Enum property editing.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / PropertyTabUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.property;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.List;\r
16 \r
17 import org.simantics.g3d.property.annotations.PropertyContributor;\r
18 \r
19 public class PropertyTabUtil {\r
20         \r
21         \r
22         @SuppressWarnings("unchecked")\r
23         public static List<PropertyTabContributor> getContributors(Object input) {\r
24                 PropertyTabContributorFactory factory = resolveFactory(input.getClass());\r
25                 if (factory == null)\r
26                         return Collections.EMPTY_LIST;\r
27                 return factory.getContributors(input);\r
28         }\r
29         \r
30         private static PropertyTabContributorFactory resolveFactory(Class<?> clazz) {\r
31                 PropertyContributor contributor = clazz.getAnnotation(PropertyContributor.class);\r
32                 if (contributor != null)\r
33                         try {\r
34                                 return contributor.value().newInstance();\r
35                         } catch (InstantiationException e) {\r
36                                 e.printStackTrace();\r
37                         } catch (IllegalAccessException e) {\r
38                                 e.printStackTrace();\r
39                         }\r
40                 Class<?> superClass = clazz.getSuperclass();\r
41                 if (superClass != null)\r
42                         return resolveFactory(superClass);\r
43                 return null;\r
44         }\r
45 \r
46 }\r